@webiny/api-headless-cms-ddb 0.0.0-unstable.6e5425ee89 → 0.0.0-unstable.78f581c1d2

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