@webiny/api-headless-cms-ddb 0.0.0-unstable.611c5af35e → 0.0.0-unstable.615a930a68
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/definitions/entry.js +5 -0
- package/definitions/entry.js.map +1 -1
- package/definitions/group.js +5 -0
- package/definitions/group.js.map +1 -1
- package/definitions/model.js +5 -0
- package/definitions/model.js.map +1 -1
- package/definitions/settings.js +5 -0
- package/definitions/settings.js.map +1 -1
- package/definitions/system.js +5 -0
- package/definitions/system.js.map +1 -1
- package/definitions/table.js +3 -0
- package/definitions/table.js.map +1 -1
- package/dynamoDb/index.js +7 -0
- package/dynamoDb/index.js.map +1 -1
- package/dynamoDb/path/plainObject.js +8 -1
- package/dynamoDb/path/plainObject.js.map +1 -1
- package/dynamoDb/storage/date.js +29 -4
- package/dynamoDb/storage/date.js.map +1 -1
- package/dynamoDb/storage/longText.js +17 -1
- package/dynamoDb/storage/longText.js.map +1 -1
- package/dynamoDb/storage/richText.js +18 -0
- package/dynamoDb/storage/richText.js.map +1 -1
- package/dynamoDb/transformValue/datetime.js +8 -1
- package/dynamoDb/transformValue/datetime.js.map +1 -1
- package/index.js +32 -5
- package/index.js.map +1 -1
- package/operations/entry/dataLoaders.js +44 -1
- package/operations/entry/dataLoaders.js.map +1 -1
- package/operations/entry/filtering/createFields.d.ts +18 -0
- package/operations/entry/filtering/createFields.js +124 -0
- package/operations/entry/filtering/createFields.js.map +1 -0
- package/operations/entry/filtering/createFilters.d.ts +20 -0
- package/operations/entry/filtering/createFilters.js +155 -0
- package/operations/entry/filtering/createFilters.js.map +1 -0
- package/operations/entry/filtering/extractSort.d.ts +13 -0
- package/operations/entry/filtering/extractSort.js +55 -0
- package/operations/entry/filtering/extractSort.js.map +1 -0
- package/operations/entry/filtering/filter.d.ts +16 -0
- package/operations/entry/filtering/filter.js +137 -0
- package/operations/entry/filtering/filter.js.map +1 -0
- package/operations/entry/filtering/fullTextSearch.d.ts +18 -0
- package/operations/entry/filtering/fullTextSearch.js +64 -0
- package/operations/entry/filtering/fullTextSearch.js.map +1 -0
- package/operations/entry/filtering/getValue.d.ts +5 -0
- package/operations/entry/filtering/getValue.js +81 -0
- package/operations/entry/filtering/getValue.js.map +1 -0
- package/operations/entry/filtering/index.d.ts +2 -0
- package/operations/entry/filtering/index.js +21 -0
- package/operations/entry/filtering/index.js.map +1 -0
- package/operations/entry/filtering/mapPlugins.d.ts +8 -0
- package/operations/entry/filtering/mapPlugins.js +39 -0
- package/operations/entry/filtering/mapPlugins.js.map +1 -0
- package/operations/entry/filtering/plugins/defaultFilterCreate.d.ts +2 -0
- package/operations/entry/filtering/plugins/defaultFilterCreate.js +48 -0
- package/operations/entry/filtering/plugins/defaultFilterCreate.js.map +1 -0
- package/operations/entry/filtering/plugins/index.d.ts +1 -0
- package/operations/entry/filtering/plugins/index.js +18 -0
- package/operations/entry/filtering/plugins/index.js.map +1 -0
- package/operations/entry/filtering/plugins/objectFilterCreate.d.ts +2 -0
- package/operations/entry/filtering/plugins/objectFilterCreate.js +107 -0
- package/operations/entry/filtering/plugins/objectFilterCreate.js.map +1 -0
- package/operations/entry/filtering/plugins/refFilterCreate.d.ts +2 -0
- package/operations/entry/filtering/plugins/refFilterCreate.js +89 -0
- package/operations/entry/filtering/plugins/refFilterCreate.js.map +1 -0
- package/operations/entry/filtering/sort.d.ts +9 -0
- package/operations/entry/filtering/sort.js +80 -0
- package/operations/entry/filtering/sort.js.map +1 -0
- package/operations/entry/filtering/systemFields.d.ts +4 -0
- package/operations/entry/filtering/systemFields.js +72 -0
- package/operations/entry/filtering/systemFields.js.map +1 -0
- package/operations/entry/filtering/transform.d.ts +6 -0
- package/operations/entry/filtering/transform.js +19 -0
- package/operations/entry/filtering/transform.js.map +1 -0
- package/operations/entry/filtering/types.d.ts +40 -0
- package/operations/entry/filtering/types.js +5 -0
- package/operations/entry/filtering/types.js.map +1 -0
- package/operations/entry/filtering/where.d.ts +5 -0
- package/operations/entry/filtering/where.js +38 -0
- package/operations/entry/filtering/where.js.map +1 -0
- package/operations/entry/index.js +98 -13
- package/operations/entry/index.js.map +1 -1
- package/operations/entry/keys.js +18 -0
- package/operations/entry/keys.js.map +1 -1
- package/operations/entry/systemFields.js +2 -0
- package/operations/entry/systemFields.js.map +1 -1
- package/operations/entry/utils.js +121 -3
- package/operations/entry/utils.js.map +1 -1
- package/operations/group/index.js +32 -2
- package/operations/group/index.js.map +1 -1
- package/operations/model/index.js +25 -0
- package/operations/model/index.js.map +1 -1
- package/operations/settings/index.js +24 -0
- package/operations/settings/index.js.map +1 -1
- package/operations/system/index.js +17 -0
- package/operations/system/index.js.map +1 -1
- package/package.json +12 -12
- package/plugins/CmsEntryFieldFilterPathPlugin.d.ts +2 -2
- package/plugins/CmsEntryFieldFilterPathPlugin.js +14 -0
- package/plugins/CmsEntryFieldFilterPathPlugin.js.map +1 -1
- package/plugins/CmsEntryFieldFilterPlugin.d.ts +43 -0
- package/plugins/CmsEntryFieldFilterPlugin.js +31 -0
- package/plugins/CmsEntryFieldFilterPlugin.js.map +1 -0
- package/plugins/index.js +2 -0
- package/plugins/index.js.map +1 -1
- package/types.d.ts +1 -1
- package/types.js +1 -0
- package/types.js.map +1 -1
|
@@ -1,28 +1,45 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
4
5
|
Object.defineProperty(exports, "__esModule", {
|
|
5
6
|
value: true
|
|
6
7
|
});
|
|
7
8
|
exports.sortEntryItems = exports.filterItems = exports.buildModelFields = void 0;
|
|
8
|
-
|
|
9
|
+
|
|
10
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
|
|
9
12
|
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
13
|
+
|
|
10
14
|
var _sortBy = _interopRequireDefault(require("lodash/sortBy"));
|
|
15
|
+
|
|
11
16
|
var _dotProp = _interopRequireDefault(require("dot-prop"));
|
|
17
|
+
|
|
12
18
|
var _systemFields = require("./systemFields");
|
|
19
|
+
|
|
13
20
|
var _ValueFilterPlugin = require("@webiny/db-dynamodb/plugins/definitions/ValueFilterPlugin");
|
|
21
|
+
|
|
14
22
|
var _CmsEntryFieldFilterPathPlugin = require("../../plugins/CmsEntryFieldFilterPathPlugin");
|
|
23
|
+
|
|
24
|
+
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; }
|
|
25
|
+
|
|
26
|
+
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; }
|
|
27
|
+
|
|
15
28
|
const VALUES_ATTRIBUTE = "values";
|
|
29
|
+
|
|
16
30
|
const extractWhereParams = key => {
|
|
17
31
|
const result = key.split("_");
|
|
18
32
|
const fieldId = result.shift();
|
|
33
|
+
|
|
19
34
|
if (!fieldId) {
|
|
20
35
|
return null;
|
|
21
36
|
}
|
|
37
|
+
|
|
22
38
|
const rawOp = result.length === 0 ? "eq" : result.join("_");
|
|
23
39
|
/**
|
|
24
40
|
* When rawOp is not, it means it is equal negated so just return that.
|
|
25
41
|
*/
|
|
42
|
+
|
|
26
43
|
if (rawOp === "not") {
|
|
27
44
|
return {
|
|
28
45
|
fieldId,
|
|
@@ -30,6 +47,7 @@ const extractWhereParams = key => {
|
|
|
30
47
|
negate: true
|
|
31
48
|
};
|
|
32
49
|
}
|
|
50
|
+
|
|
33
51
|
const negate = rawOp.match("not_") !== null;
|
|
34
52
|
const operation = rawOp.replace("not_", "");
|
|
35
53
|
return {
|
|
@@ -38,25 +56,31 @@ const extractWhereParams = key => {
|
|
|
38
56
|
negate
|
|
39
57
|
};
|
|
40
58
|
};
|
|
59
|
+
|
|
41
60
|
const transformValue = (value, transform) => {
|
|
42
61
|
if (Array.isArray(value)) {
|
|
43
62
|
return value.map(v => transform(v));
|
|
44
63
|
}
|
|
64
|
+
|
|
45
65
|
return transform(value);
|
|
46
66
|
};
|
|
67
|
+
|
|
47
68
|
const getFilterPlugin = params => {
|
|
48
69
|
const {
|
|
49
70
|
plugins,
|
|
50
71
|
operation
|
|
51
72
|
} = params;
|
|
52
73
|
const plugin = plugins[operation];
|
|
74
|
+
|
|
53
75
|
if (plugin) {
|
|
54
76
|
return plugin;
|
|
55
77
|
}
|
|
78
|
+
|
|
56
79
|
throw new _error.default(`There is no filter plugin for operation "${operation}".`, "FILTER_PLUGIN_ERROR", {
|
|
57
80
|
operation
|
|
58
81
|
});
|
|
59
82
|
};
|
|
83
|
+
|
|
60
84
|
const createValuePath = params => {
|
|
61
85
|
const {
|
|
62
86
|
field,
|
|
@@ -68,19 +92,23 @@ const createValuePath = params => {
|
|
|
68
92
|
} = field;
|
|
69
93
|
const valuePathPlugin = plugins[field.type];
|
|
70
94
|
const basePath = _systemFields.systemFields[fieldId] ? "" : `${VALUES_ATTRIBUTE}.`;
|
|
95
|
+
|
|
71
96
|
if (!valuePathPlugin || valuePathPlugin.canUse(field) === false) {
|
|
72
97
|
return `${basePath}${fieldId}`;
|
|
73
98
|
}
|
|
99
|
+
|
|
74
100
|
const path = valuePathPlugin.createPath({
|
|
75
101
|
field,
|
|
76
102
|
index
|
|
77
103
|
});
|
|
78
104
|
return `${basePath}${path}`;
|
|
79
105
|
};
|
|
106
|
+
|
|
80
107
|
const isObjectFiltering = params => {
|
|
81
108
|
const {
|
|
82
109
|
value
|
|
83
110
|
} = params;
|
|
111
|
+
|
|
84
112
|
if (!value) {
|
|
85
113
|
return false;
|
|
86
114
|
} else if (Array.isArray(value) === true) {
|
|
@@ -90,8 +118,10 @@ const isObjectFiltering = params => {
|
|
|
90
118
|
} else if (typeof value !== "object") {
|
|
91
119
|
return false;
|
|
92
120
|
}
|
|
121
|
+
|
|
93
122
|
return true;
|
|
94
123
|
};
|
|
124
|
+
|
|
95
125
|
const createFilters = params => {
|
|
96
126
|
const {
|
|
97
127
|
where,
|
|
@@ -114,54 +144,70 @@ const createFilters = params => {
|
|
|
114
144
|
property: "fieldType"
|
|
115
145
|
});
|
|
116
146
|
const filters = [];
|
|
147
|
+
|
|
117
148
|
for (const key in where) {
|
|
118
149
|
if (where.hasOwnProperty(key) === false) {
|
|
119
150
|
continue;
|
|
120
151
|
}
|
|
152
|
+
|
|
121
153
|
const value = where[key];
|
|
154
|
+
|
|
122
155
|
if (value === undefined) {
|
|
123
156
|
continue;
|
|
124
157
|
}
|
|
158
|
+
|
|
125
159
|
const whereParams = extractWhereParams(key);
|
|
160
|
+
|
|
126
161
|
if (!whereParams) {
|
|
127
162
|
continue;
|
|
128
163
|
}
|
|
164
|
+
|
|
129
165
|
const {
|
|
130
166
|
fieldId,
|
|
131
167
|
operation,
|
|
132
168
|
negate
|
|
133
169
|
} = whereParams;
|
|
134
170
|
const field = fields[fieldId];
|
|
171
|
+
|
|
135
172
|
if (!field) {
|
|
136
173
|
throw new _error.default(`There is no field with the fieldId "${fieldId}".`, "FIELD_ERROR", {
|
|
137
174
|
fieldId
|
|
138
175
|
});
|
|
139
176
|
}
|
|
177
|
+
|
|
140
178
|
const transformValuePlugin = transformValuePlugins[field.def.type];
|
|
179
|
+
|
|
141
180
|
const transformValueCallable = value => {
|
|
142
181
|
if (!transformValuePlugin) {
|
|
143
182
|
return value;
|
|
144
183
|
}
|
|
184
|
+
|
|
145
185
|
return transformValuePlugin.transform({
|
|
146
186
|
field: field.def,
|
|
147
187
|
value
|
|
148
188
|
});
|
|
149
189
|
};
|
|
190
|
+
|
|
150
191
|
const objectFilteringParams = {
|
|
151
192
|
key,
|
|
152
193
|
value,
|
|
153
194
|
field: field.def
|
|
154
195
|
};
|
|
196
|
+
|
|
155
197
|
if (isObjectFiltering(objectFilteringParams)) {
|
|
156
198
|
const propertyFilters = Object.keys(value);
|
|
199
|
+
|
|
157
200
|
if (propertyFilters.length === 0) {
|
|
158
201
|
continue;
|
|
159
202
|
}
|
|
203
|
+
|
|
160
204
|
for (const propertyFilter of propertyFilters) {
|
|
161
205
|
const whereParams = extractWhereParams(propertyFilter);
|
|
206
|
+
|
|
162
207
|
if (!whereParams) {
|
|
163
208
|
continue;
|
|
164
209
|
}
|
|
210
|
+
|
|
165
211
|
const {
|
|
166
212
|
fieldId: propertyId,
|
|
167
213
|
operation: propertyOperation,
|
|
@@ -185,8 +231,10 @@ const createFilters = params => {
|
|
|
185
231
|
transformValue: transformValueCallable
|
|
186
232
|
});
|
|
187
233
|
}
|
|
234
|
+
|
|
188
235
|
continue;
|
|
189
236
|
}
|
|
237
|
+
|
|
190
238
|
const filterPlugin = getFilterPlugin({
|
|
191
239
|
plugins: filterPlugins,
|
|
192
240
|
operation
|
|
@@ -203,26 +251,33 @@ const createFilters = params => {
|
|
|
203
251
|
transformValue: transformValueCallable
|
|
204
252
|
});
|
|
205
253
|
}
|
|
254
|
+
|
|
206
255
|
return filters;
|
|
207
256
|
};
|
|
208
257
|
/**
|
|
209
258
|
* In case filter field is not multiple values one, return exact path.
|
|
210
259
|
* If is multiple values field, use path without the last part
|
|
211
260
|
*/
|
|
261
|
+
|
|
262
|
+
|
|
212
263
|
const getFilterValuePath = filter => {
|
|
213
264
|
if (filter.path.includes(".%s.") === false) {
|
|
214
265
|
return filter.path;
|
|
215
266
|
}
|
|
267
|
+
|
|
216
268
|
const paths = filter.path.split(".%s.");
|
|
217
269
|
return paths.shift();
|
|
218
270
|
};
|
|
271
|
+
|
|
219
272
|
const getFilterValuePropertyPath = filter => {
|
|
220
273
|
if (filter.path.includes(".%s.") === false) {
|
|
221
274
|
return null;
|
|
222
275
|
}
|
|
276
|
+
|
|
223
277
|
const paths = filter.path.split(".%s.");
|
|
224
278
|
return paths.pop() || null;
|
|
225
279
|
};
|
|
280
|
+
|
|
226
281
|
const execFilter = params => {
|
|
227
282
|
const {
|
|
228
283
|
value: plainValue,
|
|
@@ -233,11 +288,14 @@ const execFilter = params => {
|
|
|
233
288
|
value,
|
|
234
289
|
compareValue: filter.compareValue
|
|
235
290
|
});
|
|
291
|
+
|
|
236
292
|
if (filter.negate) {
|
|
237
293
|
return matched === false;
|
|
238
294
|
}
|
|
295
|
+
|
|
239
296
|
return matched;
|
|
240
297
|
};
|
|
298
|
+
|
|
241
299
|
/**
|
|
242
300
|
* Unfortunately we must use the contains plugin directly as plugins do not support multi field searching.
|
|
243
301
|
*/
|
|
@@ -249,6 +307,7 @@ const createFullTextSearch = ({
|
|
|
249
307
|
if (!term || term.trim().length === 0 || !targetFields || targetFields.length === 0) {
|
|
250
308
|
return null;
|
|
251
309
|
}
|
|
310
|
+
|
|
252
311
|
return async ({
|
|
253
312
|
item,
|
|
254
313
|
fromStorage,
|
|
@@ -258,15 +317,19 @@ const createFullTextSearch = ({
|
|
|
258
317
|
const field = Object.values(fields).find(field => {
|
|
259
318
|
return field.def.fieldId === targetField;
|
|
260
319
|
});
|
|
320
|
+
|
|
261
321
|
if (!field) {
|
|
262
322
|
throw new _error.default(`Unknown field "${targetField}" in the model.`, "UNKNOWN_FIELD", {
|
|
263
323
|
target: targetField
|
|
264
324
|
});
|
|
265
325
|
}
|
|
326
|
+
|
|
266
327
|
const value = await fromStorage(field.def, item.values[targetField]);
|
|
328
|
+
|
|
267
329
|
if (!value) {
|
|
268
330
|
continue;
|
|
269
331
|
}
|
|
332
|
+
|
|
270
333
|
if (plugin.matches({
|
|
271
334
|
value,
|
|
272
335
|
compareValue: term
|
|
@@ -274,9 +337,11 @@ const createFullTextSearch = ({
|
|
|
274
337
|
return true;
|
|
275
338
|
}
|
|
276
339
|
}
|
|
340
|
+
|
|
277
341
|
return false;
|
|
278
342
|
};
|
|
279
343
|
};
|
|
344
|
+
|
|
280
345
|
const filterItems = async params => {
|
|
281
346
|
const {
|
|
282
347
|
items: records,
|
|
@@ -292,10 +357,12 @@ const filterItems = async params => {
|
|
|
292
357
|
fields
|
|
293
358
|
});
|
|
294
359
|
const fullTextSearchPlugin = plugins.byType(_ValueFilterPlugin.ValueFilterPlugin.type).find(plugin => plugin.getOperation() === "contains");
|
|
360
|
+
|
|
295
361
|
if (!fullTextSearchPlugin) {
|
|
296
362
|
throw new _error.default(`Missing "contains" plugin to run the full-text search.`, "MISSING_PLUGIN");
|
|
297
363
|
}
|
|
298
|
-
|
|
364
|
+
|
|
365
|
+
const search = createFullTextSearch(_objectSpread(_objectSpread({}, fullTextSearch), {}, {
|
|
299
366
|
plugin: fullTextSearchPlugin
|
|
300
367
|
}));
|
|
301
368
|
const promises = records.map(async record => {
|
|
@@ -308,24 +375,30 @@ const filterItems = async params => {
|
|
|
308
375
|
* -> values.categories.id -> values.categories
|
|
309
376
|
*/
|
|
310
377
|
const valuePath = getFilterValuePath(filter);
|
|
378
|
+
|
|
311
379
|
const rawValue = _dotProp.default.get(record, valuePath);
|
|
380
|
+
|
|
312
381
|
if (valuePath !== filter.path) {
|
|
313
382
|
/**
|
|
314
383
|
* Calculated is different than original because we need to search in the array of objects.
|
|
315
384
|
*/
|
|
316
385
|
const propertyPath = getFilterValuePropertyPath(filter);
|
|
386
|
+
|
|
317
387
|
if (!propertyPath) {
|
|
318
388
|
console.log(`Cannot determine the property path of "${filter.path}".`);
|
|
319
389
|
continue;
|
|
320
390
|
}
|
|
391
|
+
|
|
321
392
|
const plainValue = await fromStorage(fields[filter.fieldId].def, rawValue);
|
|
322
393
|
/**
|
|
323
394
|
* We cannot go through the value because it is not array. Log the error and continue.
|
|
324
395
|
*/
|
|
396
|
+
|
|
325
397
|
if (Array.isArray(plainValue) === false) {
|
|
326
398
|
console.log(`Cannot go through the value on ${valuePath} because it is not an array, and we expect it to be.`);
|
|
327
399
|
continue;
|
|
328
400
|
}
|
|
401
|
+
|
|
329
402
|
record = _dotProp.default.set(record, valuePath, plainValue);
|
|
330
403
|
const values = plainValue.map(value => {
|
|
331
404
|
return value[propertyPath];
|
|
@@ -334,22 +407,28 @@ const filterItems = async params => {
|
|
|
334
407
|
value: values,
|
|
335
408
|
filter
|
|
336
409
|
});
|
|
410
|
+
|
|
337
411
|
if (!result) {
|
|
338
412
|
return null;
|
|
339
413
|
}
|
|
414
|
+
|
|
340
415
|
continue;
|
|
341
416
|
}
|
|
417
|
+
|
|
342
418
|
const plainValue = await fromStorage(fields[filter.fieldId].def, rawValue);
|
|
343
419
|
/**
|
|
344
420
|
* If raw value is not same as the value after the storage transform, set the value to the items being filtered.
|
|
345
421
|
*/
|
|
422
|
+
|
|
346
423
|
if (plainValue !== rawValue) {
|
|
347
424
|
record = _dotProp.default.set(record, filter.path, plainValue);
|
|
348
425
|
}
|
|
426
|
+
|
|
349
427
|
const result = execFilter({
|
|
350
428
|
value: plainValue,
|
|
351
429
|
filter
|
|
352
430
|
});
|
|
431
|
+
|
|
353
432
|
if (result === false) {
|
|
354
433
|
return null;
|
|
355
434
|
}
|
|
@@ -357,22 +436,28 @@ const filterItems = async params => {
|
|
|
357
436
|
/**
|
|
358
437
|
* If we have full text search defined, run it. Otherwise just return the given record.
|
|
359
438
|
*/
|
|
439
|
+
|
|
440
|
+
|
|
360
441
|
if (!search) {
|
|
361
442
|
return record;
|
|
362
443
|
}
|
|
444
|
+
|
|
363
445
|
const result = await search({
|
|
364
446
|
item: record,
|
|
365
447
|
fromStorage,
|
|
366
448
|
fields
|
|
367
449
|
});
|
|
450
|
+
|
|
368
451
|
if (!result) {
|
|
369
452
|
return null;
|
|
370
453
|
}
|
|
454
|
+
|
|
371
455
|
return record;
|
|
372
456
|
});
|
|
373
457
|
/**
|
|
374
458
|
* We run filtering as promises so it is a bit faster than in for ... of loop.
|
|
375
459
|
*/
|
|
460
|
+
|
|
376
461
|
const results = await Promise.all(promises);
|
|
377
462
|
/**
|
|
378
463
|
* And filter out the null values which are returned when filter is not satisfied.
|
|
@@ -380,24 +465,30 @@ const filterItems = async params => {
|
|
|
380
465
|
|
|
381
466
|
return results.filter(Boolean);
|
|
382
467
|
};
|
|
468
|
+
|
|
383
469
|
exports.filterItems = filterItems;
|
|
470
|
+
|
|
384
471
|
const extractSort = (sortBy, fields) => {
|
|
385
472
|
const result = sortBy.split("_");
|
|
473
|
+
|
|
386
474
|
if (result.length !== 2) {
|
|
387
475
|
throw new _error.default("Problem in determining the sorting for the entry items.", "SORT_EXTRACT_ERROR", {
|
|
388
476
|
sortBy
|
|
389
477
|
});
|
|
390
478
|
}
|
|
479
|
+
|
|
391
480
|
const [fieldId, order] = result;
|
|
392
481
|
const modelField = Object.values(fields).find(field => {
|
|
393
482
|
return field.def.fieldId === fieldId;
|
|
394
483
|
});
|
|
484
|
+
|
|
395
485
|
if (!modelField) {
|
|
396
486
|
throw new _error.default("Sorting field does not exist in the content model.", "SORTING_FIELD_ERROR", {
|
|
397
487
|
fieldId,
|
|
398
488
|
fields
|
|
399
489
|
});
|
|
400
490
|
}
|
|
491
|
+
|
|
401
492
|
const valuePath = modelField.createPath({
|
|
402
493
|
field: modelField.def
|
|
403
494
|
});
|
|
@@ -408,12 +499,14 @@ const extractSort = (sortBy, fields) => {
|
|
|
408
499
|
reverse: order === "DESC"
|
|
409
500
|
};
|
|
410
501
|
};
|
|
502
|
+
|
|
411
503
|
const sortEntryItems = params => {
|
|
412
504
|
const {
|
|
413
505
|
items,
|
|
414
506
|
sort = [],
|
|
415
507
|
fields
|
|
416
508
|
} = params;
|
|
509
|
+
|
|
417
510
|
if (items.length <= 1) {
|
|
418
511
|
return items;
|
|
419
512
|
} else if (sort.length === 0) {
|
|
@@ -423,12 +516,15 @@ const sortEntryItems = params => {
|
|
|
423
516
|
sort: sort
|
|
424
517
|
});
|
|
425
518
|
}
|
|
519
|
+
|
|
426
520
|
const [firstSort] = sort;
|
|
521
|
+
|
|
427
522
|
if (!firstSort) {
|
|
428
523
|
throw new _error.default("Empty sort array item.", "SORT_EMPTY_ERROR", {
|
|
429
524
|
sort
|
|
430
525
|
});
|
|
431
526
|
}
|
|
527
|
+
|
|
432
528
|
const {
|
|
433
529
|
fieldId,
|
|
434
530
|
field,
|
|
@@ -444,21 +540,27 @@ const sortEntryItems = params => {
|
|
|
444
540
|
const sortedItems = (0, _sortBy.default)(itemsToSort, "value");
|
|
445
541
|
const newItems = sortedItems.map(s => {
|
|
446
542
|
const item = items.find(i => i.id === s.id);
|
|
543
|
+
|
|
447
544
|
if (item) {
|
|
448
545
|
return item;
|
|
449
546
|
}
|
|
547
|
+
|
|
450
548
|
throw new _error.default("Could not find item by given id after the sorting.", "SORTING_ITEMS_ERROR", {
|
|
451
549
|
id: s.id,
|
|
452
550
|
sortingBy: fieldId,
|
|
453
551
|
reverse
|
|
454
552
|
});
|
|
455
553
|
});
|
|
554
|
+
|
|
456
555
|
if (!reverse) {
|
|
457
556
|
return newItems;
|
|
458
557
|
}
|
|
558
|
+
|
|
459
559
|
return newItems.reverse();
|
|
460
560
|
};
|
|
561
|
+
|
|
461
562
|
exports.sortEntryItems = sortEntryItems;
|
|
563
|
+
|
|
462
564
|
const getMappedPlugins = params => {
|
|
463
565
|
const {
|
|
464
566
|
plugins: pluginsContainer,
|
|
@@ -466,23 +568,28 @@ const getMappedPlugins = params => {
|
|
|
466
568
|
property
|
|
467
569
|
} = params;
|
|
468
570
|
const plugins = pluginsContainer.byType(type);
|
|
571
|
+
|
|
469
572
|
if (plugins.length === 0) {
|
|
470
573
|
throw new _error.default(`There are no plugins of type "${type}".`, "PLUGINS_ERROR", {
|
|
471
574
|
type
|
|
472
575
|
});
|
|
473
576
|
}
|
|
577
|
+
|
|
474
578
|
return plugins.reduce((collection, plugin) => {
|
|
475
579
|
const key = plugin[property];
|
|
580
|
+
|
|
476
581
|
if (typeof key !== "string") {
|
|
477
582
|
throw new _error.default("Property to map the plugins on must be a string.", "PLUGIN_PROPERTY_ERROR", {
|
|
478
583
|
type,
|
|
479
584
|
property
|
|
480
585
|
});
|
|
481
586
|
}
|
|
587
|
+
|
|
482
588
|
collection[key] = plugin;
|
|
483
589
|
return collection;
|
|
484
590
|
}, {});
|
|
485
591
|
};
|
|
592
|
+
|
|
486
593
|
const buildModelFields = ({
|
|
487
594
|
plugins,
|
|
488
595
|
model
|
|
@@ -506,22 +613,27 @@ const buildModelFields = ({
|
|
|
506
613
|
field
|
|
507
614
|
});
|
|
508
615
|
}
|
|
616
|
+
|
|
509
617
|
const transformValuePlugin = transformValuePlugins[field.type];
|
|
510
618
|
const valuePathPlugin = valuePathPlugins[field.type];
|
|
619
|
+
|
|
511
620
|
let createPath = params => {
|
|
512
621
|
return params.field.fieldId;
|
|
513
622
|
};
|
|
623
|
+
|
|
514
624
|
if (valuePathPlugin) {
|
|
515
625
|
createPath = params => {
|
|
516
626
|
return valuePathPlugin.createPath(params);
|
|
517
627
|
};
|
|
518
628
|
}
|
|
629
|
+
|
|
519
630
|
collection[field.fieldId] = {
|
|
520
631
|
def: field,
|
|
521
632
|
valueTransformer: value => {
|
|
522
633
|
if (!transformValuePlugin) {
|
|
523
634
|
return value;
|
|
524
635
|
}
|
|
636
|
+
|
|
525
637
|
return transformValuePlugin.transform({
|
|
526
638
|
field,
|
|
527
639
|
value
|
|
@@ -537,22 +649,27 @@ const buildModelFields = ({
|
|
|
537
649
|
console.log(`Field "${field.storageId}" in model "${model.modelId}" is missing fieldId.`);
|
|
538
650
|
return collection;
|
|
539
651
|
}
|
|
652
|
+
|
|
540
653
|
const transformValuePlugin = transformValuePlugins[field.type];
|
|
541
654
|
const valuePathPlugin = valuePathPlugins[field.type];
|
|
655
|
+
|
|
542
656
|
let createPath = params => {
|
|
543
657
|
return `${VALUES_ATTRIBUTE}.${params.field.fieldId}`;
|
|
544
658
|
};
|
|
659
|
+
|
|
545
660
|
if (valuePathPlugin) {
|
|
546
661
|
createPath = params => {
|
|
547
662
|
return valuePathPlugin.createPath(params);
|
|
548
663
|
};
|
|
549
664
|
}
|
|
665
|
+
|
|
550
666
|
collection[field.fieldId] = {
|
|
551
667
|
def: field,
|
|
552
668
|
valueTransformer: value => {
|
|
553
669
|
if (!transformValuePlugin) {
|
|
554
670
|
return value;
|
|
555
671
|
}
|
|
672
|
+
|
|
556
673
|
return transformValuePlugin.transform({
|
|
557
674
|
field,
|
|
558
675
|
value
|
|
@@ -563,4 +680,5 @@ const buildModelFields = ({
|
|
|
563
680
|
return collection;
|
|
564
681
|
}, fields);
|
|
565
682
|
};
|
|
683
|
+
|
|
566
684
|
exports.buildModelFields = buildModelFields;
|