nodester 0.7.14 → 0.7.16
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/body/extract.js +16 -4
- package/lib/models/define.js +26 -20
- package/package.json +1 -1
package/lib/body/extract.js
CHANGED
|
@@ -22,9 +22,7 @@ module.exports = extract;
|
|
|
22
22
|
* @access public
|
|
23
23
|
* @return {Object} filteredBody
|
|
24
24
|
*/
|
|
25
|
-
function extract(body, filter=null, model) {
|
|
26
|
-
|
|
27
|
-
const sequelize = model.sequelize;
|
|
25
|
+
function extract(body, filter = null, model) {
|
|
28
26
|
const modelAttributes = Object.keys(model.tableAttributes);
|
|
29
27
|
const availableIncludes = Object.keys(model.associations);
|
|
30
28
|
|
|
@@ -87,6 +85,20 @@ function extract(body, filter=null, model) {
|
|
|
87
85
|
const association = model.associations[key];
|
|
88
86
|
|
|
89
87
|
// Mutliple includes:
|
|
88
|
+
if (association.associationType === 'BelongsToMany') {
|
|
89
|
+
const targetPkField = association.target.primaryKeyField;
|
|
90
|
+
|
|
91
|
+
if (Array.isArray(value)) {
|
|
92
|
+
filteredBody[key] = value.map(item => ({ [targetPkField]: item[targetPkField] }));
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
filteredBody[key] = { [targetPkField]: value[targetPkField] };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// HasMany / HasOne — recursive extraction:
|
|
90
102
|
if (Array.isArray(value)) {
|
|
91
103
|
filteredBody[key] = [];
|
|
92
104
|
|
|
@@ -111,7 +123,7 @@ function extract(body, filter=null, model) {
|
|
|
111
123
|
|
|
112
124
|
// Set all static attributes:
|
|
113
125
|
const staticAttributeEntries = Object.entries(staticAttributes);
|
|
114
|
-
for (const [
|
|
126
|
+
for (const [key, value] of staticAttributeEntries) {
|
|
115
127
|
filteredBody[key] = staticAttributes[key];
|
|
116
128
|
}
|
|
117
129
|
|
package/lib/models/define.js
CHANGED
|
@@ -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
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
if (
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
if (
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
-
|
|
130
|
+
result.push(formatted);
|
|
131
|
+
}
|
|
126
132
|
}
|
|
127
133
|
|
|
128
134
|
return result;
|