-
Notifications
You must be signed in to change notification settings - Fork 643
Closed
Labels
Description
Are you planning to support polymorphic associations?
Here's an example of it in another ORM.
Another one.
I haven't delved into moron's source code but perhaps an API like this would work:
// Schema for a comment model which relates polymorphically
// to several tables through the commentable and commentableId attributes.
Comment.jsonSchema = {
type: 'object',
properties: {
id: {type: 'integer'},
text: {type: 'string'},
commentable: {type: 'string'},
commentableId: {type: 'integer'},
authorId: {type: 'integer'},
createdAt: {type: 'string', format: 'date-time'},
updatedAt: {type: 'string', format: 'date-time'}
}
};
Comment.relationMappings = {
// A comment can be related to a topic.
topic: {
relation: Model.OneToOneRelation,
modelClass: __dirname + '/topic',
join: {
from: 'Comment.commentableId',
to: ['Comment.commentable', 'id']
}
// A comment can also be related to a user's profile.
profile: {
relation: Model.OneToOneRelation,
modelClass: __dirname + '/profile',
join: {
from: 'Comment.commentableId',
to: ['Comment.commentable', 'id']
}
}
};Or maybe something like this:
topic: {
...
join: {
from: 'Comment.commentableId',
morph: {table: 'Comment.commentable', attribute: 'id'},
}