nodester 0.7.15 → 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.
@@ -2,7 +2,7 @@
2
2
  * nodester
3
3
  * MIT Licensed
4
4
  */
5
-
5
+
6
6
  'use strict';
7
7
 
8
8
  // CRUD mixins.
@@ -97,32 +97,38 @@ function defineModel(
97
97
  *
98
98
  * @alias getIncludesTree
99
99
  */
100
- function _getIncludesTree(data=null) {
100
+ function _getIncludesTree(data = null) {
101
101
  const result = [];
102
102
 
103
+ if (!data || typeof data !== 'object') {
104
+ return result;
105
+ }
106
+
103
107
  const associations = this.associations;
104
108
  const associationEntries = Object.entries(associations);
105
-
106
- for (const [ associationName, associationDefinition ] of associationEntries) {
107
- const formatted = { association: associationName };
108
-
109
- if (!!data && typeof data === 'object') {
110
- // If data (for example during create)
111
- // is set, go deeper:
112
- const keys = Object.keys( data );
113
- if (keys.indexOf(associationName) > 0) {
114
- const associationModel = associationDefinition.target;
115
-
116
- if (Object.entries(associationModel.associations).length > 0) {
117
- const deepData = data[ associationName ];
118
- formatted.include = associationModel.getIncludesTree(
119
- Array.isArray(deepData) ? deepData[0] : deepData
120
- );
109
+ const keys = Object.keys(data);
110
+
111
+ for (const [associationName, associationDefinition] of associationEntries) {
112
+ // Only include if present in data:
113
+ if (keys.indexOf(associationName) > -1) {
114
+ const formatted = { association: associationName };
115
+ const associationModel = associationDefinition.target;
116
+
117
+ if (Object.entries(associationModel.associations).length > 0) {
118
+ const deepData = data[associationName];
119
+ const subData = Array.isArray(deepData) ? deepData[0] : deepData;
120
+
121
+ // Only recurse if subData is an object (not just an ID or null):
122
+ if (!!subData && typeof subData === 'object' && Object.keys(subData).length > 0) {
123
+ const subInclude = associationModel.getIncludesTree(subData);
124
+ if (subInclude.length > 0) {
125
+ formatted.include = subInclude;
126
+ }
121
127
  }
122
128
  }
123
- }
124
129
 
125
- result.push( formatted );
130
+ result.push(formatted);
131
+ }
126
132
  }
127
133
 
128
134
  return result;
@@ -218,7 +218,13 @@ async function _updateOne(
218
218
  ) {
219
219
  try {
220
220
  const include = opts.include ?? [];
221
- let instance = await this.findOne({ where, include });
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
- singleData,
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
- [pkField]: includeData[pkField]
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
- includeData,
325
+ {
326
+ ...includeData,
327
+ [foreignKey]: instance.id
328
+ },
314
329
  associationUpdateOpts
315
330
  );
316
331
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodester",
3
- "version": "0.7.15",
3
+ "version": "0.7.17",
4
4
  "description": "A versatile REST framework for Node.js",
5
5
  "directories": {
6
6
  "docs": "docs",