* Set the associated model. * * @param {Model} sourceInstance the source instance * @param {?Model|string|number} [associatedInstance] An persisted instance or the primary key of an instance to associate with this. Pass `null` or `undefined` to remove the association. * @param {object} [
(sourceInstance, associatedInstance, options = {})
| 192 | * @returns {Promise} |
| 193 | */ |
| 194 | async set(sourceInstance, associatedInstance, options = {}) { |
| 195 | let value = associatedInstance; |
| 196 | |
| 197 | if (associatedInstance instanceof this.target) { |
| 198 | value = associatedInstance[this.targetKey]; |
| 199 | } |
| 200 | |
| 201 | sourceInstance.set(this.foreignKey, value); |
| 202 | |
| 203 | if (options.save === false) return; |
| 204 | |
| 205 | options = { |
| 206 | fields: [this.foreignKey], |
| 207 | allowNull: [this.foreignKey], |
| 208 | association: true, |
| 209 | ...options |
| 210 | }; |
| 211 | |
| 212 | // passes the changed field to save, so only that field get updated. |
| 213 | return await sourceInstance.save(options); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Create a new instance of the associated model and associate it with this. |