Skip to content

Add findByAssociation to extendsTo #314

Closed

Description

@ptnplanet

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
});

Activity

  1. dresende commented on Aug 26, 2013

    @dresende
    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.

  2. ptnplanet commented on Aug 26, 2013

    @ptnplanet
    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.

  3. ptnplanet commented on Sep 23, 2013

    @ptnplanet
    Author

    Thanks!

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions