Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add findByAssociation to extendsTo #314

Closed
ptnplanet opened this issue Aug 24, 2013 · 3 comments
Closed

Add findByAssociation to extendsTo #314

ptnplanet opened this issue Aug 24, 2013 · 3 comments

Comments

@ptnplanet
Copy link

I would like to suggest adding the findByAssociation method to models that are extended using extendsTo just like the one the hasOne association provides.

var User = db.define({
    username: String
});
var Account = User.extendsTo({
    facebookId: String
}, {
    table: 'account'
} );

// This would be nice:
User.findByAccount({
    facebookId: '123456789'
}, function (err, user) {
    // user
});

I don't quite understand, why this is possible though:

Account.findByUser({
    username: 'dude'
}, function (err, account) {
    // account
});
@dresende
Copy link
Owner

The second option is possible because in the background an extendsTo is a hasOne in the opposite direction and with some sugar. I'll see if I have time now to add the findBy* and add some tests for it.

@ptnplanet
Copy link
Author

Ah, yes. Now I see it. With extendsTo a hasOne association is created in the opposite direction - the extended table gets the foreign id and uses it as its primary key. Creating a hasOne in the other direction would put the foreign id in the base table and the extended table would need a primary key on its own.

The method for creating findBy* would then need to be shared by hasOne and by extendsTo.

@ptnplanet
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants