forest-express-sequelize 9.2.1 → 9.2.2
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 +7 -0
- package/dist/adapters/sequelize.js +6 -31
- package/dist/index.js +2 -50
- package/dist/public/parse-filter.js +0 -1
- package/dist/services/apimap-field-builder.js +12 -40
- package/dist/services/apimap-field-type-detector.js +2 -15
- package/dist/services/belongs-to-updater.js +5 -16
- package/dist/services/errors.js +0 -5
- package/dist/services/filters-parser.js +9 -55
- package/dist/services/has-many-associator.js +3 -6
- package/dist/services/has-many-dissociator.js +0 -13
- package/dist/services/has-many-getter.js +2 -22
- package/dist/services/leaderboard-stat-getter.js +1 -22
- package/dist/services/line-stat-getter.js +2 -59
- package/dist/services/pie-stat-getter.js +0 -33
- package/dist/services/primary-keys-manager.js +5 -28
- package/dist/services/query-builder.js +4 -13
- package/dist/services/query-options.js +21 -78
- package/dist/services/query-stat-getter.js +3 -8
- package/dist/services/requested-fields-extractor.js +9 -14
- package/dist/services/resource-creator.js +10 -37
- package/dist/services/resource-getter.js +0 -11
- package/dist/services/resource-remover.js +0 -4
- package/dist/services/resource-updater.js +0 -15
- package/dist/services/resources-exporter.js +0 -13
- package/dist/services/resources-getter.js +8 -28
- package/dist/services/resources-remover.js +0 -9
- package/dist/services/search-builder.js +6 -71
- package/dist/services/value-stat-getter.js +7 -30
- package/dist/utils/association-record.js +0 -6
- package/dist/utils/database.js +0 -3
- package/dist/utils/object-tools.js +1 -8
- package/dist/utils/operators.js +0 -9
- package/dist/utils/orm.js +0 -12
- package/dist/utils/query.js +5 -16
- package/dist/utils/records-decorator.js +0 -10
- package/dist/utils/sequelize-compatibility.js +9 -38
- package/package.json +1 -1
|
@@ -1,37 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
4
|
require("core-js/modules/es.array.iterator.js");
|
|
6
|
-
|
|
7
5
|
require("core-js/modules/es.promise.js");
|
|
8
|
-
|
|
9
6
|
require("core-js/modules/es.string.trim.js");
|
|
10
|
-
|
|
11
7
|
require("core-js/modules/es.regexp.exec.js");
|
|
12
|
-
|
|
13
8
|
var _forestExpress = require("forest-express");
|
|
14
|
-
|
|
15
9
|
var _lodash = _interopRequireDefault(require("lodash"));
|
|
16
|
-
|
|
17
10
|
var _database = require("../utils/database");
|
|
18
|
-
|
|
19
11
|
var _operators = _interopRequireDefault(require("../utils/operators"));
|
|
20
|
-
|
|
21
12
|
var _query = _interopRequireDefault(require("../utils/query"));
|
|
22
|
-
|
|
23
13
|
var _sequelizeCompatibility = _interopRequireDefault(require("../utils/sequelize-compatibility"));
|
|
24
|
-
|
|
25
14
|
var _errors = require("./errors");
|
|
26
|
-
|
|
27
15
|
var _filtersParser = _interopRequireDefault(require("./filters-parser"));
|
|
28
|
-
|
|
29
16
|
var _primaryKeysManager = _interopRequireDefault(require("./primary-keys-manager"));
|
|
30
|
-
|
|
31
17
|
var _queryBuilder = _interopRequireDefault(require("./query-builder"));
|
|
32
|
-
|
|
33
18
|
var _searchBuilder = _interopRequireDefault(require("./search-builder"));
|
|
34
|
-
|
|
35
19
|
/**
|
|
36
20
|
* Sequelize query options generator which is configured using forest admin concepts (filters,
|
|
37
21
|
* search, segments, ...).
|
|
@@ -47,70 +31,60 @@ class QueryOptions {
|
|
|
47
31
|
if (this._sequelizeWhere) options.where = this._sequelizeWhere;
|
|
48
32
|
if (this._sequelizeInclude) options.include = this._sequelizeInclude;
|
|
49
33
|
if (this._sequelizeOrder.length) options.order = this._sequelizeOrder;
|
|
50
|
-
|
|
51
34
|
if (this._offset !== undefined && this._limit !== undefined) {
|
|
52
35
|
options.offset = this._offset;
|
|
53
36
|
options.limit = this._limit;
|
|
54
37
|
}
|
|
55
|
-
|
|
56
38
|
if (this._requestedFields.size && !this._hasRequestedSmartFields) {
|
|
57
39
|
// Restricting loaded fields on the root model is opt-in with sequelize to avoid
|
|
58
40
|
// side-effects as this was not supported historically and it would probably break
|
|
59
41
|
// smart fields.
|
|
60
42
|
// @see https://github.com/ForestAdmin/forest-express-sequelize/blob/7d7ad0/src/services/resources-getter.js#L142
|
|
43
|
+
|
|
61
44
|
const simpleSchemaFields = this._schema.fields.filter(function (field) {
|
|
62
45
|
return !field.reference;
|
|
63
46
|
}).map(function (field) {
|
|
64
47
|
return field.field;
|
|
65
48
|
});
|
|
66
|
-
|
|
67
49
|
options.attributes = [...this._requestedFields].filter(function (field) {
|
|
68
50
|
return simpleSchemaFields.includes(field);
|
|
69
51
|
});
|
|
70
52
|
options.attributes.push(...this._schema.primaryKeys);
|
|
71
53
|
}
|
|
72
|
-
|
|
73
54
|
return _sequelizeCompatibility.default.postProcess(this._model, options);
|
|
74
55
|
}
|
|
56
|
+
|
|
75
57
|
/**
|
|
76
58
|
* Used to support segments defined as a sequelize scope.
|
|
77
59
|
* This feature is _not_ in the documentation, but support should be kept.
|
|
78
60
|
*/
|
|
79
|
-
|
|
80
|
-
|
|
81
61
|
get sequelizeScopes() {
|
|
82
62
|
return this._scopes;
|
|
83
63
|
}
|
|
84
|
-
/** Compute sequelize query `.where` property */
|
|
85
|
-
|
|
86
64
|
|
|
65
|
+
/** Compute sequelize query `.where` property */
|
|
87
66
|
get _sequelizeWhere() {
|
|
88
67
|
const operators = _operators.default.getInstance({
|
|
89
68
|
Sequelize: this._Sequelize
|
|
90
69
|
});
|
|
91
|
-
|
|
92
70
|
switch (this._where.length) {
|
|
93
71
|
case 0:
|
|
94
72
|
return null;
|
|
95
|
-
|
|
96
73
|
case 1:
|
|
97
74
|
return this._where[0];
|
|
98
|
-
|
|
99
75
|
default:
|
|
100
76
|
return _query.default.mergeWhere(operators, ...this._where);
|
|
101
77
|
}
|
|
102
78
|
}
|
|
103
|
-
/** Compute sequelize query `.include` property */
|
|
104
|
-
|
|
105
79
|
|
|
80
|
+
/** Compute sequelize query `.include` property */
|
|
106
81
|
get _sequelizeInclude() {
|
|
107
82
|
const fields = [...this._requestedFields, ...this._requestedRelations, ...this._neededFields];
|
|
108
83
|
const include = [...new _queryBuilder.default().getIncludes(this._model, fields), ...this._customerIncludes];
|
|
109
84
|
return include.length ? include : null;
|
|
110
85
|
}
|
|
111
|
-
/** Compute sequelize query `.order` property */
|
|
112
|
-
|
|
113
86
|
|
|
87
|
+
/** Compute sequelize query `.order` property */
|
|
114
88
|
get _sequelizeOrder() {
|
|
115
89
|
if ((0, _database.isMSSQL)(this._model.sequelize)) {
|
|
116
90
|
// Work around sequelize bug: https://github.com/sequelize/sequelize/issues/11258
|
|
@@ -119,17 +93,15 @@ class QueryOptions {
|
|
|
119
93
|
return !primaryKeys.includes(order[0]);
|
|
120
94
|
});
|
|
121
95
|
}
|
|
122
|
-
|
|
123
96
|
return this._order;
|
|
124
97
|
}
|
|
125
|
-
|
|
126
98
|
get _hasRequestedSmartFields() {
|
|
127
99
|
var _this = this;
|
|
128
|
-
|
|
129
100
|
return this._schema.fields.some(function (field) {
|
|
130
101
|
return field.isVirtual && _this._requestedFields.has(field.field);
|
|
131
102
|
});
|
|
132
103
|
}
|
|
104
|
+
|
|
133
105
|
/**
|
|
134
106
|
* @param {sequelize.model} model Sequelize model that should be targeted
|
|
135
107
|
* @param {boolean} options.includeRelations Include BelongsTo and HasOne relations by default
|
|
@@ -138,28 +110,25 @@ class QueryOptions {
|
|
|
138
110
|
* sequelize.fn('lower', sequelize.col('<alias>.col')) in the search-builder with
|
|
139
111
|
* has-many-getter.
|
|
140
112
|
*/
|
|
141
|
-
|
|
142
|
-
|
|
143
113
|
constructor(model, options = {}) {
|
|
144
114
|
var _this2 = this;
|
|
145
|
-
|
|
146
115
|
this._Sequelize = model.sequelize.constructor;
|
|
147
116
|
this._schema = _forestExpress.Schemas.schemas[model.name];
|
|
148
117
|
this._model = model.unscoped();
|
|
149
|
-
this._options = options;
|
|
118
|
+
this._options = options;
|
|
150
119
|
|
|
120
|
+
// Used to compute relations that will go in the final 'include'
|
|
151
121
|
this._requestedFields = new Set();
|
|
152
122
|
this._requestedRelations = new Set();
|
|
153
123
|
this._neededFields = new Set();
|
|
154
124
|
this._scopes = []; // @see sequelizeScopes getter
|
|
155
|
-
// Other sequelize params
|
|
156
125
|
|
|
126
|
+
// Other sequelize params
|
|
157
127
|
this._where = [];
|
|
158
128
|
this._order = [];
|
|
159
129
|
this._offset = undefined;
|
|
160
130
|
this._limit = undefined;
|
|
161
131
|
this._customerIncludes = [];
|
|
162
|
-
|
|
163
132
|
if (this._options.includeRelations) {
|
|
164
133
|
_lodash.default.values(this._model.associations).filter(function (association) {
|
|
165
134
|
return ['HasOne', 'BelongsTo'].includes(association.associationType);
|
|
@@ -168,6 +137,7 @@ class QueryOptions {
|
|
|
168
137
|
});
|
|
169
138
|
}
|
|
170
139
|
}
|
|
140
|
+
|
|
171
141
|
/**
|
|
172
142
|
* Add the required includes from a list of field names.
|
|
173
143
|
* @param {string[]} fields Fields of HasOne and BelongTo relations are
|
|
@@ -175,57 +145,48 @@ class QueryOptions {
|
|
|
175
145
|
* @param {string[]} fields the output of the extractRequestedFields() util function
|
|
176
146
|
* @param {boolean} applyOnRootModel restrict fetched fields also on the root
|
|
177
147
|
*/
|
|
178
|
-
|
|
179
|
-
|
|
180
148
|
async requireFields(fields) {
|
|
181
149
|
var _this3 = this;
|
|
182
|
-
|
|
183
150
|
if (fields) {
|
|
184
151
|
fields.forEach(function (field) {
|
|
185
152
|
return _this3._requestedFields.add(field);
|
|
186
153
|
});
|
|
187
154
|
}
|
|
188
155
|
}
|
|
156
|
+
|
|
189
157
|
/**
|
|
190
158
|
* Filter resulting query set with packed primary ids.
|
|
191
159
|
* This works both for normal collection, and those which use composite primary keys.
|
|
192
160
|
* @param {string[]} recordIds Packed record ids
|
|
193
161
|
*/
|
|
194
|
-
|
|
195
|
-
|
|
196
162
|
async filterByIds(recordIds) {
|
|
197
163
|
this._where.push(new _primaryKeysManager.default(this._model).getRecordsConditions(recordIds));
|
|
198
164
|
}
|
|
165
|
+
|
|
199
166
|
/**
|
|
200
167
|
* Apply condition tree to those query options (scopes, user filters, charts, ...)
|
|
201
168
|
* @param {*} filters standard forest filters
|
|
202
169
|
* @param {string} timezone timezone of the user (required if filtering on dates)
|
|
203
170
|
*/
|
|
204
|
-
|
|
205
|
-
|
|
206
171
|
async filterByConditionTree(filters, timezone) {
|
|
207
172
|
var _this4 = this;
|
|
208
|
-
|
|
209
173
|
if (!filters) return;
|
|
210
174
|
const filterParser = new _filtersParser.default(this._schema, timezone, {
|
|
211
175
|
Sequelize: this._Sequelize
|
|
212
176
|
});
|
|
213
177
|
const whereClause = await filterParser.perform(filters);
|
|
214
|
-
|
|
215
178
|
this._where.push(whereClause);
|
|
216
|
-
|
|
217
179
|
const associations = await filterParser.getAssociations(filters);
|
|
218
180
|
associations.forEach(function (association) {
|
|
219
181
|
return _this4._neededFields.add(association);
|
|
220
182
|
});
|
|
221
183
|
}
|
|
184
|
+
|
|
222
185
|
/**
|
|
223
186
|
* Retrict rows to those matching a search string
|
|
224
187
|
* @param {string} search search string
|
|
225
188
|
* @param {boolean} searchExtended if truthy, enable search in relations
|
|
226
189
|
*/
|
|
227
|
-
|
|
228
|
-
|
|
229
190
|
async search(search, searchExtended) {
|
|
230
191
|
if (!search) return [];
|
|
231
192
|
const options = {
|
|
@@ -240,13 +201,11 @@ class QueryOptions {
|
|
|
240
201
|
conditions,
|
|
241
202
|
include
|
|
242
203
|
} = await helper.performWithSmartFields(this._options.tableAlias);
|
|
243
|
-
|
|
244
204
|
if (conditions) {
|
|
245
205
|
this._where.push(conditions);
|
|
246
206
|
} else {
|
|
247
207
|
this._where.push(this._Sequelize.literal('(0=1)'));
|
|
248
208
|
}
|
|
249
|
-
|
|
250
209
|
if (include) {
|
|
251
210
|
if (Array.isArray(include)) {
|
|
252
211
|
this._customerIncludes.push(...include);
|
|
@@ -254,47 +213,41 @@ class QueryOptions {
|
|
|
254
213
|
this._customerIncludes.push(include);
|
|
255
214
|
}
|
|
256
215
|
}
|
|
257
|
-
|
|
258
216
|
return helper.getFieldsSearched();
|
|
259
217
|
}
|
|
218
|
+
|
|
260
219
|
/**
|
|
261
220
|
* Apply a forestadmin segment
|
|
262
221
|
* @param {string} name name of the segment (from the querystring)
|
|
263
222
|
* @param {string} segmentQuery SQL query of the segment (also from querystring)
|
|
264
223
|
*/
|
|
265
|
-
|
|
266
|
-
|
|
267
224
|
async segment(name) {
|
|
268
225
|
var _this$_schema$segment;
|
|
269
|
-
|
|
270
226
|
if (!name) return;
|
|
271
227
|
const segment = (_this$_schema$segment = this._schema.segments) === null || _this$_schema$segment === void 0 ? void 0 : _this$_schema$segment.find(function (s) {
|
|
272
228
|
return s.name === name;
|
|
273
|
-
});
|
|
229
|
+
});
|
|
274
230
|
|
|
231
|
+
// Segments can be provided as a sequelize scope (undocumented).
|
|
275
232
|
if (segment !== null && segment !== void 0 && segment.scope) {
|
|
276
233
|
this._scopes.push(segment.scope);
|
|
277
|
-
}
|
|
278
|
-
|
|
234
|
+
}
|
|
279
235
|
|
|
236
|
+
// ... or as a function which returns a sequelize where clause ...
|
|
280
237
|
if (typeof (segment === null || segment === void 0 ? void 0 : segment.where) === 'function') {
|
|
281
238
|
this._where.push(await segment.where());
|
|
282
239
|
}
|
|
283
240
|
}
|
|
241
|
+
|
|
284
242
|
/**
|
|
285
243
|
* Apply a segment query.
|
|
286
244
|
* FIXME: Select SQL injection allows to fetch any information from database.
|
|
287
245
|
* @param {string} query
|
|
288
246
|
*/
|
|
289
|
-
|
|
290
|
-
|
|
291
247
|
async segmentQuery(query) {
|
|
292
248
|
if (!query) return;
|
|
293
|
-
|
|
294
249
|
const primaryKey = _lodash.default.values(this._model.primaryKeys)[0].field;
|
|
295
|
-
|
|
296
250
|
const queryToFilterRecords = query.trim();
|
|
297
|
-
|
|
298
251
|
try {
|
|
299
252
|
const options = {
|
|
300
253
|
type: this._Sequelize.QueryTypes.SELECT
|
|
@@ -306,46 +259,38 @@ class QueryOptions {
|
|
|
306
259
|
this.filterByIds(recordIds);
|
|
307
260
|
} catch (error) {
|
|
308
261
|
const errorMessage = `Invalid SQL query for this Live Query segment:\n${error.message}`;
|
|
309
|
-
|
|
310
262
|
_forestExpress.logger.error(errorMessage);
|
|
311
|
-
|
|
312
263
|
throw new _errors.ErrorHTTP422(errorMessage);
|
|
313
264
|
}
|
|
314
265
|
}
|
|
266
|
+
|
|
315
267
|
/**
|
|
316
268
|
* Apply sort instructions from a sort string in the form 'field', '-field' or 'field.subfield'.
|
|
317
269
|
* Multiple sorts are not supported
|
|
318
270
|
* @param {string} sortString a sort string
|
|
319
271
|
*/
|
|
320
|
-
|
|
321
|
-
|
|
322
272
|
async sort(sortString) {
|
|
323
273
|
if (!sortString) return;
|
|
324
274
|
const [sortField, order] = sortString[0] === '-' ? [sortString.substring(1), 'DESC'] : [sortString, 'ASC'];
|
|
325
|
-
|
|
326
275
|
if (sortField.includes('.')) {
|
|
327
276
|
// Sort on the belongsTo displayed field
|
|
328
277
|
const [associationName, fieldName] = sortField.split('.');
|
|
329
|
-
|
|
330
278
|
this._order.push([associationName, fieldName, order]);
|
|
331
|
-
|
|
332
279
|
this._neededFields.add(sortField);
|
|
333
280
|
} else {
|
|
334
281
|
this._order.push([sortField, order]);
|
|
335
282
|
}
|
|
336
283
|
}
|
|
284
|
+
|
|
337
285
|
/**
|
|
338
286
|
* Apply pagination.
|
|
339
287
|
* When called with invalid parameters the query will be paginated using default values.
|
|
340
288
|
* @param {number|string} number page number (starting at one)
|
|
341
289
|
* @param {number|string} size page size
|
|
342
290
|
*/
|
|
343
|
-
|
|
344
|
-
|
|
345
291
|
async paginate(number, size) {
|
|
346
292
|
const limit = Number.parseInt(size, 10);
|
|
347
293
|
const offset = (Number.parseInt(number, 10) - 1) * limit;
|
|
348
|
-
|
|
349
294
|
if (offset >= 0 && limit > 0) {
|
|
350
295
|
this._offset = offset;
|
|
351
296
|
this._limit = limit;
|
|
@@ -354,7 +299,5 @@ class QueryOptions {
|
|
|
354
299
|
this._limit = 10;
|
|
355
300
|
}
|
|
356
301
|
}
|
|
357
|
-
|
|
358
302
|
}
|
|
359
|
-
|
|
360
303
|
module.exports = QueryOptions;
|
|
@@ -1,27 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
require("core-js/modules/es.string.trim.js");
|
|
4
|
-
|
|
5
4
|
require("core-js/modules/es.regexp.exec.js");
|
|
6
|
-
|
|
7
5
|
require("core-js/modules/es.string.replace.js");
|
|
8
|
-
|
|
9
6
|
function QueryStatGetter(params, opts) {
|
|
10
7
|
this.perform = function perform() {
|
|
11
8
|
let rawQuery = params.query.trim();
|
|
12
9
|
const bind = params.contextVariables || {};
|
|
13
|
-
|
|
14
10
|
if (bind.recordId && !rawQuery.includes('$recordId')) {
|
|
15
11
|
rawQuery = rawQuery.replace(/\?/g, '$recordId');
|
|
16
|
-
}
|
|
17
|
-
// does not belongs to this database.
|
|
18
|
-
|
|
12
|
+
}
|
|
19
13
|
|
|
14
|
+
// WARNING: Choosing the first connection might generate issues if the model
|
|
15
|
+
// does not belongs to this database.
|
|
20
16
|
return Object.values(opts.connections)[0].query(rawQuery, {
|
|
21
17
|
type: opts.Sequelize.QueryTypes.SELECT,
|
|
22
18
|
bind
|
|
23
19
|
});
|
|
24
20
|
};
|
|
25
21
|
}
|
|
26
|
-
|
|
27
22
|
module.exports = QueryStatGetter;
|
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
require("core-js/modules/es.regexp.exec.js");
|
|
4
|
-
|
|
5
4
|
require("core-js/modules/es.array.flat.js");
|
|
6
|
-
|
|
7
5
|
require("core-js/modules/es.array.iterator.js");
|
|
8
|
-
|
|
9
6
|
/**
|
|
10
7
|
* @typedef {{
|
|
11
8
|
* fields: Array<{
|
|
@@ -26,18 +23,17 @@ function extractRequestedFieldsForAssociation(associationName, requestedFields,
|
|
|
26
23
|
return schema.fields.some(function (field) {
|
|
27
24
|
return field.field === fieldName && field.isVirtual;
|
|
28
25
|
});
|
|
29
|
-
});
|
|
30
|
-
// and use that info to only retrieve fields that are needed
|
|
26
|
+
});
|
|
31
27
|
|
|
28
|
+
// TODO: have a way of specifying which actual field is used for the computation of a smart field
|
|
29
|
+
// and use that info to only retrieve fields that are needed
|
|
32
30
|
if (referencesSmartFields) {
|
|
33
31
|
return [`${associationName}`];
|
|
34
32
|
}
|
|
35
|
-
|
|
36
33
|
return requestedFields.map(function (fieldName) {
|
|
37
34
|
return `${associationName}.${fieldName}`;
|
|
38
35
|
});
|
|
39
36
|
}
|
|
40
|
-
|
|
41
37
|
function extractRequestedSmartField(requestedFields, schema) {
|
|
42
38
|
const schemaSmartFields = schema.fields.filter(function ({
|
|
43
39
|
isVirtual
|
|
@@ -52,6 +48,7 @@ function extractRequestedSmartField(requestedFields, schema) {
|
|
|
52
48
|
});
|
|
53
49
|
});
|
|
54
50
|
}
|
|
51
|
+
|
|
55
52
|
/**
|
|
56
53
|
* @param {Record<string, string>} requestedFields
|
|
57
54
|
* @param {*} modelOrAssociation
|
|
@@ -60,17 +57,16 @@ function extractRequestedSmartField(requestedFields, schema) {
|
|
|
60
57
|
* }} schemas
|
|
61
58
|
* @returns {string[]}
|
|
62
59
|
*/
|
|
63
|
-
|
|
64
|
-
|
|
65
60
|
function extractRequestedFields(requestedFields, modelOrAssociation, schemas) {
|
|
66
61
|
if (!requestedFields || !requestedFields[modelOrAssociation.name]) {
|
|
67
62
|
return null;
|
|
68
|
-
}
|
|
69
|
-
// the client.
|
|
70
|
-
|
|
63
|
+
}
|
|
71
64
|
|
|
65
|
+
// NOTICE: Force the primaryKey retrieval to store the records properly in
|
|
66
|
+
// the client.
|
|
72
67
|
const primaryKeyArray = [Object.keys(modelOrAssociation.primaryKeys)[0]];
|
|
73
|
-
const allAssociationFields = Object.keys(modelOrAssociation.associations)
|
|
68
|
+
const allAssociationFields = Object.keys(modelOrAssociation.associations)
|
|
69
|
+
// NOTICE: Remove fields for which attributes are not explicitly set
|
|
74
70
|
// in the requested fields
|
|
75
71
|
.filter(function (associationName) {
|
|
76
72
|
return requestedFields[associationName];
|
|
@@ -83,5 +79,4 @@ function extractRequestedFields(requestedFields, modelOrAssociation, schemas) {
|
|
|
83
79
|
const smartFields = extractRequestedSmartField(requestedFields, schemas[modelOrAssociation.name]);
|
|
84
80
|
return Array.from(new Set([...primaryKeyArray, ...modelFields, ...smartFields, ...allAssociationFields]));
|
|
85
81
|
}
|
|
86
|
-
|
|
87
82
|
module.exports = extractRequestedFields;
|
|
@@ -1,35 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
4
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
6
|
-
|
|
7
5
|
require("core-js/modules/es.promise.js");
|
|
8
|
-
|
|
9
6
|
require("core-js/modules/es.array.iterator.js");
|
|
10
|
-
|
|
11
7
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
12
|
-
|
|
13
8
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
14
|
-
|
|
15
9
|
const _ = require('lodash');
|
|
16
|
-
|
|
17
10
|
const P = require('bluebird');
|
|
18
|
-
|
|
19
11
|
const Interface = require('forest-express');
|
|
20
|
-
|
|
21
12
|
const {
|
|
22
13
|
ErrorHTTP422
|
|
23
14
|
} = require('./errors');
|
|
24
|
-
|
|
25
15
|
const ResourceGetter = require('./resource-getter');
|
|
26
|
-
|
|
27
16
|
const PrimaryKeysManager = require('./primary-keys-manager');
|
|
28
|
-
|
|
29
17
|
const associationRecord = require('../utils/association-record');
|
|
30
|
-
|
|
31
18
|
const isPrimaryKeyAForeignKey = require('../utils/is-primary-key-a-foreign-key');
|
|
32
|
-
|
|
33
19
|
class ResourceCreator {
|
|
34
20
|
constructor(model, params, body, user) {
|
|
35
21
|
this.model = model;
|
|
@@ -38,92 +24,79 @@ class ResourceCreator {
|
|
|
38
24
|
this.schema = Interface.Schemas.schemas[model.name];
|
|
39
25
|
this.user = user;
|
|
40
26
|
}
|
|
41
|
-
|
|
42
27
|
async _getTargetKey(name, association) {
|
|
43
28
|
const primaryKey = this.body[name];
|
|
44
29
|
let targetKey = primaryKey;
|
|
45
|
-
|
|
46
30
|
if (primaryKey && association.targetKey !== association.target.primaryKeyAttribute) {
|
|
47
31
|
const record = await associationRecord.get(association.target, primaryKey);
|
|
48
32
|
targetKey = record[association.targetKey];
|
|
49
33
|
}
|
|
50
|
-
|
|
51
34
|
return targetKey;
|
|
52
35
|
}
|
|
53
|
-
|
|
54
36
|
async _makePromisesBeforeSave(record, [name, association]) {
|
|
55
37
|
if (association.associationType === 'BelongsTo') {
|
|
56
38
|
const setterName = `set${_.upperFirst(name)}`;
|
|
57
39
|
const targetKey = await this._getTargetKey(name, association);
|
|
58
40
|
const primaryKeyIsAForeignKey = isPrimaryKeyAForeignKey(association);
|
|
59
|
-
|
|
60
41
|
if (primaryKeyIsAForeignKey) {
|
|
61
42
|
record[association.source.primaryKeyAttribute] = this.body[name];
|
|
62
43
|
}
|
|
63
|
-
|
|
64
44
|
return record[setterName](targetKey, {
|
|
65
45
|
save: false
|
|
66
46
|
});
|
|
67
47
|
}
|
|
68
|
-
|
|
69
48
|
return null;
|
|
70
49
|
}
|
|
71
|
-
|
|
72
50
|
_makePromisesAfterSave(record, [name, association]) {
|
|
73
51
|
let setterName;
|
|
74
|
-
|
|
75
52
|
if (association.associationType === 'HasOne') {
|
|
76
53
|
setterName = `set${_.upperFirst(name)}`;
|
|
77
54
|
} else if (['BelongsToMany', 'HasMany'].includes(association.associationType)) {
|
|
78
55
|
setterName = `add${_.upperFirst(name)}`;
|
|
79
56
|
}
|
|
80
|
-
|
|
81
57
|
if (setterName) {
|
|
82
58
|
return record[setterName](this.body[name]);
|
|
83
59
|
}
|
|
84
|
-
|
|
85
60
|
return null;
|
|
86
61
|
}
|
|
87
|
-
|
|
88
62
|
async _handleSave(record, callback) {
|
|
89
63
|
var _this = this;
|
|
90
|
-
|
|
91
64
|
const {
|
|
92
65
|
associations
|
|
93
66
|
} = this.model;
|
|
94
|
-
|
|
95
67
|
if (associations) {
|
|
96
68
|
await P.all(Object.entries(associations).map(function (entry) {
|
|
97
69
|
return callback.bind(_this)(record, entry);
|
|
98
70
|
}));
|
|
99
71
|
}
|
|
100
72
|
}
|
|
101
|
-
|
|
102
73
|
async perform() {
|
|
103
74
|
// buildInstance
|
|
104
|
-
const recordCreated = this.model.build(this.body);
|
|
75
|
+
const recordCreated = this.model.build(this.body);
|
|
105
76
|
|
|
106
|
-
|
|
77
|
+
// handleAssociationsBeforeSave
|
|
78
|
+
await this._handleSave(recordCreated, this._makePromisesBeforeSave);
|
|
107
79
|
|
|
80
|
+
// saveInstance (validate then save)
|
|
108
81
|
try {
|
|
109
82
|
await recordCreated.validate();
|
|
110
83
|
} catch (error) {
|
|
111
84
|
throw new ErrorHTTP422(error.message);
|
|
112
85
|
}
|
|
86
|
+
const record = await recordCreated.save();
|
|
113
87
|
|
|
114
|
-
|
|
88
|
+
// handleAssociationsAfterSave
|
|
115
89
|
// NOTICE: Many to many associations have to be set after the record creation in order to
|
|
116
90
|
// have an id.
|
|
91
|
+
await this._handleSave(record, this._makePromisesAfterSave);
|
|
117
92
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
new PrimaryKeysManager(this.model).annotateRecords([record]); // return makeResourceGetter()
|
|
93
|
+
// appendCompositePrimary
|
|
94
|
+
new PrimaryKeysManager(this.model).annotateRecords([record]);
|
|
121
95
|
|
|
96
|
+
// return makeResourceGetter()
|
|
122
97
|
return new ResourceGetter(this.model, _objectSpread(_objectSpread({}, this.params), {}, {
|
|
123
98
|
recordId: record[this.schema.idField]
|
|
124
99
|
}), this.user).perform();
|
|
125
100
|
}
|
|
126
|
-
|
|
127
101
|
}
|
|
128
|
-
|
|
129
102
|
module.exports = ResourceCreator;
|
|
@@ -1,24 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
4
|
require("core-js/modules/es.promise.js");
|
|
6
|
-
|
|
7
5
|
var _forestExpress = require("forest-express");
|
|
8
|
-
|
|
9
6
|
var _httpErrors = _interopRequireDefault(require("http-errors"));
|
|
10
|
-
|
|
11
7
|
var _primaryKeysManager = _interopRequireDefault(require("./primary-keys-manager"));
|
|
12
|
-
|
|
13
8
|
var _queryOptions = _interopRequireDefault(require("./query-options"));
|
|
14
|
-
|
|
15
9
|
class ResourceGetter {
|
|
16
10
|
constructor(model, params, user) {
|
|
17
11
|
this._model = model.unscoped();
|
|
18
12
|
this._params = params;
|
|
19
13
|
this._user = user;
|
|
20
14
|
}
|
|
21
|
-
|
|
22
15
|
async perform() {
|
|
23
16
|
const {
|
|
24
17
|
timezone
|
|
@@ -30,15 +23,11 @@ class ResourceGetter {
|
|
|
30
23
|
await queryOptions.filterByIds([this._params.recordId]);
|
|
31
24
|
await queryOptions.filterByConditionTree(scopeFilters, timezone);
|
|
32
25
|
const record = await this._model.findOne(queryOptions.sequelizeOptions);
|
|
33
|
-
|
|
34
26
|
if (!record) {
|
|
35
27
|
throw (0, _httpErrors.default)(404, `The ${this._model.name} #${this._params.recordId} does not exist.`);
|
|
36
28
|
}
|
|
37
|
-
|
|
38
29
|
new _primaryKeysManager.default(this._model).annotateRecords([record]);
|
|
39
30
|
return record;
|
|
40
31
|
}
|
|
41
|
-
|
|
42
32
|
}
|
|
43
|
-
|
|
44
33
|
module.exports = ResourceGetter;
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
4
|
var _resourcesRemover = _interopRequireDefault(require("./resources-remover"));
|
|
6
|
-
|
|
7
5
|
/**
|
|
8
6
|
* Kept for retro-compatibility with forest-express.
|
|
9
7
|
*/
|
|
@@ -11,7 +9,5 @@ class ResourceRemover extends _resourcesRemover.default {
|
|
|
11
9
|
constructor(model, params, user) {
|
|
12
10
|
super(model, params, [params.recordId], user);
|
|
13
11
|
}
|
|
14
|
-
|
|
15
12
|
}
|
|
16
|
-
|
|
17
13
|
module.exports = ResourceRemover;
|