Skip to content

Polymorphic Relationships #45

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

Closed
chrissm79 opened this issue Jul 6, 2017 · 1 comment
Closed

Polymorphic Relationships #45

chrissm79 opened this issue Jul 6, 2017 · 1 comment

Comments

@chrissm79
Copy link

First off, thank you for this package, it's made my transition to Sequelize insanely easy!

I'm running into an issue w/ polymorphic relationships. On the sequelize docs, it mentions that I need to set a scope and a constraints on the HasMany side of the relationship and a constraints and as on the BelongsTo side). There's also unique, scope, and constraints options on the BelongsToMany relationships.

Would something like a function overload be possible here to allow us to pass in the additional options w/ an object?

// 1:m
class Post extends Model<Post> {
  // ...
  @HasMany({ 
    model: () => Comment,
    constraints: false, 
    scope: { commentable: "post" }
  }, 'foreign_key')
  comments: Comment[];
}

class Comment extends Model<Comment> {
  // ...
  @BelongsTo({ 
    model: () => Post,
    constraints: false, 
    as: "post" }, 
    'foreign_key'
  )
  post: Post;
}
// n:m
class Post extends Model<Post> {
  // ...
  @BelongsToMany(
    () => Tag,
    { 
      through: () => ItemTag,
      unique: false, 
      constraints: false,
      scope: { taggable: "post" }
    }, 
    'foreign_key'
  )
  tags: Tag[];
}

class Tag extends Model<Tag> {
  // ...
  @BelongsToMany(
    model: () => Post,
    { 
      through: () => ItemTag
      unique: false, 
    },
    {
      foreignKey: 'foreign_key',
      constraints: false
    }
  )
  post: Post;
}

class ItemTag extends Model<ItemTag> {
  // ...
}
@chrissm79
Copy link
Author

This is a duplicate of #34 which looks like it's already resolved [#42] 👍 💯

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

No branches or pull requests

1 participant