nodester 0.4.3 → 0.4.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/define.js
CHANGED
|
@@ -106,7 +106,7 @@ function _getIncludesTree(data=null) {
|
|
|
106
106
|
for (const [ associationName, associationDefinition ] of associationEntries) {
|
|
107
107
|
const formatted = { association: associationName };
|
|
108
108
|
|
|
109
|
-
if (typeof data === 'object') {
|
|
109
|
+
if (!!data && typeof data === 'object') {
|
|
110
110
|
// If data (for example during create)
|
|
111
111
|
// is set, go deeper:
|
|
112
112
|
const keys = Object.keys( data );
|
|
@@ -34,10 +34,11 @@ module.exports = traverse;
|
|
|
34
34
|
* @param {ModelsTreeNode} queryNode
|
|
35
35
|
* @param {NodesterFilter} filter
|
|
36
36
|
* @param {Model} model
|
|
37
|
+
* @param {Object} association (optional)
|
|
37
38
|
*
|
|
38
39
|
* @access public
|
|
39
40
|
*/
|
|
40
|
-
function traverse(queryNode, filter=null, model=null) {
|
|
41
|
+
function traverse(queryNode, filter=null, model=null, association=null) {
|
|
41
42
|
const _model = model ?? filter.model;
|
|
42
43
|
|
|
43
44
|
try {
|
|
@@ -236,21 +237,25 @@ function traverse(queryNode, filter=null, model=null) {
|
|
|
236
237
|
const staticClausesEntries = Object.entries(filter.statics.clauses);
|
|
237
238
|
|
|
238
239
|
for (let entry of staticClausesEntries) {
|
|
239
|
-
const [clauseName, staticClauseValue] = entry;
|
|
240
|
+
const [ clauseName, staticClauseValue ] = entry;
|
|
240
241
|
|
|
241
242
|
switch(clauseName) {
|
|
242
243
|
case 'limit':
|
|
243
244
|
newQuery.limit = staticClauseValue;
|
|
244
245
|
continue;
|
|
246
|
+
|
|
245
247
|
case 'skip':
|
|
246
248
|
newQuery.offset = staticClauseValue;
|
|
247
249
|
continue;
|
|
250
|
+
|
|
248
251
|
case 'order':
|
|
249
252
|
order.order = staticClauseValue;
|
|
250
253
|
continue;
|
|
254
|
+
|
|
251
255
|
case 'order_by':
|
|
252
256
|
order.by = staticClauseValue;
|
|
253
257
|
continue;
|
|
258
|
+
|
|
254
259
|
default:
|
|
255
260
|
break;
|
|
256
261
|
}
|
|
@@ -378,7 +383,7 @@ function _traverseIncludes(includes, rootModel, filter, resultQuery) {
|
|
|
378
383
|
|
|
379
384
|
const includeModel = association.target;
|
|
380
385
|
// Build query for this include.
|
|
381
|
-
const associationQuery = traverse(include, filter.includes[includeName], includeModel);
|
|
386
|
+
const associationQuery = traverse(include, filter.includes[includeName], includeModel, association);
|
|
382
387
|
|
|
383
388
|
addAssociationQuery(associationQuery, includeName, resultQuery);
|
|
384
389
|
}
|
|
@@ -392,23 +397,24 @@ function _traverseIncludedOrders(resultQuery, rootModel) {
|
|
|
392
397
|
continue;
|
|
393
398
|
}
|
|
394
399
|
|
|
395
|
-
if (!resultQuery.order) {
|
|
396
|
-
|
|
397
|
-
}
|
|
400
|
+
// if (!resultQuery.order) {
|
|
401
|
+
// resultQuery.order = [];
|
|
402
|
+
// }
|
|
398
403
|
|
|
399
404
|
const { association } = include;
|
|
400
405
|
const {
|
|
401
|
-
associatedModel,
|
|
406
|
+
// associatedModel,
|
|
402
407
|
associationType
|
|
403
408
|
} = getModelAssociationProps(rootModel.associations[association]);
|
|
404
409
|
|
|
405
410
|
switch(associationType) {
|
|
406
411
|
case 'HasMany': {
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
]
|
|
411
|
-
|
|
412
|
+
include.separate = true;
|
|
413
|
+
// resultQuery.order.push([
|
|
414
|
+
// path,
|
|
415
|
+
// ...include.order[0]
|
|
416
|
+
// ]);
|
|
417
|
+
// delete resultQuery.include[i].order;
|
|
412
418
|
break;
|
|
413
419
|
}
|
|
414
420
|
default:
|
package/package.json
CHANGED
package/tests/nql.test.js
CHANGED
|
@@ -76,9 +76,9 @@ describe('nodester Query Language', () => {
|
|
|
76
76
|
// Horizontals queried.
|
|
77
77
|
'includes=comments(order=desc),users,likes(order=rand),reposts&id=1000',
|
|
78
78
|
// Horizontals queried №2.
|
|
79
|
+
'in=comments(order_by=index&order=asc).users.karma',
|
|
80
|
+
// Horizontals queried №3.
|
|
79
81
|
'in=reactions,comments(user_id=gte(4)&skip=10&limit=2).users,likes,reposts',
|
|
80
|
-
|
|
81
|
-
// Separated includes.
|
|
82
82
|
'includes=comments(order=rand)&id=7&limit=3&includes=users(a=id,content)',
|
|
83
83
|
];
|
|
84
84
|
|
|
@@ -150,6 +150,24 @@ describe('nodester Query Language', () => {
|
|
|
150
150
|
const lexer = new QueryLexer( queryStrings[4] );
|
|
151
151
|
const result = lexer.query;
|
|
152
152
|
|
|
153
|
+
const tree = new ModelsTree();
|
|
154
|
+
tree.include('comments').use('comments');
|
|
155
|
+
|
|
156
|
+
tree.node.order_by = 'index';
|
|
157
|
+
tree.node.order = 'asc';
|
|
158
|
+
|
|
159
|
+
tree.include('users') && tree.use('users');
|
|
160
|
+
tree.include('karma');
|
|
161
|
+
|
|
162
|
+
const expected = tree.root.toObject();
|
|
163
|
+
|
|
164
|
+
expect(result).toMatchObject(expected);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test('Horizontals queried №3', () => {
|
|
168
|
+
const lexer = new QueryLexer( queryStrings[5] );
|
|
169
|
+
const result = lexer.query;
|
|
170
|
+
|
|
153
171
|
const tree = new ModelsTree();
|
|
154
172
|
tree.include('reactions');
|
|
155
173
|
|
|
@@ -174,7 +192,7 @@ describe('nodester Query Language', () => {
|
|
|
174
192
|
});
|
|
175
193
|
|
|
176
194
|
test('Separated includes"', () => {
|
|
177
|
-
const lexer = new QueryLexer( queryStrings[
|
|
195
|
+
const lexer = new QueryLexer( queryStrings[6] );
|
|
178
196
|
const result = lexer.query;
|
|
179
197
|
|
|
180
198
|
const tree = new ModelsTree();
|