strongloop/loopback-datasource-juggler — pull request #193 by fabien (2014-07-26 13:32:40+00:00)
Title: Rails-style polymorphic relations

All relations are supported: hasOne, belongsTo, hasMany + through, hasAndBelongsToMany + through.

Note that explicit through-models are needed, since the intermediate model cannot be created automatically - it's being shared with different models.

Inverse relations are supported as well, and the right model type will be returned.

See also: http://guides.rubyonrails.org/association_basics.html#polymorphic-associations

Examples:

``` javascript
// belongsTo (main polymorphic model)
Picture.belongsTo('imageable', { polymorphic: true });

// hasOne
Author.hasOne(Picture, { as: 'avatar', polymorphic: 'imageable' });
Reader.hasOne(Picture, { as: 'mugshot', polymorphic: 'imageable' });

// hasMany
Author.hasMany(Picture, { polymorphic: 'imageable' });
Reader.hasMany(Picture, { polymorphic: 'imageable' });

// hasAndBelongsToMany + through (will setup Picture.belongsTo)
Author.hasAndBelongsToMany(Picture, { through: PictureLink, polymorphic: 'imageable' });
Reader.hasAndBelongsToMany(Picture, { through: PictureLink, polymorphic: 'imageable' });
```