nodester 0.6.4 → 0.6.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/models/mixins.js +29 -12
- package/package.json +1 -1
package/lib/models/mixins.js
CHANGED
|
@@ -212,10 +212,8 @@ async function _updateOne(
|
|
|
212
212
|
|
|
213
213
|
// Will contain data from parent instance and associations.
|
|
214
214
|
const fullInstanceData = instance.toJSON();
|
|
215
|
+
const parentData = { ...data };
|
|
215
216
|
|
|
216
|
-
const parentData = {
|
|
217
|
-
...data
|
|
218
|
-
}
|
|
219
217
|
for (let includeConfig of include) {
|
|
220
218
|
const { association } = includeConfig;
|
|
221
219
|
|
|
@@ -223,6 +221,7 @@ async function _updateOne(
|
|
|
223
221
|
continue;
|
|
224
222
|
}
|
|
225
223
|
|
|
224
|
+
// Remove association from parentData (handled separately)
|
|
226
225
|
delete parentData[association];
|
|
227
226
|
|
|
228
227
|
const associationDefinition = this.associations[association];
|
|
@@ -246,6 +245,15 @@ async function _updateOne(
|
|
|
246
245
|
// If association type is HasMany or HasOne (We don't work with any other):
|
|
247
246
|
switch(associationType) {
|
|
248
247
|
case 'HasMany': {
|
|
248
|
+
// Handle empty array (remove all old associations):
|
|
249
|
+
if (Array.isArray(includeData) && includeData.length === 0) {
|
|
250
|
+
const where = {
|
|
251
|
+
[foreignKey]: instance.id
|
|
252
|
+
}
|
|
253
|
+
await associatedModel.destroy({ where });
|
|
254
|
+
fullInstanceData[association] = [];
|
|
255
|
+
}
|
|
256
|
+
|
|
249
257
|
const promises = includeData.map(singleData => {
|
|
250
258
|
// Note: for now we are only able to work with a model with single PrimaryKey:
|
|
251
259
|
const where = {
|
|
@@ -263,16 +271,25 @@ async function _updateOne(
|
|
|
263
271
|
}
|
|
264
272
|
|
|
265
273
|
case 'HasOne': {
|
|
266
|
-
//
|
|
267
|
-
|
|
268
|
-
|
|
274
|
+
// Handle null case (remove old association)
|
|
275
|
+
if (includeData === null) {
|
|
276
|
+
const where = {
|
|
277
|
+
[foreignKey]: instance.id
|
|
278
|
+
}
|
|
279
|
+
await associatedModel.destroy({ where });
|
|
280
|
+
fullInstanceData[association] = null;
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
// Note: for now we are only able to work with a model with single PrimaryKey:
|
|
284
|
+
const where = {
|
|
285
|
+
[pkField]: includeData[pkField]
|
|
286
|
+
}
|
|
287
|
+
fullInstanceData[association] = await associatedModel.updateOne(
|
|
288
|
+
where,
|
|
289
|
+
includeData,
|
|
290
|
+
associationUpdateOpts
|
|
291
|
+
);
|
|
269
292
|
}
|
|
270
|
-
fullInstanceData[association] = await associatedModel.updateOne(
|
|
271
|
-
where,
|
|
272
|
-
includeData,
|
|
273
|
-
associationUpdateOpts
|
|
274
|
-
);
|
|
275
|
-
|
|
276
293
|
continue;
|
|
277
294
|
}
|
|
278
295
|
|