| 582 | }; |
| 583 | |
| 584 | const updateAssociations = currentRows => { |
| 585 | const obsoleteAssociations = []; |
| 586 | const promises = []; |
| 587 | const defaultAttributes = options.through || {}; |
| 588 | |
| 589 | const unassociatedObjects = newAssociatedObjects.filter(obj => |
| 590 | !currentRows.some(currentRow => currentRow[foreignIdentifier] === obj.get(targetKey)) |
| 591 | ); |
| 592 | |
| 593 | for (const currentRow of currentRows) { |
| 594 | const newObj = newAssociatedObjects.find(obj => currentRow[foreignIdentifier] === obj.get(targetKey)); |
| 595 | |
| 596 | if (!newObj) { |
| 597 | obsoleteAssociations.push(currentRow); |
| 598 | } else { |
| 599 | let throughAttributes = newObj[this.through.model.name]; |
| 600 | // Quick-fix for subtle bug when using existing objects that might have the through model attached (not as an attribute object) |
| 601 | if (throughAttributes instanceof this.through.model) { |
| 602 | throughAttributes = {}; |
| 603 | } |
| 604 | |
| 605 | const attributes = { ...defaultAttributes, ...throughAttributes }; |
| 606 | |
| 607 | if (Object.keys(attributes).length) { |
| 608 | promises.push( |
| 609 | this.through.model.update(attributes, Object.assign(options, { |
| 610 | where: { |
| 611 | [identifier]: sourceInstance.get(sourceKey), |
| 612 | [foreignIdentifier]: newObj.get(targetKey) |
| 613 | } |
| 614 | } |
| 615 | )) |
| 616 | ); |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | if (obsoleteAssociations.length > 0) { |
| 622 | promises.push( |
| 623 | this.through.model.destroy({ |
| 624 | ...options, |
| 625 | where: { |
| 626 | [identifier]: sourceInstance.get(sourceKey), |
| 627 | [foreignIdentifier]: obsoleteAssociations.map(obsoleteAssociation => obsoleteAssociation[foreignIdentifier]), |
| 628 | ...this.through.scope |
| 629 | } |
| 630 | }) |
| 631 | ); |
| 632 | } |
| 633 | |
| 634 | if (unassociatedObjects.length > 0) { |
| 635 | const bulk = unassociatedObjects.map(unassociatedObject => { |
| 636 | return { |
| 637 | ...defaultAttributes, |
| 638 | ...unassociatedObject[this.through.model.name], |
| 639 | [identifier]: sourceInstance.get(sourceKey), |
| 640 | [foreignIdentifier]: unassociatedObject.get(targetKey), |
| 641 | ...this.through.scope |
nothing calls this directly
no test coverage detected