MCPcopy
hub / github.com/sequelize/sequelize / set

Method set

lib/associations/belongs-to.js:194–214  ·  view source on GitHub ↗

* Set the associated model. * * @param {Model} sourceInstance the source instance * @param {?Model|string|number} [associatedInstance] An persisted instance or the primary key of an instance to associate with this. Pass `null` or `undefined` to remove the association. * @param {object} [

(sourceInstance, associatedInstance, options = {})

Source from the content-addressed store, hash-verified

192 * @returns {Promise}
193 */
194 async set(sourceInstance, associatedInstance, options = {}) {
195 let value = associatedInstance;
196
197 if (associatedInstance instanceof this.target) {
198 value = associatedInstance[this.targetKey];
199 }
200
201 sourceInstance.set(this.foreignKey, value);
202
203 if (options.save === false) return;
204
205 options = {
206 fields: [this.foreignKey],
207 allowNull: [this.foreignKey],
208 association: true,
209 ...options
210 };
211
212 // passes the changed field to save, so only that field get updated.
213 return await sourceInstance.save(options);
214 }
215
216 /**
217 * Create a new instance of the associated model and associate it with this.

Callers

nothing calls this directly

Calls 2

saveMethod · 0.80
setMethod · 0.65

Tested by

no test coverage detected