@webiny/api-headless-cms-ddb 0.0.0-unstable.99666aeb00 → 0.0.0-unstable.9bd236cf5e

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 (154) hide show
  1. package/definitions/entry.d.ts +4 -3
  2. package/definitions/entry.js +104 -13
  3. package/definitions/entry.js.map +1 -1
  4. package/definitions/group.d.ts +4 -3
  5. package/definitions/group.js +9 -8
  6. package/definitions/group.js.map +1 -1
  7. package/definitions/model.d.ts +4 -3
  8. package/definitions/model.js +9 -8
  9. package/definitions/model.js.map +1 -1
  10. package/definitions/system.d.ts +4 -3
  11. package/definitions/system.js +9 -8
  12. package/definitions/system.js.map +1 -1
  13. package/definitions/table.d.ts +5 -5
  14. package/definitions/table.js +8 -4
  15. package/definitions/table.js.map +1 -1
  16. package/dynamoDb/index.js +4 -3
  17. package/dynamoDb/index.js.map +1 -1
  18. package/dynamoDb/path/locationFolderId.js +4 -2
  19. package/dynamoDb/path/locationFolderId.js.map +1 -1
  20. package/dynamoDb/path/plainObject.js +3 -1
  21. package/dynamoDb/path/plainObject.js.map +1 -1
  22. package/dynamoDb/storage/longText.js +4 -1
  23. package/dynamoDb/storage/longText.js.map +1 -1
  24. package/dynamoDb/storage/richText.js +24 -58
  25. package/dynamoDb/storage/richText.js.map +1 -1
  26. package/dynamoDb/transformValue/datetime.d.ts +1 -1
  27. package/dynamoDb/transformValue/datetime.js +3 -1
  28. package/dynamoDb/transformValue/datetime.js.map +1 -1
  29. package/index.d.ts +1 -1
  30. package/index.js +5 -12
  31. package/index.js.map +1 -1
  32. package/operations/entry/dataLoader/DataLoaderCache.d.ts +1 -1
  33. package/operations/entry/dataLoader/DataLoaderCache.js +7 -9
  34. package/operations/entry/dataLoader/DataLoaderCache.js.map +1 -1
  35. package/operations/entry/dataLoader/constants.js +3 -2
  36. package/operations/entry/dataLoader/constants.js.map +1 -1
  37. package/operations/entry/dataLoader/createBatchScheduleFn.js +3 -1
  38. package/operations/entry/dataLoader/createBatchScheduleFn.js.map +1 -1
  39. package/operations/entry/dataLoader/getAllEntryRevisions.d.ts +2 -2
  40. package/operations/entry/dataLoader/getAllEntryRevisions.js +3 -1
  41. package/operations/entry/dataLoader/getAllEntryRevisions.js.map +1 -1
  42. package/operations/entry/dataLoader/getLatestRevisionByEntryId.d.ts +2 -2
  43. package/operations/entry/dataLoader/getLatestRevisionByEntryId.js +5 -6
  44. package/operations/entry/dataLoader/getLatestRevisionByEntryId.js.map +1 -1
  45. package/operations/entry/dataLoader/getPublishedRevisionByEntryId.d.ts +2 -2
  46. package/operations/entry/dataLoader/getPublishedRevisionByEntryId.js +5 -6
  47. package/operations/entry/dataLoader/getPublishedRevisionByEntryId.js.map +1 -1
  48. package/operations/entry/dataLoader/getRevisionById.d.ts +2 -2
  49. package/operations/entry/dataLoader/getRevisionById.js +5 -6
  50. package/operations/entry/dataLoader/getRevisionById.js.map +1 -1
  51. package/operations/entry/dataLoader/index.d.ts +3 -3
  52. package/operations/entry/dataLoader/index.js +3 -1
  53. package/operations/entry/dataLoader/index.js.map +1 -1
  54. package/operations/entry/dataLoader/types.d.ts +1 -1
  55. package/operations/entry/dataLoader/types.js +3 -1
  56. package/operations/entry/dataLoader/types.js.map +1 -1
  57. package/operations/entry/dataLoaders.d.ts +3 -3
  58. package/operations/entry/dataLoaders.js +15 -14
  59. package/operations/entry/dataLoaders.js.map +1 -1
  60. package/operations/entry/filtering/createExpressions.d.ts +4 -4
  61. package/operations/entry/filtering/createExpressions.js +3 -1
  62. package/operations/entry/filtering/createExpressions.js.map +1 -1
  63. package/operations/entry/filtering/createFields.d.ts +3 -3
  64. package/operations/entry/filtering/createFields.js +12 -9
  65. package/operations/entry/filtering/createFields.js.map +1 -1
  66. package/operations/entry/filtering/extractSort.d.ts +3 -3
  67. package/operations/entry/filtering/extractSort.js +3 -1
  68. package/operations/entry/filtering/extractSort.js.map +1 -1
  69. package/operations/entry/filtering/filter.d.ts +3 -3
  70. package/operations/entry/filtering/filter.js +24 -9
  71. package/operations/entry/filtering/filter.js.map +1 -1
  72. package/operations/entry/filtering/fullTextSearch.d.ts +3 -3
  73. package/operations/entry/filtering/fullTextSearch.js +3 -1
  74. package/operations/entry/filtering/fullTextSearch.js.map +1 -1
  75. package/operations/entry/filtering/getValue.js +6 -3
  76. package/operations/entry/filtering/getValue.js.map +1 -1
  77. package/operations/entry/filtering/index.js +3 -1
  78. package/operations/entry/filtering/index.js.map +1 -1
  79. package/operations/entry/filtering/mapPlugins.d.ts +2 -2
  80. package/operations/entry/filtering/mapPlugins.js +3 -1
  81. package/operations/entry/filtering/mapPlugins.js.map +1 -1
  82. package/operations/entry/filtering/plugins/defaultFilterCreate.d.ts +1 -1
  83. package/operations/entry/filtering/plugins/defaultFilterCreate.js +3 -1
  84. package/operations/entry/filtering/plugins/defaultFilterCreate.js.map +1 -1
  85. package/operations/entry/filtering/plugins/index.d.ts +1 -1
  86. package/operations/entry/filtering/plugins/index.js +5 -2
  87. package/operations/entry/filtering/plugins/index.js.map +1 -1
  88. package/operations/entry/filtering/plugins/objectFilterCreate.d.ts +1 -1
  89. package/operations/entry/filtering/plugins/objectFilterCreate.js +3 -1
  90. package/operations/entry/filtering/plugins/objectFilterCreate.js.map +1 -1
  91. package/operations/entry/filtering/plugins/refFilterCreate.d.ts +2 -1
  92. package/operations/entry/filtering/plugins/refFilterCreate.js +9 -2
  93. package/operations/entry/filtering/plugins/refFilterCreate.js.map +1 -1
  94. package/operations/entry/filtering/plugins/searchableJsonFilterCreate.d.ts +2 -0
  95. package/operations/entry/filtering/plugins/searchableJsonFilterCreate.js +65 -0
  96. package/operations/entry/filtering/plugins/searchableJsonFilterCreate.js.map +1 -0
  97. package/operations/entry/filtering/sort.d.ts +3 -3
  98. package/operations/entry/filtering/sort.js +5 -3
  99. package/operations/entry/filtering/sort.js.map +1 -1
  100. package/operations/entry/filtering/systemFields.d.ts +2 -2
  101. package/operations/entry/filtering/systemFields.js +34 -32
  102. package/operations/entry/filtering/systemFields.js.map +1 -1
  103. package/operations/entry/filtering/transform.d.ts +1 -1
  104. package/operations/entry/filtering/transform.js +3 -1
  105. package/operations/entry/filtering/transform.js.map +1 -1
  106. package/operations/entry/filtering/types.d.ts +2 -2
  107. package/operations/entry/filtering/types.js +3 -1
  108. package/operations/entry/filtering/types.js.map +1 -1
  109. package/operations/entry/filtering/values.d.ts +1 -1
  110. package/operations/entry/filtering/values.js +3 -1
  111. package/operations/entry/filtering/values.js.map +1 -1
  112. package/operations/entry/filtering/where.js +3 -1
  113. package/operations/entry/filtering/where.js.map +1 -1
  114. package/operations/entry/index.d.ts +3 -3
  115. package/operations/entry/index.js +585 -197
  116. package/operations/entry/index.js.map +1 -1
  117. package/operations/entry/keys.js +3 -1
  118. package/operations/entry/keys.js.map +1 -1
  119. package/operations/group/index.d.ts +3 -3
  120. package/operations/group/index.js +33 -21
  121. package/operations/group/index.js.map +1 -1
  122. package/operations/model/index.d.ts +2 -2
  123. package/operations/model/index.js +26 -13
  124. package/operations/model/index.js.map +1 -1
  125. package/operations/system/index.d.ts +2 -2
  126. package/operations/system/index.js +19 -7
  127. package/operations/system/index.js.map +1 -1
  128. package/package.json +19 -24
  129. package/plugins/CmsEntryFieldFilterPathPlugin.d.ts +1 -1
  130. package/plugins/CmsEntryFieldFilterPathPlugin.js +8 -3
  131. package/plugins/CmsEntryFieldFilterPathPlugin.js.map +1 -1
  132. package/plugins/CmsEntryFieldFilterPlugin.d.ts +10 -10
  133. package/plugins/CmsEntryFieldFilterPlugin.js +9 -6
  134. package/plugins/CmsEntryFieldFilterPlugin.js.map +1 -1
  135. package/plugins/CmsEntryFieldSortingPlugin.d.ts +2 -2
  136. package/plugins/CmsEntryFieldSortingPlugin.js +4 -5
  137. package/plugins/CmsEntryFieldSortingPlugin.js.map +1 -1
  138. package/plugins/CmsFieldFilterValueTransformPlugin.d.ts +2 -2
  139. package/plugins/CmsFieldFilterValueTransformPlugin.js +3 -4
  140. package/plugins/CmsFieldFilterValueTransformPlugin.js.map +1 -1
  141. package/plugins/index.js +3 -1
  142. package/plugins/index.js.map +1 -1
  143. package/types.d.ts +11 -13
  144. package/types.js +5 -5
  145. package/types.js.map +1 -1
  146. package/definitions/settings.d.ts +0 -9
  147. package/definitions/settings.js +0 -53
  148. package/definitions/settings.js.map +0 -1
  149. package/dynamoDb/storage/date.d.ts +0 -3
  150. package/dynamoDb/storage/date.js +0 -82
  151. package/dynamoDb/storage/date.js.map +0 -1
  152. package/operations/settings/index.d.ts +0 -7
  153. package/operations/settings/index.js +0 -111
  154. package/operations/settings/index.js.map +0 -1
