@webiny/api-headless-cms-ddb 0.0.0-unstable.b14eaecf38 → 0.0.0-unstable.b9b64acb6d

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 (169) hide show
  1. package/README.md +6 -20
  2. package/definitions/entry.d.ts +4 -5
  3. package/definitions/entry.js +7 -88
  4. package/definitions/entry.js.map +1 -1
  5. package/definitions/group.d.ts +4 -5
  6. package/definitions/group.js +7 -59
  7. package/definitions/group.js.map +1 -1
  8. package/definitions/model.d.ts +4 -5
  9. package/definitions/model.js +7 -103
  10. package/definitions/model.js.map +1 -1
  11. package/definitions/table.d.ts +4 -6
  12. package/definitions/table.js +8 -26
  13. package/definitions/table.js.map +1 -1
  14. package/definitions/types.d.ts +60 -0
  15. package/definitions/types.js +3 -0
  16. package/definitions/types.js.map +1 -0
  17. package/dynamoDb/index.d.ts +1 -1
  18. package/dynamoDb/index.js +7 -12
  19. package/dynamoDb/index.js.map +1 -1
  20. package/dynamoDb/path/locationFolderId.d.ts +2 -0
  21. package/dynamoDb/path/locationFolderId.js +34 -0
  22. package/dynamoDb/path/locationFolderId.js.map +1 -0
  23. package/dynamoDb/path/plainObject.d.ts +1 -1
  24. package/dynamoDb/path/plainObject.js +7 -14
  25. package/dynamoDb/path/plainObject.js.map +1 -1
  26. package/dynamoDb/storage/longText.js +13 -18
  27. package/dynamoDb/storage/longText.js.map +1 -1
  28. package/dynamoDb/storage/richText.js +28 -71
  29. package/dynamoDb/storage/richText.js.map +1 -1
  30. package/dynamoDb/transformValue/datetime.d.ts +1 -1
  31. package/dynamoDb/transformValue/datetime.js +7 -12
  32. package/dynamoDb/transformValue/datetime.js.map +1 -1
  33. package/index.d.ts +2 -2
  34. package/index.js +45 -85
  35. package/index.js.map +1 -1
  36. package/operations/entry/dataLoader/DataLoaderCache.d.ts +15 -0
  37. package/operations/entry/dataLoader/DataLoaderCache.js +34 -0
  38. package/operations/entry/dataLoader/DataLoaderCache.js.map +1 -0
  39. package/operations/entry/dataLoader/constants.d.ts +1 -0
  40. package/operations/entry/dataLoader/constants.js +4 -0
  41. package/operations/entry/dataLoader/constants.js.map +1 -0
  42. package/operations/entry/dataLoader/createBatchScheduleFn.d.ts +8 -0
  43. package/operations/entry/dataLoader/createBatchScheduleFn.js +19 -0
  44. package/operations/entry/dataLoader/createBatchScheduleFn.js.map +1 -0
  45. package/operations/entry/dataLoader/getAllEntryRevisions.d.ts +4 -0
  46. package/operations/entry/dataLoader/getAllEntryRevisions.js +32 -0
  47. package/operations/entry/dataLoader/getAllEntryRevisions.js.map +1 -0
  48. package/operations/entry/dataLoader/getLatestRevisionByEntryId.d.ts +4 -0
  49. package/operations/entry/dataLoader/getLatestRevisionByEntryId.js +46 -0
  50. package/operations/entry/dataLoader/getLatestRevisionByEntryId.js.map +1 -0
  51. package/operations/entry/dataLoader/getPublishedRevisionByEntryId.d.ts +4 -0
  52. package/operations/entry/dataLoader/getPublishedRevisionByEntryId.js +46 -0
  53. package/operations/entry/dataLoader/getPublishedRevisionByEntryId.js.map +1 -0
  54. package/operations/entry/dataLoader/getRevisionById.d.ts +4 -0
  55. package/operations/entry/dataLoader/getRevisionById.js +52 -0
  56. package/operations/entry/dataLoader/getRevisionById.js.map +1 -0
  57. package/operations/entry/dataLoader/index.d.ts +8 -0
  58. package/operations/entry/dataLoader/index.js +19 -0
  59. package/operations/entry/dataLoader/index.js.map +1 -0
  60. package/operations/entry/dataLoader/types.d.ts +5 -0
  61. package/operations/entry/dataLoader/types.js +3 -0
  62. package/operations/entry/dataLoader/types.js.map +1 -0
  63. package/operations/entry/dataLoaders.d.ts +14 -30
  64. package/operations/entry/dataLoaders.js +56 -228
  65. package/operations/entry/dataLoaders.js.map +1 -1
  66. package/operations/entry/filtering/createExpressions.d.ts +5 -5
  67. package/operations/entry/filtering/createExpressions.js +29 -30
  68. package/operations/entry/filtering/createExpressions.js.map +1 -1
  69. package/operations/entry/filtering/createFields.d.ts +6 -4
  70. package/operations/entry/filtering/createFields.js +87 -86
  71. package/operations/entry/filtering/createFields.js.map +1 -1
  72. package/operations/entry/filtering/extractSort.d.ts +6 -6
  73. package/operations/entry/filtering/extractSort.js +40 -23
  74. package/operations/entry/filtering/extractSort.js.map +1 -1
  75. package/operations/entry/filtering/filter.d.ts +6 -6
  76. package/operations/entry/filtering/filter.js +36 -29
  77. package/operations/entry/filtering/filter.js.map +1 -1
  78. package/operations/entry/filtering/fullTextSearch.d.ts +3 -3
  79. package/operations/entry/filtering/fullTextSearch.js +7 -13
  80. package/operations/entry/filtering/fullTextSearch.js.map +1 -1
  81. package/operations/entry/filtering/getValue.js +6 -10
  82. package/operations/entry/filtering/getValue.js.map +1 -1
  83. package/operations/entry/filtering/index.d.ts +2 -2
  84. package/operations/entry/filtering/index.js +3 -18
  85. package/operations/entry/filtering/index.js.map +1 -1
  86. package/operations/entry/filtering/mapPlugins.d.ts +2 -2
  87. package/operations/entry/filtering/mapPlugins.js +5 -11
  88. package/operations/entry/filtering/mapPlugins.js.map +1 -1
  89. package/operations/entry/filtering/plugins/defaultFilterCreate.d.ts +2 -2
  90. package/operations/entry/filtering/plugins/defaultFilterCreate.js +10 -14
  91. package/operations/entry/filtering/plugins/defaultFilterCreate.js.map +1 -1
  92. package/operations/entry/filtering/plugins/index.d.ts +1 -1
  93. package/operations/entry/filtering/plugins/index.js +8 -12
  94. package/operations/entry/filtering/plugins/index.js.map +1 -1
  95. package/operations/entry/filtering/plugins/objectFilterCreate.d.ts +2 -2
  96. package/operations/entry/filtering/plugins/objectFilterCreate.js +17 -19
  97. package/operations/entry/filtering/plugins/objectFilterCreate.js.map +1 -1
  98. package/operations/entry/filtering/plugins/refFilterCreate.d.ts +3 -2
  99. package/operations/entry/filtering/plugins/refFilterCreate.js +22 -19
  100. package/operations/entry/filtering/plugins/refFilterCreate.js.map +1 -1
  101. package/operations/entry/filtering/plugins/searchableJsonFilterCreate.d.ts +2 -0
  102. package/operations/entry/filtering/plugins/searchableJsonFilterCreate.js +57 -0
  103. package/operations/entry/filtering/plugins/searchableJsonFilterCreate.js.map +1 -0
  104. package/operations/entry/filtering/sort.d.ts +6 -6
  105. package/operations/entry/filtering/sort.js +18 -20
  106. package/operations/entry/filtering/sort.js.map +1 -1
  107. package/operations/entry/filtering/systemFields.d.ts +2 -4
  108. package/operations/entry/filtering/systemFields.js +103 -42
  109. package/operations/entry/filtering/systemFields.js.map +1 -1
  110. package/operations/entry/filtering/transform.d.ts +1 -1
  111. package/operations/entry/filtering/transform.js +3 -8
  112. package/operations/entry/filtering/transform.js.map +1 -1
  113. package/operations/entry/filtering/types.d.ts +3 -3
  114. package/operations/entry/filtering/types.js +2 -4
  115. package/operations/entry/filtering/types.js.map +1 -1
  116. package/operations/entry/filtering/values.d.ts +1 -1
  117. package/operations/entry/filtering/values.js +6 -12
  118. package/operations/entry/filtering/values.js.map +1 -1
  119. package/operations/entry/filtering/where.js +6 -10
  120. package/operations/entry/filtering/where.js.map +1 -1
  121. package/operations/entry/index.d.ts +3 -4
  122. package/operations/entry/index.js +689 -309
  123. package/operations/entry/index.js.map +1 -1
  124. package/operations/entry/keys.d.ts +40 -2
  125. package/operations/entry/keys.js +55 -30
  126. package/operations/entry/keys.js.map +1 -1
  127. package/operations/group/index.d.ts +4 -4
  128. package/operations/group/index.js +44 -61
  129. package/operations/group/index.js.map +1 -1
  130. package/operations/model/index.d.ts +3 -3
  131. package/operations/model/index.js +37 -46
  132. package/operations/model/index.js.map +1 -1
  133. package/package.json +22 -29
  134. package/plugins/CmsEntryFieldFilterPathPlugin.d.ts +4 -3
  135. package/plugins/CmsEntryFieldFilterPathPlugin.js +15 -16
  136. package/plugins/CmsEntryFieldFilterPathPlugin.js.map +1 -1
  137. package/plugins/CmsEntryFieldFilterPlugin.d.ts +10 -10
  138. package/plugins/CmsEntryFieldFilterPlugin.js +11 -14
  139. package/plugins/CmsEntryFieldFilterPlugin.js.map +1 -1
  140. package/plugins/CmsEntryFieldSortingPlugin.d.ts +2 -2
  141. package/plugins/CmsEntryFieldSortingPlugin.js +6 -15
  142. package/plugins/CmsEntryFieldSortingPlugin.js.map +1 -1
  143. package/plugins/CmsFieldFilterValueTransformPlugin.d.ts +2 -2
  144. package/plugins/CmsFieldFilterValueTransformPlugin.js +5 -13
  145. package/plugins/CmsFieldFilterValueTransformPlugin.js.map +1 -1
  146. package/plugins/index.d.ts +4 -4
  147. package/plugins/index.js +5 -48
  148. package/plugins/index.js.map +1 -1
  149. package/types.d.ts +24 -19
  150. package/types.js +5 -12
  151. package/types.js.map +1 -1
  152. package/definitions/settings.d.ts +0 -9
  153. package/definitions/settings.js +0 -53
  154. package/definitions/settings.js.map +0 -1
  155. package/definitions/system.d.ts +0 -9
  156. package/definitions/system.js +0 -41
  157. package/definitions/system.js.map +0 -1
  158. package/dynamoDb/storage/date.d.ts +0 -3
  159. package/dynamoDb/storage/date.js +0 -91
  160. package/dynamoDb/storage/date.js.map +0 -1
  161. package/operations/entry/systemFields.d.ts +0 -2
  162. package/operations/entry/systemFields.js +0 -72
  163. package/operations/entry/systemFields.js.map +0 -1
  164. package/operations/settings/index.d.ts +0 -7
  165. package/operations/settings/index.js +0 -111
  166. package/operations/settings/index.js.map +0 -1
  167. package/operations/system/index.d.ts +0 -7
  168. package/operations/system/index.js +0 -82
  169. package/operations/system/index.js.map +0 -1
@@ -1,32 +1,13 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports.createEntriesStorageOperations = void 0;
8
- var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
9
- var _error = _interopRequireDefault(require("@webiny/error"));
10
- var _dataLoaders = require("./dataLoaders");
11
- var _types = require("@webiny/api-headless-cms/types");
12
- 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");
16
- var _cursor = require("@webiny/utils/cursor");
17
- var _zeroPad = require("@webiny/utils/zeroPad");
18
- var _apiHeadlessCms = require("@webiny/api-headless-cms");
19
- var _createFields = require("./filtering/createFields");
20
- var _filtering = require("./filtering");
21
- const createType = () => {
22
- return "cms.entry";
23
- };
24
- const createLatestType = () => {
25
- return `${createType()}.l`;
26
- };
27
- const createPublishedType = () => {
28
- return `${createType()}.p`;
29
- };
1
+ import WebinyError from "@webiny/error";
2
+ import { DataLoadersHandler } from "./dataLoaders.js";
3
+ import { CONTENT_ENTRY_STATUS } from "@webiny/api-headless-cms/types/index.js";
4
+ import { createEntryLatestKeys, createEntryPublishedKeys, createEntryRevisionKeys, createGSIPartitionKey, createPartitionKey, createPublishedSortKey, createRevisionSortKey } from "./keys.js";
5
+ import { decodeCursor, encodeCursor } from "@webiny/utils";
6
+ import { StorageOperationsCmsModelPlugin, StorageTransformPlugin } from "@webiny/api-headless-cms";
7
+ import { createFields } from "./filtering/createFields.js";
8
+ import { filter, sort } from "./filtering/index.js";
9
+ import { isDeletedEntryMetaField, isEntryLevelEntryMetaField, isRestoredEntryMetaField, pickEntryMetaFields } from "@webiny/api-headless-cms/constants.js";
10
+ import { getBaseFieldType } from "@webiny/api-headless-cms/utils/getBaseFieldType.js";
30
11
  const convertToStorageEntry = params => {
31
12
  const {
32
13
  model,
@@ -36,9 +17,10 @@ const convertToStorageEntry = params => {
36
17
  fields: model.fields,
37
18
  values: storageEntry.values
38
19
  });
39
- return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
20
+ return {
21
+ ...storageEntry,
40
22
  values
41
- });
23
+ };
42
24
  };
43
25
  const convertFromStorageEntry = params => {
44
26
  const {
@@ -49,11 +31,13 @@ const convertFromStorageEntry = params => {
49
31
  fields: model.fields,
50
32
  values: storageEntry.values
51
33
  });
52
- return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
34
+ return {
35
+ ...storageEntry,
53
36
  values
54
- });
37
+ };
55
38
  };
56
- const createEntriesStorageOperations = params => {
39
+ const MAX_LIST_LIMIT = 1000000;
40
+ export const createEntriesStorageOperations = params => {
57
41
  const {
58
42
  entity,
59
43
  plugins
@@ -63,23 +47,25 @@ const createEntriesStorageOperations = params => {
63
47
  if (storageOperationsCmsModelPlugin) {
64
48
  return storageOperationsCmsModelPlugin;
65
49
  }
66
- storageOperationsCmsModelPlugin = plugins.oneByType(_apiHeadlessCms.StorageOperationsCmsModelPlugin.type);
50
+ storageOperationsCmsModelPlugin = plugins.oneByType(StorageOperationsCmsModelPlugin.type);
67
51
  return storageOperationsCmsModelPlugin;
68
52
  };
69
53
  const getStorageOperationsModel = model => {
70
54
  const plugin = getStorageOperationsCmsModelPlugin();
71
55
  return plugin.getModel(model);
72
56
  };
73
- const dataLoaders = new _dataLoaders.DataLoadersHandler({
57
+ const dataLoaders = new DataLoadersHandler({
74
58
  entity
75
59
  });
76
- const storageTransformPlugins = plugins.byType(_apiHeadlessCms.StorageTransformPlugin.type).reduce((collection, plugin) => {
77
- collection[plugin.fieldType] = plugin;
78
- return collection;
79
- }, {});
80
60
  const createStorageTransformCallable = model => {
61
+ // Cache StorageTransformPlugin to optimize execution.
62
+ const storageTransformPlugins = plugins.byType(StorageTransformPlugin.type).reduce((collection, plugin) => {
63
+ collection[plugin.fieldType] = plugin;
64
+ return collection;
65
+ }, {});
81
66
  return (field, value) => {
82
- const plugin = storageTransformPlugins[field.type];
67
+ const fieldType = getBaseFieldType(field);
68
+ const plugin = storageTransformPlugins[fieldType];
83
69
  if (!plugin) {
84
70
  return value;
85
71
  }
@@ -100,61 +86,55 @@ const createEntriesStorageOperations = params => {
100
86
  storageEntry: initialStorageEntry
101
87
  } = params;
102
88
  const model = getStorageOperationsModel(initialModel);
103
- const partitionKey = (0, _keys.createPartitionKey)({
104
- id: entry.id,
105
- locale: model.locale,
106
- tenant: model.tenant
107
- });
108
89
  const isPublished = entry.status === "published";
109
90
  const locked = isPublished ? true : entry.locked;
110
91
  const storageEntry = convertToStorageEntry({
111
92
  model,
112
93
  storageEntry: initialStorageEntry
113
94
  });
95
+ const storageEntryRevisionKeys = createEntryRevisionKeys(entry);
96
+ const storageEntryLatestKeys = createEntryLatestKeys(entry);
114
97
  /**
115
98
  * We need to:
116
99
  * - create new main entry item
117
100
  * - create new or update the latest entry item
118
101
  */
119
- const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
120
- locked,
121
- PK: partitionKey,
122
- SK: (0, _keys.createRevisionSortKey)(entry),
123
- TYPE: createType(),
124
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
125
- GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
126
- })), entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
127
- locked,
128
- PK: partitionKey,
129
- SK: (0, _keys.createLatestSortKey)(),
130
- TYPE: createLatestType(),
131
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
132
- GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
133
- }))];
102
+ const entityBatch = entity.createEntityWriter({
103
+ put: [{
104
+ ...storageEntryRevisionKeys,
105
+ data: {
106
+ ...storageEntry,
107
+ locked
108
+ }
109
+ }, {
110
+ ...storageEntryLatestKeys,
111
+ data: {
112
+ ...storageEntry,
113
+ locked
114
+ }
115
+ }]
116
+ });
134
117
 
135
118
  /**
136
119
  * We need to create published entry if
137
120
  */
138
121
  if (isPublished) {
139
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
140
- locked,
141
- PK: partitionKey,
142
- SK: (0, _keys.createPublishedSortKey)(),
143
- TYPE: createLatestType(),
144
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "P"),
145
- GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
146
- })));
122
+ const storageEntryPublishedKeys = createEntryPublishedKeys(storageEntry);
123
+ entityBatch.put({
124
+ ...storageEntryPublishedKeys,
125
+ data: {
126
+ ...storageEntry,
127
+ locked
128
+ }
129
+ });
147
130
  }
148
131
  try {
149
- await (0, _batchWrite.batchWriteAll)({
150
- table: entity.table,
151
- items
152
- });
132
+ await entityBatch.execute();
153
133
  dataLoaders.clearAll({
154
134
  model
155
135
  });
156
136
  } catch (ex) {
157
- throw new _error.default(ex.message || "Could not insert data into the DynamoDB.", ex.code || "CREATE_ENTRY_ERROR", {
137
+ throw new WebinyError(ex.message || "Could not insert data into the DynamoDB.", ex.code || "CREATE_ENTRY_ERROR", {
158
138
  error: ex,
159
139
  entry
160
140
  });
@@ -167,43 +147,63 @@ const createEntriesStorageOperations = params => {
167
147
  storageEntry: initialStorageEntry
168
148
  } = params;
169
149
  const model = getStorageOperationsModel(initialModel);
170
- const partitionKey = (0, _keys.createPartitionKey)({
171
- id: entry.id,
172
- locale: model.locale,
173
- tenant: model.tenant
174
- });
175
150
  const storageEntry = convertToStorageEntry({
176
151
  storageEntry: initialStorageEntry,
177
152
  model
178
153
  });
154
+
179
155
  /**
180
156
  * We need to:
181
157
  * - create the main entry item
182
- * - update the last entry item to a current one
158
+ * - update the latest entry item to the current one
159
+ * - if the entry's status was set to "published":
160
+ * - update the published entry item to the current one
161
+ * - unpublish previously published revision (if any)
183
162
  */
184
- const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
185
- PK: partitionKey,
186
- SK: (0, _keys.createRevisionSortKey)(storageEntry),
187
- TYPE: createType(),
188
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
189
- GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
190
- })), entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
191
- PK: partitionKey,
192
- SK: (0, _keys.createLatestSortKey)(),
193
- TYPE: createLatestType(),
194
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
195
- GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
196
- }))];
197
- try {
198
- await (0, _batchWrite.batchWriteAll)({
199
- table: entity.table,
200
- items
163
+ const entityBatch = entity.createEntityWriter({
164
+ put: [{
165
+ ...createEntryRevisionKeys(storageEntry),
166
+ data: {
167
+ ...storageEntry
168
+ }
169
+ }, {
170
+ ...createEntryLatestKeys(storageEntry),
171
+ data: {
172
+ ...storageEntry
173
+ }
174
+ }]
175
+ });
176
+ const isPublished = entry.status === "published";
177
+ if (isPublished) {
178
+ entityBatch.put({
179
+ ...createEntryPublishedKeys(storageEntry),
180
+ data: {
181
+ ...storageEntry
182
+ }
201
183
  });
184
+
185
+ // Unpublish previously published revision (if any).
186
+ const [publishedRevisionStorageEntry] = await dataLoaders.getPublishedRevisionByEntryId({
187
+ model,
188
+ ids: [entry.id]
189
+ });
190
+ if (publishedRevisionStorageEntry) {
191
+ entityBatch.put({
192
+ ...createEntryRevisionKeys(publishedRevisionStorageEntry),
193
+ data: {
194
+ ...publishedRevisionStorageEntry,
195
+ status: CONTENT_ENTRY_STATUS.UNPUBLISHED
196
+ }
197
+ });
198
+ }
199
+ }
200
+ try {
201
+ await entityBatch.execute();
202
202
  dataLoaders.clearAll({
203
203
  model
204
204
  });
205
205
  } catch (ex) {
206
- throw new _error.default(ex.message || "Could not create revision from given entry.", ex.code || "CREATE_REVISION_ERROR", {
206
+ throw new WebinyError(ex.message || "Could not create revision from given entry.", ex.code || "CREATE_REVISION_ERROR", {
207
207
  error: ex,
208
208
  entry,
209
209
  storageEntry
@@ -220,14 +220,8 @@ const createEntriesStorageOperations = params => {
220
220
  storageEntry: initialStorageEntry
221
221
  } = params;
222
222
  const model = getStorageOperationsModel(initialModel);
223
- const partitionKey = (0, _keys.createPartitionKey)({
224
- id: entry.id,
225
- locale: model.locale,
226
- tenant: model.tenant
227
- });
228
223
  const isPublished = entry.status === "published";
229
224
  const locked = isPublished ? true : entry.locked;
230
- const items = [];
231
225
  const storageEntry = convertToStorageEntry({
232
226
  model,
233
227
  storageEntry: initialStorageEntry
@@ -237,100 +231,319 @@ const createEntriesStorageOperations = params => {
237
231
  * - update the current entry
238
232
  * - update the latest entry if the current entry is the latest one
239
233
  */
240
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
241
- locked,
242
- PK: partitionKey,
243
- SK: (0, _keys.createRevisionSortKey)(storageEntry),
244
- TYPE: createType(),
245
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
246
- GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
247
- })));
234
+
235
+ const entityBatch = entity.createEntityWriter({
236
+ put: [{
237
+ ...createEntryRevisionKeys(storageEntry),
238
+ data: {
239
+ ...storageEntry,
240
+ locked
241
+ }
242
+ }]
243
+ });
248
244
  if (isPublished) {
249
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
250
- locked,
251
- PK: partitionKey,
252
- SK: (0, _keys.createPublishedSortKey)(),
253
- TYPE: createPublishedType(),
254
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "P"),
255
- GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
256
- })));
245
+ entityBatch.put({
246
+ ...createEntryPublishedKeys(storageEntry),
247
+ data: {
248
+ ...storageEntry,
249
+ locked
250
+ }
251
+ });
257
252
  }
258
253
 
259
254
  /**
260
255
  * We need the latest entry to update it as well if necessary.
261
256
  */
262
257
  const latestStorageEntry = await getLatestRevisionByEntryId(model, entry);
263
- if (latestStorageEntry && latestStorageEntry.id === entry.id) {
264
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
265
- locked,
266
- PK: partitionKey,
267
- SK: (0, _keys.createLatestSortKey)(),
268
- TYPE: createLatestType(),
269
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
270
- GSI1_SK: (0, _keys.createGSISortKey)(entry)
271
- })));
258
+ if (latestStorageEntry) {
259
+ const updatingLatestRevision = latestStorageEntry.id === entry.id;
260
+ if (updatingLatestRevision) {
261
+ entityBatch.put({
262
+ ...createEntryLatestKeys(storageEntry),
263
+ data: {
264
+ ...storageEntry,
265
+ locked
266
+ }
267
+ });
268
+ } else {
269
+ /**
270
+ * If not updating latest revision, we still want to update the latest revision's
271
+ * entry-level meta fields to match the current revision's entry-level meta fields.
272
+ */
273
+ const updatedEntryLevelMetaFields = pickEntryMetaFields(entry, isEntryLevelEntryMetaField);
274
+
275
+ /**
276
+ * First we update the regular DynamoDB table. Two updates are needed:
277
+ * - one for the actual revision record
278
+ * - one for the latest record
279
+ */
280
+ entityBatch.put({
281
+ ...createEntryRevisionKeys(latestStorageEntry),
282
+ data: {
283
+ ...latestStorageEntry,
284
+ ...updatedEntryLevelMetaFields
285
+ }
286
+ });
287
+ entityBatch.put({
288
+ ...createEntryLatestKeys(latestStorageEntry),
289
+ data: {
290
+ ...latestStorageEntry,
291
+ ...updatedEntryLevelMetaFields
292
+ }
293
+ });
294
+ }
272
295
  }
273
296
  try {
274
- await (0, _batchWrite.batchWriteAll)({
275
- table: entity.table,
276
- items
277
- });
297
+ await entityBatch.execute();
278
298
  dataLoaders.clearAll({
279
299
  model
280
300
  });
281
301
  return initialStorageEntry;
282
302
  } catch (ex) {
283
- throw new _error.default(ex.message || "Could not update entry.", ex.code || "UPDATE_ERROR", {
303
+ throw new WebinyError(ex.message || "Could not update entry.", ex.code || "UPDATE_ERROR", {
284
304
  error: ex,
285
305
  entry,
286
306
  latestStorageEntry
287
307
  });
288
308
  }
289
309
  };
290
- const deleteEntry = async (initialModel, params) => {
291
- const {
292
- entry
293
- } = params;
310
+ const move = async (initialModel, id, folderId) => {
311
+ /**
312
+ * We need to:
313
+ * - load all the revisions of the entry, including published and latest
314
+ * - update all the revisions (published and latest ) of the entry with new folderId
315
+ */
294
316
  const model = getStorageOperationsModel(initialModel);
295
- const queryAllParams = {
296
- entity,
297
- partitionKey: (0, _keys.createPartitionKey)({
298
- id: entry.id,
299
- locale: model.locale,
300
- tenant: model.tenant
301
- }),
317
+ /**
318
+ * First we need to load all the revisions and published / latest entry.
319
+ */
320
+ const partitionKey = createPartitionKey({
321
+ id,
322
+ tenant: model.tenant
323
+ });
324
+ const records = await entity.queryAll({
325
+ partitionKey,
302
326
  options: {
303
327
  gte: " "
304
328
  }
305
- };
329
+ });
330
+ /**
331
+ * Then create the batch writes for the DynamoDB, with the updated folderId.
332
+ */
333
+ const entityBatch = entity.createEntityWriter({
334
+ put: records.map(item => {
335
+ return {
336
+ ...item,
337
+ data: {
338
+ ...item.data,
339
+ location: {
340
+ ...item.data.location,
341
+ folderId
342
+ }
343
+ }
344
+ };
345
+ })
346
+ });
347
+
348
+ /**
349
+ * And finally write it...
350
+ */
351
+ try {
352
+ await entityBatch.execute();
353
+ } catch (ex) {
354
+ throw WebinyError.from(ex, {
355
+ message: "Could not move records to a new folder.",
356
+ data: {
357
+ id,
358
+ folderId
359
+ }
360
+ });
361
+ }
362
+ };
363
+ const moveToBin = async (initialModel, params) => {
364
+ const {
365
+ entry,
366
+ storageEntry: initialStorageEntry
367
+ } = params;
368
+ const model = getStorageOperationsModel(initialModel);
369
+
370
+ /**
371
+ * First we need to load all the revisions and published / latest entries.
372
+ */
373
+
306
374
  let records = [];
307
375
  try {
308
- records = await (0, _query.queryAll)(queryAllParams);
376
+ records = await entity.queryAll({
377
+ partitionKey: createPartitionKey({
378
+ id: entry.id,
379
+ tenant: model.tenant
380
+ }),
381
+ options: {
382
+ gte: " "
383
+ }
384
+ });
309
385
  } catch (ex) {
310
- throw new _error.default(ex.message || "Could not load all records.", ex.code || "LOAD_ALL_RECORDS_ERROR", {
386
+ throw new WebinyError(ex.message || "Could not load all records.", ex.code || "LOAD_ALL_RECORDS_ERROR", {
311
387
  error: ex,
312
- entry
388
+ id: entry.id
313
389
  });
314
390
  }
315
- const items = records.map(item => {
316
- return entity.deleteBatch({
317
- PK: item.PK,
318
- SK: item.SK
391
+ if (records.length === 0) {
392
+ return;
393
+ }
394
+ const storageEntry = convertToStorageEntry({
395
+ model,
396
+ storageEntry: initialStorageEntry
397
+ });
398
+
399
+ /**
400
+ * Let's pick the `deleted` meta fields from the storage entry.
401
+ */
402
+ const updatedDeletedMetaFields = pickEntryMetaFields(storageEntry, isDeletedEntryMetaField);
403
+
404
+ /**
405
+ * Then create the batch writes for the DynamoDB, with the updated data.
406
+ */
407
+ const entityBatch = entity.createEntityWriter({
408
+ put: records.map(record => {
409
+ return {
410
+ ...record,
411
+ data: {
412
+ ...record.data,
413
+ ...updatedDeletedMetaFields,
414
+ wbyDeleted: storageEntry.wbyDeleted,
415
+ location: storageEntry.location,
416
+ binOriginalFolderId: storageEntry.binOriginalFolderId
417
+ }
418
+ };
419
+ })
420
+ });
421
+ /**
422
+ * And finally write it...
423
+ */
424
+ try {
425
+ await entityBatch.execute();
426
+ } catch (ex) {
427
+ throw new WebinyError(ex.message || "Could not move the entry to the bin.", ex.code || "MOVE_ENTRY_TO_BIN_ERROR", {
428
+ error: ex,
429
+ entry,
430
+ storageEntry
319
431
  });
432
+ }
433
+ };
434
+ const deleteEntry = async (initialModel, params) => {
435
+ const {
436
+ entry
437
+ } = params;
438
+ const id = entry.id || entry.entryId;
439
+ const model = getStorageOperationsModel(initialModel);
440
+ const partitionKey = createPartitionKey({
441
+ id,
442
+ tenant: model.tenant
320
443
  });
444
+ let records = [];
321
445
  try {
322
- await (0, _batchWrite.batchWriteAll)({
323
- table: entity.table,
324
- items
446
+ records = await entity.queryAll({
447
+ partitionKey,
448
+ options: {
449
+ gte: " "
450
+ }
451
+ });
452
+ } catch (ex) {
453
+ throw new WebinyError(ex.message || "Could not load all records.", ex.code || "LOAD_ALL_RECORDS_ERROR", {
454
+ error: ex,
455
+ id
325
456
  });
457
+ }
458
+ const entityBatch = entity.createEntityWriter({
459
+ delete: records.map(item => {
460
+ return {
461
+ PK: item.PK,
462
+ SK: item.SK
463
+ };
464
+ })
465
+ });
466
+ try {
467
+ await entityBatch.execute();
326
468
  dataLoaders.clearAll({
327
469
  model
328
470
  });
329
471
  } catch (ex) {
330
- throw new _error.default(ex.message || "Could not delete the entry.", ex.code || "DELETE_ENTRY_ERROR", {
472
+ throw new WebinyError(ex.message || "Could not delete the entry.", ex.code || "DELETE_ENTRY_ERROR", {
331
473
  error: ex,
332
- partitionKey: queryAllParams.partitionKey,
333
- entry
474
+ partitionKey,
475
+ id
476
+ });
477
+ }
478
+ };
479
+ const restoreFromBin = async (initialModel, params) => {
480
+ const {
481
+ entry,
482
+ storageEntry: initialStorageEntry
483
+ } = params;
484
+ const model = getStorageOperationsModel(initialModel);
485
+
486
+ /**
487
+ * First we need to load all the revisions and published / latest entries.
488
+ */
489
+ let records = [];
490
+ try {
491
+ records = await entity.queryAll({
492
+ partitionKey: createPartitionKey({
493
+ id: entry.id,
494
+ tenant: model.tenant
495
+ }),
496
+ options: {
497
+ gte: " "
498
+ }
499
+ });
500
+ } catch (ex) {
501
+ throw new WebinyError(ex.message || "Could not load all records.", ex.code || "LOAD_ALL_RECORDS_ERROR", {
502
+ error: ex,
503
+ id: entry.id
504
+ });
505
+ }
506
+ if (records.length === 0) {
507
+ return initialStorageEntry;
508
+ }
509
+ const storageEntry = convertToStorageEntry({
510
+ model,
511
+ storageEntry: initialStorageEntry
512
+ });
513
+
514
+ /**
515
+ * Let's pick the `restored` meta fields from the storage entry.
516
+ */
517
+ const updatedRestoredMetaFields = pickEntryMetaFields(storageEntry, isRestoredEntryMetaField);
518
+ const entityBatch = entity.createEntityWriter({
519
+ put: records.map(record => {
520
+ return {
521
+ ...record,
522
+ data: {
523
+ ...record.data,
524
+ ...updatedRestoredMetaFields,
525
+ wbyDeleted: storageEntry.wbyDeleted,
526
+ location: storageEntry.location,
527
+ binOriginalFolderId: storageEntry.binOriginalFolderId
528
+ }
529
+ };
530
+ })
531
+ });
532
+
533
+ /**
534
+ * And finally write it...
535
+ */
536
+ try {
537
+ await entityBatch.execute();
538
+ dataLoaders.clearAll({
539
+ model
540
+ });
541
+ return initialStorageEntry;
542
+ } catch (ex) {
543
+ throw new WebinyError(ex.message || "Could not restore the entry from the bin.", ex.code || "RESTORE_ENTRY_ERROR", {
544
+ error: ex,
545
+ entry,
546
+ storageEntry
334
547
  });
335
548
  }
336
549
  };
@@ -341,55 +554,108 @@ const createEntriesStorageOperations = params => {
341
554
  latestStorageEntry: initialLatestStorageEntry
342
555
  } = params;
343
556
  const model = getStorageOperationsModel(initialModel);
344
- const partitionKey = (0, _keys.createPartitionKey)({
557
+ const partitionKey = createPartitionKey({
345
558
  id: entry.id,
346
- locale: model.locale,
347
559
  tenant: model.tenant
348
560
  });
349
- const items = [entity.deleteBatch({
350
- PK: partitionKey,
351
- SK: (0, _keys.createRevisionSortKey)(entry)
352
- })];
561
+ const entityBatch = entity.createEntityWriter({
562
+ delete: [{
563
+ PK: partitionKey,
564
+ SK: createRevisionSortKey(entry)
565
+ }]
566
+ });
353
567
  const publishedStorageEntry = await getPublishedRevisionByEntryId(model, entry);
354
568
 
355
569
  /**
356
570
  * If revision we are deleting is the published one as well, we need to delete those records as well.
357
571
  */
358
572
  if (publishedStorageEntry && entry.id === publishedStorageEntry.id) {
359
- items.push(entity.deleteBatch({
573
+ entityBatch.delete({
360
574
  PK: partitionKey,
361
- SK: (0, _keys.createPublishedSortKey)()
362
- }));
575
+ SK: createPublishedSortKey()
576
+ });
363
577
  }
364
578
  if (initialLatestStorageEntry) {
365
579
  const latestStorageEntry = convertToStorageEntry({
366
580
  storageEntry: initialLatestStorageEntry,
367
581
  model
368
582
  });
369
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, latestStorageEntry), {}, {
370
- PK: partitionKey,
371
- SK: (0, _keys.createLatestSortKey)(),
372
- TYPE: createLatestType(),
373
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
374
- GSI1_SK: (0, _keys.createGSISortKey)(latestStorageEntry)
375
- })));
583
+ entityBatch.put({
584
+ ...createEntryLatestKeys(latestStorageEntry),
585
+ data: {
586
+ ...latestStorageEntry
587
+ }
588
+ });
589
+
590
+ // Do an update on the latest revision. We need to update the latest revision's
591
+ // entry-level meta fields to match the previous revision's entry-level meta fields.
592
+ entityBatch.put({
593
+ ...createEntryRevisionKeys(latestStorageEntry),
594
+ data: {
595
+ ...latestStorageEntry
596
+ }
597
+ });
376
598
  }
377
599
  try {
378
- await (0, _batchWrite.batchWriteAll)({
379
- table: entity.table,
380
- items
381
- });
600
+ await entityBatch.execute();
382
601
  dataLoaders.clearAll({
383
602
  model
384
603
  });
385
604
  } catch (ex) {
386
- throw new _error.default(ex.message, ex.code, {
605
+ throw new WebinyError(ex.message, ex.code, {
387
606
  error: ex,
388
607
  entry,
389
608
  latestEntry
390
609
  });
391
610
  }
392
611
  };
612
+ const deleteMultipleEntries = async (initialModel, params) => {
613
+ const {
614
+ entries
615
+ } = params;
616
+ const model = getStorageOperationsModel(initialModel);
617
+ /**
618
+ * First we need all the revisions of the entries we want to delete.
619
+ */
620
+ const revisions = await dataLoaders.getAllEntryRevisions({
621
+ model,
622
+ ids: entries
623
+ });
624
+ /**
625
+ * Then we need to construct the queries for all the revisions and entries.
626
+ */
627
+
628
+ const entityBatch = entity.createEntityWriter();
629
+ for (const id of entries) {
630
+ const partitionKey = createPartitionKey({
631
+ id,
632
+ tenant: model.tenant
633
+ });
634
+ entityBatch.delete({
635
+ PK: partitionKey,
636
+ SK: "L"
637
+ });
638
+ entityBatch.delete({
639
+ PK: partitionKey,
640
+ SK: "P"
641
+ });
642
+ }
643
+ /**
644
+ * Exact revisions of all the entries
645
+ */
646
+ for (const revision of revisions) {
647
+ entityBatch.delete({
648
+ PK: createPartitionKey({
649
+ id: revision.id,
650
+ tenant: model.tenant
651
+ }),
652
+ SK: createRevisionSortKey({
653
+ version: revision.version
654
+ })
655
+ });
656
+ }
657
+ await entityBatch.execute();
658
+ };
393
659
  const getLatestRevisionByEntryId = async (initialModel, params) => {
394
660
  const model = getStorageOperationsModel(initialModel);
395
661
  const items = await dataLoaders.getLatestRevisionByEntryId({
@@ -493,45 +759,36 @@ const createEntriesStorageOperations = params => {
493
759
  entryId,
494
760
  version
495
761
  } = params;
496
- const queryParams = {
497
- entity,
498
- partitionKey: (0, _keys.createPartitionKey)({
499
- tenant: model.tenant,
500
- locale: model.locale,
501
- id: entryId
502
- }),
762
+ const partitionKey = createPartitionKey({
763
+ tenant: model.tenant,
764
+ id: entryId
765
+ });
766
+ const unfilteredRevisions = await entity.queryAll({
767
+ partitionKey,
503
768
  options: {
504
- lt: `REV#${(0, _zeroPad.zeroPad)(version)}`,
505
- /**
506
- * We need to have extra checks because DynamoDB will return published or latest record if there is no REV# record.
507
- */
508
- filters: [{
509
- attr: "TYPE",
510
- eq: createType()
511
- }, {
512
- attr: "version",
513
- lt: version
514
- }],
769
+ beginsWith: `REV#`,
515
770
  reverse: true
516
771
  }
517
- };
772
+ });
773
+ const filteredRevisions = unfilteredRevisions.filter(item => {
774
+ return item.data.version < version;
775
+ });
776
+ const storageEntry = filteredRevisions[0];
777
+ if (!storageEntry) {
778
+ return null;
779
+ }
518
780
  try {
519
- const result = await (0, _query.queryOne)(queryParams);
520
- const storageEntry = (0, _cleanup.cleanupItem)(entity, result);
521
- if (!storageEntry) {
522
- return null;
523
- }
524
781
  return convertFromStorageEntry({
525
- storageEntry,
782
+ storageEntry: storageEntry.data,
526
783
  model
527
784
  });
528
785
  } catch (ex) {
529
- 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), {}, {
786
+ throw new WebinyError(ex.message || "Could not get previous version of given entry.", ex.code || "GET_PREVIOUS_VERSION_ERROR", {
787
+ ...params,
530
788
  error: ex,
531
- partitionKey: queryParams.partitionKey,
532
- options: queryParams.options,
789
+ partitionKey,
533
790
  model
534
- }));
791
+ });
535
792
  }
536
793
  };
537
794
  const list = async (initialModel, params) => {
@@ -544,24 +801,24 @@ const createEntriesStorageOperations = params => {
544
801
  fields,
545
802
  search
546
803
  } = params;
547
- const limit = initialLimit <= 0 || initialLimit >= 10000 ? 10000 : initialLimit;
804
+ const limit = initialLimit <= 0 || initialLimit >= MAX_LIST_LIMIT ? MAX_LIST_LIMIT : initialLimit;
548
805
  const type = initialWhere.published ? "P" : "L";
549
- const queryAllParams = {
550
- entity,
551
- partitionKey: (0, _keys.createGSIPartitionKey)(model, type),
552
- options: {
553
- index: "GSI1",
554
- gte: " "
555
- }
806
+ const partitionKey = createGSIPartitionKey(model, type);
807
+ const options = {
808
+ index: "GSI1",
809
+ gte: " "
556
810
  };
557
811
  let storageEntries = [];
558
812
  try {
559
- storageEntries = await (0, _query.queryAll)(queryAllParams);
813
+ storageEntries = await entity.queryAll({
814
+ partitionKey,
815
+ options
816
+ });
560
817
  } catch (ex) {
561
- throw new _error.default(ex.message, "QUERY_ENTRIES_ERROR", {
818
+ throw new WebinyError(ex.message, "QUERY_ENTRIES_ERROR", {
562
819
  error: ex,
563
- partitionKey: queryAllParams.partitionKey,
564
- options: queryAllParams.options
820
+ partitionKey,
821
+ options
565
822
  });
566
823
  }
567
824
  if (storageEntries.length === 0) {
@@ -572,14 +829,16 @@ const createEntriesStorageOperations = params => {
572
829
  items: []
573
830
  };
574
831
  }
575
- const where = (0, _objectSpread2.default)({}, initialWhere);
832
+ const where = {
833
+ ...initialWhere
834
+ };
576
835
  delete where["published"];
577
836
  delete where["latest"];
578
837
  /**
579
838
  * We need an object containing field, transformers and paths.
580
839
  * Just build it here and pass on into other methods that require it to avoid mapping multiple times.
581
840
  */
582
- const modelFields = (0, _createFields.createFields)({
841
+ const modelFields = createFields({
583
842
  plugins,
584
843
  fields: model.fields
585
844
  });
@@ -591,7 +850,7 @@ const createEntriesStorageOperations = params => {
591
850
  */
592
851
  const records = await Promise.all(storageEntries.map(async storageEntry => {
593
852
  const entry = convertFromStorageEntry({
594
- storageEntry,
853
+ storageEntry: storageEntry.data,
595
854
  model
596
855
  });
597
856
  for (const field of model.fields) {
@@ -603,7 +862,7 @@ const createEntriesStorageOperations = params => {
603
862
  * Filter the read items via the code.
604
863
  * It will build the filters out of the where input and transform the values it is using.
605
864
  */
606
- const filteredItems = (0, _filtering.filter)({
865
+ const filteredItems = filter({
607
866
  items: records,
608
867
  where,
609
868
  plugins,
@@ -619,14 +878,14 @@ const createEntriesStorageOperations = params => {
619
878
  * Sorting is also done via the code.
620
879
  * It takes the sort input and sorts by it via the lodash sortBy method.
621
880
  */
622
- const sortedItems = (0, _filtering.sort)({
881
+ const sortedItems = sort({
623
882
  model,
624
883
  plugins,
625
884
  items: filteredItems,
626
885
  sort: sortBy,
627
886
  fields: modelFields
628
887
  });
629
- const start = parseInt((0, _cursor.decodeCursor)(after) || "0") || 0;
888
+ const start = parseInt(decodeCursor(after) || "0") || 0;
630
889
  const hasMoreItems = totalCount > start + limit;
631
890
  const end = limit > totalCount + start + limit ? undefined : start + limit;
632
891
  const slicedItems = sortedItems.slice(start, end);
@@ -634,21 +893,22 @@ const createEntriesStorageOperations = params => {
634
893
  * Although we do not need a cursor here, we will use it as such to keep it standardized.
635
894
  * Number is simply encoded.
636
895
  */
637
- const cursor = totalCount > start + limit ? (0, _cursor.encodeCursor)(`${start + limit}`) : null;
896
+ const cursor = encodeCursor(`${start + limit}`);
638
897
  return {
639
898
  hasMoreItems,
640
899
  totalCount,
641
900
  cursor,
642
- items: (0, _cleanup.cleanupItems)(entity, slicedItems)
901
+ items: slicedItems
643
902
  };
644
903
  };
645
904
  const get = async (initialModel, params) => {
646
905
  const model = getStorageOperationsModel(initialModel);
647
906
  const {
648
907
  items
649
- } = await list(model, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), {}, {
908
+ } = await list(model, {
909
+ ...params,
650
910
  limit: 1
651
- }));
911
+ });
652
912
  return items.shift() || null;
653
913
  };
654
914
  const publish = async (initialModel, params) => {
@@ -657,75 +917,125 @@ const createEntriesStorageOperations = params => {
657
917
  storageEntry: initialStorageEntry
658
918
  } = params;
659
919
  const model = getStorageOperationsModel(initialModel);
660
- const partitionKey = (0, _keys.createPartitionKey)({
661
- id: entry.id,
662
- locale: model.locale,
663
- tenant: model.tenant
664
- });
665
920
 
666
921
  /**
667
922
  * We need the latest and published entries to see if something needs to be updated alongside the publishing one.
668
923
  */
669
924
  const initialLatestStorageEntry = await getLatestRevisionByEntryId(model, entry);
925
+ if (!initialLatestStorageEntry) {
926
+ throw new WebinyError(`Could not publish entry. Could not load latest ("L") record.`, "PUBLISH_ERROR", {
927
+ entry
928
+ });
929
+ }
670
930
  const initialPublishedStorageEntry = await getPublishedRevisionByEntryId(model, entry);
671
931
  const storageEntry = convertToStorageEntry({
672
932
  model,
673
933
  storageEntry: initialStorageEntry
674
934
  });
675
- /**
676
- * We need to update:
677
- * - current entry revision sort key
678
- * - published sort key
679
- * - the latest sort key - if entry updated is actually latest
680
- * - previous published entry to unpublished status - if any previously published entry
681
- */
682
- const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
683
- PK: partitionKey,
684
- SK: (0, _keys.createRevisionSortKey)(entry),
685
- TYPE: createType(),
686
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
687
- GSI1_SK: (0, _keys.createGSISortKey)(entry)
688
- })), entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
689
- PK: partitionKey,
690
- SK: (0, _keys.createPublishedSortKey)(),
691
- TYPE: createPublishedType(),
692
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "P"),
693
- GSI1_SK: (0, _keys.createGSISortKey)(entry)
694
- }))];
695
- if (initialLatestStorageEntry && entry.id === initialLatestStorageEntry.id) {
696
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
697
- PK: partitionKey,
698
- SK: (0, _keys.createLatestSortKey)(),
699
- TYPE: createLatestType(),
700
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
701
- GSI1_SK: (0, _keys.createGSISortKey)(entry)
702
- })));
703
- }
704
- if (initialPublishedStorageEntry && initialPublishedStorageEntry.id !== entry.id) {
705
- const publishedStorageEntry = convertToStorageEntry({
706
- storageEntry: initialPublishedStorageEntry,
935
+
936
+ // 1. Update REV# and P records with new data.
937
+ const entityBatch = entity.createEntityWriter({
938
+ put: [{
939
+ ...createEntryRevisionKeys(storageEntry),
940
+ data: {
941
+ ...storageEntry
942
+ }
943
+ }, {
944
+ ...createEntryPublishedKeys(storageEntry),
945
+ data: {
946
+ ...storageEntry
947
+ }
948
+ }]
949
+ });
950
+
951
+ // 2. When it comes to the latest record, we need to perform a couple of different
952
+ // updates, based on whether the entry being published is the latest revision or not.
953
+ const publishedRevisionId = initialPublishedStorageEntry?.id;
954
+ const publishingLatestRevision = entry.id === initialLatestStorageEntry.id;
955
+ if (publishingLatestRevision) {
956
+ // 2.1 If we're publishing the latest revision, we first need to update the L record.
957
+ entityBatch.put({
958
+ ...createEntryLatestKeys(storageEntry),
959
+ data: {
960
+ ...storageEntry
961
+ }
962
+ });
963
+
964
+ // 2.2 Additionally, if we have a previously published entry, we need to mark it as unpublished.
965
+ if (publishedRevisionId && publishedRevisionId !== entry.id) {
966
+ const publishedStorageEntry = convertToStorageEntry({
967
+ storageEntry: initialPublishedStorageEntry,
968
+ model
969
+ });
970
+ entityBatch.put({
971
+ ...createEntryRevisionKeys(publishedStorageEntry),
972
+ data: {
973
+ ...publishedStorageEntry,
974
+ status: CONTENT_ENTRY_STATUS.UNPUBLISHED
975
+ }
976
+ });
977
+ }
978
+ } else {
979
+ // 2.3 If the published revision is not the latest one, the situation is a bit
980
+ // more complex. We first need to update the L and REV# records with the new
981
+ // values of *only entry-level* meta fields.
982
+ const updatedEntryLevelMetaFields = pickEntryMetaFields(entry, isEntryLevelEntryMetaField);
983
+ const latestStorageEntry = convertToStorageEntry({
984
+ storageEntry: initialLatestStorageEntry,
707
985
  model
708
986
  });
709
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, publishedStorageEntry), {}, {
710
- PK: partitionKey,
711
- SK: (0, _keys.createRevisionSortKey)(publishedStorageEntry),
712
- TYPE: createType(),
713
- status: _types.CONTENT_ENTRY_STATUS.UNPUBLISHED,
714
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
715
- GSI1_SK: (0, _keys.createGSISortKey)(publishedStorageEntry)
716
- })));
987
+
988
+ // 2.3.1 Update L record. Apart from updating the entry-level meta fields, we also need
989
+ // to change the status from "published" to "unpublished" (if the status is set to "published").
990
+ let latestRevisionStatus = latestStorageEntry.status;
991
+ if (latestRevisionStatus === CONTENT_ENTRY_STATUS.PUBLISHED) {
992
+ latestRevisionStatus = CONTENT_ENTRY_STATUS.UNPUBLISHED;
993
+ }
994
+ const latestStorageEntryFields = {
995
+ ...latestStorageEntry,
996
+ ...updatedEntryLevelMetaFields,
997
+ status: latestRevisionStatus
998
+ };
999
+ entityBatch.put({
1000
+ ...createEntryLatestKeys(latestStorageEntryFields),
1001
+ data: {
1002
+ ...latestStorageEntryFields
1003
+ }
1004
+ });
1005
+
1006
+ // 2.3.2 Update REV# record.
1007
+ entityBatch.put({
1008
+ ...createEntryRevisionKeys(latestStorageEntryFields),
1009
+ data: {
1010
+ ...latestStorageEntryFields
1011
+ }
1012
+ });
1013
+
1014
+ // 2.3.3 Finally, if we got a published entry, but it wasn't the latest one, we need to take
1015
+ // an extra step and mark it as unpublished.
1016
+ const publishedRevisionDifferentFromLatest = publishedRevisionId && publishedRevisionId !== latestStorageEntry.id;
1017
+ if (publishedRevisionDifferentFromLatest) {
1018
+ const publishedStorageEntry = convertToStorageEntry({
1019
+ storageEntry: initialPublishedStorageEntry,
1020
+ model
1021
+ });
1022
+ entityBatch.put({
1023
+ ...createEntryRevisionKeys(publishedStorageEntry),
1024
+ data: {
1025
+ ...publishedStorageEntry,
1026
+ status: CONTENT_ENTRY_STATUS.UNPUBLISHED
1027
+ }
1028
+ });
1029
+ }
717
1030
  }
718
1031
  try {
719
- await (0, _batchWrite.batchWriteAll)({
720
- table: entity.table,
721
- items
722
- });
1032
+ await entityBatch.execute();
723
1033
  dataLoaders.clearAll({
724
1034
  model
725
1035
  });
726
1036
  return initialStorageEntry;
727
1037
  } catch (ex) {
728
- throw new _error.default(ex.message || "Could not execute the publishing batch.", ex.code || "PUBLISH_ERROR", {
1038
+ throw new WebinyError(ex.message || "Could not execute the publishing batch.", ex.code || "PUBLISH_ERROR", {
729
1039
  entry,
730
1040
  latestStorageEntry: initialLatestStorageEntry,
731
1041
  publishedStorageEntry: initialPublishedStorageEntry
@@ -738,9 +1048,8 @@ const createEntriesStorageOperations = params => {
738
1048
  storageEntry: initialStorageEntry
739
1049
  } = params;
740
1050
  const model = getStorageOperationsModel(initialModel);
741
- const partitionKey = (0, _keys.createPartitionKey)({
1051
+ const partitionKey = createPartitionKey({
742
1052
  id: entry.id,
743
- locale: model.locale,
744
1053
  tenant: model.tenant
745
1054
  });
746
1055
  const storageEntry = convertToStorageEntry({
@@ -753,52 +1062,120 @@ const createEntriesStorageOperations = params => {
753
1062
  * - update current entry revision with new data
754
1063
  * - update the latest entry status - if entry being unpublished is latest
755
1064
  */
756
- const items = [entity.deleteBatch({
757
- PK: partitionKey,
758
- SK: (0, _keys.createPublishedSortKey)()
759
- }), entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
760
- PK: partitionKey,
761
- SK: (0, _keys.createRevisionSortKey)(entry),
762
- TYPE: createType(),
763
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
764
- GSI1_SK: (0, _keys.createGSISortKey)(entry)
765
- }))];
1065
+ const entityBatch = entity.createEntityWriter({
1066
+ delete: [{
1067
+ PK: partitionKey,
1068
+ SK: createPublishedSortKey()
1069
+ }],
1070
+ put: [{
1071
+ ...createEntryRevisionKeys(storageEntry),
1072
+ data: {
1073
+ ...storageEntry
1074
+ }
1075
+ }]
1076
+ });
766
1077
 
767
1078
  /**
768
1079
  * We need the latest entry to see if something needs to be updated alongside the unpublishing one.
769
1080
  */
770
- const latestStorageEntry = await getLatestRevisionByEntryId(model, entry);
771
- if (latestStorageEntry && entry.id === latestStorageEntry.id) {
772
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
773
- PK: partitionKey,
774
- SK: (0, _keys.createLatestSortKey)(),
775
- TYPE: createLatestType(),
776
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
777
- GSI1_SK: (0, _keys.createGSISortKey)(entry)
778
- })));
1081
+ const initialLatestStorageEntry = await getLatestRevisionByEntryId(model, entry);
1082
+ if (initialLatestStorageEntry) {
1083
+ const unpublishingLatestRevision = entry.id === initialLatestStorageEntry.id;
1084
+ if (unpublishingLatestRevision) {
1085
+ entityBatch.put({
1086
+ ...createEntryLatestKeys(storageEntry),
1087
+ data: {
1088
+ ...storageEntry
1089
+ }
1090
+ });
1091
+ } else {
1092
+ const latestStorageEntry = convertToStorageEntry({
1093
+ storageEntry: initialLatestStorageEntry,
1094
+ model
1095
+ });
1096
+
1097
+ // If the unpublished revision is not the latest one, we still need to
1098
+ // update the latest record with the new values of entry-level meta fields.
1099
+ const updatedEntryLevelMetaFields = pickEntryMetaFields(entry, isEntryLevelEntryMetaField);
1100
+
1101
+ // 1. Update actual revision record.
1102
+ entityBatch.put({
1103
+ ...createEntryRevisionKeys(latestStorageEntry),
1104
+ data: {
1105
+ ...latestStorageEntry,
1106
+ ...updatedEntryLevelMetaFields
1107
+ }
1108
+ });
1109
+
1110
+ // 2. Update latest record.
1111
+ entityBatch.put({
1112
+ ...createEntryLatestKeys(latestStorageEntry),
1113
+ data: {
1114
+ ...latestStorageEntry,
1115
+ ...updatedEntryLevelMetaFields
1116
+ }
1117
+ });
1118
+ }
779
1119
  }
780
1120
  try {
781
- await (0, _batchWrite.batchWriteAll)({
782
- table: entity.table,
783
- items
784
- });
1121
+ await entityBatch.execute();
785
1122
  dataLoaders.clearAll({
786
1123
  model
787
1124
  });
788
1125
  return initialStorageEntry;
789
1126
  } catch (ex) {
790
- throw new _error.default(ex.message || "Could not execute unpublish batch.", ex.code || "UNPUBLISH_ERROR", {
1127
+ throw new WebinyError(ex.message || "Could not execute unpublish batch.", ex.code || "UNPUBLISH_ERROR", {
791
1128
  entry,
792
1129
  storageEntry
793
1130
  });
794
1131
  }
795
1132
  };
1133
+ const getUniqueFieldValues = async (model, params) => {
1134
+ const {
1135
+ where,
1136
+ fieldId
1137
+ } = params;
1138
+ const field = model.fields.find(f => f.fieldId === fieldId);
1139
+ if (!field) {
1140
+ throw new WebinyError(`Could not find field with given "fieldId" value.`, "FIELD_NOT_FOUND", {
1141
+ fieldId
1142
+ });
1143
+ }
1144
+ const {
1145
+ items
1146
+ } = await list(model, {
1147
+ where,
1148
+ limit: MAX_LIST_LIMIT
1149
+ });
1150
+ const result = {};
1151
+ for (const item of items) {
1152
+ const fieldValue = item.values[field.fieldId];
1153
+ if (!fieldValue) {
1154
+ continue;
1155
+ }
1156
+ const values = Array.isArray(fieldValue) ? fieldValue : [fieldValue];
1157
+ if (values.length === 0) {
1158
+ continue;
1159
+ }
1160
+ for (const value of values) {
1161
+ result[value] = {
1162
+ value,
1163
+ count: (result[value]?.count || 0) + 1
1164
+ };
1165
+ }
1166
+ }
1167
+ 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);
1168
+ };
796
1169
  return {
797
1170
  create,
798
1171
  createRevisionFrom,
799
1172
  update,
1173
+ move,
800
1174
  delete: deleteEntry,
1175
+ moveToBin,
1176
+ restoreFromBin,
801
1177
  deleteRevision,
1178
+ deleteMultipleEntries,
802
1179
  getPreviousRevision,
803
1180
  getPublishedByIds,
804
1181
  getLatestByIds,
@@ -810,7 +1187,10 @@ const createEntriesStorageOperations = params => {
810
1187
  getRevisions,
811
1188
  publish,
812
1189
  list,
813
- unpublish
1190
+ unpublish,
1191
+ dataLoaders,
1192
+ getUniqueFieldValues
814
1193
  };
815
1194
  };
816
- exports.createEntriesStorageOperations = createEntriesStorageOperations;
1195
+
1196
+ //# sourceMappingURL=index.js.map