* Check if one or more instance(s) are associated with this. If a list of instances is passed, the function returns true if _all_ instances are associated * * @param {Model} sourceInstance source instance to check for an association with * @param {Model|Model[]|string[]|string|number[]|numb
(sourceInstance, instances, options)
| 517 | * @returns {Promise<boolean>} |
| 518 | */ |
| 519 | async has(sourceInstance, instances, options) { |
| 520 | if (!Array.isArray(instances)) { |
| 521 | instances = [instances]; |
| 522 | } |
| 523 | |
| 524 | options = { |
| 525 | raw: true, |
| 526 | ...options, |
| 527 | scope: false, |
| 528 | attributes: [this.targetKey], |
| 529 | joinTableAttributes: [] |
| 530 | }; |
| 531 | |
| 532 | const instancePrimaryKeys = instances.map(instance => { |
| 533 | if (instance instanceof this.target) { |
| 534 | return instance.where(); |
| 535 | } |
| 536 | return { |
| 537 | [this.targetKey]: instance |
| 538 | }; |
| 539 | }); |
| 540 | |
| 541 | options.where = { |
| 542 | [Op.and]: [ |
| 543 | { [Op.or]: instancePrimaryKeys }, |
| 544 | options.where |
| 545 | ] |
| 546 | }; |
| 547 | |
| 548 | const associatedObjects = await this.get(sourceInstance, options); |
| 549 | |
| 550 | return _.differenceWith(instancePrimaryKeys, associatedObjects, |
| 551 | (a, b) => _.isEqual(a[this.targetKey], b[this.targetKey])).length === 0; |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * Set the associated models by passing an array of instances or their primary keys. |