* Set is used to update values on the instance (the sequelize representation of the instance that is, remember that nothing will be persisted before you actually call `save`). * In its most basic form `set` will update a value stored in the underlying `dataValues` object. However, if a custom set
(key, value, options)
| 3730 | * @returns {Model} |
| 3731 | */ |
| 3732 | set(key, value, options) { |
| 3733 | let values; |
| 3734 | let originalValue; |
| 3735 | |
| 3736 | if (typeof key === 'object' && key !== null) { |
| 3737 | values = key; |
| 3738 | options = value || {}; |
| 3739 | |
| 3740 | if (options.reset) { |
| 3741 | this.dataValues = {}; |
| 3742 | for (const key in values) { |
| 3743 | this.changed(key, false); |
| 3744 | } |
| 3745 | } |
| 3746 | |
| 3747 | // If raw, and we're not dealing with includes or special attributes, just set it straight on the dataValues object |
| 3748 | if (options.raw && !(this._options && this._options.include) && !(options && options.attributes) && !this.constructor._hasDateAttributes && !this.constructor._hasBooleanAttributes) { |
| 3749 | if (Object.keys(this.dataValues).length) { |
| 3750 | Object.assign(this.dataValues, values); |
| 3751 | } else { |
| 3752 | this.dataValues = values; |
| 3753 | } |
| 3754 | // If raw, .changed() shouldn't be true |
| 3755 | this._previousDataValues = { ...this.dataValues }; |
| 3756 | } else { |
| 3757 | // Loop and call set |
| 3758 | if (options.attributes) { |
| 3759 | const setKeys = data => { |
| 3760 | for (const k of data) { |
| 3761 | if (values[k] === undefined) { |
| 3762 | continue; |
| 3763 | } |
| 3764 | this.set(k, values[k], options); |
| 3765 | } |
| 3766 | }; |
| 3767 | setKeys(options.attributes); |
| 3768 | if (this.constructor._hasVirtualAttributes) { |
| 3769 | setKeys(this.constructor._virtualAttributes); |
| 3770 | } |
| 3771 | if (this._options.includeNames) { |
| 3772 | setKeys(this._options.includeNames); |
| 3773 | } |
| 3774 | } else { |
| 3775 | for (const key in values) { |
| 3776 | this.set(key, values[key], options); |
| 3777 | } |
| 3778 | } |
| 3779 | |
| 3780 | if (options.raw) { |
| 3781 | // If raw, .changed() shouldn't be true |
| 3782 | this._previousDataValues = { ...this.dataValues }; |
| 3783 | } |
| 3784 | } |
| 3785 | return this; |
| 3786 | } |
| 3787 | if (!options) |
| 3788 | options = {}; |
| 3789 | if (!options.raw) { |
no test coverage detected