@webiny/api-headless-cms-ddb 0.0.0-unstable.97a151f74d → 0.0.0-unstable.99666aeb00

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 (140) hide show
  1. package/definitions/entry.js +6 -5
  2. package/definitions/entry.js.map +1 -1
  3. package/definitions/group.js +0 -5
  4. package/definitions/group.js.map +1 -1
  5. package/definitions/model.js +17 -5
  6. package/definitions/model.js.map +1 -1
  7. package/definitions/settings.js +0 -5
  8. package/definitions/settings.js.map +1 -1
  9. package/definitions/system.js +0 -5
  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.js +2 -8
  14. package/dynamoDb/index.js.map +1 -1
  15. package/dynamoDb/path/locationFolderId.d.ts +2 -0
  16. package/dynamoDb/path/locationFolderId.js +41 -0
  17. package/dynamoDb/path/locationFolderId.js.map +1 -0
  18. package/dynamoDb/path/plainObject.js +1 -8
  19. package/dynamoDb/path/plainObject.js.map +1 -1
  20. package/dynamoDb/storage/date.js +3 -30
  21. package/dynamoDb/storage/date.js.map +1 -1
  22. package/dynamoDb/storage/longText.js +7 -17
  23. package/dynamoDb/storage/longText.js.map +1 -1
  24. package/dynamoDb/storage/richText.js +0 -18
  25. package/dynamoDb/storage/richText.js.map +1 -1
  26. package/dynamoDb/transformValue/datetime.js +1 -8
  27. package/dynamoDb/transformValue/datetime.js.map +1 -1
  28. package/index.js +12 -28
  29. package/index.js.map +1 -1
  30. package/operations/entry/dataLoader/DataLoaderCache.d.ts +17 -0
  31. package/operations/entry/dataLoader/DataLoaderCache.js +43 -0
  32. package/operations/entry/dataLoader/DataLoaderCache.js.map +1 -0
  33. package/operations/entry/dataLoader/constants.d.ts +1 -0
  34. package/operations/entry/dataLoader/constants.js +9 -0
  35. package/operations/entry/dataLoader/constants.js.map +1 -0
  36. package/operations/entry/dataLoader/createBatchScheduleFn.d.ts +8 -0
  37. package/operations/entry/dataLoader/createBatchScheduleFn.js +23 -0
  38. package/operations/entry/dataLoader/createBatchScheduleFn.js.map +1 -0
  39. package/operations/entry/dataLoader/getAllEntryRevisions.d.ts +4 -0
  40. package/operations/entry/dataLoader/getAllEntryRevisions.js +43 -0
  41. package/operations/entry/dataLoader/getAllEntryRevisions.js.map +1 -0
  42. package/operations/entry/dataLoader/getLatestRevisionByEntryId.d.ts +4 -0
  43. package/operations/entry/dataLoader/getLatestRevisionByEntryId.js +57 -0
  44. package/operations/entry/dataLoader/getLatestRevisionByEntryId.js.map +1 -0
  45. package/operations/entry/dataLoader/getPublishedRevisionByEntryId.d.ts +4 -0
  46. package/operations/entry/dataLoader/getPublishedRevisionByEntryId.js +57 -0
  47. package/operations/entry/dataLoader/getPublishedRevisionByEntryId.js.map +1 -0
  48. package/operations/entry/dataLoader/getRevisionById.d.ts +4 -0
  49. package/operations/entry/dataLoader/getRevisionById.js +63 -0
  50. package/operations/entry/dataLoader/getRevisionById.js.map +1 -0
  51. package/operations/entry/dataLoader/index.d.ts +8 -0
  52. package/operations/entry/dataLoader/index.js +38 -0
  53. package/operations/entry/dataLoader/index.js.map +1 -0
  54. package/operations/entry/dataLoader/types.d.ts +6 -0
  55. package/operations/entry/dataLoader/types.js +5 -0
  56. package/operations/entry/dataLoader/types.js.map +1 -0
  57. package/operations/entry/dataLoaders.d.ts +11 -26
  58. package/operations/entry/dataLoaders.js +43 -249
  59. package/operations/entry/dataLoaders.js.map +1 -1
  60. package/operations/entry/filtering/{createFilters.d.ts → createExpressions.d.ts} +9 -3
  61. package/operations/entry/filtering/createExpressions.js +176 -0
  62. package/operations/entry/filtering/createExpressions.js.map +1 -0
  63. package/operations/entry/filtering/createFields.d.ts +5 -7
  64. package/operations/entry/filtering/createFields.js +73 -92
  65. package/operations/entry/filtering/createFields.js.map +1 -1
  66. package/operations/entry/filtering/extractSort.d.ts +4 -0
  67. package/operations/entry/filtering/extractSort.js +23 -11
  68. package/operations/entry/filtering/extractSort.js.map +1 -1
  69. package/operations/entry/filtering/filter.d.ts +2 -3
  70. package/operations/entry/filtering/filter.js +79 -73
  71. package/operations/entry/filtering/filter.js.map +1 -1
  72. package/operations/entry/filtering/fullTextSearch.d.ts +2 -6
  73. package/operations/entry/filtering/fullTextSearch.js +2 -18
  74. package/operations/entry/filtering/fullTextSearch.js.map +1 -1
  75. package/operations/entry/filtering/getValue.js +0 -18
  76. package/operations/entry/filtering/getValue.js.map +1 -1
  77. package/operations/entry/filtering/index.js +0 -2
  78. package/operations/entry/filtering/index.js.map +1 -1
  79. package/operations/entry/filtering/mapPlugins.js +0 -8
  80. package/operations/entry/filtering/mapPlugins.js.map +1 -1
  81. package/operations/entry/filtering/plugins/defaultFilterCreate.js +3 -8
  82. package/operations/entry/filtering/plugins/defaultFilterCreate.js.map +1 -1
  83. package/operations/entry/filtering/plugins/index.d.ts +1 -1
  84. package/operations/entry/filtering/plugins/index.js +0 -5
  85. package/operations/entry/filtering/plugins/index.js.map +1 -1
  86. package/operations/entry/filtering/plugins/objectFilterCreate.js +3 -22
  87. package/operations/entry/filtering/plugins/objectFilterCreate.js.map +1 -1
  88. package/operations/entry/filtering/plugins/refFilterCreate.js +3 -19
  89. package/operations/entry/filtering/plugins/refFilterCreate.js.map +1 -1
  90. package/operations/entry/filtering/sort.d.ts +4 -1
  91. package/operations/entry/filtering/sort.js +6 -17
  92. package/operations/entry/filtering/sort.js.map +1 -1
  93. package/operations/entry/filtering/systemFields.js +18 -2
  94. package/operations/entry/filtering/systemFields.js.map +1 -1
  95. package/operations/entry/filtering/transform.js +0 -3
  96. package/operations/entry/filtering/transform.js.map +1 -1
  97. package/operations/entry/filtering/values.d.ts +2 -0
  98. package/operations/entry/filtering/values.js +22 -0
  99. package/operations/entry/filtering/values.js.map +1 -0
  100. package/operations/entry/filtering/where.js +3 -8
  101. package/operations/entry/filtering/where.js.map +1 -1
  102. package/operations/entry/index.d.ts +1 -1
  103. package/operations/entry/index.js +189 -130
  104. package/operations/entry/index.js.map +1 -1
  105. package/operations/entry/keys.js +0 -18
  106. package/operations/entry/keys.js.map +1 -1
  107. package/operations/group/index.js +2 -32
  108. package/operations/group/index.js.map +1 -1
  109. package/operations/model/index.js +0 -25
  110. package/operations/model/index.js.map +1 -1
  111. package/operations/settings/index.js +0 -24
  112. package/operations/settings/index.js.map +1 -1
  113. package/operations/system/index.js +0 -17
  114. package/operations/system/index.js.map +1 -1
  115. package/package.json +21 -21
  116. package/plugins/CmsEntryFieldFilterPathPlugin.d.ts +2 -1
  117. package/plugins/CmsEntryFieldFilterPathPlugin.js +3 -15
  118. package/plugins/CmsEntryFieldFilterPathPlugin.js.map +1 -1
  119. package/plugins/CmsEntryFieldFilterPlugin.js +0 -7
  120. package/plugins/CmsEntryFieldFilterPlugin.js.map +1 -1
  121. package/plugins/CmsEntryFieldSortingPlugin.d.ts +42 -0
  122. package/plugins/CmsEntryFieldSortingPlugin.js +28 -0
  123. package/plugins/CmsEntryFieldSortingPlugin.js.map +1 -0
  124. package/plugins/CmsFieldFilterValueTransformPlugin.d.ts +11 -0
  125. package/plugins/CmsFieldFilterValueTransformPlugin.js +24 -0
  126. package/plugins/CmsFieldFilterValueTransformPlugin.js.map +1 -0
  127. package/plugins/index.d.ts +3 -0
  128. package/plugins/index.js +33 -2
  129. package/plugins/index.js.map +1 -1
  130. package/types.d.ts +10 -1
  131. package/types.js +0 -1
  132. package/types.js.map +1 -1
  133. package/operations/entry/filtering/createFilters.js +0 -155
  134. package/operations/entry/filtering/createFilters.js.map +0 -1
  135. package/operations/entry/systemFields.d.ts +0 -2
  136. package/operations/entry/systemFields.js +0 -74
  137. package/operations/entry/systemFields.js.map +0 -1
  138. package/operations/entry/utils.d.ts +0 -36
  139. package/operations/entry/utils.js +0 -684
  140. package/operations/entry/utils.js.map +0 -1
@@ -1,50 +1,32 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
-
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
7
  exports.createEntriesStorageOperations = void 0;
9
-
10
8
  var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
11
-
12
9
  var _error = _interopRequireDefault(require("@webiny/error"));
13
-
14
10
  var _dataLoaders = require("./dataLoaders");
15
-
16
11
  var _types = require("@webiny/api-headless-cms/types");
17
-
18
12
  var _keys = require("./keys");
19
-
20
13
  var _batchWrite = require("@webiny/db-dynamodb/utils/batchWrite");
21
-
22
14
  var _query = require("@webiny/db-dynamodb/utils/query");
23
-
24
15
  var _cleanup = require("@webiny/db-dynamodb/utils/cleanup");
25
-
26
16
  var _cursor = require("@webiny/utils/cursor");
27
-
28
17
  var _zeroPad = require("@webiny/utils/zeroPad");
29
-
30
18
  var _apiHeadlessCms = require("@webiny/api-headless-cms");
31
-
32
19
  var _createFields = require("./filtering/createFields");
33
-
34
20
  var _filtering = require("./filtering");
35
-
36
21
  const createType = () => {
37
22
  return "cms.entry";
38
23
  };
39
-
40
24
  const createLatestType = () => {
41
25
  return `${createType()}.l`;
42
26
  };
