(target, options = {})
| 45 | }, |
| 46 | |
| 47 | belongsToMany(target, options = {}) { |
| 48 | if (!isModel(target, this.sequelize)) { |
| 49 | throw new Error(`${this.name}.belongsToMany called with something that's not a subclass of Sequelize.Model`); |
| 50 | } |
| 51 | |
| 52 | const source = this; |
| 53 | |
| 54 | // Since this is a mixin, we'll need a unique letiable name for hooks (since Model will override our hooks option) |
| 55 | options.hooks = options.hooks === undefined ? false : Boolean(options.hooks); |
| 56 | options.useHooks = options.hooks; |
| 57 | options.timestamps = options.timestamps === undefined ? this.sequelize.options.timestamps : options.timestamps; |
| 58 | Object.assign(options, _.omit(source.options, ['hooks', 'timestamps', 'scopes', 'defaultScope'])); |
| 59 | |
| 60 | if (options.useHooks) { |
| 61 | this.runHooks('beforeAssociate', { source, target, type: BelongsToMany }, options); |
| 62 | } |
| 63 | // the id is in the foreign table or in a connecting table |
| 64 | const association = new BelongsToMany(source, target, options); |
| 65 | source.associations[association.associationAccessor] = association; |
| 66 | |
| 67 | association._injectAttributes(); |
| 68 | association.mixin(source.prototype); |
| 69 | |
| 70 | if (options.useHooks) { |
| 71 | this.runHooks('afterAssociate', { source, target, type: BelongsToMany, association }, options); |
| 72 | } |
| 73 | |
| 74 | return association; |
| 75 | }, |
| 76 | |
| 77 | getAssociations(target) { |
| 78 | return Object.values(this.associations).filter(association => association.target.name === target.name); |
nothing calls this directly
no test coverage detected