@@ -5,19 +5,17 @@ Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
7
  exports.createEntriesStorageOperations = void 0;
8
- var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
9
8
  var _error = _interopRequireDefault(require("@webiny/error"));
10
9
  var _dataLoaders = require("./dataLoaders");
11
10
  var _types = require("@webiny/api-headless-cms/types");
12
11
  var _keys = require("./keys");
13
- var _batchWrite = require("@webiny/db-dynamodb/utils/batchWrite");
14
- var _query = require("@webiny/db-dynamodb/utils/query");
15
- var _cleanup = require("@webiny/db-dynamodb/utils/cleanup");
12
+ var _dbDynamodb = require("@webiny/db-dynamodb");
16
13
  var _cursor = require("@webiny/utils/cursor");
17
14
  var _zeroPad = require("@webiny/utils/zeroPad");
18
15
  var _apiHeadlessCms = require("@webiny/api-headless-cms");
19
16
  var _createFields = require("./filtering/createFields");
20
17
  var _filtering = require("./filtering");
18
+ var _constants = require("@webiny/api-headless-cms/constants");
21
19
  const createType = () => {
22
20
  return "cms.entry";
23
21
  };
@@ -36,9 +34,10 @@ const convertToStorageEntry = params => {
36
34
  fields: model.fields,
37
35
  values: storageEntry.values
38
36
  });
39
- return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
37
+ return {
38
+ ...storageEntry,
40
39
  values
41
- });
40
+ };
42
41
  };
43
42
  const convertFromStorageEntry = params => {
44
43
  const {
@@ -49,11 +48,12 @@ const convertFromStorageEntry = params => {
49
48
  fields: model.fields,
50
49
  values: storageEntry.values
51
50
  });
52
- return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
51
+ return {
52
+ ...storageEntry,
53
53
  values
54
- });
54
+ };
55
55
  };
56
- const MAX_LIST_LIMIT = 10000;
56
+ const MAX_LIST_LIMIT = 1000000;
57
57
  const createEntriesStorageOperations = params => {
58
58
  const {
59
59
  entity,
@@ -74,11 +74,12 @@ const createEntriesStorageOperations = params => {
74
74
  const dataLoaders = new _dataLoaders.DataLoadersHandler({
75
75
  entity
76
76
  });
77
- const storageTransformPlugins = plugins.byType(_apiHeadlessCms.StorageTransformPlugin.type).reduce((collection, plugin) => {
78
- collection[plugin.fieldType] = plugin;
79
- return collection;
80
- }, {});
81
77
  const createStorageTransformCallable = model => {
78
+ // Cache StorageTransformPlugin to optimize execution.
79
+ const storageTransformPlugins = plugins.byType(_apiHeadlessCms.StorageTransformPlugin.type).reduce((collection, plugin) => {
80
+ collection[plugin.fieldType] = plugin;
81
+ return collection;
82
+ }, {});
82
83
  return (field, value) => {
83
84
  const plugin = storageTransformPlugins[field.type];
84
85
  if (!plugin) {
@@ -117,40 +118,43 @@ const createEntriesStorageOperations = params => {
117
118
  * - create new main entry item
118
119
  * - create new or update the latest entry item
119
120
  */
120
- const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
121
- locked,
122
- PK: partitionKey,
123
- SK: (0, _keys.createRevisionSortKey)(entry),
124
- TYPE: createType(),
125
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
126
- GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
127
- })), entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
128
- locked,
129
- PK: partitionKey,
130
- SK: (0, _keys.createLatestSortKey)(),
131
- TYPE: createLatestType(),
132
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
133
- GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
134
- }))];
121
+ const entityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
122
+ entity,
123
+ put: [{
124
+ ...storageEntry,
125
+ locked,
126
+ PK: partitionKey,
127
+ SK: (0, _keys.createRevisionSortKey)(entry),
128
+ TYPE: createType(),
129
+ GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
130
+ GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
131
+ }, {
132
+ ...storageEntry,
133
+ locked,
134
+ PK: partitionKey,
135
+ SK: (0, _keys.createLatestSortKey)(),
136
+ TYPE: createLatestType(),
137
+ GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
138
+ GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
139
+ }]
140
+ });
135
141
 
136
142
  /**
137
143
  * We need to create published entry if
138
144
  */
139
145
  if (isPublished) {
140
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
146
+ entityBatch.put({
147
+ ...storageEntry,
141
148
  locked,
142
149
  PK: partitionKey,
143
150
  SK: (0, _keys.createPublishedSortKey)(),
144
151
  TYPE: createLatestType(),
145
152
  GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "P"),
146
153
  GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
147
- })));
154
+ });
148
155
  }
149
156
  try {
150
- await (0, _batchWrite.batchWriteAll)({
151
- table: entity.table,
152
- items
153
- });
157
+ await entityBatch.execute();
154
158
  dataLoaders.clearAll({
155
159
  model
156
160
  });
@@ -177,29 +181,63 @@ const createEntriesStorageOperations = params => {
177
181
  storageEntry: initialStorageEntry,
178
182
  model
179
183
  });
184
+
180
185
  /**
181
186
  * We need to:
182
187
  * - create the main entry item
183
- * - update the last entry item to a current one
188
+ * - update the latest entry item to the current one
189
+ * - if the entry's status was set to "published":
190
+ * - update the published entry item to the current one
191
+ * - unpublish previously published revision (if any)
184
192
  */
185
- const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
186
- PK: partitionKey,
187
- SK: (0, _keys.createRevisionSortKey)(storageEntry),
188
- TYPE: createType(),
189
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
190
- GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
191
- })), entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
192
- PK: partitionKey,
193
- SK: (0, _keys.createLatestSortKey)(),
194
- TYPE: createLatestType(),
195
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
196
- GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
197
- }))];
198
- try {
199
- await (0, _batchWrite.batchWriteAll)({
200
- table: entity.table,
201
- items
193
+ const entityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
194
+ entity,
195
+ put: [{
196
+ ...storageEntry,
197
+ PK: partitionKey,
198
+ SK: (0, _keys.createRevisionSortKey)(storageEntry),
199
+ TYPE: createType(),
200
+ GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
201
+ GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
202
+ }, {
203
+ ...storageEntry,
204
+ PK: partitionKey,
205
+ SK: (0, _keys.createLatestSortKey)(),
206
+ TYPE: createLatestType(),
207
+ GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
208
+ GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
209
+ }]
210
+ });
211
+ const isPublished = entry.status === "published";
212
+ if (isPublished) {
213
+ entityBatch.put({
214
+ ...storageEntry,
215
+ PK: partitionKey,
216
+ SK: (0, _keys.createPublishedSortKey)(),
217
+ TYPE: createPublishedType(),
218
+ GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "P"),
219
+ GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
220
+ });
221
+
222
+ // Unpublish previously published revision (if any).
223
+ const [publishedRevisionStorageEntry] = await dataLoaders.getPublishedRevisionByEntryId({
224
+ model,
225
+ ids: [entry.id]
202
226
  });
227
+ if (publishedRevisionStorageEntry) {
228
+ entityBatch.put({
229
+ ...publishedRevisionStorageEntry,
230
+ PK: partitionKey,
231
+ SK: (0, _keys.createRevisionSortKey)(publishedRevisionStorageEntry),
232
+ TYPE: createType(),
233
+ status: _types.CONTENT_ENTRY_STATUS.UNPUBLISHED,
234
+ GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
235
+ GSI1_SK: (0, _keys.createGSISortKey)(publishedRevisionStorageEntry)
236
+ });
237
+ }
238
+ }
239
+ try {
240
+ await entityBatch.execute();
203
241
  dataLoaders.clearAll({
204
242
  model
205
243
  });
@@ -228,7 +266,6 @@ const createEntriesStorageOperations = params => {
228
266
  });
229
267
  const isPublished = entry.status === "published";
230
268
  const locked = isPublished ? true : entry.locked;
231
- const items = [];
232
269
  const storageEntry = convertToStorageEntry({
233
270
  model,
234
271
  storageEntry: initialStorageEntry
@@ -238,44 +275,81 @@ const createEntriesStorageOperations = params => {
238
275
  * - update the current entry
239
276
  * - update the latest entry if the current entry is the latest one
240
277
  */
241
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
242
- locked,
243
- PK: partitionKey,
244
- SK: (0, _keys.createRevisionSortKey)(storageEntry),
245
- TYPE: createType(),
246
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
247
- GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
248
- })));
278
+
279
+ const entityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
280
+ entity,
281
+ put: [{
282
+ ...storageEntry,
283
+ locked,
284
+ PK: partitionKey,
285
+ SK: (0, _keys.createRevisionSortKey)(storageEntry),
286
+ TYPE: createType(),
287
+ GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
288
+ GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
289
+ }]
290
+ });
249
291
  if (isPublished) {
250
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
292
+ entityBatch.put({
293
+ ...storageEntry,
251
294
  locked,
252
295
  PK: partitionKey,
253
296
  SK: (0, _keys.createPublishedSortKey)(),
254
297
  TYPE: createPublishedType(),
255
298
  GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "P"),
256
299
  GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
257
- })));
300
+ });
258
301
  }
259
302
 
260
303
  /**
261
304
  * We need the latest entry to update it as well if necessary.
262
305
  */
263
306
  const latestStorageEntry = await getLatestRevisionByEntryId(model, entry);
264
- if (latestStorageEntry && latestStorageEntry.id === entry.id) {
265
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
266
- locked,
267
- PK: partitionKey,
268
- SK: (0, _keys.createLatestSortKey)(),
269
- TYPE: createLatestType(),
270
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
271
- GSI1_SK: (0, _keys.createGSISortKey)(entry)
272
- })));
307
+ if (latestStorageEntry) {
308
+ const updatingLatestRevision = latestStorageEntry.id === entry.id;
309
+ if (updatingLatestRevision) {
310
+ entityBatch.put({
311
+ ...storageEntry,
312
+ locked,
313
+ PK: partitionKey,
314
+ SK: (0, _keys.createLatestSortKey)(),
315
+ TYPE: createLatestType(),
316
+ GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
317
+ GSI1_SK: (0, _keys.createGSISortKey)(entry)
318
+ });
319
+ } else {
320
+ /**
321
+ * If not updating latest revision, we still want to update the latest revision's
322
+ * entry-level meta fields to match the current revision's entry-level meta fields.
323
+ */
324
+ const updatedEntryLevelMetaFields = (0, _constants.pickEntryMetaFields)(entry, _constants.isEntryLevelEntryMetaField);
325
+
326
+ /**
327
+ * First we update the regular DynamoDB table. Two updates are needed:
328
+ * - one for the actual revision record
329
+ * - one for the latest record
330
+ */
331
+ entityBatch.put({
332
+ ...latestStorageEntry,
333
+ ...updatedEntryLevelMetaFields,
334
+ PK: partitionKey,
335
+ SK: (0, _keys.createRevisionSortKey)(latestStorageEntry),
336
+ TYPE: createType(),
337
+ GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
338
+ GSI1_SK: (0, _keys.createGSISortKey)(latestStorageEntry)
339
+ });
340
+ entityBatch.put({
341
+ ...latestStorageEntry,
342
+ ...updatedEntryLevelMetaFields,
343
+ PK: partitionKey,
344
+ SK: (0, _keys.createLatestSortKey)(),
345
+ TYPE: createLatestType(),
346
+ GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
347
+ GSI1_SK: (0, _keys.createGSISortKey)(latestStorageEntry)
348
+ });
349
+ }
273
350
  }
274
351
  try {
275
- await (0, _batchWrite.batchWriteAll)({
276
- table: entity.table,
277
- items
278
- });
352
+ await entityBatch.execute();
279
353
  dataLoaders.clearAll({
280
354
  model
281
355
  });
@@ -288,6 +362,130 @@ const createEntriesStorageOperations = params => {
288
362
  });
289
363
  }
290
364
  };
365
+ const move = async (initialModel, id, folderId) => {
366
+ /**
367
+ * We need to:
368
+ * - load all the revisions of the entry, including published and latest
369
+ * - update all the revisions (published and latest ) of the entry with new folderId
370
+ */
371
+ const model = getStorageOperationsModel(initialModel);
372
+ /**
373
+ * First we need to load all the revisions and published / latest entry.
374
+ */
375
+ const queryAllParams = {
376
+ entity,
377
+ partitionKey: (0, _keys.createPartitionKey)({
378
+ id,
379
+ locale: model.locale,
380
+ tenant: model.tenant
381
+ }),
382
+ options: {
383
+ gte: " "
384
+ }
385
+ };
386
+ const records = await (0, _dbDynamodb.queryAll)(queryAllParams);
387
+ /**
388
+ * Then create the batch writes for the DynamoDB, with the updated folderId.
389
+ */
390
+ const entityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
391
+ entity,
392
+ put: records.map(item => {
393
+ return {
394
+ ...item,
395
+ location: {
396
+ ...item.location,
397
+ folderId
398
+ }
399
+ };
400
+ })
401
+ });
402
+
403
+ /**
404
+ * And finally write it...
405
+ */
406
+ try {
407
+ await entityBatch.execute();
408
+ } catch (ex) {
409
+ throw _error.default.from(ex, {
410
+ message: "Could not move records to a new folder.",
411
+ data: {
412
+ id,
413
+ folderId
414
+ }
415
+ });
416
+ }
417
+ };
418
+ const moveToBin = async (initialModel, params) => {
419
+ const {
420
+ entry,
421
+ storageEntry: initialStorageEntry
422
+ } = params;
423
+ const model = getStorageOperationsModel(initialModel);
424
+
425
+ /**
426
+ * First we need to load all the revisions and published / latest entries.
427
+ */
428
+ const queryAllParams = {
429
+ entity,
430
+ partitionKey: (0, _keys.createPartitionKey)({
431
+ id: entry.id,
432
+ locale: model.locale,
433
+ tenant: model.tenant
434
+ }),
435
+ options: {
436
+ gte: " "
437
+ }
438
+ };
439
+ let records = [];
440
+ try {
441
+ records = await (0, _dbDynamodb.queryAll)(queryAllParams);
442
+ } catch (ex) {
443
+ throw new _error.default(ex.message || "Could not load all records.", ex.code || "LOAD_ALL_RECORDS_ERROR", {
444
+ error: ex,
445
+ id: entry.id
446
+ });
447
+ }
448
+ if (records.length === 0) {
449
+ return;
450
+ }
451
+ const storageEntry = convertToStorageEntry({
452
+ model,
453
+ storageEntry: initialStorageEntry
454
+ });
455
+
456
+ /**
457
+ * Let's pick the `deleted` meta fields from the storage entry.
458
+ */
459
+ const updatedDeletedMetaFields = (0, _constants.pickEntryMetaFields)(storageEntry, _constants.isDeletedEntryMetaField);
460
+
461
+ /**
462
+ * Then create the batch writes for the DynamoDB, with the updated data.
463
+ */
464
+ const entityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
465
+ entity,
466
+ put: records.map(record => {
467
+ return {
468
+ ...record,
469
+ ...updatedDeletedMetaFields,
470
+ wbyDeleted: storageEntry.wbyDeleted,
471
+ location: storageEntry.location,
472
+ binOriginalFolderId: storageEntry.binOriginalFolderId
473
+ };
474
+ })
475
+ });
476
+ /**
477
+ * And finally write it...
478
+ */
479
+ try {
480
+ await entityBatch.execute();
481
+ } catch (ex) {
482
+ throw new _error.default(ex.message || "Could not move the entry to the bin.", ex.code || "MOVE_ENTRY_TO_BIN_ERROR", {
483
+ error: ex,
484
+ entry,
485
+ storageEntry
486
+ });
487
+ }
488
+ };
291
489
  const deleteEntry = async (initialModel, params) => {
292
490
  const {
293
491
  entry
@@ -307,24 +505,24 @@ const createEntriesStorageOperations = params => {
307
505
  };
308
506
  let records = [];
309
507
  try {
310
- records = await (0, _query.queryAll)(queryAllParams);
508
+ records = await (0, _dbDynamodb.queryAll)(queryAllParams);
311
509
  } catch (ex) {
312
510
  throw new _error.default(ex.message || "Could not load all records.", ex.code || "LOAD_ALL_RECORDS_ERROR", {
313
511
  error: ex,
314
512
  id
315
513
  });
316
514
  }
317
- const items = records.map(item => {
318
- return entity.deleteBatch({
319
- PK: item.PK,
320
- SK: item.SK
321
- });
515
+ const entityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
516
+ entity,
517
+ delete: records.map(item => {
518
+ return {
519
+ PK: item.PK,
520
+ SK: item.SK
521
+ };
522
+ })
322
523
  });
323
524
  try {
324
- await (0, _batchWrite.batchWriteAll)({
325
- table: entity.table,
326
- items
327
- });
525
+ await entityBatch.execute();
328
526
  dataLoaders.clearAll({
329
527
  model
330
528
  });
@@ -336,6 +534,78 @@ const createEntriesStorageOperations = params => {
336
534
  });
337
535
  }
338
536
  };
