* 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 = {})
| 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. |
no test coverage detected