Skip to content

Support for polymorphic associations? #19

@einnjo

Description

@einnjo

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'},
        }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions