forest-express-sequelize 8.4.1 → 8.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/CHANGELOG.md +28 -0
- package/dist/services/query-options.js +6 -2
- package/dist/services/requested-fields-extractor.js +19 -3
- package/dist/services/search-builder.js +1 -1
- package/dist/utils/sequelize-compatibility.js +15 -17
- package/package.json +1 -1
- package/test/databases.test.js +62 -67
- package/test/fixtures/db.json +7 -7
- package/test/services/query-options.test.js +40 -14
- package/test/services/requested-fields-extractor.test.js +80 -2
- package/test/utils/sequelize-compatibility.test.js +42 -2
- package/types/index.d.ts +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
+
## [8.4.5](https://github.com/ForestAdmin/forest-express-sequelize/compare/v8.4.4...v8.4.5) (2021-10-21)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* nested include duplication in smart field search ([#863](https://github.com/ForestAdmin/forest-express-sequelize/issues/863)) ([d61da0c](https://github.com/ForestAdmin/forest-express-sequelize/commit/d61da0c2b9dbf337e26ac1f2d816855787023afe))
|
|
7
|
+
|
|
8
|
+
## [8.4.4](https://github.com/ForestAdmin/forest-express-sequelize/compare/v8.4.3...v8.4.4) (2021-10-20)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* **segment query:** segment query returns no result when primary key is camelCased ([#859](https://github.com/ForestAdmin/forest-express-sequelize/issues/859)) ([7244a11](https://github.com/ForestAdmin/forest-express-sequelize/commit/7244a11acfe730830d7baa5d70c5d7dc6d14fe0e))
|
|
14
|
+
|
|
15
|
+
## [8.4.3](https://github.com/ForestAdmin/forest-express-sequelize/compare/v8.4.2...v8.4.3) (2021-10-20)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Bug Fixes
|
|
19
|
+
|
|
20
|
+
* **smart-field:** smart field should allow to request all fields ([#861](https://github.com/ForestAdmin/forest-express-sequelize/issues/861)) ([8f27c23](https://github.com/ForestAdmin/forest-express-sequelize/commit/8f27c236aa58501b426ec1fd0306b1b38e92bde1))
|
|
21
|
+
|
|
22
|
+
## [8.4.2](https://github.com/ForestAdmin/forest-express-sequelize/compare/v8.4.1...v8.4.2) (2021-10-20)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Bug Fixes
|
|
26
|
+
|
|
27
|
+
* **retrocompatibility:** handle sequelize includables on smart field search ([#850](https://github.com/ForestAdmin/forest-express-sequelize/issues/850)) ([114e88f](https://github.com/ForestAdmin/forest-express-sequelize/commit/114e88fc4ea008fff2cf8db1e72d638085b4d492))
|
|
28
|
+
|
|
1
29
|
## [8.4.1](https://github.com/ForestAdmin/forest-express-sequelize/compare/v8.4.0...v8.4.1) (2021-10-20)
|
|
2
30
|
|
|
3
31
|
|
|
@@ -252,7 +252,11 @@ class QueryOptions {
|
|
|
252
252
|
}
|
|
253
253
|
|
|
254
254
|
if (include) {
|
|
255
|
-
|
|
255
|
+
if (Array.isArray(include)) {
|
|
256
|
+
this._customerIncludes.push(...include);
|
|
257
|
+
} else {
|
|
258
|
+
this._customerIncludes.push(include);
|
|
259
|
+
}
|
|
256
260
|
}
|
|
257
261
|
|
|
258
262
|
return helper.getFieldsSearched();
|
|
@@ -291,7 +295,7 @@ class QueryOptions {
|
|
|
291
295
|
async segmentQuery(query) {
|
|
292
296
|
if (!query) return;
|
|
293
297
|
|
|
294
|
-
const
|
|
298
|
+
const primaryKey = _lodash.default.values(this._model.primaryKeys)[0].field;
|
|
295
299
|
|
|
296
300
|
const queryToFilterRecords = query.trim();
|
|
297
301
|
new _liveQueryChecker.default().perform(queryToFilterRecords);
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
require("core-js/modules/es.array.flat.js");
|
|
4
|
-
|
|
5
3
|
require("core-js/modules/es.regexp.exec.js");
|
|
6
4
|
|
|
5
|
+
require("core-js/modules/es.array.flat.js");
|
|
6
|
+
|
|
7
7
|
require("core-js/modules/es.array.iterator.js");
|
|
8
8
|
|
|
9
9
|
/**
|
|
@@ -37,6 +37,21 @@ function extractRequestedFieldsForAssociation(associationName, requestedFields,
|
|
|
37
37
|
return `${associationName}.${fieldName}`;
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
|
+
|
|
41
|
+
function extractRequestedSmartField(requestedFields, schema) {
|
|
42
|
+
const schemaSmartFields = schema.fields.filter(function ({
|
|
43
|
+
isVirtual
|
|
44
|
+
}) {
|
|
45
|
+
return isVirtual;
|
|
46
|
+
});
|
|
47
|
+
return requestedFields[schema.name].split(',').filter(function (fieldName) {
|
|
48
|
+
return schemaSmartFields.some(function ({
|
|
49
|
+
field
|
|
50
|
+
}) {
|
|
51
|
+
return field === fieldName;
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
}
|
|
40
55
|
/**
|
|
41
56
|
* @param {Record<string, string>} requestedFields
|
|
42
57
|
* @param {*} modelOrAssociation
|
|
@@ -65,7 +80,8 @@ function extractRequestedFields(requestedFields, modelOrAssociation, schemas) {
|
|
|
65
80
|
const modelFields = requestedFields[modelOrAssociation.name].split(',').filter(function (fieldName) {
|
|
66
81
|
return !requestedFields[fieldName];
|
|
67
82
|
});
|
|
68
|
-
|
|
83
|
+
const smartFields = extractRequestedSmartField(requestedFields, schemas[modelOrAssociation.name]);
|
|
84
|
+
return Array.from(new Set([...primaryKeyArray, ...modelFields, ...smartFields, ...allAssociationFields]));
|
|
69
85
|
}
|
|
70
86
|
|
|
71
87
|
module.exports = extractRequestedFields;
|
|
@@ -119,7 +119,7 @@ function SearchBuilder(model, opts, params, fieldNamesRequested) {
|
|
|
119
119
|
try {
|
|
120
120
|
await field.search(query, params.search);
|
|
121
121
|
} catch (error) {
|
|
122
|
-
const errorMessage = `Cannot search properly on Smart Field ${field.field}`;
|
|
122
|
+
const errorMessage = `Cannot search properly on Smart Field ${field.field}: `;
|
|
123
123
|
Interface.logger.error(errorMessage, error);
|
|
124
124
|
}
|
|
125
125
|
|
|
@@ -12,8 +12,6 @@ var _objectTools = _interopRequireDefault(require("./object-tools"));
|
|
|
12
12
|
|
|
13
13
|
var _operators = _interopRequireDefault(require("./operators"));
|
|
14
14
|
|
|
15
|
-
var _orm = require("./orm");
|
|
16
|
-
|
|
17
15
|
var _query = _interopRequireDefault(require("./query"));
|
|
18
16
|
|
|
19
17
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
@@ -110,9 +108,13 @@ function normalizeInclude(model, include) {
|
|
|
110
108
|
|
|
111
109
|
|
|
112
110
|
if (include.include) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
111
|
+
if (Array.isArray(include.include)) {
|
|
112
|
+
include.include = include.include.map(function (childInclude) {
|
|
113
|
+
return normalizeInclude(include.model, childInclude);
|
|
114
|
+
});
|
|
115
|
+
} else {
|
|
116
|
+
include.include = [normalizeInclude(include.model, include.include)];
|
|
117
|
+
}
|
|
116
118
|
}
|
|
117
119
|
|
|
118
120
|
return include;
|
|
@@ -154,8 +156,9 @@ function removeDuplicateAssociations(model, includeList) {
|
|
|
154
156
|
|
|
155
157
|
|
|
156
158
|
includeList.forEach(function (include) {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
+
const association = model.associations[include.as];
|
|
160
|
+
|
|
161
|
+
if (include.include && association) {
|
|
159
162
|
removeDuplicateAssociations(association.target, include.include);
|
|
160
163
|
}
|
|
161
164
|
});
|
|
@@ -169,15 +172,10 @@ exports.postProcess = function (model, rawOptions) {
|
|
|
169
172
|
Sequelize: model.sequelize.constructor
|
|
170
173
|
});
|
|
171
174
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
removeDuplicateAssociations(model, options.include);
|
|
178
|
-
} else {
|
|
179
|
-
bubbleWheresInPlace(operators, options);
|
|
180
|
-
}
|
|
181
|
-
|
|
175
|
+
options.include = options.include.map(function (include) {
|
|
176
|
+
return normalizeInclude(model, include);
|
|
177
|
+
});
|
|
178
|
+
bubbleWheresInPlace(operators, options);
|
|
179
|
+
removeDuplicateAssociations(model, options.include);
|
|
182
180
|
return options;
|
|
183
181
|
};
|
package/package.json
CHANGED
package/test/databases.test.js
CHANGED
|
@@ -26,6 +26,9 @@ const user = { renderingId: 1 };
|
|
|
26
26
|
const options = { Sequelize, connections: { sequelize } };
|
|
27
27
|
|
|
28
28
|
models.user = sequelize.define('user', {
|
|
29
|
+
primaryId: {
|
|
30
|
+
type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true,
|
|
31
|
+
},
|
|
29
32
|
email: { type: Sequelize.STRING, unique: true, validate: { isEmail: true } },
|
|
30
33
|
emailValid: { type: Sequelize.BOOLEAN },
|
|
31
34
|
firstName: { type: Sequelize.STRING },
|
|
@@ -152,11 +155,11 @@ const user = { renderingId: 1 };
|
|
|
152
155
|
},
|
|
153
156
|
});
|
|
154
157
|
|
|
155
|
-
models.address.belongsTo(models.user);
|
|
156
|
-
models.addressWithUserAlias.belongsTo(models.user, { as: 'userAlias' });
|
|
157
|
-
models.user.hasMany(models.address);
|
|
158
|
-
models.team.belongsToMany(models.user, { through: 'userTeam' });
|
|
159
|
-
models.user.belongsToMany(models.team, { through: 'userTeam' });
|
|
158
|
+
models.address.belongsTo(models.user, { foreignKey: 'userId' });
|
|
159
|
+
models.addressWithUserAlias.belongsTo(models.user, { foreignKey: 'userId', as: 'userAlias' });
|
|
160
|
+
models.user.hasMany(models.address, { foreignKey: 'userId' });
|
|
161
|
+
models.team.belongsToMany(models.user, { through: 'userTeam', otherKey: 'userId' });
|
|
162
|
+
models.user.belongsToMany(models.team, { through: 'userTeam', foreignKey: 'userId' });
|
|
160
163
|
models.membership.belongsTo(models.member);
|
|
161
164
|
models.member.hasOne(models.membership);
|
|
162
165
|
models.member.hasMany(models.friend);
|
|
@@ -184,11 +187,11 @@ const user = { renderingId: 1 };
|
|
|
184
187
|
Interface.Schemas.schemas = {
|
|
185
188
|
user: {
|
|
186
189
|
name: 'user',
|
|
187
|
-
idField: '
|
|
188
|
-
primaryKeys: ['
|
|
190
|
+
idField: 'primaryId',
|
|
191
|
+
primaryKeys: ['primaryId'],
|
|
189
192
|
isCompositePrimary: false,
|
|
190
193
|
fields: [
|
|
191
|
-
{ field: '
|
|
194
|
+
{ field: 'primaryId', type: 'Number' },
|
|
192
195
|
{ field: 'email', type: 'String' },
|
|
193
196
|
{ field: 'emailValid', type: 'Boolean' },
|
|
194
197
|
{ field: 'firstName', type: 'String' },
|
|
@@ -250,7 +253,7 @@ const user = { renderingId: 1 };
|
|
|
250
253
|
{ field: 'zipCode', type: 'String' },
|
|
251
254
|
{ field: 'city', type: 'String' },
|
|
252
255
|
{ field: 'country', type: 'String' },
|
|
253
|
-
{ field: 'user', type: 'Number', reference: 'user.
|
|
256
|
+
{ field: 'user', type: 'Number', reference: 'user.primaryId' },
|
|
254
257
|
{ field: 'createdAt', type: 'Date' },
|
|
255
258
|
{ field: 'updatedAt', type: 'Date' },
|
|
256
259
|
],
|
|
@@ -266,7 +269,7 @@ const user = { renderingId: 1 };
|
|
|
266
269
|
{ field: 'zipCode', type: 'String' },
|
|
267
270
|
{ field: 'city', type: 'String' },
|
|
268
271
|
{ field: 'country', type: 'String' },
|
|
269
|
-
{ field: 'user', type: 'Number', reference: 'userAlias.
|
|
272
|
+
{ field: 'user', type: 'Number', reference: 'userAlias.primaryId' },
|
|
270
273
|
{ field: 'createdAt', type: 'Date' },
|
|
271
274
|
{ field: 'updatedAt', type: 'Date' },
|
|
272
275
|
],
|
|
@@ -313,7 +316,7 @@ const user = { renderingId: 1 };
|
|
|
313
316
|
primaryKeys: ['userId', 'teamId'],
|
|
314
317
|
isCompositePrimary: true,
|
|
315
318
|
fields: [
|
|
316
|
-
{ field: 'user', type: 'Number', reference: 'user.
|
|
319
|
+
{ field: 'user', type: 'Number', reference: 'user.primaryId' },
|
|
317
320
|
{ field: 'team', type: 'Number', reference: 'team.id' },
|
|
318
321
|
],
|
|
319
322
|
},
|
|
@@ -458,9 +461,9 @@ const user = { renderingId: 1 };
|
|
|
458
461
|
it('should define an idField', async () => {
|
|
459
462
|
expect.assertions(3);
|
|
460
463
|
const schema = await initializeSchema('user');
|
|
461
|
-
expect(schema.idField).toStrictEqual('
|
|
464
|
+
expect(schema.idField).toStrictEqual('primaryId');
|
|
462
465
|
expect(schema.primaryKeys).toHaveLength(1);
|
|
463
|
-
expect(schema.primaryKeys[0]).toStrictEqual('
|
|
466
|
+
expect(schema.primaryKeys[0]).toStrictEqual('primaryId');
|
|
464
467
|
});
|
|
465
468
|
|
|
466
469
|
it('should not detect a composite primary key', async () => {
|
|
@@ -566,7 +569,7 @@ const user = { renderingId: 1 };
|
|
|
566
569
|
...baseParams,
|
|
567
570
|
type: 'Pie',
|
|
568
571
|
collection: 'addressWithUserAlias',
|
|
569
|
-
group_by_field: 'userAlias:
|
|
572
|
+
group_by_field: 'userAlias:primaryId',
|
|
570
573
|
aggregate: 'Count',
|
|
571
574
|
time_range: null,
|
|
572
575
|
filters: null,
|
|
@@ -687,7 +690,7 @@ const user = { renderingId: 1 };
|
|
|
687
690
|
group_by_date_field: 'createdAt',
|
|
688
691
|
aggregate: 'Count',
|
|
689
692
|
time_range: 'Year',
|
|
690
|
-
filters: JSON.stringify({ field: 'user:
|
|
693
|
+
filters: JSON.stringify({ field: 'user:primaryId', operator: 'equal', value: 100 }),
|
|
691
694
|
}, options, user).perform();
|
|
692
695
|
expect(stat.value).toHaveLength(1);
|
|
693
696
|
} finally {
|
|
@@ -709,7 +712,7 @@ const user = { renderingId: 1 };
|
|
|
709
712
|
...baseParams,
|
|
710
713
|
type: 'Value',
|
|
711
714
|
aggregate: 'Sum',
|
|
712
|
-
aggregate_field: '
|
|
715
|
+
aggregate_field: 'primaryId',
|
|
713
716
|
}, options, user).perform();
|
|
714
717
|
|
|
715
718
|
expect(stat.value).toStrictEqual({ countCurrent: 305, countPrevious: undefined });
|
|
@@ -729,7 +732,7 @@ const user = { renderingId: 1 };
|
|
|
729
732
|
...baseParams,
|
|
730
733
|
type: 'Value',
|
|
731
734
|
aggregate: 'Sum',
|
|
732
|
-
aggregate_field: '
|
|
735
|
+
aggregate_field: 'primaryId',
|
|
733
736
|
filters: '{"field":"createdAt","operator":"previous_month_to_date","value":null}',
|
|
734
737
|
}, options, user).perform();
|
|
735
738
|
|
|
@@ -750,12 +753,12 @@ const user = { renderingId: 1 };
|
|
|
750
753
|
...baseParams,
|
|
751
754
|
type: 'Value',
|
|
752
755
|
aggregate: 'Sum',
|
|
753
|
-
aggregate_field: '
|
|
756
|
+
aggregate_field: 'primaryId',
|
|
754
757
|
filters: JSON.stringify({
|
|
755
758
|
aggregator: 'and',
|
|
756
759
|
conditions: [
|
|
757
760
|
{ field: 'createdAt', operator: 'previous_month_to_date', value: null },
|
|
758
|
-
{ field: '
|
|
761
|
+
{ field: 'primaryId', operator: 'greater_than', value: 100 },
|
|
759
762
|
],
|
|
760
763
|
}),
|
|
761
764
|
}, options, user).perform();
|
|
@@ -777,7 +780,7 @@ const user = { renderingId: 1 };
|
|
|
777
780
|
...baseParams,
|
|
778
781
|
type: 'Value',
|
|
779
782
|
aggregate: 'Count',
|
|
780
|
-
filters: '{"field":"user:
|
|
783
|
+
filters: '{"field":"user:primaryId","operator":"greater_than","value":0}',
|
|
781
784
|
}, options, user).perform();
|
|
782
785
|
|
|
783
786
|
expect(stat.value).toStrictEqual({ countCurrent: 4, countPrevious: undefined });
|
|
@@ -796,7 +799,7 @@ const user = { renderingId: 1 };
|
|
|
796
799
|
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
797
800
|
try {
|
|
798
801
|
const result = await new ResourceCreator(models.user, baseParams, {
|
|
799
|
-
|
|
802
|
+
primaryId: '1',
|
|
800
803
|
email: 'jack@forestadmin.com',
|
|
801
804
|
firstName: 'Jack',
|
|
802
805
|
lastName: 'Lumberjack',
|
|
@@ -804,7 +807,7 @@ const user = { renderingId: 1 };
|
|
|
804
807
|
password: 'bonpoissonnet',
|
|
805
808
|
teams: [],
|
|
806
809
|
}, user).perform();
|
|
807
|
-
expect(result.
|
|
810
|
+
expect(result.primaryId).toStrictEqual(1);
|
|
808
811
|
expect(result.firstName).toStrictEqual('Jack');
|
|
809
812
|
expect(result.username).toStrictEqual('Jacouille');
|
|
810
813
|
|
|
@@ -1130,7 +1133,7 @@ const user = { renderingId: 1 };
|
|
|
1130
1133
|
try {
|
|
1131
1134
|
const params = {
|
|
1132
1135
|
...baseParams,
|
|
1133
|
-
fields: { user: '
|
|
1136
|
+
fields: { user: 'primaryprimaryId,firstName,lastName,username,password,createdAt,updatedAt,resetPasswordToken' },
|
|
1134
1137
|
page: { number: '1' },
|
|
1135
1138
|
};
|
|
1136
1139
|
await new ResourcesGetter(models.user, null, params, user).perform();
|
|
@@ -1150,7 +1153,7 @@ const user = { renderingId: 1 };
|
|
|
1150
1153
|
const params = {
|
|
1151
1154
|
...baseParams,
|
|
1152
1155
|
fields: {
|
|
1153
|
-
user: '
|
|
1156
|
+
user: 'primaryprimaryId,firstName,lastName,username,password,createdAt,updatedAt,resetPasswordToken',
|
|
1154
1157
|
},
|
|
1155
1158
|
page: { number: '1', size: '30' },
|
|
1156
1159
|
};
|
|
@@ -1184,8 +1187,8 @@ const user = { renderingId: 1 };
|
|
|
1184
1187
|
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
1185
1188
|
const params = {
|
|
1186
1189
|
...baseParams,
|
|
1187
|
-
fields: { user: '
|
|
1188
|
-
sort: '-
|
|
1190
|
+
fields: { user: 'primaryId,firstName,lastName,username,password,createdAt,updatedAt,resetPasswordToken' },
|
|
1191
|
+
sort: '-primaryId',
|
|
1189
1192
|
page: { number: '1', size: '30' },
|
|
1190
1193
|
};
|
|
1191
1194
|
try {
|
|
@@ -1221,7 +1224,7 @@ const user = { renderingId: 1 };
|
|
|
1221
1224
|
const params = {
|
|
1222
1225
|
...baseParams,
|
|
1223
1226
|
fields: {
|
|
1224
|
-
user: '
|
|
1227
|
+
user: 'primaryId,firstName,lastName,username,password,createdAt,updatedAt,resetPasswordToken',
|
|
1225
1228
|
},
|
|
1226
1229
|
page: { number: '1', size: '30' },
|
|
1227
1230
|
search: 'hello',
|
|
@@ -1257,7 +1260,7 @@ const user = { renderingId: 1 };
|
|
|
1257
1260
|
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
1258
1261
|
const params = {
|
|
1259
1262
|
...baseParams,
|
|
1260
|
-
fields: { user: '
|
|
1263
|
+
fields: { user: 'primaryId,firstName,lastName,username,password,createdAt,updatedAt,resetPasswordToken,age' },
|
|
1261
1264
|
page: { number: '1', size: '30' },
|
|
1262
1265
|
search: '10',
|
|
1263
1266
|
};
|
|
@@ -1397,7 +1400,7 @@ const user = { renderingId: 1 };
|
|
|
1397
1400
|
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
1398
1401
|
const params = {
|
|
1399
1402
|
...baseParams,
|
|
1400
|
-
fields: { user: '
|
|
1403
|
+
fields: { user: 'primaryId,firstName,lastName,username,password,createdAt,updatedAt,resetPasswordToken,age' },
|
|
1401
1404
|
page: { number: '1', size: '30' },
|
|
1402
1405
|
segmentQuery: "SELECT * FROM users WHERE users.email = 'richard@piedpiper.com'",
|
|
1403
1406
|
};
|
|
@@ -1436,7 +1439,7 @@ const user = { renderingId: 1 };
|
|
|
1436
1439
|
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
1437
1440
|
const params = {
|
|
1438
1441
|
...baseParams,
|
|
1439
|
-
fields: { user: '
|
|
1442
|
+
fields: { user: 'primaryId,firstName,lastName,username,password,createdAt,updatedAt,resetPasswordToken,age' },
|
|
1440
1443
|
page: { number: '1', size: '30' },
|
|
1441
1444
|
segmentQuery: 'SELECT * FROM use',
|
|
1442
1445
|
};
|
|
@@ -1810,7 +1813,7 @@ const user = { renderingId: 1 };
|
|
|
1810
1813
|
describe('request on the resources getter with filters conditions', () => {
|
|
1811
1814
|
const paramsBaseList = {
|
|
1812
1815
|
...baseParams,
|
|
1813
|
-
fields: { user: '
|
|
1816
|
+
fields: { user: 'primaryprimaryId,firstName,lastName,username,password,createdAt,updatedAt,resetPasswordToken' },
|
|
1814
1817
|
page: { number: '1', size: '30' },
|
|
1815
1818
|
};
|
|
1816
1819
|
|
|
@@ -1831,7 +1834,7 @@ const user = { renderingId: 1 };
|
|
|
1831
1834
|
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
1832
1835
|
const params = {
|
|
1833
1836
|
...paramsBaseList,
|
|
1834
|
-
filters: JSON.stringify({ field: '
|
|
1837
|
+
filters: JSON.stringify({ field: 'primaryId', operator: 'equal', value: 100 }),
|
|
1835
1838
|
};
|
|
1836
1839
|
|
|
1837
1840
|
try {
|
|
@@ -1849,7 +1852,7 @@ const user = { renderingId: 1 };
|
|
|
1849
1852
|
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
1850
1853
|
const params = {
|
|
1851
1854
|
...paramsBaseCount,
|
|
1852
|
-
filters: JSON.stringify({ field: '
|
|
1855
|
+
filters: JSON.stringify({ field: 'primaryId', operator: 'equal', value: 100 }),
|
|
1853
1856
|
};
|
|
1854
1857
|
|
|
1855
1858
|
try {
|
|
@@ -1869,7 +1872,7 @@ const user = { renderingId: 1 };
|
|
|
1869
1872
|
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
1870
1873
|
const params = {
|
|
1871
1874
|
...paramsBaseList,
|
|
1872
|
-
filters: JSON.stringify({ field: '
|
|
1875
|
+
filters: JSON.stringify({ field: 'primaryId', operator: 'greater_than', value: 101 }),
|
|
1873
1876
|
};
|
|
1874
1877
|
|
|
1875
1878
|
try {
|
|
@@ -1887,7 +1890,7 @@ const user = { renderingId: 1 };
|
|
|
1887
1890
|
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
1888
1891
|
const params = {
|
|
1889
1892
|
...paramsBaseCount,
|
|
1890
|
-
filters: JSON.stringify({ field: '
|
|
1893
|
+
filters: JSON.stringify({ field: 'primaryId', operator: 'greater_than', value: 101 }),
|
|
1891
1894
|
};
|
|
1892
1895
|
|
|
1893
1896
|
try {
|
|
@@ -1907,7 +1910,7 @@ const user = { renderingId: 1 };
|
|
|
1907
1910
|
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
1908
1911
|
const params = {
|
|
1909
1912
|
...paramsBaseList,
|
|
1910
|
-
filters: JSON.stringify({ field: '
|
|
1913
|
+
filters: JSON.stringify({ field: 'primaryId', operator: 'less_than', value: 104 }),
|
|
1911
1914
|
};
|
|
1912
1915
|
|
|
1913
1916
|
try {
|
|
@@ -1925,7 +1928,7 @@ const user = { renderingId: 1 };
|
|
|
1925
1928
|
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
1926
1929
|
const params = {
|
|
1927
1930
|
...paramsBaseCount,
|
|
1928
|
-
filters: JSON.stringify({ field: '
|
|
1931
|
+
filters: JSON.stringify({ field: 'primaryId', operator: 'less_than', value: 104 }),
|
|
1929
1932
|
};
|
|
1930
1933
|
|
|
1931
1934
|
try {
|
|
@@ -1945,7 +1948,7 @@ const user = { renderingId: 1 };
|
|
|
1945
1948
|
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
1946
1949
|
const params = {
|
|
1947
1950
|
...paramsBaseList,
|
|
1948
|
-
filters: JSON.stringify({ field: '
|
|
1951
|
+
filters: JSON.stringify({ field: 'primaryId', operator: 'not_equal', value: 100 }),
|
|
1949
1952
|
};
|
|
1950
1953
|
|
|
1951
1954
|
try {
|
|
@@ -1963,7 +1966,7 @@ const user = { renderingId: 1 };
|
|
|
1963
1966
|
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
1964
1967
|
const params = {
|
|
1965
1968
|
...paramsBaseCount,
|
|
1966
|
-
filters: JSON.stringify({ field: '
|
|
1969
|
+
filters: JSON.stringify({ field: 'primaryId', operator: 'not_equal', value: 100 }),
|
|
1967
1970
|
};
|
|
1968
1971
|
|
|
1969
1972
|
try {
|
|
@@ -2583,11 +2586,7 @@ const user = { renderingId: 1 };
|
|
|
2583
2586
|
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
2584
2587
|
const params = {
|
|
2585
2588
|
...paramsBaseList,
|
|
2586
|
-
filters: JSON.stringify({
|
|
2587
|
-
field: 'id',
|
|
2588
|
-
operator: 'in',
|
|
2589
|
-
value: [100, 101, 102],
|
|
2590
|
-
}),
|
|
2589
|
+
filters: JSON.stringify({ field: 'primaryId', operator: 'in', value: [100, 101, 102] }),
|
|
2591
2590
|
};
|
|
2592
2591
|
|
|
2593
2592
|
try {
|
|
@@ -2607,11 +2606,7 @@ const user = { renderingId: 1 };
|
|
|
2607
2606
|
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
2608
2607
|
const params = {
|
|
2609
2608
|
...paramsBaseList,
|
|
2610
|
-
filters: JSON.stringify({
|
|
2611
|
-
field: 'id',
|
|
2612
|
-
operator: 'in',
|
|
2613
|
-
value: [100, 101, 102],
|
|
2614
|
-
}),
|
|
2609
|
+
filters: JSON.stringify({ field: 'primaryId', operator: 'in', value: [100, 101, 102] }),
|
|
2615
2610
|
};
|
|
2616
2611
|
|
|
2617
2612
|
try {
|
|
@@ -2644,7 +2639,7 @@ const user = { renderingId: 1 };
|
|
|
2644
2639
|
value: 'Lumb',
|
|
2645
2640
|
}],
|
|
2646
2641
|
}, {
|
|
2647
|
-
field: '
|
|
2642
|
+
field: 'primaryId',
|
|
2648
2643
|
operator: 'greater_than',
|
|
2649
2644
|
value: 12,
|
|
2650
2645
|
}],
|
|
@@ -2689,7 +2684,7 @@ const user = { renderingId: 1 };
|
|
|
2689
2684
|
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
2690
2685
|
const params = {
|
|
2691
2686
|
...baseParams,
|
|
2692
|
-
fields: { user: '
|
|
2687
|
+
fields: { user: 'primaryId,firstName,lastName,username,password,createdAt,updatedAt,resetPasswordToken' },
|
|
2693
2688
|
page: { number: '2', size: '50' },
|
|
2694
2689
|
filters: JSON.stringify({ field: 'username', operator: 'contains', value: 'hello' }),
|
|
2695
2690
|
search: 'world',
|
|
@@ -2731,7 +2726,7 @@ const user = { renderingId: 1 };
|
|
|
2731
2726
|
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
2732
2727
|
const params = {
|
|
2733
2728
|
...baseParams,
|
|
2734
|
-
fields: { user: '
|
|
2729
|
+
fields: { user: 'primaryId', address: 'line,zipCode,city,country,user' },
|
|
2735
2730
|
page: { number: '1', size: '10' },
|
|
2736
2731
|
search: '1a11dc05-4e04-4d8f-958b-0a9f23a141a3',
|
|
2737
2732
|
searchExtended: 1,
|
|
@@ -2773,10 +2768,10 @@ const user = { renderingId: 1 };
|
|
|
2773
2768
|
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
2774
2769
|
const params = {
|
|
2775
2770
|
...baseParams,
|
|
2776
|
-
fields: { user: '
|
|
2771
|
+
fields: { user: 'primaryId,firstName,lastName,username,password,createdAt,updatedAt,resetPasswordToken' },
|
|
2777
2772
|
page: { number: '2', size: '50' },
|
|
2778
2773
|
filters: JSON.stringify({ field: 'username', operator: 'contains', value: 'hello' }),
|
|
2779
|
-
sort: '-
|
|
2774
|
+
sort: '-primaryId',
|
|
2780
2775
|
search: 'world',
|
|
2781
2776
|
};
|
|
2782
2777
|
|
|
@@ -2816,10 +2811,10 @@ const user = { renderingId: 1 };
|
|
|
2816
2811
|
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
2817
2812
|
const params = {
|
|
2818
2813
|
...baseParams,
|
|
2819
|
-
fields: { user: '
|
|
2814
|
+
fields: { user: 'primaryId,firstName,lastName,username,password,createdAt,updatedAt,resetPasswordToken' },
|
|
2820
2815
|
page: { number: '1', size: '50' },
|
|
2821
|
-
sort: '-
|
|
2822
|
-
segmentQuery: 'select * from users
|
|
2816
|
+
sort: '-primaryId',
|
|
2817
|
+
segmentQuery: 'select * from users limit 2;',
|
|
2823
2818
|
};
|
|
2824
2819
|
|
|
2825
2820
|
try {
|
|
@@ -2836,7 +2831,7 @@ const user = { renderingId: 1 };
|
|
|
2836
2831
|
const { models } = initializeSequelize();
|
|
2837
2832
|
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
2838
2833
|
const params = {
|
|
2839
|
-
segmentQuery: 'select * from users
|
|
2834
|
+
segmentQuery: 'select * from users limit 2',
|
|
2840
2835
|
timezone: 'Europe/Paris',
|
|
2841
2836
|
};
|
|
2842
2837
|
try {
|
|
@@ -2866,7 +2861,7 @@ const user = { renderingId: 1 };
|
|
|
2866
2861
|
expect(result[0]).not.toBeEmpty();
|
|
2867
2862
|
expect(result[0][0]).toHaveProperty('user');
|
|
2868
2863
|
expect(result[0][0].user.dataValues).toHaveProperty('firstName');
|
|
2869
|
-
expect(result[0][0].user.dataValues).toHaveProperty('
|
|
2864
|
+
expect(result[0][0].user.dataValues).toHaveProperty('primaryId');
|
|
2870
2865
|
expect(result[0][0].user.dataValues).not.toHaveProperty('lastName');
|
|
2871
2866
|
} finally {
|
|
2872
2867
|
spy.mockRestore();
|
|
@@ -2889,8 +2884,8 @@ const user = { renderingId: 1 };
|
|
|
2889
2884
|
const result = await new ResourcesGetter(models.address, null, params).perform();
|
|
2890
2885
|
expect(result[0]).not.toBeEmpty();
|
|
2891
2886
|
expect(result[0][0]).toHaveProperty('user');
|
|
2887
|
+
expect(result[0][0].user.dataValues).toHaveProperty('primaryId');
|
|
2892
2888
|
expect(result[0][0].user.dataValues).toHaveProperty('firstName');
|
|
2893
|
-
expect(result[0][0].user.dataValues).toHaveProperty('id');
|
|
2894
2889
|
expect(result[0][0].user.dataValues).toHaveProperty('lastName');
|
|
2895
2890
|
} finally {
|
|
2896
2891
|
spy.mockRestore();
|
|
@@ -2991,7 +2986,7 @@ const user = { renderingId: 1 };
|
|
|
2991
2986
|
associationName: 'addresses',
|
|
2992
2987
|
fields: {
|
|
2993
2988
|
address: 'line,zipCode,city,country',
|
|
2994
|
-
user: '
|
|
2989
|
+
user: 'primaryId',
|
|
2995
2990
|
},
|
|
2996
2991
|
page: { number: '1', size: '20' },
|
|
2997
2992
|
};
|
|
@@ -3006,7 +3001,7 @@ const user = { renderingId: 1 };
|
|
|
3006
3001
|
expect(result[0]).not.toBeEmpty();
|
|
3007
3002
|
|
|
3008
3003
|
const firstEntry = result[0][0];
|
|
3009
|
-
expect(Object.keys(firstEntry.user.dataValues)).toStrictEqual(['
|
|
3004
|
+
expect(Object.keys(firstEntry.user.dataValues)).toStrictEqual(['primaryId']);
|
|
3010
3005
|
} finally {
|
|
3011
3006
|
spy.mockRestore();
|
|
3012
3007
|
connectionManager.closeConnection();
|
|
@@ -3111,7 +3106,7 @@ const user = { renderingId: 1 };
|
|
|
3111
3106
|
associationName: 'addresses',
|
|
3112
3107
|
fields: { address: 'line,zipCode,city,country,user' },
|
|
3113
3108
|
page: { number: '1', size: '20' },
|
|
3114
|
-
sort: '-user.
|
|
3109
|
+
sort: '-user.primaryId',
|
|
3115
3110
|
};
|
|
3116
3111
|
try {
|
|
3117
3112
|
const result = await new HasManyGetter(
|
|
@@ -3171,7 +3166,7 @@ const user = { renderingId: 1 };
|
|
|
3171
3166
|
user,
|
|
3172
3167
|
).perform();
|
|
3173
3168
|
expect(result[0]).not.toBeEmpty();
|
|
3174
|
-
expect(result[0][0].user.dataValues).toHaveProperty('
|
|
3169
|
+
expect(result[0][0].user.dataValues).toHaveProperty('primaryId');
|
|
3175
3170
|
expect(result[0][0].user.dataValues).toHaveProperty('firstName');
|
|
3176
3171
|
expect(result[0][0].user.dataValues).toHaveProperty('lastName');
|
|
3177
3172
|
} finally {
|
|
@@ -3202,7 +3197,7 @@ const user = { renderingId: 1 };
|
|
|
3202
3197
|
expect(result[0]).not.toBeEmpty();
|
|
3203
3198
|
expect(result[0][0]).toHaveProperty('user');
|
|
3204
3199
|
expect(result[0][0].user.dataValues).toHaveProperty('firstName');
|
|
3205
|
-
expect(result[0][0].user.dataValues).toHaveProperty('
|
|
3200
|
+
expect(result[0][0].user.dataValues).toHaveProperty('primaryId');
|
|
3206
3201
|
expect(result[0][0].user.dataValues).not.toHaveProperty('lastName');
|
|
3207
3202
|
} finally {
|
|
3208
3203
|
spy.mockRestore();
|
|
@@ -3226,7 +3221,7 @@ const user = { renderingId: 1 };
|
|
|
3226
3221
|
},
|
|
3227
3222
|
page: { number: '1', size: '20' },
|
|
3228
3223
|
search: 'SF',
|
|
3229
|
-
sort: '-user.
|
|
3224
|
+
sort: '-user.primaryId',
|
|
3230
3225
|
};
|
|
3231
3226
|
try {
|
|
3232
3227
|
const result = await new HasManyGetter(
|
|
@@ -3279,7 +3274,7 @@ const user = { renderingId: 1 };
|
|
|
3279
3274
|
try {
|
|
3280
3275
|
const record = await new ResourceGetter(models.user, params, user).perform();
|
|
3281
3276
|
expect(record).not.toBeNull();
|
|
3282
|
-
expect(record.
|
|
3277
|
+
expect(record.primaryId).toStrictEqual(100);
|
|
3283
3278
|
expect(record.firstName).toStrictEqual('Richard');
|
|
3284
3279
|
} finally {
|
|
3285
3280
|
spy.mockRestore();
|
package/test/fixtures/db.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
{
|
|
10
10
|
"model": "user",
|
|
11
11
|
"data": {
|
|
12
|
-
"
|
|
12
|
+
"primaryId": 100,
|
|
13
13
|
"email": "richard@piedpiper.com",
|
|
14
14
|
"firstName": "Richard",
|
|
15
15
|
"lastName": "Hendricks",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"id": 100,
|
|
33
33
|
"city": "SF",
|
|
34
34
|
"country": "USA",
|
|
35
|
-
"
|
|
35
|
+
"userId": 100,
|
|
36
36
|
"archivedAt": "2020-07-02 09:42:08.893+00"
|
|
37
37
|
}
|
|
38
38
|
},
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"id": 101,
|
|
43
43
|
"city": "Paris",
|
|
44
44
|
"country": "France",
|
|
45
|
-
"
|
|
45
|
+
"userId": 100,
|
|
46
46
|
"archivedAt": "2020-07-02 09:42:08.893+00"
|
|
47
47
|
}
|
|
48
48
|
},
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"data": {
|
|
52
52
|
"id": 102,
|
|
53
53
|
"city": "Hong Kong",
|
|
54
|
-
"
|
|
54
|
+
"userId": 100,
|
|
55
55
|
"archivedAt": "2020-07-02 09:42:08.893+00"
|
|
56
56
|
}
|
|
57
57
|
},
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
"id": 103,
|
|
62
62
|
"city": "London",
|
|
63
63
|
"country": "United Kingdom",
|
|
64
|
-
"
|
|
64
|
+
"userId": 100
|
|
65
65
|
}
|
|
66
66
|
},
|
|
67
67
|
{
|
|
@@ -75,7 +75,7 @@
|
|
|
75
75
|
{
|
|
76
76
|
"model": "user",
|
|
77
77
|
"data": {
|
|
78
|
-
"
|
|
78
|
+
"primaryId": 102,
|
|
79
79
|
"email": "elrich@piedpiper.com",
|
|
80
80
|
"firstName": "Elrich",
|
|
81
81
|
"lastName": "Bachman",
|
|
@@ -88,7 +88,7 @@
|
|
|
88
88
|
{
|
|
89
89
|
"model": "user",
|
|
90
90
|
"data": {
|
|
91
|
-
"
|
|
91
|
+
"primaryId": 103,
|
|
92
92
|
"email": "dinesh@piedpiper.com",
|
|
93
93
|
"firstName": "Dinesh",
|
|
94
94
|
"lastName": "Chugtai",
|
|
@@ -3,6 +3,17 @@ import Interface from 'forest-express';
|
|
|
3
3
|
import QueryOptions from '../../src/services/query-options';
|
|
4
4
|
|
|
5
5
|
describe('services > query-options', () => {
|
|
6
|
+
const resetSchema = () => {
|
|
7
|
+
Interface.Schemas = {
|
|
8
|
+
schemas: {
|
|
9
|
+
actor: {
|
|
10
|
+
idField: 'id',
|
|
11
|
+
fields: [{ field: 'smartField' }, { field: 'secondSmartField' }],
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
|
|
6
17
|
const buildModelMock = (dialect) => {
|
|
7
18
|
// Sequelize is created here without connection to a database
|
|
8
19
|
const sequelize = new Sequelize({ dialect });
|
|
@@ -10,16 +21,9 @@ describe('services > query-options', () => {
|
|
|
10
21
|
const modelActor = sequelize.define('actor', {});
|
|
11
22
|
const modelMovie = sequelize.define('movie', {});
|
|
12
23
|
|
|
13
|
-
|
|
24
|
+
resetSchema();
|
|
14
25
|
|
|
15
|
-
|
|
16
|
-
schemas: {
|
|
17
|
-
actor: {
|
|
18
|
-
idField: 'id',
|
|
19
|
-
fields: [{ field: 'smartField', search: (query) => { query.include = 'movie'; } }],
|
|
20
|
-
},
|
|
21
|
-
},
|
|
22
|
-
};
|
|
26
|
+
modelActor.belongsTo(modelMovie);
|
|
23
27
|
|
|
24
28
|
return modelActor;
|
|
25
29
|
};
|
|
@@ -80,23 +84,30 @@ describe('services > query-options', () => {
|
|
|
80
84
|
|
|
81
85
|
const options = new QueryOptions(model);
|
|
82
86
|
await options.search('search string', null);
|
|
83
|
-
expect(loggerErrorSpy).toHaveBeenCalledWith('Cannot search properly on Smart Field smartField', errorThrown);
|
|
87
|
+
expect(loggerErrorSpy).toHaveBeenCalledWith('Cannot search properly on Smart Field smartField: ', errorThrown);
|
|
84
88
|
|
|
85
89
|
loggerErrorSpy.mockClear();
|
|
90
|
+
resetSchema();
|
|
86
91
|
});
|
|
87
92
|
});
|
|
88
93
|
|
|
89
|
-
it('should add the search
|
|
94
|
+
it('should add the search includes', async () => {
|
|
90
95
|
expect.assertions(1);
|
|
91
96
|
|
|
92
97
|
Interface.Schemas.schemas.actor.fields[0].search = async (query) => {
|
|
93
98
|
await Promise.resolve();
|
|
94
|
-
query.include
|
|
99
|
+
query.include.push('movie');
|
|
100
|
+
};
|
|
101
|
+
Interface.Schemas.schemas.actor.fields[1].search = async (query) => {
|
|
102
|
+
await Promise.resolve();
|
|
103
|
+
query.include.push('toto');
|
|
95
104
|
};
|
|
96
105
|
|
|
97
106
|
const options = new QueryOptions(model);
|
|
98
107
|
await options.search('search string', null);
|
|
99
|
-
expect(options._customerIncludes).toStrictEqual(['movie']);
|
|
108
|
+
expect(options._customerIncludes).toStrictEqual(['movie', 'toto']);
|
|
109
|
+
|
|
110
|
+
resetSchema();
|
|
100
111
|
});
|
|
101
112
|
});
|
|
102
113
|
|
|
@@ -111,9 +122,24 @@ describe('services > query-options', () => {
|
|
|
111
122
|
|
|
112
123
|
const options = new QueryOptions(model);
|
|
113
124
|
await options.search('search string', null);
|
|
114
|
-
expect(loggerErrorSpy).toHaveBeenCalledWith('Cannot search properly on Smart Field smartField', errorThrown);
|
|
125
|
+
expect(loggerErrorSpy).toHaveBeenCalledWith('Cannot search properly on Smart Field smartField: ', errorThrown);
|
|
115
126
|
|
|
116
127
|
loggerErrorSpy.mockClear();
|
|
128
|
+
resetSchema();
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
describe('when smartField return none array include', () => {
|
|
133
|
+
it('should transform include to array', async () => {
|
|
134
|
+
expect.assertions(1);
|
|
135
|
+
|
|
136
|
+
Interface.Schemas.schemas.actor.fields[0].search = (query) => { query.include = 'movie'; };
|
|
137
|
+
|
|
138
|
+
const options = new QueryOptions(model);
|
|
139
|
+
await options.search('search string', null);
|
|
140
|
+
expect(options._customerIncludes).toStrictEqual(['movie']);
|
|
141
|
+
|
|
142
|
+
resetSchema();
|
|
117
143
|
});
|
|
118
144
|
});
|
|
119
145
|
});
|
|
@@ -25,7 +25,17 @@ describe('services > requested-fields-extractor', () => {
|
|
|
25
25
|
associations: {},
|
|
26
26
|
};
|
|
27
27
|
|
|
28
|
-
const
|
|
28
|
+
const schemas = {
|
|
29
|
+
user: {
|
|
30
|
+
name: 'user',
|
|
31
|
+
fields: [{
|
|
32
|
+
field: 'name',
|
|
33
|
+
isVirtual: false,
|
|
34
|
+
}],
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const result = extractRequestedFields(fields, model, schemas);
|
|
29
39
|
|
|
30
40
|
expect(result).toStrictEqual(['id', 'name']);
|
|
31
41
|
});
|
|
@@ -43,7 +53,17 @@ describe('services > requested-fields-extractor', () => {
|
|
|
43
53
|
associations: {},
|
|
44
54
|
};
|
|
45
55
|
|
|
46
|
-
const
|
|
56
|
+
const schemas = {
|
|
57
|
+
user: {
|
|
58
|
+
name: 'user',
|
|
59
|
+
fields: [{
|
|
60
|
+
field: 'name',
|
|
61
|
+
isVirtual: false,
|
|
62
|
+
}],
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const result = extractRequestedFields(fields, model, schemas);
|
|
47
67
|
|
|
48
68
|
expect(result).toStrictEqual(['id', 'name']);
|
|
49
69
|
});
|
|
@@ -70,7 +90,15 @@ describe('services > requested-fields-extractor', () => {
|
|
|
70
90
|
};
|
|
71
91
|
|
|
72
92
|
const schemas = {
|
|
93
|
+
user: {
|
|
94
|
+
name: 'user',
|
|
95
|
+
fields: [{
|
|
96
|
+
field: 'name',
|
|
97
|
+
isVirtual: false,
|
|
98
|
+
}],
|
|
99
|
+
},
|
|
73
100
|
addresses: {
|
|
101
|
+
name: 'addresses',
|
|
74
102
|
fields: [{
|
|
75
103
|
field: 'street',
|
|
76
104
|
isVirtual: false,
|
|
@@ -105,7 +133,15 @@ describe('services > requested-fields-extractor', () => {
|
|
|
105
133
|
};
|
|
106
134
|
|
|
107
135
|
const schemas = {
|
|
136
|
+
user: {
|
|
137
|
+
name: 'user',
|
|
138
|
+
fields: [{
|
|
139
|
+
field: 'name',
|
|
140
|
+
isVirtual: false,
|
|
141
|
+
}],
|
|
142
|
+
},
|
|
108
143
|
addresses: {
|
|
144
|
+
name: 'addresses',
|
|
109
145
|
fields: [
|
|
110
146
|
{
|
|
111
147
|
field: 'street',
|
|
@@ -147,7 +183,15 @@ describe('services > requested-fields-extractor', () => {
|
|
|
147
183
|
};
|
|
148
184
|
|
|
149
185
|
const schemas = {
|
|
186
|
+
user: {
|
|
187
|
+
name: 'user',
|
|
188
|
+
fields: [{
|
|
189
|
+
field: 'name',
|
|
190
|
+
isVirtual: false,
|
|
191
|
+
}],
|
|
192
|
+
},
|
|
150
193
|
addresses: {
|
|
194
|
+
name: 'addresses',
|
|
151
195
|
fields: [
|
|
152
196
|
{
|
|
153
197
|
field: 'street',
|
|
@@ -166,4 +210,38 @@ describe('services > requested-fields-extractor', () => {
|
|
|
166
210
|
'homeAddress',
|
|
167
211
|
]);
|
|
168
212
|
});
|
|
213
|
+
|
|
214
|
+
it('should include requested smart field', () => {
|
|
215
|
+
expect.assertions(1);
|
|
216
|
+
|
|
217
|
+
const fields = {
|
|
218
|
+
user: 'smartField',
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const model = {
|
|
222
|
+
name: 'user',
|
|
223
|
+
primaryKeys: { id: null, uid: null },
|
|
224
|
+
associations: {},
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
const schemas = {
|
|
228
|
+
user: {
|
|
229
|
+
name: 'user',
|
|
230
|
+
fields: [{
|
|
231
|
+
field: 'smartField',
|
|
232
|
+
isVirtual: true,
|
|
233
|
+
}, {
|
|
234
|
+
field: 'anotherSmartField',
|
|
235
|
+
isVirtual: true,
|
|
236
|
+
}],
|
|
237
|
+
},
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
const result = extractRequestedFields(fields, model, schemas);
|
|
241
|
+
|
|
242
|
+
expect(result).toStrictEqual([
|
|
243
|
+
'id',
|
|
244
|
+
'smartField',
|
|
245
|
+
]);
|
|
246
|
+
});
|
|
169
247
|
});
|
|
@@ -107,7 +107,7 @@ describe('utils > sequelize-compatibility', () => {
|
|
|
107
107
|
});
|
|
108
108
|
});
|
|
109
109
|
|
|
110
|
-
it('
|
|
110
|
+
it('should add attributes when both sides are defined', () => {
|
|
111
111
|
expect.assertions(1);
|
|
112
112
|
|
|
113
113
|
const options = postProcess(Model, {
|
|
@@ -122,7 +122,7 @@ describe('utils > sequelize-compatibility', () => {
|
|
|
122
122
|
});
|
|
123
123
|
});
|
|
124
124
|
|
|
125
|
-
it('
|
|
125
|
+
it('should drop attributes when either side is undefined', () => {
|
|
126
126
|
expect.assertions(1);
|
|
127
127
|
|
|
128
128
|
const options = postProcess(Model, {
|
|
@@ -233,5 +233,45 @@ describe('utils > sequelize-compatibility', () => {
|
|
|
233
233
|
},
|
|
234
234
|
});
|
|
235
235
|
});
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
it('should add attributes when sub include is not an array', () => {
|
|
239
|
+
expect.assertions(1);
|
|
240
|
+
|
|
241
|
+
const options = postProcess(Model, {
|
|
242
|
+
include: [{ as: 'submodelAlias', attributes: ['id'], include: 'subsubmodel1Alias' }],
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
expect(options).toStrictEqual({
|
|
246
|
+
include: [{
|
|
247
|
+
as: 'submodelAlias',
|
|
248
|
+
model: SubModel,
|
|
249
|
+
attributes: ['id'],
|
|
250
|
+
include: [{ as: 'subsubmodel1Alias', model: SubSubModel1 }],
|
|
251
|
+
}],
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
describe('postProcess -> removeDuplicateAssociations', () => {
|
|
257
|
+
describe('when include alias is not valid', () => {
|
|
258
|
+
it('should not throw an error', () => {
|
|
259
|
+
expect.assertions(1);
|
|
260
|
+
|
|
261
|
+
const include = [{ model: SubModel, as: 'notAValidAlias', include: SubModel }];
|
|
262
|
+
|
|
263
|
+
expect(() => postProcess(Model, { include })).not.toThrow();
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
describe('when the main model is reincluded', () => {
|
|
268
|
+
it('should not throw an error', () => {
|
|
269
|
+
expect.assertions(1);
|
|
270
|
+
|
|
271
|
+
const include = [{ model: Model, include: SubSubModel1 }];
|
|
272
|
+
|
|
273
|
+
expect(() => postProcess(Model, { include })).not.toThrow();
|
|
274
|
+
});
|
|
275
|
+
});
|
|
236
276
|
});
|
|
237
277
|
});
|
package/types/index.d.ts
CHANGED
|
@@ -222,7 +222,7 @@ export interface SmartFieldValueSetter<M extends Sequelize.Model = any> {
|
|
|
222
222
|
}
|
|
223
223
|
|
|
224
224
|
export interface SmartFieldSearchQuery {
|
|
225
|
-
include:
|
|
225
|
+
include: Sequelize.Includeable | Sequelize.Includeable[],
|
|
226
226
|
where: Sequelize.WhereOptions,
|
|
227
227
|
}
|
|
228
228
|
|