537
+ const restoreFromBin = async (initialModel, params) => {
538
+ const {
539
+ entry,
540
+ storageEntry: initialStorageEntry
541
+ } = params;
542
+ const model = getStorageOperationsModel(initialModel);
543
+
544
+ /**
545
+ * First we need to load all the revisions and published / latest entries.
546
+ */
547
+ const queryAllParams = {
548
+ entity,
549
+ partitionKey: (0, _keys.createPartitionKey)({
550
+ id: entry.id,
551
+ locale: model.locale,
552
+ tenant: model.tenant
553
+ }),
554
+ options: {
555
+ gte: " "
556
+ }
557
+ };
558
+ let records = [];
559
+ try {
560
+ records = await (0, _dbDynamodb.queryAll)(queryAllParams);
561
+ } catch (ex) {
562
+ throw new _error.default(ex.message || "Could not load all records.", ex.code || "LOAD_ALL_RECORDS_ERROR", {
563
+ error: ex,
564
+ id: entry.id
565
+ });
566
+ }
567
+ if (records.length === 0) {
568
+ return initialStorageEntry;
569
+ }
570
+ const storageEntry = convertToStorageEntry({
571
+ model,
572
+ storageEntry: initialStorageEntry
573
+ });
574
+
575
+ /**
576
+ * Let's pick the `restored` meta fields from the storage entry.
577
+ */
578
+ const updatedRestoredMetaFields = (0, _constants.pickEntryMetaFields)(storageEntry, _constants.isRestoredEntryMetaField);
579
+ const entityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
580
+ entity,
581
+ put: records.map(record => {
582
+ return {
583
+ ...record,
584
+ ...updatedRestoredMetaFields,
585
+ wbyDeleted: storageEntry.wbyDeleted,
586
+ location: storageEntry.location,
587
+ binOriginalFolderId: storageEntry.binOriginalFolderId
588
+ };
589
+ })
590
+ });
591
+
592
+ /**
593
+ * And finally write it...
594
+ */
595
+ try {
596
+ await entityBatch.execute();
597
+ dataLoaders.clearAll({
598
+ model
599
+ });
600
+ return initialStorageEntry;
601
+ } catch (ex) {
602
+ throw new _error.default(ex.message || "Could not restore the entry from the bin.", ex.code || "RESTORE_ENTRY_ERROR", {
603
+ error: ex,
604
+ entry,
605
+ storageEntry
606
+ });
607
+ }
608
+ };
339
609
  const deleteRevision = async (initialModel, params) => {
340
610
  const {
341
611
  entry,
@@ -348,39 +618,51 @@ const createEntriesStorageOperations = params => {
348
618
  locale: model.locale,
349
619
  tenant: model.tenant
350
620
  });
351
- const items = [entity.deleteBatch({
352
- PK: partitionKey,
353
- SK: (0, _keys.createRevisionSortKey)(entry)
354
- })];
621
+ const entityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
622
+ entity,
623
+ delete: [{
624
+ PK: partitionKey,
625
+ SK: (0, _keys.createRevisionSortKey)(entry)
626
+ }]
627
+ });
355
628
  const publishedStorageEntry = await getPublishedRevisionByEntryId(model, entry);
356
629
 
357
630
  /**
358
631
  * If revision we are deleting is the published one as well, we need to delete those records as well.
359
632
  */
360
633
  if (publishedStorageEntry && entry.id === publishedStorageEntry.id) {
361
- items.push(entity.deleteBatch({
634
+ entityBatch.delete({
362
635
  PK: partitionKey,
363
636
  SK: (0, _keys.createPublishedSortKey)()
364
- }));
637
+ });
365
638
  }
