MCPcopy
hub / github.com/sequelize/sequelize / add

Method add

lib/associations/has-many.js:404–424  ·  view source on GitHub ↗

* Associate one or more target rows with `this`. This method accepts a Model / string / number to associate a single row, * or a mixed array of Model / string / numbers to associate multiple rows. * * @param {Model} sourceInstance the source instance * @param {Model|Model[]|string[]|stri

(sourceInstance, targetInstances, options = {})

Source from the content-addressed store, hash-verified

402 * @returns {Promise}
403 */
404 async add(sourceInstance, targetInstances, options = {}) {
405 if (!targetInstances) return Promise.resolve();
406
407
408 targetInstances = this.toInstanceArray(targetInstances);
409
410 const update = {
411 [this.foreignKey]: sourceInstance.get(this.sourceKey),
412 ...this.scope
413 };
414
415 const where = {
416 [this.target.primaryKeyAttribute]: targetInstances.map(unassociatedObject =>
417 unassociatedObject.get(this.target.primaryKeyAttribute)
418 )
419 };
420
421 await this.target.unscoped().update(update, { ...options, where });
422
423 return sourceInstance;
424 }
425
426 /**
427 * Un-associate one or several target rows.

Callers 7

initMethod · 0.45
refreshAttributesMethod · 0.45
changedMethod · 0.45
forEachModelMethod · 0.45
support.jsFile · 0.45
findAll.test.jsFile · 0.45
json.test.jsFile · 0.45

Calls 4

toInstanceArrayMethod · 0.80
unscopedMethod · 0.80
getMethod · 0.65
updateMethod · 0.45

Tested by

no test coverage detected