forest-express-sequelize 8.1.2 → 8.2.3
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/CHANGELOG.md +28 -0
- package/dist/services/filters-parser.js +13 -30
- package/dist/utils/orm.js +5 -3
- package/package.json +3 -3
- package/test/databases.test.js +251 -253
- package/test/services/filters-parser.test.js +27 -104
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
+
## [8.2.3](https://github.com/ForestAdmin/forest-express-sequelize/compare/v8.2.2...v8.2.3) (2021-09-21)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* **security:** patch semantic-release dependency vulnerabilities ([#810](https://github.com/ForestAdmin/forest-express-sequelize/issues/810)) ([305f69f](https://github.com/ForestAdmin/forest-express-sequelize/commit/305f69fcfc391f33087be4ff89061af611fe5a6f))
|
|
7
|
+
|
|
8
|
+
## [8.2.2](https://github.com/ForestAdmin/forest-express-sequelize/compare/v8.2.1...v8.2.2) (2021-09-14)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* **segments:** after fixing performance issue, some segments were ignored by delete and smart actions ([#808](https://github.com/ForestAdmin/forest-express-sequelize/issues/808)) ([8611f61](https://github.com/ForestAdmin/forest-express-sequelize/commit/8611f61e9193417ff77d52f65879d15641b7c2c8))
|
|
14
|
+
|
|
15
|
+
## [8.2.1](https://github.com/ForestAdmin/forest-express-sequelize/compare/v8.2.0...v8.2.1) (2021-09-14)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Bug Fixes
|
|
19
|
+
|
|
20
|
+
* **smart field:** fix filter on smart reference ([#807](https://github.com/ForestAdmin/forest-express-sequelize/issues/807)) ([41edb92](https://github.com/ForestAdmin/forest-express-sequelize/commit/41edb9269b60d18c88fbdbbe871adcd2e479c497))
|
|
21
|
+
|
|
22
|
+
# [8.2.0](https://github.com/ForestAdmin/forest-express-sequelize/compare/v8.1.2...v8.2.0) (2021-09-10)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Features
|
|
26
|
+
|
|
27
|
+
* **filter:** add "is in" filter ([#806](https://github.com/ForestAdmin/forest-express-sequelize/issues/806)) ([bb10856](https://github.com/ForestAdmin/forest-express-sequelize/commit/bb108567d0fa6b93982c39e5b5cca68468972536))
|
|
28
|
+
|
|
1
29
|
## [8.1.2](https://github.com/ForestAdmin/forest-express-sequelize/compare/v8.1.1...v8.1.2) (2021-09-08)
|
|
2
30
|
|
|
3
31
|
|
|
@@ -6,6 +6,8 @@ require("core-js/modules/es.array.iterator");
|
|
|
6
6
|
|
|
7
7
|
require("core-js/modules/es.promise");
|
|
8
8
|
|
|
9
|
+
require("core-js/modules/es.string.trim");
|
|
10
|
+
|
|
9
11
|
var _forestExpress = require("forest-express");
|
|
10
12
|
|
|
11
13
|
var _operators = _interopRequireDefault(require("../utils/operators"));
|
|
@@ -27,7 +29,7 @@ function FiltersParser(modelSchema, timezone, options) {
|
|
|
27
29
|
});
|
|
28
30
|
|
|
29
31
|
this.perform = async function (filtersString) {
|
|
30
|
-
return _forestExpress.BaseFiltersParser.perform(filtersString, _this.formatAggregation, _this.formatCondition);
|
|
32
|
+
return _forestExpress.BaseFiltersParser.perform(filtersString, _this.formatAggregation, _this.formatCondition, modelSchema);
|
|
31
33
|
};
|
|
32
34
|
|
|
33
35
|
this.formatAggregation = async function (aggregator, formattedConditions) {
|
|
@@ -38,20 +40,11 @@ function FiltersParser(modelSchema, timezone, options) {
|
|
|
38
40
|
};
|
|
39
41
|
};
|
|
40
42
|
|
|
41
|
-
this.formatCondition = async function (condition) {
|
|
43
|
+
this.formatCondition = async function (condition, isSmartField = false) {
|
|
42
44
|
const isTextField = _this.isTextField(condition.field);
|
|
43
45
|
|
|
44
|
-
if (
|
|
45
|
-
|
|
46
|
-
return field.field === condition.field;
|
|
47
|
-
});
|
|
48
|
-
if (!fieldFound.filter) throw new Error(`"filter" method missing on smart field "${fieldFound.field}"`);
|
|
49
|
-
const formattedCondition = fieldFound.filter({
|
|
50
|
-
where: _this.formatOperatorValue(condition.operator, condition.value, isTextField),
|
|
51
|
-
condition
|
|
52
|
-
});
|
|
53
|
-
if (!formattedCondition) throw new Error(`"filter" method on smart field "${fieldFound.field}" must return a condition`);
|
|
54
|
-
return formattedCondition;
|
|
46
|
+
if (isSmartField) {
|
|
47
|
+
return _this.formatOperatorValue(condition.operator, condition.value, isTextField);
|
|
55
48
|
}
|
|
56
49
|
|
|
57
50
|
const formattedField = _this.formatField(condition.field);
|
|
@@ -151,7 +144,11 @@ function FiltersParser(modelSchema, timezone, options) {
|
|
|
151
144
|
};
|
|
152
145
|
|
|
153
146
|
case 'in':
|
|
154
|
-
return {
|
|
147
|
+
return typeof value === 'string' ? {
|
|
148
|
+
[_this.OPERATORS.IN]: value.split(',').map(function (elem) {
|
|
149
|
+
return elem.trim();
|
|
150
|
+
})
|
|
151
|
+
} : {
|
|
155
152
|
[_this.OPERATORS.IN]: value
|
|
156
153
|
};
|
|
157
154
|
|
|
@@ -175,27 +172,13 @@ function FiltersParser(modelSchema, timezone, options) {
|
|
|
175
172
|
const associationSchema = getReferenceSchema(_forestExpress.Schemas.schemas, modelSchema, associationName, fieldName);
|
|
176
173
|
|
|
177
174
|
if (associationSchema) {
|
|
178
|
-
return
|
|
175
|
+
return _forestExpress.SchemaUtils.getFieldType(associationSchema, field) === 'String';
|
|
179
176
|
}
|
|
180
177
|
|
|
181
178
|
return false;
|
|
182
179
|
}
|
|
183
180
|
|
|
184
|
-
return
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
this.isSmartField = function (schema, fieldName) {
|
|
188
|
-
const fieldFound = schema.fields.find(function (field) {
|
|
189
|
-
return field.field === fieldName;
|
|
190
|
-
});
|
|
191
|
-
return !!fieldFound && !!fieldFound.isVirtual;
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
this.getFieldTypeFromSchema = function (schema, fieldName) {
|
|
195
|
-
const fieldFound = schema.fields.find(function (field) {
|
|
196
|
-
return field.field === fieldName;
|
|
197
|
-
});
|
|
198
|
-
return fieldFound && fieldFound.type;
|
|
181
|
+
return _forestExpress.SchemaUtils.getFieldType(modelSchema, field) === 'String';
|
|
199
182
|
}; // NOTICE: Look for a previous interval condition matching the following:
|
|
200
183
|
// - If the filter is a simple condition at the root the check is done right away.
|
|
201
184
|
// - There can't be a previous interval condition if the aggregator is 'or' (no meaning).
|
package/dist/utils/orm.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
const {
|
|
4
|
+
SchemaUtils
|
|
5
|
+
} = require('forest-express');
|
|
6
|
+
|
|
3
7
|
const semver = require('semver');
|
|
4
8
|
|
|
5
9
|
const REGEX_VERSION = /(\d+\.)?(\d+\.)?(\*|\d+)/;
|
|
@@ -31,9 +35,7 @@ const findRecord = function (model, recordId, options) {
|
|
|
31
35
|
};
|
|
32
36
|
|
|
33
37
|
const getColumnName = function (schema, fieldName) {
|
|
34
|
-
const schemaField =
|
|
35
|
-
return field.field === fieldName;
|
|
36
|
-
});
|
|
38
|
+
const schemaField = SchemaUtils.getField(schema, fieldName);
|
|
37
39
|
return schemaField && schemaField.columnName ? schemaField.columnName : fieldName;
|
|
38
40
|
};
|
|
39
41
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "forest-express-sequelize",
|
|
3
3
|
"description": "Official Express/Sequelize Liana for Forest",
|
|
4
|
-
"version": "8.
|
|
4
|
+
"version": "8.2.3",
|
|
5
5
|
"author": "Sandro Munda <sandro@munda.me>",
|
|
6
6
|
"contributors": [
|
|
7
7
|
"Arnaud Besnier <arnaudibesnier@gmail.com>",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"@babel/runtime": "7.10.1",
|
|
29
29
|
"bluebird": "2.9.25",
|
|
30
30
|
"core-js": "3.6.5",
|
|
31
|
-
"forest-express": "9.2.
|
|
31
|
+
"forest-express": "9.2.5",
|
|
32
32
|
"http-errors": "1.6.1",
|
|
33
33
|
"lodash": "4.17.21",
|
|
34
34
|
"moment": "2.19.4",
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
"mysql2": "2.1.0",
|
|
64
64
|
"onchange": "6.0.0",
|
|
65
65
|
"pg": "8.4.2",
|
|
66
|
-
"semantic-release": "17.4.
|
|
66
|
+
"semantic-release": "17.4.7",
|
|
67
67
|
"semantic-release-npm-deprecate-old-versions": "1.1.2",
|
|
68
68
|
"semantic-release-slack-bot": "1.6.2",
|
|
69
69
|
"sequelize": "5.21.3",
|
package/test/databases.test.js
CHANGED
|
@@ -181,260 +181,258 @@ const user = { renderingId: 1 };
|
|
|
181
181
|
as: 'customer',
|
|
182
182
|
});
|
|
183
183
|
|
|
184
|
-
Interface.Schemas = {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
search
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
[Sequelize.Op.
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
return query;
|
|
430
|
-
},
|
|
184
|
+
Interface.Schemas.schemas = {
|
|
185
|
+
user: {
|
|
186
|
+
name: 'user',
|
|
187
|
+
idField: 'id',
|
|
188
|
+
primaryKeys: ['id'],
|
|
189
|
+
isCompositePrimary: false,
|
|
190
|
+
fields: [
|
|
191
|
+
{ field: 'id', type: 'Number' },
|
|
192
|
+
{ field: 'email', type: 'String' },
|
|
193
|
+
{ field: 'emailValid', type: 'Boolean' },
|
|
194
|
+
{ field: 'firstName', type: 'String' },
|
|
195
|
+
{ field: 'lastName', type: 'String' },
|
|
196
|
+
{ field: 'username', type: 'String' },
|
|
197
|
+
{ field: 'password', type: 'String' },
|
|
198
|
+
{ field: 'createdAt', type: 'Date' },
|
|
199
|
+
{ field: 'updatedAt', type: 'Date' },
|
|
200
|
+
{ field: 'resetPasswordToken', type: 'String' },
|
|
201
|
+
{ field: 'addresses', type: ['Number'] },
|
|
202
|
+
{ field: 'uuid', type: 'String' },
|
|
203
|
+
{ field: 'fullName', isVirtual: true, type: 'String' },
|
|
204
|
+
{ field: 'age', type: 'Number' },
|
|
205
|
+
],
|
|
206
|
+
},
|
|
207
|
+
bird: {
|
|
208
|
+
name: 'bird',
|
|
209
|
+
idField: 'id',
|
|
210
|
+
primaryKeys: ['id'],
|
|
211
|
+
isCompositePrimary: false,
|
|
212
|
+
fields: [
|
|
213
|
+
{ field: 'id', type: 'Number' },
|
|
214
|
+
{ field: 'name', type: 'String' },
|
|
215
|
+
{ field: 'createdAt', type: 'Date' },
|
|
216
|
+
{ field: 'updatedAt', type: 'Date' },
|
|
217
|
+
],
|
|
218
|
+
},
|
|
219
|
+
bike: {
|
|
220
|
+
name: 'bike',
|
|
221
|
+
idField: 'id',
|
|
222
|
+
primaryKeys: ['id'],
|
|
223
|
+
isCompositePrimary: false,
|
|
224
|
+
fields: [
|
|
225
|
+
{ field: 'id', type: 'String' },
|
|
226
|
+
{ field: 'name', type: 'String' },
|
|
227
|
+
{ field: 'createdAt', type: 'Date' },
|
|
228
|
+
{ field: 'updatedAt', type: 'Date' },
|
|
229
|
+
],
|
|
230
|
+
},
|
|
231
|
+
georegion: {
|
|
232
|
+
name: 'georegion',
|
|
233
|
+
idField: 'isocode',
|
|
234
|
+
primaryKeys: ['isocode'],
|
|
235
|
+
isCompositePrimary: false,
|
|
236
|
+
fields: [
|
|
237
|
+
{ field: 'isocode', type: 'String' },
|
|
238
|
+
{ field: 'nameEnglish', type: 'String' },
|
|
239
|
+
{ field: 'nameFrench', type: 'String' },
|
|
240
|
+
],
|
|
241
|
+
},
|
|
242
|
+
address: {
|
|
243
|
+
name: 'address',
|
|
244
|
+
idField: 'id',
|
|
245
|
+
primaryKeys: ['id'],
|
|
246
|
+
isCompositePrimary: false,
|
|
247
|
+
fields: [
|
|
248
|
+
{ field: 'id', type: 'Number' },
|
|
249
|
+
{ field: 'line', type: 'String' },
|
|
250
|
+
{ field: 'zipCode', type: 'String' },
|
|
251
|
+
{ field: 'city', type: 'String' },
|
|
252
|
+
{ field: 'country', type: 'String' },
|
|
253
|
+
{ field: 'user', type: 'Number', reference: 'user.id' },
|
|
254
|
+
{ field: 'createdAt', type: 'Date' },
|
|
255
|
+
{ field: 'updatedAt', type: 'Date' },
|
|
256
|
+
],
|
|
257
|
+
},
|
|
258
|
+
addressWithUserAlias: {
|
|
259
|
+
name: 'addressWithUserAlias',
|
|
260
|
+
idField: 'id',
|
|
261
|
+
primaryKeys: ['id'],
|
|
262
|
+
isCompositePrimary: false,
|
|
263
|
+
fields: [
|
|
264
|
+
{ field: 'id', type: 'Number' },
|
|
265
|
+
{ field: 'line', type: 'String' },
|
|
266
|
+
{ field: 'zipCode', type: 'String' },
|
|
267
|
+
{ field: 'city', type: 'String' },
|
|
268
|
+
{ field: 'country', type: 'String' },
|
|
269
|
+
{ field: 'user', type: 'Number', reference: 'userAlias.id' },
|
|
270
|
+
{ field: 'createdAt', type: 'Date' },
|
|
271
|
+
{ field: 'updatedAt', type: 'Date' },
|
|
272
|
+
],
|
|
273
|
+
},
|
|
274
|
+
log: {
|
|
275
|
+
name: 'log',
|
|
276
|
+
idField: 'forestCompositePrimary',
|
|
277
|
+
primaryKeys: ['code', 'trace'],
|
|
278
|
+
isCompositePrimary: true,
|
|
279
|
+
fields: [
|
|
280
|
+
{ field: 'code', type: 'String' },
|
|
281
|
+
{ field: 'trace', type: 'String' },
|
|
282
|
+
{ field: 'stack', type: 'String' },
|
|
283
|
+
{ field: 'createdAt', type: 'Date' },
|
|
284
|
+
{ field: 'updatedAt', type: 'Date' },
|
|
285
|
+
],
|
|
286
|
+
},
|
|
287
|
+
order: {
|
|
288
|
+
name: 'order',
|
|
289
|
+
idField: 'id',
|
|
290
|
+
primaryKeys: ['id'],
|
|
291
|
+
isCompositePrimary: false,
|
|
292
|
+
searchFields: ['amount', 'comment'],
|
|
293
|
+
fields: [
|
|
294
|
+
{ field: 'id', type: 'Number' },
|
|
295
|
+
{ field: 'amount', type: 'Number' },
|
|
296
|
+
{ field: 'comment', type: 'String' },
|
|
297
|
+
{ field: 'giftMessage', type: 'String' },
|
|
298
|
+
],
|
|
299
|
+
},
|
|
300
|
+
team: {
|
|
301
|
+
name: 'team',
|
|
302
|
+
idField: 'id',
|
|
303
|
+
primaryKeys: ['id'],
|
|
304
|
+
isCompositePrimary: false,
|
|
305
|
+
fields: [
|
|
306
|
+
{ field: 'id', type: 'Number' },
|
|
307
|
+
{ field: 'name', type: 'String' },
|
|
308
|
+
],
|
|
309
|
+
},
|
|
310
|
+
userTeam: {
|
|
311
|
+
name: 'userTeam',
|
|
312
|
+
idField: 'forestCompositePrimary',
|
|
313
|
+
primaryKeys: ['userId', 'teamId'],
|
|
314
|
+
isCompositePrimary: true,
|
|
315
|
+
fields: [
|
|
316
|
+
{ field: 'user', type: 'Number', reference: 'user.id' },
|
|
317
|
+
{ field: 'team', type: 'Number', reference: 'team.id' },
|
|
318
|
+
],
|
|
319
|
+
},
|
|
320
|
+
member: {
|
|
321
|
+
name: 'member',
|
|
322
|
+
idField: 'id',
|
|
323
|
+
primaryKeys: ['id'],
|
|
324
|
+
isCompositePrimary: false,
|
|
325
|
+
fields: [
|
|
326
|
+
{ field: 'id', type: 'Number' },
|
|
327
|
+
{ field: 'name', type: 'String' },
|
|
328
|
+
],
|
|
329
|
+
},
|
|
330
|
+
membership: {
|
|
331
|
+
name: 'membership',
|
|
332
|
+
idField: 'id',
|
|
333
|
+
primaryKeys: ['id'],
|
|
334
|
+
isCompositePrimary: false,
|
|
335
|
+
fields: [
|
|
336
|
+
{ field: 'id', type: 'Number' },
|
|
337
|
+
{ field: 'type', type: 'String' },
|
|
338
|
+
{ field: 'member', type: 'Number', reference: 'member.id' },
|
|
339
|
+
],
|
|
340
|
+
},
|
|
341
|
+
friend: {
|
|
342
|
+
name: 'friend',
|
|
343
|
+
idField: 'id',
|
|
344
|
+
primaryKeys: ['id'],
|
|
345
|
+
isCompositePrimary: false,
|
|
346
|
+
fields: [
|
|
347
|
+
{ field: 'id', type: 'Number' },
|
|
348
|
+
{ field: 'name', type: 'String' },
|
|
349
|
+
{ field: 'member', type: 'Number', reference: 'member.id' },
|
|
350
|
+
],
|
|
351
|
+
},
|
|
352
|
+
owner: {
|
|
353
|
+
name: 'owner',
|
|
354
|
+
idField: 'id',
|
|
355
|
+
primaryKeys: ['id'],
|
|
356
|
+
isCompositePrimary: false,
|
|
357
|
+
fields: [
|
|
358
|
+
{ field: 'id', type: 'Number' },
|
|
359
|
+
{ field: 'name', type: 'STRING' },
|
|
360
|
+
{ field: 'ownerId', type: 'Number' },
|
|
361
|
+
],
|
|
362
|
+
},
|
|
363
|
+
project: {
|
|
364
|
+
name: 'project',
|
|
365
|
+
idField: 'id',
|
|
366
|
+
primaryKeys: ['id'],
|
|
367
|
+
isCompositePrimary: false,
|
|
368
|
+
fields: [
|
|
369
|
+
{ field: 'id', type: 'Number' },
|
|
370
|
+
{ field: 'name', type: 'STRING' },
|
|
371
|
+
{ field: 'ownerId', type: 'Number', reference: 'owner.ownerId' },
|
|
372
|
+
],
|
|
373
|
+
},
|
|
374
|
+
counter: {
|
|
375
|
+
name: 'counter',
|
|
376
|
+
idField: 'id',
|
|
377
|
+
primaryKeys: ['id'],
|
|
378
|
+
isCompositePrimary: false,
|
|
379
|
+
fields: [
|
|
380
|
+
{ field: 'id', type: 'Number' },
|
|
381
|
+
{ field: 'clicks', type: 'Number' },
|
|
382
|
+
{ field: 'quantity', type: 'Number' },
|
|
383
|
+
],
|
|
384
|
+
},
|
|
385
|
+
customer: {
|
|
386
|
+
name: 'owner',
|
|
387
|
+
idField: 'id',
|
|
388
|
+
primaryKeys: ['id'],
|
|
389
|
+
isCompositePrimary: false,
|
|
390
|
+
fields: [
|
|
391
|
+
{ field: 'id', type: 'Number' },
|
|
392
|
+
{ field: 'name', type: 'STRING' },
|
|
393
|
+
],
|
|
394
|
+
},
|
|
395
|
+
picture: {
|
|
396
|
+
name: 'picture',
|
|
397
|
+
idField: 'customerId',
|
|
398
|
+
primaryKeys: ['customerId'],
|
|
399
|
+
isCompositePrimary: false,
|
|
400
|
+
fields: [
|
|
401
|
+
{ field: 'customerId', type: 'Number', reference: 'customer.id' },
|
|
402
|
+
{ field: 'name', type: 'STRING' },
|
|
403
|
+
],
|
|
404
|
+
},
|
|
405
|
+
car: {
|
|
406
|
+
name: 'car',
|
|
407
|
+
idField: 'id',
|
|
408
|
+
primaryKeys: ['id'],
|
|
409
|
+
isCompositePrimary: false,
|
|
410
|
+
fields: [
|
|
411
|
+
{ field: 'id', type: 'Number' },
|
|
412
|
+
{ field: 'brand', type: 'String' },
|
|
413
|
+
{ field: 'model', type: 'String' },
|
|
414
|
+
{
|
|
415
|
+
field: 'name',
|
|
416
|
+
isVirtual: true,
|
|
417
|
+
type: 'String',
|
|
418
|
+
get: (car) => `${car.brand} ${car.model}`,
|
|
419
|
+
search: (query, search) => {
|
|
420
|
+
const split = search.split(' ');
|
|
421
|
+
const searchCondition = {
|
|
422
|
+
[Sequelize.Op.and]: [
|
|
423
|
+
{ brand: { [Sequelize.Op.like]: `%${split[0]}%` } },
|
|
424
|
+
{ model: { [Sequelize.Op.like]: `%${split[1]}%` } },
|
|
425
|
+
],
|
|
426
|
+
};
|
|
427
|
+
query.where[Sequelize.Op.and][0][Sequelize.Op.or].push(searchCondition);
|
|
428
|
+
return query;
|
|
431
429
|
},
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
}
|
|
437
|
-
},
|
|
430
|
+
},
|
|
431
|
+
],
|
|
432
|
+
segments: [{
|
|
433
|
+
name: 'only monza sp*',
|
|
434
|
+
where: () => ({ model: { [Sequelize.Op.like]: '%Monza SP%' } }),
|
|
435
|
+
}],
|
|
438
436
|
},
|
|
439
437
|
};
|
|
440
438
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import moment from 'moment';
|
|
2
2
|
import Sequelize from 'sequelize';
|
|
3
|
+
import { SchemaUtils } from 'forest-express';
|
|
3
4
|
import FiltersParser from '../../src/services/filters-parser';
|
|
4
5
|
import Operators from '../../src/utils/operators';
|
|
5
6
|
import { NoMatchingOperatorError } from '../../src/services/errors';
|
|
@@ -48,47 +49,8 @@ describe('services > filters-parser', () => {
|
|
|
48
49
|
{ operator: OPERATORS.LTE, value: moment().subtract(1, 'week').endOf('isoweek').toDate() },
|
|
49
50
|
]);
|
|
50
51
|
|
|
51
|
-
describe('isSmartField', () => {
|
|
52
|
-
describe('on a unknown field', () => {
|
|
53
|
-
it('should return false', () => {
|
|
54
|
-
expect.assertions(1);
|
|
55
|
-
const schemaToTest = { fields: [] };
|
|
56
|
-
|
|
57
|
-
expect(defaultFiltersParser.isSmartField(schemaToTest, 'unknown')).toBeFalse();
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
describe('on a non smart field', () => {
|
|
62
|
-
it('should return false', () => {
|
|
63
|
-
expect.assertions(1);
|
|
64
|
-
const schemaToTest = {
|
|
65
|
-
fields: [{
|
|
66
|
-
field: 'name',
|
|
67
|
-
isVirtual: false,
|
|
68
|
-
}],
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
expect(defaultFiltersParser.isSmartField(schemaToTest, 'name')).toBeFalse();
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
describe('on a smart field', () => {
|
|
76
|
-
it('should return true', () => {
|
|
77
|
-
expect.assertions(1);
|
|
78
|
-
const schemaToTest = {
|
|
79
|
-
fields: [{
|
|
80
|
-
field: 'name',
|
|
81
|
-
isVirtual: true,
|
|
82
|
-
}],
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
expect(defaultFiltersParser.isSmartField(schemaToTest, 'name')).toBeTrue();
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
|
|
90
52
|
describe('formatOperatorValue function', () => {
|
|
91
|
-
const values = [5, 'toto', null];
|
|
53
|
+
const values = [5, 'toto,tutu ', null];
|
|
92
54
|
|
|
93
55
|
values.forEach((value) => {
|
|
94
56
|
it(`should return the appropriate value (${typeof value})`, () => {
|
|
@@ -113,7 +75,11 @@ describe('services > filters-parser', () => {
|
|
|
113
75
|
[OPERATORS.EQ]: '',
|
|
114
76
|
}],
|
|
115
77
|
});
|
|
116
|
-
expect(defaultFiltersParser.formatOperatorValue('in', value)).toStrictEqual(
|
|
78
|
+
expect(defaultFiltersParser.formatOperatorValue('in', value)).toStrictEqual(
|
|
79
|
+
typeof value === 'string'
|
|
80
|
+
? { [OPERATORS.IN]: value.split(',').map((elem) => elem.trim()) }
|
|
81
|
+
: { [OPERATORS.IN]: value },
|
|
82
|
+
);
|
|
117
83
|
});
|
|
118
84
|
|
|
119
85
|
it('should raise an error on unknown operator', () => {
|
|
@@ -174,72 +140,24 @@ describe('services > filters-parser', () => {
|
|
|
174
140
|
});
|
|
175
141
|
|
|
176
142
|
describe('on a smart field', () => {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
expect.assertions(1);
|
|
143
|
+
it('should call formatOperatorValue', async () => {
|
|
144
|
+
expect.assertions(5);
|
|
180
145
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
type: 'String',
|
|
184
|
-
isVirtual: true,
|
|
185
|
-
get() {},
|
|
186
|
-
}];
|
|
187
|
-
|
|
188
|
-
await expect(defaultFiltersParser.formatCondition({
|
|
189
|
-
field: 'smart name',
|
|
190
|
-
operator: 'present',
|
|
191
|
-
})).rejects.toThrow('"filter" method missing on smart field "smart name"');
|
|
192
|
-
});
|
|
193
|
-
});
|
|
146
|
+
const resultIsTextField = 'resultOfIsTextField';
|
|
147
|
+
jest.spyOn(defaultFiltersParser, 'isTextField').mockReturnValue(resultIsTextField);
|
|
194
148
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
it('should throw an error', async () => {
|
|
198
|
-
expect.assertions(1);
|
|
199
|
-
|
|
200
|
-
schema.fields = [{
|
|
201
|
-
field: 'smart name',
|
|
202
|
-
type: 'String',
|
|
203
|
-
isVirtual: true,
|
|
204
|
-
get() {},
|
|
205
|
-
filter() {},
|
|
206
|
-
}];
|
|
207
|
-
|
|
208
|
-
await expect(defaultFiltersParser.formatCondition({
|
|
209
|
-
field: 'smart name',
|
|
210
|
-
operator: 'present',
|
|
211
|
-
})).rejects.toThrow('"filter" method on smart field "smart name" must return a condition');
|
|
212
|
-
});
|
|
213
|
-
});
|
|
149
|
+
const formattedCondition = 'myFormattedCondition';
|
|
150
|
+
jest.spyOn(defaultFiltersParser, 'formatOperatorValue').mockReturnValue(formattedCondition);
|
|
214
151
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
get() {},
|
|
225
|
-
filter: jest.fn(() => where),
|
|
226
|
-
}];
|
|
227
|
-
|
|
228
|
-
const condition = {
|
|
229
|
-
field: 'smart name',
|
|
230
|
-
operator: 'present',
|
|
231
|
-
};
|
|
232
|
-
expect(await defaultFiltersParser.formatCondition(condition)).toStrictEqual(where);
|
|
233
|
-
expect(schema.fields[0].filter.mock.calls).toHaveLength(1);
|
|
234
|
-
expect(schema.fields[0].filter.mock.calls[0]).toHaveLength(1);
|
|
235
|
-
expect(schema.fields[0].filter.mock.calls[0][0]).toStrictEqual({
|
|
236
|
-
where: {
|
|
237
|
-
[OPERATORS.NE]: null,
|
|
238
|
-
},
|
|
239
|
-
condition,
|
|
240
|
-
});
|
|
241
|
-
});
|
|
242
|
-
});
|
|
152
|
+
const condition = { field: 'power', operator: 'is', value: 'mine' };
|
|
153
|
+
expect(await defaultFiltersParser.formatCondition(condition, true))
|
|
154
|
+
.toStrictEqual(formattedCondition);
|
|
155
|
+
|
|
156
|
+
expect(defaultFiltersParser.isTextField).toHaveBeenCalledTimes(1);
|
|
157
|
+
expect(defaultFiltersParser.isTextField).toHaveBeenCalledWith('power');
|
|
158
|
+
|
|
159
|
+
expect(defaultFiltersParser.formatOperatorValue).toHaveBeenCalledTimes(1);
|
|
160
|
+
expect(defaultFiltersParser.formatOperatorValue).toHaveBeenCalledWith('is', 'mine', resultIsTextField);
|
|
243
161
|
});
|
|
244
162
|
});
|
|
245
163
|
});
|
|
@@ -314,8 +232,13 @@ describe('services > filters-parser', () => {
|
|
|
314
232
|
|
|
315
233
|
it('should not be null', async () => {
|
|
316
234
|
expect.assertions(1);
|
|
235
|
+
|
|
236
|
+
const spy = jest.spyOn(SchemaUtils, 'isSmartField').mockReturnValue(false);
|
|
237
|
+
|
|
317
238
|
expect(await filtersParser.perform(filters))
|
|
318
239
|
.toStrictEqual({ '$car.brandName$': { [OPERATORS.LIKE]: 'Ferrari%' } });
|
|
240
|
+
|
|
241
|
+
spy.mockRestore();
|
|
319
242
|
});
|
|
320
243
|
});
|
|
321
244
|
});
|