43
-
44
27
  const createPublishedType = () => {
45
28
  return `${createType()}.p`;
46
29
  };
47
-
48
30
  const convertToStorageEntry = params => {
49
31
  const {
50
32
  model,
@@ -58,7 +40,6 @@ const convertToStorageEntry = params => {
58
40
  values
59
41
  });
60
42
  };
61
-
62
43
  const convertFromStorageEntry = params => {
63
44
  const {
64
45
  model,
@@ -72,12 +53,24 @@ const convertFromStorageEntry = params => {
72
53
  values
73
54
  });
74
55
  };
75
-
56
+ const MAX_LIST_LIMIT = 10000;
76
57
  const createEntriesStorageOperations = params => {
77
58
  const {
78
59
  entity,
79
60
  plugins
80
61
  } = params;
62
+ let storageOperationsCmsModelPlugin;
63
+ const getStorageOperationsCmsModelPlugin = () => {
64
+ if (storageOperationsCmsModelPlugin) {
65
+ return storageOperationsCmsModelPlugin;
66
+ }
67
+ storageOperationsCmsModelPlugin = plugins.oneByType(_apiHeadlessCms.StorageOperationsCmsModelPlugin.type);
68
+ return storageOperationsCmsModelPlugin;
69
+ };
70
+ const getStorageOperationsModel = model => {
71
+ const plugin = getStorageOperationsCmsModelPlugin();
72
+ return plugin.getModel(model);
73
+ };
81
74
  const dataLoaders = new _dataLoaders.DataLoadersHandler({
82
75
  entity
83
76
  });
@@ -85,34 +78,29 @@ const createEntriesStorageOperations = params => {
85
78
  collection[plugin.fieldType] = plugin;
86
79
  return collection;
87
80
  }, {});
88
-
89
81
  const createStorageTransformCallable = model => {
90
82
  return (field, value) => {
91
83
  const plugin = storageTransformPlugins[field.type];
92
-
93
84
  if (!plugin) {
94
85
  return value;
95
86
  }
96
-
97
87
  return plugin.fromStorage({
98
88
  model,
99
89
  field,
100
90
  value,
101
-
102
91
  getStoragePlugin(fieldType) {
103
92
  return storageTransformPlugins[fieldType] || storageTransformPlugins["*"];
104
93
  },
105
-
106
94
  plugins
107
95
  });
108
96
  };
109
97
  };
110
-
111
- const create = async (model, params) => {
98
+ const create = async (initialModel, params) => {
112
99
  const {
113
100
  entry,
114
101
  storageEntry: initialStorageEntry
115
102
  } = params;
103
+ const model = getStorageOperationsModel(initialModel);
116
104
  const partitionKey = (0, _keys.createPartitionKey)({
117
105
  id: entry.id,
118
106
  locale: model.locale,
@@ -127,9 +115,8 @@ const createEntriesStorageOperations = params => {
127
115
  /**
128
116
  * We need to:
129
117
  * - create new main entry item
130
- * - create new or update latest entry item
118
+ * - create new or update the latest entry item
131
119
  */
132
-
133
120
  const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
134
121
  locked,
135
122
  PK: partitionKey,
@@ -145,10 +132,10 @@ const createEntriesStorageOperations = params => {
145
132
  GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
146
133
  GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
147
134
  }))];
135
+
148
136
  /**
149
137
  * We need to create published entry if
150
138
  */
151
-
152
139
  if (isPublished) {
153
140
  items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
154
141
  locked,
@@ -159,7 +146,6 @@ const createEntriesStorageOperations = params => {
159
146
  GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
160
147
  })));
161
148
  }
162
-
163
149
  try {
164
150
  await (0, _batchWrite.batchWriteAll)({
165
151
  table: entity.table,
@@ -174,15 +160,14 @@ const createEntriesStorageOperations = params => {
174
160
  entry
175
161
  });
176
162
  }
177
-
178
163
  return initialStorageEntry;
179
164
  };
