(Type)
| 86 | |
| 87 | // The logic for hasOne and belongsTo is exactly the same |
| 88 | function singleLinked(Type) { |
| 89 | return function(target, options = {}) { |
| 90 | // eslint-disable-next-line no-invalid-this |
| 91 | const source = this; |
| 92 | if (!isModel(target, source.sequelize)) { |
| 93 | throw new Error(`${source.name}.${_.lowerFirst(Type.name)} called with something that's not a subclass of Sequelize.Model`); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | // Since this is a mixin, we'll need a unique letiable name for hooks (since Model will override our hooks option) |
| 98 | options.hooks = options.hooks === undefined ? false : Boolean(options.hooks); |
| 99 | options.useHooks = options.hooks; |
| 100 | |
| 101 | if (options.useHooks) { |
| 102 | source.runHooks('beforeAssociate', { source, target, type: Type }, options); |
| 103 | } |
| 104 | // the id is in the foreign table |
| 105 | const association = new Type(source, target, Object.assign(options, source.options)); |
| 106 | source.associations[association.associationAccessor] = association; |
| 107 | |
| 108 | association._injectAttributes(); |
| 109 | association.mixin(source.prototype); |
| 110 | |
| 111 | if (options.useHooks) { |
| 112 | source.runHooks('afterAssociate', { source, target, type: Type, association }, options); |
| 113 | } |
| 114 | |
| 115 | return association; |
| 116 | }; |
| 117 | } |
| 118 | |
| 119 | Mixin.hasOne = singleLinked(HasOne); |
| 120 | Mixin.belongsTo = singleLinked(BelongsTo); |
no test coverage detected