366
639
  if (initialLatestStorageEntry) {
367
640
  const latestStorageEntry = convertToStorageEntry({
368
641
  storageEntry: initialLatestStorageEntry,
369
642
  model
370
643
  });
371
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, latestStorageEntry), {}, {
644
+ entityBatch.put({
645
+ ...latestStorageEntry,
372
646
  PK: partitionKey,
373
647
  SK: (0, _keys.createLatestSortKey)(),
374
648
  TYPE: createLatestType(),
375
649
  GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
376
650
  GSI1_SK: (0, _keys.createGSISortKey)(latestStorageEntry)
377
- })));
651
+ });
652
+
653
+ // Do an update on the latest revision. We need to update the latest revision's
654
+ // entry-level meta fields to match the previous revision's entry-level meta fields.
655
+ entityBatch.put({
656
+ ...latestStorageEntry,
657
+ PK: partitionKey,
658
+ SK: (0, _keys.createRevisionSortKey)(initialLatestStorageEntry),
659
+ TYPE: createType(),
660
+ GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
661
+ GSI1_SK: (0, _keys.createGSISortKey)(initialLatestStorageEntry)
662
+ });
378
663
  }
379
664
  try {
380
- await (0, _batchWrite.batchWriteAll)({
381
- table: entity.table,
382
- items
383
- });
665
+ entityBatch.execute();
384
666
  dataLoaders.clearAll({
385
667
  model
386
668
  });
@@ -407,36 +689,30 @@ const createEntriesStorageOperations = params => {
407
689
  /**
408
690
  * Then we need to construct the queries for all the revisions and entries.
409
691
  */
410
- const items = [];
692
+
693
+ const entityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
694
+ entity
695
+ });
411
696
  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
- }),
697
+ const partitionKey = (0, _keys.createPartitionKey)({
698
+ id,
699
+ locale: model.locale,
700
+ tenant: model.tenant
701
+ });
702
+ entityBatch.delete({
703
+ PK: partitionKey,
421
704
  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
- }),
705
+ });
706
+ entityBatch.delete({
707
+ PK: partitionKey,
432
708
  SK: "P"
433
- }));
709
+ });
434
710
  }
435
711
  /**
436
712
  * Exact revisions of all the entries
437
713
  */
438
714
  for (const revision of revisions) {
439
- items.push(entity.deleteBatch({
715
+ entityBatch.delete({
440
716
  PK: (0, _keys.createPartitionKey)({
441
717
  id: revision.id,
442
718
  locale: model.locale,
@@ -445,12 +721,9 @@ const createEntriesStorageOperations = params => {
445
721
  SK: (0, _keys.createRevisionSortKey)({
446
722
  version: revision.version
447
723
  })
448
- }));
724
+ });
449
725
  }
450
- await (0, _batchWrite.batchWriteAll)({
451
- table: entity.table,
452
- items
453
- });
726
+ await entityBatch.execute();
454
727
  };
455
728
  const getLatestRevisionByEntryId = async (initialModel, params) => {
456
729
  const model = getStorageOperationsModel(initialModel);
@@ -578,8 +851,8 @@ const createEntriesStorageOperations = params => {
578
851
  }
579
852
  };
580
853
  try {
581
- const result = await (0, _query.queryOne)(queryParams);
582
- const storageEntry = (0, _cleanup.cleanupItem)(entity, result);
854
+ const result = await (0, _dbDynamodb.queryOne)(queryParams);
855
+ const storageEntry = (0, _dbDynamodb.cleanupItem)(entity, result);
583
856
  if (!storageEntry) {
584
857
  return null;
585
858
  }
@@ -588,12 +861,13 @@ const createEntriesStorageOperations = params => {
588
861
  model
589
862
  });
590
863
  } catch (ex) {
591
- throw new _error.default(ex.message || "Could not get previous version of given entry.", ex.code || "GET_PREVIOUS_VERSION_ERROR", (0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), {}, {
864
+ throw new _error.default(ex.message || "Could not get previous version of given entry.", ex.code || "GET_PREVIOUS_VERSION_ERROR", {
865
+ ...params,
592
866
  error: ex,
593
867
  partitionKey: queryParams.partitionKey,
594
868
  options: queryParams.options,
595
869
  model
596
- }));
870
+ });
597
871
  }
598
872
  };
599
873
  const list = async (initialModel, params) => {
@@ -618,7 +892,7 @@ const createEntriesStorageOperations = params => {
618
892
  };
619
893
  let storageEntries = [];
620
894
  try {
621
- storageEntries = await (0, _query.queryAll)(queryAllParams);
895
+ storageEntries = await (0, _dbDynamodb.queryAll)(queryAllParams);
622
896
  } catch (ex) {
623
897
  throw new _error.default(ex.message, "QUERY_ENTRIES_ERROR", {
624
898
  error: ex,
@@ -634,7 +908,9 @@ const createEntriesStorageOperations = params => {
634
908
  items: []
635
909
  };
636
910
  }
637
- const where = (0, _objectSpread2.default)({}, initialWhere);
911
+ const where = {
912
+ ...initialWhere
913
+ };
638
914
  delete where["published"];
639
915
  delete where["latest"];
640
916
  /**
@@ -696,21 +972,22 @@ const createEntriesStorageOperations = params => {
696
972
  * Although we do not need a cursor here, we will use it as such to keep it standardized.
697
973
  * Number is simply encoded.
698
974
  */
699
- const cursor = totalCount > start + limit ? (0, _cursor.encodeCursor)(`${start + limit}`) : null;
975
+ const cursor = (0, _cursor.encodeCursor)(`${start + limit}`);
700
976
  return {
701
977
  hasMoreItems,
702
978
  totalCount,
703
979
  cursor,
704
- items: (0, _cleanup.cleanupItems)(entity, slicedItems)
980
+ items: (0, _dbDynamodb.cleanupItems)(entity, slicedItems)
705
981
  };
706
982
  };
707
983
  const get = async (initialModel, params) => {
708
984
  const model = getStorageOperationsModel(initialModel);
709
985
  const {
710
986
  items
711
- } = await list(model, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), {}, {
987
+ } = await list(model, {
988
+ ...params,
712
989
  limit: 1
713
- }));
990
+ });
714
991
  return items.shift() || null;
715
992
  };
