@webiny/api-headless-cms-ddb 0.0.0-unstable.5e7233243f → 0.0.0-unstable.6e5425ee89

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