@webiny/api-headless-cms-ddb 0.0.0-unstable.97a151f74d → 0.0.0-unstable.98511f29f1

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 +1 -20
  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 +240 -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 +4 -6
  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 +4 -5
  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,66 @@ const createEntriesStorageOperations = params => {
312
288
  });
313
289
  }
314
290
  };
315
-
316
- const deleteEntry = async (model, params) => {
291
+ const move = async (initialModel, id, folderId) => {
292
+ /**
293
+ * We need to:
294
+ * - load all the revisions of the entry, including published and latest
295
+ * - update all the revisions (published and latest ) of the entry with new folderId
296
+ */
297
+ const model = getStorageOperationsModel(initialModel);
298
+ /**
299
+ * First we need to load all the revisions and published / latest entry.
300
+ */
301
+ const queryAllParams = {
302
+ entity,
303
+ partitionKey: (0, _keys.createPartitionKey)({
304
+ id,
305
+ locale: model.locale,
306
+ tenant: model.tenant
307
+ }),
308
+ options: {
309
+ gte: " "
310
+ }
311
+ };
312
+ const records = await (0, _query.queryAll)(queryAllParams);
313
+ /**
314
+ * Then create the batch writes for the DynamoDB, with the updated folderId.
315
+ */
316
+ const items = records.map(item => {
317
+ return entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, item), {}, {
318
+ location: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, item.location), {}, {
319
+ folderId
320
+ })
321
+ }));
322
+ });
323
+ /**
324
+ * And finally write it...
325
+ */
326
+ try {
327
+ await (0, _batchWrite.batchWriteAll)({
328
+ table: entity.table,
329
+ items
330
+ });
331
+ } catch (ex) {
332
+ throw _error.default.from(ex, {
333
+ message: "Could not move records to a new folder.",
334
+ data: {
335
+ id,
336
+ folderId
337
+ }
338
+ });
339
+ }
340
+ };
341
+ const deleteEntry = async (initialModel, params) => {
317
342
  const {
318
343
  entry
319
344
  } = params;
345
+ const id = entry.id || entry.entryId;
346
+ const model = getStorageOperationsModel(initialModel);
320
347
  const queryAllParams = {
321
348
  entity,
322
349
  partitionKey: (0, _keys.createPartitionKey)({
323
- id: entry.id,
350
+ id,
324
351
  locale: model.locale,
325
352
  tenant: model.tenant
326
353
  }),
@@ -329,23 +356,20 @@ const createEntriesStorageOperations = params => {
329
356
  }
330
357
  };
331
358
  let records = [];
332
-
333
359
  try {
334
360
  records = await (0, _query.queryAll)(queryAllParams);
335
361
  } catch (ex) {
336
362
  throw new _error.default(ex.message || "Could not load all records.", ex.code || "LOAD_ALL_RECORDS_ERROR", {
337
363
  error: ex,
338
- entry
364
+ id
339
365
  });
340
366
  }
341
-
342
367
  const items = records.map(item => {
343
368
  return entity.deleteBatch({
344
369
  PK: item.PK,
345
370
  SK: item.SK
346
371
  });
347
372
  });
348
-
349
373
  try {
350
374
  await (0, _batchWrite.batchWriteAll)({
351
375
  table: entity.table,
@@ -358,17 +382,17 @@ const createEntriesStorageOperations = params => {
358
382
  throw new _error.default(ex.message || "Could not delete the entry.", ex.code || "DELETE_ENTRY_ERROR", {
359
383
  error: ex,
360
384
  partitionKey: queryAllParams.partitionKey,
361
- entry
385
+ id
362
386
  });
363
387
  }
364
388
  };
365
-
366
- const deleteRevision = async (model, params) => {
389
+ const deleteRevision = async (initialModel, params) => {
367
390
  const {
368
391
  entry,
369
392
  latestEntry,
370
393
  latestStorageEntry: initialLatestStorageEntry
371
394
  } = params;
395
+ const model = getStorageOperationsModel(initialModel);
372
396
  const partitionKey = (0, _keys.createPartitionKey)({
373
397
  id: entry.id,
374
398
  locale: model.locale,
@@ -379,17 +403,16 @@ const createEntriesStorageOperations = params => {
379
403
  SK: (0, _keys.createRevisionSortKey)(entry)
380
404
  })];
381
405
  const publishedStorageEntry = await getPublishedRevisionByEntryId(model, entry);
406
+
382
407
  /**
383
408
  * If revision we are deleting is the published one as well, we need to delete those records as well.
384
409
  */
385
-
386
410
  if (publishedStorageEntry && entry.id === publishedStorageEntry.id) {
387
411
  items.push(entity.deleteBatch({
388
412
  PK: partitionKey,
389
413
  SK: (0, _keys.createPublishedSortKey)()
390
414
  }));
391
415
  }
392
-
393
416
  if (initialLatestStorageEntry) {
394
417
  const latestStorageEntry = convertToStorageEntry({
395
418
  storageEntry: initialLatestStorageEntry,
@@ -403,7 +426,6 @@ const createEntriesStorageOperations = params => {
403
426
  GSI1_SK: (0, _keys.createGSISortKey)(latestStorageEntry)
404
427
  })));
405
428
  }
406
-
407
429
  try {
408
430
  await (0, _batchWrite.batchWriteAll)({
409
431
  table: entity.table,
@@ -420,59 +442,113 @@ const createEntriesStorageOperations = params => {
420
442
  });
421
443
  }
422
444
  };
423
-
424
- const getLatestRevisionByEntryId = async (model, params) => {
445
+ const deleteMultipleEntries = async (initialModel, params) => {
446
+ const {
447
+ entries
448
+ } = params;
449
+ const model = getStorageOperationsModel(initialModel);
450
+ /**
451
+ * First we need all the revisions of the entries we want to delete.
452
+ */
453
+ const revisions = await dataLoaders.getAllEntryRevisions({
454
+ model,
455
+ ids: entries
456
+ });
457
+ /**
458
+ * Then we need to construct the queries for all the revisions and entries.
459
+ */
460
+ const items = [];
461
+ for (const id of entries) {
462
+ /**
463
+ * Latest item.
464
+ */
465
+ items.push(entity.deleteBatch({
466
+ PK: (0, _keys.createPartitionKey)({
467
+ id,
468
+ locale: model.locale,
469
+ tenant: model.tenant
470
+ }),
471
+ SK: "L"
472
+ }));
473
+ /**
474
+ * Published item.
475
+ */
476
+ items.push(entity.deleteBatch({
477
+ PK: (0, _keys.createPartitionKey)({
478
+ id,
479
+ locale: model.locale,
480
+ tenant: model.tenant
481
+ }),
482
+ SK: "P"
483
+ }));
484
+ }
485
+ /**
486
+ * Exact revisions of all the entries
487
+ */
488
+ for (const revision of revisions) {
489
+ items.push(entity.deleteBatch({
490
+ PK: (0, _keys.createPartitionKey)({
491
+ id: revision.id,
492
+ locale: model.locale,
493
+ tenant: model.tenant
494
+ }),
495
+ SK: (0, _keys.createRevisionSortKey)({
496
+ version: revision.version
497
+ })
498
+ }));
499
+ }
500
+ await (0, _batchWrite.batchWriteAll)({
501
+ table: entity.table,
502
+ items
503
+ });
504
+ };
505
+ const getLatestRevisionByEntryId = async (initialModel, params) => {
506
+ const model = getStorageOperationsModel(initialModel);
425
507
  const items = await dataLoaders.getLatestRevisionByEntryId({
426
508
  model,
427
509
  ids: [params.id]
428
510
  });
429
511
  const item = items.shift() || null;
430
-
431
512
  if (!item) {
432
513
  return null;
433
514
  }
434
-
435
515
  return convertFromStorageEntry({
436
516
  storageEntry: item,
437
517
  model
438
518
  });
439
519
  };
440
-
441
- const getPublishedRevisionByEntryId = async (model, params) => {
520
+ const getPublishedRevisionByEntryId = async (initialModel, params) => {
521
+ const model = getStorageOperationsModel(initialModel);
442
522
  const items = await dataLoaders.getPublishedRevisionByEntryId({
443
523
  model,
444
524
  ids: [params.id]
445
525
  });
446
526
  const item = items.shift() || null;
447
-
448
527
  if (!item) {
449
528
  return null;
450
529
  }
451
-
452
530
  return convertFromStorageEntry({
453
531
  storageEntry: item,
454
532
  model
455
533
  });
456
534
  };
457
-
458
- const getRevisionById = async (model, params) => {
535
+ const getRevisionById = async (initialModel, params) => {
536
+ const model = getStorageOperationsModel(initialModel);
459
537
  const items = await dataLoaders.getRevisionById({
460
538
  model,
461
539
  ids: [params.id]
462
540
  });
463
541
  const item = items.shift() || null;
464
-
465
542
  if (!item) {
466
543
  return null;
467
544
  }
468
-
469
545
  return convertFromStorageEntry({
470
546
  storageEntry: item,
471
547
  model
472
548
  });
473
549
  };
474
-
475
- const getRevisions = async (model, params) => {
550
+ const getRevisions = async (initialModel, params) => {
551
+ const model = getStorageOperationsModel(initialModel);
476
552
  const items = await dataLoaders.getAllEntryRevisions({
477
553
  model,
478
554
  ids: [params.id]
@@ -484,8 +560,8 @@ const createEntriesStorageOperations = params => {
484
560
  });
485
561
  });
486
562
  };
487
-
488
- const getByIds = async (model, params) => {
563
+ const getByIds = async (initialModel, params) => {
564
+ const model = getStorageOperationsModel(initialModel);
489
565
  const items = await dataLoaders.getRevisionById({
490
566
  model,
491
567
  ids: params.ids
@@ -497,8 +573,8 @@ const createEntriesStorageOperations = params => {
497
573
  });
498
574
  });
499
575
  };
500
-
501
- const getLatestByIds = async (model, params) => {
576
+ const getLatestByIds = async (initialModel, params) => {
577
+ const model = getStorageOperationsModel(initialModel);
502
578
  const items = await dataLoaders.getLatestRevisionByEntryId({
503
579
  model,
504
580
  ids: params.ids
@@ -510,8 +586,8 @@ const createEntriesStorageOperations = params => {
510
586
  });
511
587
  });
512
588
  };
513
-
514
- const getPublishedByIds = async (model, params) => {
589
+ const getPublishedByIds = async (initialModel, params) => {
590
+ const model = getStorageOperationsModel(initialModel);
515
591
  const items = await dataLoaders.getPublishedRevisionByEntryId({
516
592
  model,
517
593
  ids: params.ids
@@ -523,8 +599,8 @@ const createEntriesStorageOperations = params => {
523
599
  });
524
600
  });
525
601
  };
526
-
527
- const getPreviousRevision = async (model, params) => {
602
+ const getPreviousRevision = async (initialModel, params) => {
603
+ const model = getStorageOperationsModel(initialModel);
528
604
  const {
529
605
  entryId,
530
606
  version
@@ -538,7 +614,6 @@ const createEntriesStorageOperations = params => {
538
614
  }),
539
615
  options: {
540
616
  lt: `REV#${(0, _zeroPad.zeroPad)(version)}`,
541
-
542
617
  /**
543
618
  * We need to have extra checks because DynamoDB will return published or latest record if there is no REV# record.
544
619
  */
@@ -552,15 +627,12 @@ const createEntriesStorageOperations = params => {
552
627
  reverse: true
553
628
  }
554
629
  };
555
-
556
630
  try {
557
631
  const result = await (0, _query.queryOne)(queryParams);
558
632
  const storageEntry = (0, _cleanup.cleanupItem)(entity, result);
559
-
560
633
  if (!storageEntry) {
561
634
  return null;
562
635
  }
563
-
564
636
  return convertFromStorageEntry({
565
637
  storageEntry,
566
638
  model
@@ -574,8 +646,8 @@ const createEntriesStorageOperations = params => {
574
646
  }));
575
647
  }
576
648
  };
577
-
578
- const list = async (model, params) => {
649
+ const list = async (initialModel, params) => {
650
+ const model = getStorageOperationsModel(initialModel);
579
651
  const {
580
652
  limit: initialLimit = 10,
581
653
  where: initialWhere,
@@ -584,7 +656,7 @@ const createEntriesStorageOperations = params => {
584
656
  fields,
585
657
  search
586
658
  } = params;
587
- const limit = initialLimit <= 0 || initialLimit >= 10000 ? 10000 : initialLimit;
659
+ const limit = initialLimit <= 0 || initialLimit >= MAX_LIST_LIMIT ? MAX_LIST_LIMIT : initialLimit;
588
660
  const type = initialWhere.published ? "P" : "L";
589
661
  const queryAllParams = {
590
662
  entity,
@@ -594,10 +666,9 @@ const createEntriesStorageOperations = params => {
594
666
  gte: " "
595
667
  }
596
668
  };
597
- let records = [];
598
-
669
+ let storageEntries = [];
599
670
  try {
600
- records = await (0, _query.queryAll)(queryAllParams);
671
+ storageEntries = await (0, _query.queryAll)(queryAllParams);
601
672
  } catch (ex) {
602
673
  throw new _error.default(ex.message, "QUERY_ENTRIES_ERROR", {
603
674
  error: ex,
@@ -605,8 +676,7 @@ const createEntriesStorageOperations = params => {
605
676
  options: queryAllParams.options
606
677
  });
607
678
  }
608
-
609
- if (records.length === 0) {
679
+ if (storageEntries.length === 0) {
610
680
  return {
611
681
  hasMoreItems: false,
612
682
  totalCount: 0,
@@ -614,7 +684,6 @@ const createEntriesStorageOperations = params => {
614
684
  items: []
615
685
  };
616
686
  }
617
-
618
687
  const where = (0, _objectSpread2.default)({}, initialWhere);
619
688
  delete where["published"];
620
689
  delete where["latest"];
@@ -622,39 +691,49 @@ const createEntriesStorageOperations = params => {
622
691
  * We need an object containing field, transformers and paths.
623
692
  * Just build it here and pass on into other methods that require it to avoid mapping multiple times.
624
693
  */
625
-
626
694
  const modelFields = (0, _createFields.createFields)({
627
695
  plugins,
628
- model
696
+ fields: model.fields
629
697
  });
698
+ const fromStorage = createStorageTransformCallable(model);
699
+ /**
700
+ * Let's transform records from storage ones to regular ones, so we do not need to do it later.
701
+ *
702
+ * This is always being done, but at least its in parallel.
703
+ */
704
+ const records = await Promise.all(storageEntries.map(async storageEntry => {
705
+ const entry = convertFromStorageEntry({
706
+ storageEntry,
707
+ model
708
+ });
709
+ for (const field of model.fields) {
710
+ entry.values[field.fieldId] = await fromStorage(field, entry.values[field.fieldId]);
711
+ }
712
+ return entry;
713
+ }));
630
714
  /**
631
715
  * Filter the read items via the code.
632
716
  * It will build the filters out of the where input and transform the values it is using.
633
717
  */
634
-
635
- const filteredItems = await (0, _filtering.filter)({
636
- items: records.map(record => {
637
- return convertFromStorageEntry({
638
- storageEntry: record,
639
- model
640
- });
641
- }),
718
+ const filteredItems = (0, _filtering.filter)({
719
+ items: records,
642
720
  where,
643
721
  plugins,
644
722
  fields: modelFields,
645
- fromStorage: createStorageTransformCallable(model),
646
723
  fullTextSearch: {
647
724
  term: search,
648
725
  fields: fields || []
649
726
  }
650
727
  });
651
728
  const totalCount = filteredItems.length;
729
+
652
730
  /**
653
731
  * Sorting is also done via the code.
654
732
  * It takes the sort input and sorts by it via the lodash sortBy method.
655
733
  */
656
-
657
734
  const sortedItems = (0, _filtering.sort)({
735
+ model,
736
+ plugins,
658
737
  items: filteredItems,
659
738
  sort: sortBy,
660
739
  fields: modelFields
@@ -667,7 +746,6 @@ const createEntriesStorageOperations = params => {
667
746
  * Although we do not need a cursor here, we will use it as such to keep it standardized.
668
747
  * Number is simply encoded.
669
748
  */
670
-
671
749
  const cursor = totalCount > start + limit ? (0, _cursor.encodeCursor)(`${start + limit}`) : null;
672
750
  return {
673
751
  hasMoreItems,
@@ -676,8 +754,8 @@ const createEntriesStorageOperations = params => {
676
754
  items: (0, _cleanup.cleanupItems)(entity, slicedItems)
677
755
  };
678
756
  };
679
-
680
- const get = async (model, params) => {
757
+ const get = async (initialModel, params) => {
758
+ const model = getStorageOperationsModel(initialModel);
681
759
  const {
682
760
  items
683
761
  } = await list(model, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), {}, {
@@ -685,21 +763,21 @@ const createEntriesStorageOperations = params => {
685
763
  }));
686
764
  return items.shift() || null;
687
765
  };
688
-
689
- const publish = async (model, params) => {
766
+ const publish = async (initialModel, params) => {
690
767
  const {
691
768
  entry,
692
769
  storageEntry: initialStorageEntry
693
770
  } = params;
771
+ const model = getStorageOperationsModel(initialModel);
694
772
  const partitionKey = (0, _keys.createPartitionKey)({
695
773
  id: entry.id,
696
774
  locale: model.locale,
697
775
  tenant: model.tenant
698
776
  });
777
+
699
778
  /**
700
- * We need the latest and published entries to see if something needs to be updated along side the publishing one.
779
+ * We need the latest and published entries to see if something needs to be updated alongside the publishing one.
701
780
  */
702
-
703
781
  const initialLatestStorageEntry = await getLatestRevisionByEntryId(model, entry);
704
782
  const initialPublishedStorageEntry = await getPublishedRevisionByEntryId(model, entry);
705
783
  const storageEntry = convertToStorageEntry({
@@ -710,10 +788,9 @@ const createEntriesStorageOperations = params => {
710
788
  * We need to update:
711
789
  * - current entry revision sort key
712
790
  * - published sort key
713
- * - latest sort key - if entry updated is actually latest
791
+ * - the latest sort key - if entry updated is actually latest
714
792
  * - previous published entry to unpublished status - if any previously published entry
715
793
  */
716
-
717
794
  const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
718
795
  PK: partitionKey,
719
796
  SK: (0, _keys.createRevisionSortKey)(entry),
@@ -727,7 +804,6 @@ const createEntriesStorageOperations = params => {
727
804
  GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "P"),
728
805
  GSI1_SK: (0, _keys.createGSISortKey)(entry)
729
806
  }))];
730
-
731
807
  if (initialLatestStorageEntry && entry.id === initialLatestStorageEntry.id) {
732
808
  items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
733
809
  PK: partitionKey,
@@ -737,7 +813,6 @@ const createEntriesStorageOperations = params => {
737
813
  GSI1_SK: (0, _keys.createGSISortKey)(entry)
738
814
  })));
739
815
  }
740
-
741
816
  if (initialPublishedStorageEntry && initialPublishedStorageEntry.id !== entry.id) {
742
817
  const publishedStorageEntry = convertToStorageEntry({
743
818
  storageEntry: initialPublishedStorageEntry,
@@ -752,7 +827,6 @@ const createEntriesStorageOperations = params => {
752
827
  GSI1_SK: (0, _keys.createGSISortKey)(publishedStorageEntry)
753
828
  })));
754
829
  }
755
-
756
830
  try {
757
831
  await (0, _batchWrite.batchWriteAll)({
758
832
  table: entity.table,
@@ -770,12 +844,12 @@ const createEntriesStorageOperations = params => {
770
844
  });
771
845
  }
772
846
  };
773
-
774
- const unpublish = async (model, params) => {
847
+ const unpublish = async (initialModel, params) => {
775
848
  const {
776
849
  entry,
777
850
  storageEntry: initialStorageEntry
778
851
  } = params;
852
+ const model = getStorageOperationsModel(initialModel);
779
853
  const partitionKey = (0, _keys.createPartitionKey)({
780
854
  id: entry.id,
781
855
  locale: model.locale,
@@ -789,9 +863,8 @@ const createEntriesStorageOperations = params => {
789
863
  * We need to:
790
864
  * - delete currently published entry
791
865
  * - update current entry revision with new data
792
- * - update latest entry status - if entry being unpublished is latest
866
+ * - update the latest entry status - if entry being unpublished is latest
793
867
  */
794
-
795
868
  const items = [entity.deleteBatch({
796
869
  PK: partitionKey,
797
870
  SK: (0, _keys.createPublishedSortKey)()
@@ -802,12 +875,11 @@ const createEntriesStorageOperations = params => {
802
875
  GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
803
876
  GSI1_SK: (0, _keys.createGSISortKey)(entry)
804
877
  }))];
878
+
805
879
  /**
806
- * We need the latest entry to see if something needs to be updated along side the unpublishing one.
880
+ * We need the latest entry to see if something needs to be updated alongside the unpublishing one.
807
881
  */
808
-
809
882
  const latestStorageEntry = await getLatestRevisionByEntryId(model, entry);
810
-
811
883
  if (latestStorageEntry && entry.id === latestStorageEntry.id) {
812
884
  items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
813
885
  PK: partitionKey,
@@ -817,7 +889,6 @@ const createEntriesStorageOperations = params => {
817
889
  GSI1_SK: (0, _keys.createGSISortKey)(entry)
818
890
  })));
819
891
  }
820
-
821
892
  try {
822
893
  await (0, _batchWrite.batchWriteAll)({
823
894
  table: entity.table,
@@ -834,13 +905,51 @@ const createEntriesStorageOperations = params => {
834
905
  });
835
906
  }
836
907
  };
837
-
908
+ const getUniqueFieldValues = async (model, params) => {
909
+ const {
910
+ where,
911
+ fieldId
912
+ } = params;
913
+ const field = model.fields.find(f => f.fieldId === fieldId);
914
+ if (!field) {
915
+ throw new _error.default(`Could not find field with given "fieldId" value.`, "FIELD_NOT_FOUND", {
916
+ fieldId
917
+ });
918
+ }
919
+ const {
920
+ items
921
+ } = await list(model, {
922
+ where,
923
+ limit: MAX_LIST_LIMIT
924
+ });
925
+ const result = {};
926
+ for (const item of items) {
927
+ const fieldValue = item.values[field.fieldId];
928
+ if (!fieldValue) {
929
+ continue;
930
+ }
931
+ const values = Array.isArray(fieldValue) ? fieldValue : [fieldValue];
932
+ if (values.length === 0) {
933
+ continue;
934
+ }
935
+ for (const value of values) {
936
+ var _result$value;
937
+ result[value] = {
938
+ value,
939
+ count: (((_result$value = result[value]) === null || _result$value === void 0 ? void 0 : _result$value.count) || 0) + 1
940
+ };
941
+ }
942
+ }
943
+ 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);
944
+ };
838
945
  return {
839
946
  create,
840
947
  createRevisionFrom,
841
948
  update,
949
+ move,
842
950
  delete: deleteEntry,
843
951
  deleteRevision,
952
+ deleteMultipleEntries,
844
953
  getPreviousRevision,
845
954
  getPublishedByIds,
846
955
  getLatestByIds,
@@ -852,8 +961,9 @@ const createEntriesStorageOperations = params => {
852
961
  getRevisions,
853
962
  publish,
854
963
  list,
855
- unpublish
964
+ unpublish,
965
+ dataLoaders,
966
+ getUniqueFieldValues
856
967
  };
857
968
  };
858
-
859
969
  exports.createEntriesStorageOperations = createEntriesStorageOperations;