716
993
  const publish = async (initialModel, params) => {
@@ -729,59 +1006,129 @@ const createEntriesStorageOperations = params => {
729
1006
  * We need the latest and published entries to see if something needs to be updated alongside the publishing one.
730
1007
  */
731
1008
  const initialLatestStorageEntry = await getLatestRevisionByEntryId(model, entry);
1009
+ if (!initialLatestStorageEntry) {
1010
+ throw new _error.default(`Could not publish entry. Could not load latest ("L") record.`, "PUBLISH_ERROR", {
1011
+ entry
1012
+ });
1013
+ }
732
1014
  const initialPublishedStorageEntry = await getPublishedRevisionByEntryId(model, entry);
733
1015
  const storageEntry = convertToStorageEntry({
734
1016
  model,
735
1017
  storageEntry: initialStorageEntry
736
1018
  });
737
- /**
738
- * We need to update:
739
- * - current entry revision sort key
740
- * - published sort key
741
- * - the latest sort key - if entry updated is actually latest
742
- * - previous published entry to unpublished status - if any previously published entry
743
- */
744
- const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
745
- PK: partitionKey,
746
- SK: (0, _keys.createRevisionSortKey)(entry),
747
- TYPE: createType(),
748
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
749
- GSI1_SK: (0, _keys.createGSISortKey)(entry)
750
- })), entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
751
- PK: partitionKey,
752
- SK: (0, _keys.createPublishedSortKey)(),
753
- TYPE: createPublishedType(),
754
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "P"),
755
- GSI1_SK: (0, _keys.createGSISortKey)(entry)
756
- }))];
757
- if (initialLatestStorageEntry && entry.id === initialLatestStorageEntry.id) {
758
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
1019
+
1020
+ // 1. Update REV# and P records with new data.
1021
+ const entityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
1022
+ entity,
1023
+ put: [{
1024
+ ...storageEntry,
1025
+ PK: partitionKey,
1026
+ SK: (0, _keys.createRevisionSortKey)(entry),
1027
+ TYPE: createType(),
1028
+ GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
1029
+ GSI1_SK: (0, _keys.createGSISortKey)(entry)
1030
+ }, {
1031
+ ...storageEntry,
1032
+ PK: partitionKey,
1033
+ SK: (0, _keys.createPublishedSortKey)(),
1034
+ TYPE: createPublishedType(),
1035
+ GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "P"),
1036
+ GSI1_SK: (0, _keys.createGSISortKey)(entry)
1037
+ }]
1038
+ });
1039
+
1040
+ // 2. When it comes to the latest record, we need to perform a couple of different
1041
+ // updates, based on whether the entry being published is the latest revision or not.
1042
+ const publishedRevisionId = initialPublishedStorageEntry?.id;
1043
+ const publishingLatestRevision = entry.id === initialLatestStorageEntry.id;
1044
+ if (publishingLatestRevision) {
1045
+ // 2.1 If we're publishing the latest revision, we first need to update the L record.
1046
+ entityBatch.put({
1047
+ ...storageEntry,
759
1048
  PK: partitionKey,
760
1049
  SK: (0, _keys.createLatestSortKey)(),
761
1050
  TYPE: createLatestType(),
762
1051
  GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
763
1052
  GSI1_SK: (0, _keys.createGSISortKey)(entry)
764
- })));
765
- }
766
- if (initialPublishedStorageEntry && initialPublishedStorageEntry.id !== entry.id) {
767
- const publishedStorageEntry = convertToStorageEntry({
768
- storageEntry: initialPublishedStorageEntry,
1053
+ });
1054
+
1055
+ // 2.2 Additionally, if we have a previously published entry, we need to mark it as unpublished.
1056
+ if (publishedRevisionId && publishedRevisionId !== entry.id) {
1057
+ const publishedStorageEntry = convertToStorageEntry({
1058
+ storageEntry: initialPublishedStorageEntry,
1059
+ model
1060
+ });
1061
+ entityBatch.put({
1062
+ ...publishedStorageEntry,
1063
+ PK: partitionKey,
1064
+ SK: (0, _keys.createRevisionSortKey)(publishedStorageEntry),
1065
+ TYPE: createType(),
1066
+ status: _types.CONTENT_ENTRY_STATUS.UNPUBLISHED,
1067
+ GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
1068
+ GSI1_SK: (0, _keys.createGSISortKey)(publishedStorageEntry)
1069
+ });
1070
+ }
1071
+ } else {
1072
+ // 2.3 If the published revision is not the latest one, the situation is a bit
1073
+ // more complex. We first need to update the L and REV# records with the new
1074
+ // values of *only entry-level* meta fields.
1075
+ const updatedEntryLevelMetaFields = (0, _constants.pickEntryMetaFields)(entry, _constants.isEntryLevelEntryMetaField);
1076
+ const latestStorageEntry = convertToStorageEntry({
1077
+ storageEntry: initialLatestStorageEntry,
769
1078
  model
770
1079
  });
771
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, publishedStorageEntry), {}, {
1080
+
1081
+ // 2.3.1 Update L record. Apart from updating the entry-level meta fields, we also need
1082
+ // to change the status from "published" to "unpublished" (if the status is set to "published").
1083
+ let latestRevisionStatus = latestStorageEntry.status;
1084
+ if (latestRevisionStatus === _types.CONTENT_ENTRY_STATUS.PUBLISHED) {
1085
+ latestRevisionStatus = _types.CONTENT_ENTRY_STATUS.UNPUBLISHED;
1086
+ }
1087
+ const latestStorageEntryFields = {
1088
+ ...latestStorageEntry,
1089
+ ...updatedEntryLevelMetaFields,
1090
+ status: latestRevisionStatus
1091
+ };
1092
+ entityBatch.put({
1093
+ ...latestStorageEntryFields,
772
1094
  PK: partitionKey,
773
- SK: (0, _keys.createRevisionSortKey)(publishedStorageEntry),
1095
+ SK: (0, _keys.createLatestSortKey)(),
1096
+ TYPE: createLatestType(),
1097
+ GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
1098
+ GSI1_SK: (0, _keys.createGSISortKey)(latestStorageEntry)
1099
+ });
1100
+
1101
+ // 2.3.2 Update REV# record.
1102
+ entityBatch.put({
1103
+ ...latestStorageEntryFields,
1104
+ PK: partitionKey,
1105
+ SK: (0, _keys.createRevisionSortKey)(latestStorageEntry),
774
1106
  TYPE: createType(),
775
- status: _types.CONTENT_ENTRY_STATUS.UNPUBLISHED,
776
1107
  GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
777
- GSI1_SK: (0, _keys.createGSISortKey)(publishedStorageEntry)
778
- })));
1108
+ GSI1_SK: (0, _keys.createGSISortKey)(latestStorageEntry)
1109
+ });
1110
+
1111
+ // 2.3.3 Finally, if we got a published entry, but it wasn't the latest one, we need to take
1112
+ // an extra step and mark it as unpublished.
1113
+ const publishedRevisionDifferentFromLatest = publishedRevisionId && publishedRevisionId !== latestStorageEntry.id;
1114
+ if (publishedRevisionDifferentFromLatest) {
1115
+ const publishedStorageEntry = convertToStorageEntry({
1116
+ storageEntry: initialPublishedStorageEntry,
1117
+ model
1118
+ });
1119
+ entityBatch.put({
1120
+ ...publishedStorageEntry,
1121
+ PK: partitionKey,
1122
+ SK: (0, _keys.createRevisionSortKey)(publishedStorageEntry),
1123
+ TYPE: createType(),
1124
+ status: _types.CONTENT_ENTRY_STATUS.UNPUBLISHED,
1125
+ GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
1126
+ GSI1_SK: (0, _keys.createGSISortKey)(publishedStorageEntry)
1127
+ });
1128
+ }
779
1129
  }