180
-
181
- const createRevisionFrom = async (model, params) => {
165
+ const createRevisionFrom = async (initialModel, params) => {
182
166
  const {
183
167
  entry,
184
168
  storageEntry: initialStorageEntry
185
169
  } = params;
170
+ const model = getStorageOperationsModel(initialModel);
186
171
  const partitionKey = (0, _keys.createPartitionKey)({
187
172
  id: entry.id,
188
173
  locale: model.locale,
@@ -197,7 +182,6 @@ const createEntriesStorageOperations = params => {
197
182
  * - create the main entry item
198
183
  * - update the last entry item to a current one
199
184
  */
200
-
201
185
  const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
202
186
  PK: partitionKey,
203
187
  SK: (0, _keys.createRevisionSortKey)(storageEntry),
@@ -211,7 +195,6 @@ const createEntriesStorageOperations = params => {
211
195
  GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
212
196
  GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
213
197
  }))];
214
-
215
198
  try {
216
199
  await (0, _batchWrite.batchWriteAll)({
217
200
  table: entity.table,
@@ -230,16 +213,14 @@ const createEntriesStorageOperations = params => {
230
213
  /**
231
214
  * There are no modifications on the entry created so just return the data.
232
215
  */
233
-
234
-
235
216
  return initialStorageEntry;
236
217
  };
237
-
238
- const update = async (model, params) => {
218
+ const update = async (initialModel, params) => {
239
219
  const {
240
220
  entry,
241
221
  storageEntry: initialStorageEntry
242
222
  } = params;
223
+ const model = getStorageOperationsModel(initialModel);
243
224
  const partitionKey = (0, _keys.createPartitionKey)({
244
225
  id: entry.id,
245
226
  locale: model.locale,
@@ -257,7 +238,6 @@ const createEntriesStorageOperations = params => {
257
238
  * - update the current entry
258
239
  * - update the latest entry if the current entry is the latest one
259
240
  */
260
-
261
241
  items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
262
242
  locked,
263
243
  PK: partitionKey,
@@ -266,7 +246,6 @@ const createEntriesStorageOperations = params => {
266
246
  GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
267
247
  GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
268
248
  })));
269
-
270
249
  if (isPublished) {
271
250
  items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
272
251
  locked,
@@ -277,13 +256,11 @@ const createEntriesStorageOperations = params => {
277
256
  GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
278
257
  })));
279
258
  }
259
+
280
260
  /**
281
- * We need the latest entry to update it as well if neccessary.
261
+ * We need the latest entry to update it as well if necessary.
282
262
  */
283
-
284
-
285
263
  const latestStorageEntry = await getLatestRevisionByEntryId(model, entry);
286
-
287
264
  if (latestStorageEntry && latestStorageEntry.id === entry.id) {
288
265
  items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
289
266
  locked,
@@ -294,7 +271,6 @@ const createEntriesStorageOperations = params => {
294
271
  GSI1_SK: (0, _keys.createGSISortKey)(entry)
295
272
  })));
296
273
  }
297
-
298
274
  try {
299
275
  await (0, _batchWrite.batchWriteAll)({
300
276
  table: entity.table,
@@ -312,15 +288,16 @@ const createEntriesStorageOperations = params => {
312
288
  });
313
289
  }
314
290
  };
315
-
316
- const deleteEntry = async (model, params) => {
291
+ const deleteEntry = async (initialModel, params) => {
317
292
  const {
318
293
  entry
319
294
  } = params;
295
+ const id = entry.id || entry.entryId;
296
+ const model = getStorageOperationsModel(initialModel);
320
297
  const queryAllParams = {
321
298
  entity,
322
299
  partitionKey: (0, _keys.createPartitionKey)({
323
- id: entry.id,
300
+ id,
324
301
  locale: model.locale,
325
302
  tenant: model.tenant
326
303
  }),
@@ -329,23 +306,20 @@ const createEntriesStorageOperations = params => {
329
306
  }
330
307
  };
331
308
  let records = [];
332
-
333
309
  try {
334
310
  records = await (0, _query.queryAll)(queryAllParams);
335
311
  } catch (ex) {
336
312
  throw new _error.default(ex.message || "Could not load all records.", ex.code || "LOAD_ALL_RECORDS_ERROR", {
337
313
  error: ex,
338
- entry
314
+ id
339
315
  });
340
316
  }
341
-
342
317
  const items = records.map(item => {
343
318
  return entity.deleteBatch({
344
319
  PK: item.PK,
345
320
  SK: item.SK
346
321
  });
347
322
  });
348
-
349
323
  try {
350
324
  await (0, _batchWrite.batchWriteAll)({
351
325
  table: entity.table,
@@ -358,17 +332,17 @@ const createEntriesStorageOperations = params => {
358
332
  throw new _error.default(ex.message || "Could not delete the entry.", ex.code || "DELETE_ENTRY_ERROR", {
359
333
  error: ex,
360
334
  partitionKey: queryAllParams.partitionKey,
361
- entry
335
+ id
362
336
  });
363
337
  }
364
338
  };
365
-
366
- const deleteRevision = async (model, params) => {
339
+ const deleteRevision = async (initialModel, params) => {
367
340
  const {
368
341
  entry,
369
342
  latestEntry,
370
343
  latestStorageEntry: initialLatestStorageEntry
371
344
  } = params;
345
+ const model = getStorageOperationsModel(initialModel);
372
346
  const partitionKey = (0, _keys.createPartitionKey)({
373
347
  id: entry.id,
374
348
  locale: model.locale,
@@ -379,17 +353,16 @@ const createEntriesStorageOperations = params => {
379
353
  SK: (0, _keys.createRevisionSortKey)(entry)
380
354
  })];
381
355
  const publishedStorageEntry = await getPublishedRevisionByEntryId(model, entry);
356
+
382
357
  /**
383
358
  * If revision we are deleting is the published one as well, we need to delete those records as well.
384
359
  */
385
-
386
360
  if (publishedStorageEntry && entry.id === publishedStorageEntry.id) {
387
361
  items.push(entity.deleteBatch({
388
362
  PK: partitionKey,
389
363
  SK: (0, _keys.createPublishedSortKey)()
390
364
  }));
391
365
  }
392
-
393
366
  if (initialLatestStorageEntry) {
394
367
  const latestStorageEntry = convertToStorageEntry({
395
368
  storageEntry: initialLatestStorageEntry,
@@ -403,7 +376,6 @@ const createEntriesStorageOperations = params => {
403
376
  GSI1_SK: (0, _keys.createGSISortKey)(latestStorageEntry)
404
377
  })));
405
378
  }
406
-
407
379
  try {
408
380
  await (0, _batchWrite.batchWriteAll)({
409
381
  table: entity.table,
@@ -420,59 +392,113 @@ const createEntriesStorageOperations = params => {
420
392
  });
421
393
  }
422
394
  };
423
-
424
- const getLatestRevisionByEntryId = async (model, params) => {
395
+ const deleteMultipleEntries = async (initialModel, params) => {
396
+ const {
397
+ entries
398
+ } = params;
399
+ const model = getStorageOperationsModel(initialModel);
400
+ /**
401
+ * First we need all the revisions of the entries we want to delete.
402
+ */
403
+ const revisions = await dataLoaders.getAllEntryRevisions({
404
+ model,
405
+ ids: entries
406
+ });
407
+ /**
408
+ * Then we need to construct the queries for all the revisions and entries.
409
+ */
410
+ const items = [];
411
+ for (const id of entries) {
412
+ /**
413
+ * Latest item.
414
+ */
415
+ items.push(entity.deleteBatch({
416
+ PK: (0, _keys.createPartitionKey)({
417
+ id,
418
+ locale: model.locale,
419
+ tenant: model.tenant
420
+ }),
421
+ SK: "L"
422
+ }));
423
+ /**
424
+ * Published item.
425
+ */
426
+ items.push(entity.deleteBatch({
427
+ PK: (0, _keys.createPartitionKey)({
428
+ id,
429
+ locale: model.locale,
430
+ tenant: model.tenant
431
+ }),
432
+ SK: "P"
433
+ }));
434
+ }
435
+ /**
436
+ * Exact revisions of all the entries
437
+ */
438
+ for (const revision of revisions) {
439
+ items.push(entity.deleteBatch({
440
+ PK: (0, _keys.createPartitionKey)({
441
+ id: revision.id,
442
+ locale: model.locale,
443
+ tenant: model.tenant
444
+ }),
445
+ SK: (0, _keys.createRevisionSortKey)({
446
+ version: revision.version
447
+ })
448
+ }));
449
+ }
450
+ await (0, _batchWrite.batchWriteAll)({
451
+ table: entity.table,
452
+ items
453
+ });
454
+ };
455
+ const getLatestRevisionByEntryId = async (initialModel, params) => {
456
+ const model = getStorageOperationsModel(initialModel);
425
457
  const items = await dataLoaders.getLatestRevisionByEntryId({
426
458
  model,
427
459
  ids: [params.id]
428
460
  });
429
461
  const item = items.shift() || null;
430
-
431
462
  if (!item) {
432
463
  return null;
433
464
  }
434
-
435
465
  return convertFromStorageEntry({
436
466
  storageEntry: item,
437
467
  model
438
468
  });
439
469
  };
440
-
441
- const getPublishedRevisionByEntryId = async (model, params) => {
470
+ const getPublishedRevisionByEntryId = async (initialModel, params) => {
471
+ const model = getStorageOperationsModel(initialModel);
442
472
  const items = await dataLoaders.getPublishedRevisionByEntryId({
443
473
  model,
444
474
  ids: [params.id]
445
475
  });
446
476
  const item = items.shift() || null;
447
-
448
477
  if (!item) {
449
478
  return null;
450
479
  }
451
-
452
480
  return convertFromStorageEntry({
453
481
  storageEntry: item,
454
482
  model
455
483
  });
456
484
  };
457
-
458
- const getRevisionById = async (model, params) => {
485
+ const getRevisionById = async (initialModel, params) => {
486
+ const model = getStorageOperationsModel(initialModel);
459
487
  const items = await dataLoaders.getRevisionById({
460
488
  model,
461
489
  ids: [params.id]
462
490
  });
463
491
  const item = items.shift() || null;
464
-
465
492
  if (!item) {
466
493
  return null;
467
494
  }
468
-
469
495
  return convertFromStorageEntry({
470
496
  storageEntry: item,
471
497
  model
472
498
  });
473
499
  };
474
-
475
- const getRevisions = async (model, params) => {
500
+ const getRevisions = async (initialModel, params) => {
501
+ const model = getStorageOperationsModel(initialModel);
476
502
  const items = await dataLoaders.getAllEntryRevisions({
477
503
  model,
478
504
  ids: [params.id]
@@ -484,8 +510,8 @@ const createEntriesStorageOperations = params => {
484
510
  });
485
511
  });
486
512
  };
487
-
488
- const getByIds = async (model, params) => {
513
+ const getByIds = async (initialModel, params) => {
514
+ const model = getStorageOperationsModel(initialModel);
489
515
  const items = await dataLoaders.getRevisionById({
490
516
  model,
491
517
  ids: params.ids
@@ -497,8 +523,8 @@ const createEntriesStorageOperations = params => {
497
523
  });
498
524
  });
499
525
  };
500
-
501
- const getLatestByIds = async (model, params) => {
526
+ const getLatestByIds = async (initialModel, params) => {
527
+ const model = getStorageOperationsModel(initialModel);
502
528
  const items = await dataLoaders.getLatestRevisionByEntryId({
503
529
  model,
504
530
  ids: params.ids
@@ -510,8 +536,8 @@ const createEntriesStorageOperations = params => {
510
536
  });
511
537
  });
512
538
  };
513
-
514
- const getPublishedByIds = async (model, params) => {
539
+ const getPublishedByIds = async (initialModel, params) => {
540
+ const model = getStorageOperationsModel(initialModel);
515
541
  const items = await dataLoaders.getPublishedRevisionByEntryId({
516
542
  model,
517
543
  ids: params.ids
@@ -523,8 +549,8 @@ const createEntriesStorageOperations = params => {
523
549
  });
524
550
  });
525
551
  };
526
-
527
- const getPreviousRevision = async (model, params) => {
552
+ const getPreviousRevision = async (initialModel, params) => {
553
+ const model = getStorageOperationsModel(initialModel);
528
554
  const {
529
555
  entryId,
530
556
  version
@@ -538,7 +564,6 @@ const createEntriesStorageOperations = params => {
538
564
  }),
539
565
  options: {
540
566
  lt: `REV#${(0, _zeroPad.zeroPad)(version)}`,
541
-
542
567
  /**
543
568
  * We need to have extra checks because DynamoDB will return published or latest record if there is no REV# record.
544
569
  */
@@ -552,15 +577,12 @@ const createEntriesStorageOperations = params => {
552
577
  reverse: true
553
578
  }
554
579
  };
555
-
556
580
  try {
557
581
  const result = await (0, _query.queryOne)(queryParams);
558
582
  const storageEntry = (0, _cleanup.cleanupItem)(entity, result);
559
-
560
583
  if (!storageEntry) {
561
584
  return null;
562
585
  }
563
-
564
586
  return convertFromStorageEntry({
565
587
  storageEntry,
566
588
  model
@@ -574,8 +596,8 @@ const createEntriesStorageOperations = params => {
574
596
  }));
575
597
  }
576
598
  };
577
-
578
- const list = async (model, params) => {
599
+ const list = async (initialModel, params) => {
600
+ const model = getStorageOperationsModel(initialModel);
579
601
  const {
580
602
  limit: initialLimit = 10,
581
603
  where: initialWhere,
@@ -584,7 +606,7 @@ const createEntriesStorageOperations = params => {
584
606
  fields,
585
607
  search
586
608
  } = params;
587
- const limit = initialLimit <= 0 || initialLimit >= 10000 ? 10000 : initialLimit;
609
+ const limit = initialLimit <= 0 || initialLimit >= MAX_LIST_LIMIT ? MAX_LIST_LIMIT : initialLimit;
588
610
  const type = initialWhere.published ? "P" : "L";
589
611
  const queryAllParams = {
590
612
  entity,
@@ -594,10 +616,9 @@ const createEntriesStorageOperations = params => {
594
616
  gte: " "
595
617
  }
596
618
  };
597
- let records = [];
598
-
619
+ let storageEntries = [];
599
620
  try {
600
- records = await (0, _query.queryAll)(queryAllParams);
621
+ storageEntries = await (0, _query.queryAll)(queryAllParams);
601
622
  } catch (ex) {
602
623
  throw new _error.default(ex.message, "QUERY_ENTRIES_ERROR", {
603
624
  error: ex,
@@ -605,8 +626,7 @@ const createEntriesStorageOperations = params => {
605
626
  options: queryAllParams.options
606
627
  });
607
628
  }
608
-
609
- if (records.length === 0) {
629
+ if (storageEntries.length === 0) {
610
630
  return {
611
631
  hasMoreItems: false,
612
632
  totalCount: 0,
@@ -614,7 +634,6 @@ const createEntriesStorageOperations = params => {
614
634
  items: []
615
635
  };
616
636
  }
617
-
618
637
  const where = (0, _objectSpread2.default)({}, initialWhere);
619
638
  delete where["published"];
620
639
  delete where["latest"];
@@ -622,39 +641,49 @@ const createEntriesStorageOperations = params => {
622
641
  * We need an object containing field, transformers and paths.
623
642
  * Just build it here and pass on into other methods that require it to avoid mapping multiple times.
624
643
  */
625
-
626
644
  const modelFields = (0, _createFields.createFields)({
627
645
  plugins,
628
- model
646
+ fields: model.fields
629
647
  });
648
+ const fromStorage = createStorageTransformCallable(model);
649
+ /**
650
+ * Let's transform records from storage ones to regular ones, so we do not need to do it later.
651
+ *
652
+ * This is always being done, but at least its in parallel.
653
+ */
654
+ const records = await Promise.all(storageEntries.map(async storageEntry => {
655
+ const entry = convertFromStorageEntry({
656
+ storageEntry,
657
+ model
658
+ });
659
+ for (const field of model.fields) {
660
+ entry.values[field.fieldId] = await fromStorage(field, entry.values[field.fieldId]);
661
+ }
662
+ return entry;
663
+ }));
630
664
  /**
631
665
  * Filter the read items via the code.
632
666
  * It will build the filters out of the where input and transform the values it is using.
633
667
  */
634
-
635
- const filteredItems = await (0, _filtering.filter)({
636
- items: records.map(record => {
637
- return convertFromStorageEntry({
638
- storageEntry: record,
639
- model
640
- });
641
- }),
668
+ const filteredItems = (0, _filtering.filter)({
669
+ items: records,
642
670
  where,
643
671
  plugins,
644
672
  fields: modelFields,
645
- fromStorage: createStorageTransformCallable(model),
646
673
  fullTextSearch: {
647
674
  term: search,
648
675
  fields: fields || []
649
676
  }
650
677
  });
651
678
  const totalCount = filteredItems.length;
679
+
652
680
  /**
653
681
  * Sorting is also done via the code.
654
682
  * It takes the sort input and sorts by it via the lodash sortBy method.
655
683
  */
656
-
657
684
  const sortedItems = (0, _filtering.sort)({
685
+ model,
686
+ plugins,
658
687
  items: filteredItems,
659
688
  sort: sortBy,
660
689
  fields: modelFields
@@ -667,7 +696,6 @@ const createEntriesStorageOperations = params => {
667
696
  * Although we do not need a cursor here, we will use it as such to keep it standardized.
668
697
  * Number is simply encoded.
669
698
  */
670
-
671
699
  const cursor = totalCount > start + limit ? (0, _cursor.encodeCursor)(`${start + limit}`) : null;
672
700
  return {
673
701
  hasMoreItems,
@@ -676,8 +704,8 @@ const createEntriesStorageOperations = params => {
676
704
  items: (0, _cleanup.cleanupItems)(entity, slicedItems)
677
705
  };
678
706
  };
679
-
680
- const get = async (model, params) => {
707
+ const get = async (initialModel, params) => {
708
+ const model = getStorageOperationsModel(initialModel);
681
709
  const {
682
710
  items
683
711
  } = await list(model, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), {}, {
@@ -685,21 +713,21 @@ const createEntriesStorageOperations = params => {
685
713
  }));
686
714
  return items.shift() || null;
687
715
  };
688
-
689
- const publish = async (model, params) => {
716
+ const publish = async (initialModel, params) => {
690
717
  const {
691
718
  entry,
692
719
  storageEntry: initialStorageEntry
693
720
  } = params;
721
+ const model = getStorageOperationsModel(initialModel);
694
722
  const partitionKey = (0, _keys.createPartitionKey)({
695
723
  id: entry.id,
696
724
  locale: model.locale,
697
725
  tenant: model.tenant
698
726
  });
727
+
699
728
  /**
700
- * We need the latest and published entries to see if something needs to be updated along side the publishing one.
729
+ * We need the latest and published entries to see if something needs to be updated alongside the publishing one.
701
730
  */
702
-
703
731
  const initialLatestStorageEntry = await getLatestRevisionByEntryId(model, entry);
704
732
  const initialPublishedStorageEntry = await getPublishedRevisionByEntryId(model, entry);
705
733
  const storageEntry = convertToStorageEntry({
@@ -710,10 +738,9 @@ const createEntriesStorageOperations = params => {
710
738
  * We need to update:
711
739
  * - current entry revision sort key
712
740
  * - published sort key
713
- * - latest sort key - if entry updated is actually latest
741
+ * - the latest sort key - if entry updated is actually latest
714
742
  * - previous published entry to unpublished status - if any previously published entry
715
743
  */
716
-
717
744
  const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
718
745
  PK: partitionKey,
719
746
  SK: (0, _keys.createRevisionSortKey)(entry),
@@ -727,7 +754,6 @@ const createEntriesStorageOperations = params => {
727
754
  GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "P"),
728
755
  GSI1_SK: (0, _keys.createGSISortKey)(entry)
729
756
  }))];
730
-
731
757
  if (initialLatestStorageEntry && entry.id === initialLatestStorageEntry.id) {
732
758
  items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
733
759
  PK: partitionKey,
@@ -737,7 +763,6 @@ const createEntriesStorageOperations = params => {
737
763
  GSI1_SK: (0, _keys.createGSISortKey)(entry)
738
764
  })));
739
765
  }
740
-
741
766
  if (initialPublishedStorageEntry && initialPublishedStorageEntry.id !== entry.id) {
742
767
  const publishedStorageEntry = convertToStorageEntry({
743
768
  storageEntry: initialPublishedStorageEntry,
@@ -752,7 +777,6 @@ const createEntriesStorageOperations = params => {
752
777
  GSI1_SK: (0, _keys.createGSISortKey)(publishedStorageEntry)
753
778
  })));
754
779
  }
755
-
756
780
  try {
757
781
  await (0, _batchWrite.batchWriteAll)({
758
782
  table: entity.table,
@@ -770,12 +794,12 @@ const createEntriesStorageOperations = params => {
770
794
  });
771
795
  }
772
796
  };
773
-
774
- const unpublish = async (model, params) => {
797
+ const unpublish = async (initialModel, params) => {
775
798
  const {
776
799
  entry,
777
800
  storageEntry: initialStorageEntry
778
801
  } = params;
802
+ const model = getStorageOperationsModel(initialModel);
779
803
  const partitionKey = (0, _keys.createPartitionKey)({
780
804
  id: entry.id,
781
805
  locale: model.locale,
@@ -789,9 +813,8 @@ const createEntriesStorageOperations = params => {
789
813
  * We need to:
790
814
  * - delete currently published entry
791
815
  * - update current entry revision with new data
792
- * - update latest entry status - if entry being unpublished is latest
816
+ * - update the latest entry status - if entry being unpublished is latest
793
817
  */
794
-
795
818
  const items = [entity.deleteBatch({
796
819
  PK: partitionKey,
797
820
  SK: (0, _keys.createPublishedSortKey)()
@@ -802,12 +825,11 @@ const createEntriesStorageOperations = params => {
802
825
  GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
803
826
  GSI1_SK: (0, _keys.createGSISortKey)(entry)
804
827
  }))];
828
+
805
829
  /**
806
- * We need the latest entry to see if something needs to be updated along side the unpublishing one.
830
+ * We need the latest entry to see if something needs to be updated alongside the unpublishing one.
807
831
  */
808
-
809
832
  const latestStorageEntry = await getLatestRevisionByEntryId(model, entry);
810
-
811
833
  if (latestStorageEntry && entry.id === latestStorageEntry.id) {
812
834
  items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
813
835
  PK: partitionKey,
@@ -817,7 +839,6 @@ const createEntriesStorageOperations = params => {
817
839
  GSI1_SK: (0, _keys.createGSISortKey)(entry)
818
840
  })));
819
841
  }
820
-
821
842
  try {
822
843
  await (0, _batchWrite.batchWriteAll)({
823
844
  table: entity.table,
@@ -834,13 +855,50 @@ const createEntriesStorageOperations = params => {
834
855
  });
835
856
  }
836
857
  };
837
-
858
+ const getUniqueFieldValues = async (model, params) => {
859
+ const {
860
+ where,
861
+ fieldId
862
+ } = params;
863
+ const field = model.fields.find(f => f.fieldId === fieldId);
864
+ if (!field) {
865
+ throw new _error.default(`Could not find field with given "fieldId" value.`, "FIELD_NOT_FOUND", {
866
+ fieldId
867
+ });
868
+ }
869
+ const {
870
+ items
871
+ } = await list(model, {
872
+ where,
873
+ limit: MAX_LIST_LIMIT
874
+ });
875
+ const result = {};
876
+ for (const item of items) {
877
+ const fieldValue = item.values[field.fieldId];
878
+ if (!fieldValue) {
879
+ continue;
880
+ }
881
+ const values = Array.isArray(fieldValue) ? fieldValue : [fieldValue];
882
+ if (values.length === 0) {
883
+ continue;
884
+ }
885
+ for (const value of values) {
886
+ var _result$value;
887
+ result[value] = {
888
+ value,
889
+ count: (((_result$value = result[value]) === null || _result$value === void 0 ? void 0 : _result$value.count) || 0) + 1
890
+ };
891
+ }
892
+ }
893
+ return Object.values(result).sort((a, b) => a.value > b.value ? 1 : b.value > a.value ? -1 : 0).sort((a, b) => b.count - a.count);
894
+ };
838
895
  return {
839
896
  create,
840
897
  createRevisionFrom,
841
898
  update,
842
899
  delete: deleteEntry,
843
900
  deleteRevision,
901
+ deleteMultipleEntries,
844
902
  getPreviousRevision,
845
903
  getPublishedByIds,
846
904
  getLatestByIds,
@@ -852,8 +910,9 @@ const createEntriesStorageOperations = params => {
852
910
  getRevisions,
853
911
  publish,
854
912
  list,
855
- unpublish
913
+ unpublish,
914
+ dataLoaders,
915
+ getUniqueFieldValues
856
916
  };
857
917
  };
858
-
859
918
  exports.createEntriesStorageOperations = createEntriesStorageOperations;