MCPcopy
hub / github.com/sequelize/sequelize / singleLinked

Function singleLinked

lib/associations/mixin.js:88–117  ·  view source on GitHub ↗
(Type)

Source from the content-addressed store, hash-verified

86
87// The logic for hasOne and belongsTo is exactly the same
88function 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
119Mixin.hasOne = singleLinked(HasOne);
120Mixin.belongsTo = singleLinked(BelongsTo);

Callers 1

mixin.jsFile · 0.85

Calls 3

isModelFunction · 0.85
_injectAttributesMethod · 0.45
mixinMethod · 0.45

Tested by

no test coverage detected