780
1130
  try {
781
- await (0, _batchWrite.batchWriteAll)({
782
- table: entity.table,
783
- items
784
- });
1131
+ await entityBatch.execute();
785
1132
  dataLoaders.clearAll({
786
1133
  model
787
1134
  });
@@ -815,35 +1162,72 @@ const createEntriesStorageOperations = params => {
815
1162
  * - update current entry revision with new data
816
1163
  * - update the latest entry status - if entry being unpublished is latest
817
1164
  */
818
- const items = [entity.deleteBatch({
819
- PK: partitionKey,
820
- SK: (0, _keys.createPublishedSortKey)()
821
- }), entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
822
- PK: partitionKey,
823
- SK: (0, _keys.createRevisionSortKey)(entry),
824
- TYPE: createType(),
825
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
826
- GSI1_SK: (0, _keys.createGSISortKey)(entry)
827
- }))];
1165
+ const entityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
1166
+ entity,
1167
+ delete: [{
1168
+ PK: partitionKey,
1169
+ SK: (0, _keys.createPublishedSortKey)()
1170
+ }],
1171
+ put: [{
1172
+ ...storageEntry,
1173
+ PK: partitionKey,
1174
+ SK: (0, _keys.createRevisionSortKey)(entry),
1175
+ TYPE: createType(),
1176
+ GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
1177
+ GSI1_SK: (0, _keys.createGSISortKey)(entry)
1178
+ }]
1179
+ });
828
1180
 
829
1181
  /**
830
1182
  * We need the latest entry to see if something needs to be updated alongside the unpublishing one.
831
1183
  */
832
- const latestStorageEntry = await getLatestRevisionByEntryId(model, entry);
833
- if (latestStorageEntry && entry.id === latestStorageEntry.id) {
834
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
835
- PK: partitionKey,
836
- SK: (0, _keys.createLatestSortKey)(),
837
- TYPE: createLatestType(),
838
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
839
- GSI1_SK: (0, _keys.createGSISortKey)(entry)
840
- })));
1184
+ const initialLatestStorageEntry = await getLatestRevisionByEntryId(model, entry);
1185
+ if (initialLatestStorageEntry) {
1186
+ const unpublishingLatestRevision = entry.id === initialLatestStorageEntry.id;
1187
+ if (unpublishingLatestRevision) {
1188
+ entityBatch.put({
1189
+ ...storageEntry,
1190
+ PK: partitionKey,
1191
+ SK: (0, _keys.createLatestSortKey)(),
1192
+ TYPE: createLatestType(),
1193
+ GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
1194
+ GSI1_SK: (0, _keys.createGSISortKey)(entry)
1195
+ });
1196
+ } else {
1197
+ const latestStorageEntry = convertToStorageEntry({
1198
+ storageEntry: initialLatestStorageEntry,
1199
+ model
1200
+ });
1201
+
1202
+ // If the unpublished revision is not the latest one, we still need to
1203
+ // update the latest record with the new values of entry-level meta fields.
1204
+ const updatedEntryLevelMetaFields = (0, _constants.pickEntryMetaFields)(entry, _constants.isEntryLevelEntryMetaField);
1205
+
1206
+ // 1. Update actual revision record.
1207
+ entityBatch.put({
1208
+ ...latestStorageEntry,
1209
+ ...updatedEntryLevelMetaFields,
1210
+ PK: partitionKey,
1211
+ SK: (0, _keys.createRevisionSortKey)(latestStorageEntry),
1212
+ TYPE: createType(),
1213
+ GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
1214
+ GSI1_SK: (0, _keys.createGSISortKey)(latestStorageEntry)
1215
+ });
1216
+
1217
+ // 2. Update latest record.
1218
+ entityBatch.put({
1219
+ ...latestStorageEntry,
1220
+ ...updatedEntryLevelMetaFields,
1221
+ PK: partitionKey,
1222
+ SK: (0, _keys.createLatestSortKey)(),
1223
+ TYPE: createLatestType(),
1224
+ GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
1225
+ GSI1_SK: (0, _keys.createGSISortKey)(latestStorageEntry)
1226
+ });
1227
+ }
841
1228
  }
842
1229
  try {
843
- await (0, _batchWrite.batchWriteAll)({
844
- table: entity.table,
845
- items
846
- });
1230
+ await entityBatch.execute();
847
1231
  dataLoaders.clearAll({
848
1232
  model
849
1233
  });
@@ -883,10 +1267,9 @@ const createEntriesStorageOperations = params => {
883
1267
  continue;
884
1268
  }
885
1269
  for (const value of values) {
886
- var _result$value;
887
1270
  result[value] = {
888
1271
  value,
889
- count: (((_result$value = result[value]) === null || _result$value === void 0 ? void 0 : _result$value.count) || 0) + 1
1272
+ count: (result[value]?.count || 0) + 1
890
1273
  };
891
1274
  }
892
1275
  }
@@ -896,7 +1279,10 @@ const createEntriesStorageOperations = params => {
896
1279
  create,
897
1280
  createRevisionFrom,
898
1281
  update,
1282
+ move,
899
1283
  delete: deleteEntry,
1284
+ moveToBin,
1285
+ restoreFromBin,
900
1286
  deleteRevision,
901
1287
  deleteMultipleEntries,
902
1288
  getPreviousRevision,
@@ -915,4 +1301,6 @@ const createEntriesStorageOperations = params => {
915
1301
  getUniqueFieldValues
916
1302
  };
917
1303
  };
918
- exports.createEntriesStorageOperations = createEntriesStorageOperations;
1304
+ exports.createEntriesStorageOperations = createEntriesStorageOperations;
1305
+
1306
+ //# sourceMappingURL=index.js.map