nodester 0.7.16 → 0.7.17
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 +20 -5
- package/package.json +1 -1
package/lib/models/mixins.js
CHANGED
|
@@ -218,7 +218,13 @@ async function _updateOne(
|
|
|
218
218
|
) {
|
|
219
219
|
try {
|
|
220
220
|
const include = opts.include ?? [];
|
|
221
|
-
|
|
221
|
+
const hasValidWhere = where && Object.keys(where).length > 0 && Object.values(where).every(val => val !== undefined);
|
|
222
|
+
let instance = null;
|
|
223
|
+
|
|
224
|
+
if (hasValidWhere) {
|
|
225
|
+
instance = await this.findOne({ where, include });
|
|
226
|
+
}
|
|
227
|
+
|
|
222
228
|
let isNewRecord = false;
|
|
223
229
|
|
|
224
230
|
if (!instance) {
|
|
@@ -285,7 +291,10 @@ async function _updateOne(
|
|
|
285
291
|
}
|
|
286
292
|
return associatedModel.updateOne(
|
|
287
293
|
where,
|
|
288
|
-
|
|
294
|
+
{
|
|
295
|
+
...singleData,
|
|
296
|
+
[foreignKey]: instance.id
|
|
297
|
+
},
|
|
289
298
|
associationUpdateOpts
|
|
290
299
|
)
|
|
291
300
|
});
|
|
@@ -305,12 +314,18 @@ async function _updateOne(
|
|
|
305
314
|
}
|
|
306
315
|
else {
|
|
307
316
|
// Note: for now we are only able to work with a model with single PrimaryKey:
|
|
308
|
-
const where = {
|
|
309
|
-
|
|
317
|
+
const where = {}
|
|
318
|
+
if (includeData[pkField] !== undefined && includeData[pkField] !== null) {
|
|
319
|
+
where[pkField] = includeData[pkField];
|
|
320
|
+
} else {
|
|
321
|
+
where[foreignKey] = instance.id;
|
|
310
322
|
}
|
|
311
323
|
fullInstanceData[association] = await associatedModel.updateOne(
|
|
312
324
|
where,
|
|
313
|
-
|
|
325
|
+
{
|
|
326
|
+
...includeData,
|
|
327
|
+
[foreignKey]: instance.id
|
|
328
|
+
},
|
|
314
329
|
associationUpdateOpts
|
|
315
330
|
);
|
|
316
331
|
}
|