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 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 (_this.isSmartField(modelSchema, condition.field)) {
45
- const fieldFound = modelSchema.fields.find(function (field) {
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 _this.getFieldTypeFromSchema(associationSchema, field) === 'String';
175
+ return _forestExpress.SchemaUtils.getFieldType(associationSchema, field) === 'String';
179
176
  }
180
177
 
181
178
  return false;
182
179
  }
183
180
 
184
- return _this.getFieldTypeFromSchema(modelSchema, field) === 'String';
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 = schema.fields.find(function (field) {
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.1.2",
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.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.2",
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",
@@ -181,260 +181,258 @@ const user = { renderingId: 1 };
181
181
  as: 'customer',
182
182
  });
183
183
 
184
- Interface.Schemas = {
185
- schemas: {
186
- user: {
187
- name: 'user',
188
- idField: 'id',
189
- primaryKeys: ['id'],
190
- isCompositePrimary: false,
191
- fields: [
192
- { field: 'id', type: 'Number' },
193
- { field: 'email', type: 'String' },
194
- { field: 'emailValid', type: 'Boolean' },
195
- { field: 'firstName', type: 'String' },
196
- { field: 'lastName', type: 'String' },
197
- { field: 'username', type: 'String' },
198
- { field: 'password', type: 'String' },
199
- { field: 'createdAt', type: 'Date' },
200
- { field: 'updatedAt', type: 'Date' },
201
- { field: 'resetPasswordToken', type: 'String' },
202
- { field: 'addresses', type: ['Number'] },
203
- { field: 'uuid', type: 'String' },
204
- { field: 'fullName', isVirtual: true, type: 'String' },
205
- { field: 'age', type: 'Number' },
206
- ],
207
- },
208
- bird: {
209
- name: 'bird',
210
- idField: 'id',
211
- primaryKeys: ['id'],
212
- isCompositePrimary: false,
213
- fields: [
214
- { field: 'id', type: 'Number' },
215
- { field: 'name', type: 'String' },
216
- { field: 'createdAt', type: 'Date' },
217
- { field: 'updatedAt', type: 'Date' },
218
- ],
219
- },
220
- bike: {
221
- name: 'bike',
222
- idField: 'id',
223
- primaryKeys: ['id'],
224
- isCompositePrimary: false,
225
- fields: [
226
- { field: 'id', type: 'String' },
227
- { field: 'name', type: 'String' },
228
- { field: 'createdAt', type: 'Date' },
229
- { field: 'updatedAt', type: 'Date' },
230
- ],
231
- },
232
- georegion: {
233
- name: 'georegion',
234
- idField: 'isocode',
235
- primaryKeys: ['isocode'],
236
- isCompositePrimary: false,
237
- fields: [
238
- { field: 'isocode', type: 'String' },
239
- { field: 'nameEnglish', type: 'String' },
240
- { field: 'nameFrench', type: 'String' },
241
- ],
242
- },
243
- address: {
244
- name: 'address',
245
- idField: 'id',
246
- primaryKeys: ['id'],
247
- isCompositePrimary: false,
248
- fields: [
249
- { field: 'id', type: 'Number' },
250
- { field: 'line', type: 'String' },
251
- { field: 'zipCode', type: 'String' },
252
- { field: 'city', type: 'String' },
253
- { field: 'country', type: 'String' },
254
- { field: 'user', type: 'Number', reference: 'user.id' },
255
- { field: 'createdAt', type: 'Date' },
256
- { field: 'updatedAt', type: 'Date' },
257
- ],
258
- },
259
- addressWithUserAlias: {
260
- name: 'addressWithUserAlias',
261
- idField: 'id',
262
- primaryKeys: ['id'],
263
- isCompositePrimary: false,
264
- fields: [
265
- { field: 'id', type: 'Number' },
266
- { field: 'line', type: 'String' },
267
- { field: 'zipCode', type: 'String' },
268
- { field: 'city', type: 'String' },
269
- { field: 'country', type: 'String' },
270
- { field: 'user', type: 'Number', reference: 'userAlias.id' },
271
- { field: 'createdAt', type: 'Date' },
272
- { field: 'updatedAt', type: 'Date' },
273
- ],
274
- },
275
- log: {
276
- name: 'log',
277
- idField: 'forestCompositePrimary',
278
- primaryKeys: ['code', 'trace'],
279
- isCompositePrimary: true,
280
- fields: [
281
- { field: 'code', type: 'String' },
282
- { field: 'trace', type: 'String' },
283
- { field: 'stack', type: 'String' },
284
- { field: 'createdAt', type: 'Date' },
285
- { field: 'updatedAt', type: 'Date' },
286
- ],
287
- },
288
- order: {
289
- name: 'order',
290
- idField: 'id',
291
- primaryKeys: ['id'],
292
- isCompositePrimary: false,
293
- searchFields: ['amount', 'comment'],
294
- fields: [
295
- { field: 'id', type: 'Number' },
296
- { field: 'amount', type: 'Number' },
297
- { field: 'comment', type: 'String' },
298
- { field: 'giftMessage', type: 'String' },
299
- ],
300
- },
301
- team: {
302
- name: 'team',
303
- idField: 'id',
304
- primaryKeys: ['id'],
305
- isCompositePrimary: false,
306
- fields: [
307
- { field: 'id', type: 'Number' },
308
- { field: 'name', type: 'String' },
309
- ],
310
- },
311
- userTeam: {
312
- name: 'userTeam',
313
- idField: 'forestCompositePrimary',
314
- primaryKeys: ['userId', 'teamId'],
315
- isCompositePrimary: true,
316
- fields: [
317
- { field: 'user', type: 'Number', reference: 'user.id' },
318
- { field: 'team', type: 'Number', reference: 'team.id' },
319
- ],
320
- },
321
- member: {
322
- name: 'member',
323
- idField: 'id',
324
- primaryKeys: ['id'],
325
- isCompositePrimary: false,
326
- fields: [
327
- { field: 'id', type: 'Number' },
328
- { field: 'name', type: 'String' },
329
- ],
330
- },
331
- membership: {
332
- name: 'membership',
333
- idField: 'id',
334
- primaryKeys: ['id'],
335
- isCompositePrimary: false,
336
- fields: [
337
- { field: 'id', type: 'Number' },
338
- { field: 'type', type: 'String' },
339
- { field: 'member', type: 'Number', reference: 'member.id' },
340
- ],
341
- },
342
- friend: {
343
- name: 'friend',
344
- idField: 'id',
345
- primaryKeys: ['id'],
346
- isCompositePrimary: false,
347
- fields: [
348
- { field: 'id', type: 'Number' },
349
- { field: 'name', type: 'String' },
350
- { field: 'member', type: 'Number', reference: 'member.id' },
351
- ],
352
- },
353
- owner: {
354
- name: 'owner',
355
- idField: 'id',
356
- primaryKeys: ['id'],
357
- isCompositePrimary: false,
358
- fields: [
359
- { field: 'id', type: 'Number' },
360
- { field: 'name', type: 'STRING' },
361
- { field: 'ownerId', type: 'Number' },
362
- ],
363
- },
364
- project: {
365
- name: 'project',
366
- idField: 'id',
367
- primaryKeys: ['id'],
368
- isCompositePrimary: false,
369
- fields: [
370
- { field: 'id', type: 'Number' },
371
- { field: 'name', type: 'STRING' },
372
- { field: 'ownerId', type: 'Number', reference: 'owner.ownerId' },
373
- ],
374
- },
375
- counter: {
376
- name: 'counter',
377
- idField: 'id',
378
- primaryKeys: ['id'],
379
- isCompositePrimary: false,
380
- fields: [
381
- { field: 'id', type: 'Number' },
382
- { field: 'clicks', type: 'Number' },
383
- { field: 'quantity', type: 'Number' },
384
- ],
385
- },
386
- customer: {
387
- name: 'owner',
388
- idField: 'id',
389
- primaryKeys: ['id'],
390
- isCompositePrimary: false,
391
- fields: [
392
- { field: 'id', type: 'Number' },
393
- { field: 'name', type: 'STRING' },
394
- ],
395
- },
396
- picture: {
397
- name: 'picture',
398
- idField: 'customerId',
399
- primaryKeys: ['customerId'],
400
- isCompositePrimary: false,
401
- fields: [
402
- { field: 'customerId', type: 'Number', reference: 'customer.id' },
403
- { field: 'name', type: 'STRING' },
404
- ],
405
- },
406
- car: {
407
- name: 'car',
408
- idField: 'id',
409
- primaryKeys: ['id'],
410
- isCompositePrimary: false,
411
- fields: [
412
- { field: 'id', type: 'Number' },
413
- { field: 'brand', type: 'String' },
414
- { field: 'model', type: 'String' },
415
- {
416
- field: 'name',
417
- isVirtual: true,
418
- type: 'String',
419
- get: (car) => `${car.brand} ${car.model}`,
420
- search: (query, search) => {
421
- const split = search.split(' ');
422
- const searchCondition = {
423
- [Sequelize.Op.and]: [
424
- { brand: { [Sequelize.Op.like]: `%${split[0]}%` } },
425
- { model: { [Sequelize.Op.like]: `%${split[1]}%` } },
426
- ],
427
- };
428
- query.where[Sequelize.Op.and][0][Sequelize.Op.or].push(searchCondition);
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
- segments: [{
434
- name: 'only monza sp*',
435
- where: () => ({ model: { [Sequelize.Op.like]: '%Monza SP%' } }),
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({ [OPERATORS.IN]: value });
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
- describe('with filter method not defined', () => {
178
- it('should throw an error', async () => {
179
- expect.assertions(1);
143
+ it('should call formatOperatorValue', async () => {
144
+ expect.assertions(5);
180
145
 
181
- schema.fields = [{
182
- field: 'smart name',
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
- describe('with filter method defined', () => {
196
- describe('when filter method return null or undefined', () => {
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
- describe('when filter method return a condition', () => {
216
- it('should return the condition', async () => {
217
- expect.assertions(4);
218
-
219
- const where = { id: 1 };
220
- schema.fields = [{
221
- field: 'smart name',
222
- type: 'String',
223
- isVirtual: true,
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
  });