@webiny/api-headless-cms-ddb 0.0.0-unstable.df6d94b531 → 0.0.0-unstable.df7a8bb475

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 +59 -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 -84
  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 +8 -14
  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 +88 -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 +738 -329
  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,25 +31,41 @@ 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
60
44
  } = params;
61
- const dataLoaders = new _dataLoaders.DataLoadersHandler({
45
+ let storageOperationsCmsModelPlugin;
46
+ const getStorageOperationsCmsModelPlugin = () => {
47
+ if (storageOperationsCmsModelPlugin) {
48
+ return storageOperationsCmsModelPlugin;
49
+ }
50
+ storageOperationsCmsModelPlugin = plugins.oneByType(StorageOperationsCmsModelPlugin.type);
51
+ return storageOperationsCmsModelPlugin;
52
+ };
53
+ const getStorageOperationsModel = model => {
54
+ const plugin = getStorageOperationsCmsModelPlugin();
55
+ return plugin.getModel(model);
56
+ };
57
+ const dataLoaders = new DataLoadersHandler({
62
58
  entity
63
59
  });
64
- const storageTransformPlugins = plugins.byType(_apiHeadlessCms.StorageTransformPlugin.type).reduce((collection, plugin) => {
65
- collection[plugin.fieldType] = plugin;
66
- return collection;
67
- }, {});
68
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
+ }, {});
69
66
  return (field, value) => {
70
- const plugin = storageTransformPlugins[field.type];
67
+ const fieldType = getBaseFieldType(field);
68
+ const plugin = storageTransformPlugins[fieldType];
71
69
  if (!plugin) {
72
70
  return value;
73
71
  }
@@ -82,114 +80,130 @@ const createEntriesStorageOperations = params => {
82
80
  });
83
81
  };
84
82
  };
85
- const create = async (model, params) => {
83
+ const create = async (initialModel, params) => {
86
84
  const {
87
85
  entry,
88
86
  storageEntry: initialStorageEntry
89
87
  } = params;
90
- const partitionKey = (0, _keys.createPartitionKey)({
91
- id: entry.id,
92
- locale: model.locale,
93
- tenant: model.tenant
94
- });
88
+ const model = getStorageOperationsModel(initialModel);
95
89
  const isPublished = entry.status === "published";
96
90
  const locked = isPublished ? true : entry.locked;
97
91
  const storageEntry = convertToStorageEntry({
98
92
  model,
99
93
  storageEntry: initialStorageEntry
100
94
  });
95
+ const storageEntryRevisionKeys = createEntryRevisionKeys(entry);
96
+ const storageEntryLatestKeys = createEntryLatestKeys(entry);
101
97
  /**
102
98
  * We need to:
103
99
  * - create new main entry item
104
- * - create new or update latest entry item
100
+ * - create new or update the latest entry item
105
101
  */
106
- const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
107
- locked,
108
- PK: partitionKey,
109
- SK: (0, _keys.createRevisionSortKey)(entry),
110
- TYPE: createType(),
111
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
112
- GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
113
- })), entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
114
- locked,
115
- PK: partitionKey,
116
- SK: (0, _keys.createLatestSortKey)(),
117
- TYPE: createLatestType(),
118
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
119
- GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
120
- }))];
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
+ });
121
117
 
122
118
  /**
123
119
  * We need to create published entry if
124
120
  */
125
121
  if (isPublished) {
126
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
127
- locked,
128
- PK: partitionKey,
129
- SK: (0, _keys.createPublishedSortKey)(),
130
- TYPE: createLatestType(),
131
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "P"),
132
- GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
133
- })));
122
+ const storageEntryPublishedKeys = createEntryPublishedKeys(storageEntry);
123
+ entityBatch.put({
124
+ ...storageEntryPublishedKeys,
125
+ data: {
126
+ ...storageEntry,
127
+ locked
128
+ }
129
+ });
134
130
  }
135
131
  try {
136
- await (0, _batchWrite.batchWriteAll)({
137
- table: entity.table,
138
- items
139
- });
132
+ await entityBatch.execute();
140
133
  dataLoaders.clearAll({
141
134
  model
142
135
  });
143
136
  } catch (ex) {
144
- 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", {
145
138
  error: ex,
146
139
  entry
147
140
  });
148
141
  }
149
142
  return initialStorageEntry;
150
143
  };
151
- const createRevisionFrom = async (model, params) => {
144
+ const createRevisionFrom = async (initialModel, params) => {
152
145
  const {
153
146
  entry,
154
147
  storageEntry: initialStorageEntry
155
148
  } = params;
156
- const partitionKey = (0, _keys.createPartitionKey)({
157
- id: entry.id,
158
- locale: model.locale,
159
- tenant: model.tenant
160
- });
149
+ const model = getStorageOperationsModel(initialModel);
161
150
  const storageEntry = convertToStorageEntry({
162
151
  storageEntry: initialStorageEntry,
163
152
  model
164
153
  });
154
+
165
155
  /**
166
156
  * We need to:
167
157
  * - create the main entry item
168
- * - 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)
169
162
  */
170
- const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
171
- PK: partitionKey,
172
- SK: (0, _keys.createRevisionSortKey)(storageEntry),
173
- TYPE: createType(),
174
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
175
- GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
176
- })), entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
177
- PK: partitionKey,
178
- SK: (0, _keys.createLatestSortKey)(),
179
- TYPE: createLatestType(),
180
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
181
- GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
182
- }))];
183
- try {
184
- await (0, _batchWrite.batchWriteAll)({
185
- table: entity.table,
186
- 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
+ }
187
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();
188
202
  dataLoaders.clearAll({
189
203
  model
190
204
  });
191
205
  } catch (ex) {
192
- 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", {
193
207
  error: ex,
194
208
  entry,
195
209
  storageEntry
@@ -200,19 +214,14 @@ const createEntriesStorageOperations = params => {
200
214
  */
201
215
  return initialStorageEntry;
202
216
  };
203
- const update = async (model, params) => {
217
+ const update = async (initialModel, params) => {
204
218
  const {
205
219
  entry,
206
220
  storageEntry: initialStorageEntry
207
221
  } = params;
208
- const partitionKey = (0, _keys.createPartitionKey)({
209
- id: entry.id,
210
- locale: model.locale,
211
- tenant: model.tenant
212
- });
222
+ const model = getStorageOperationsModel(initialModel);
213
223
  const isPublished = entry.status === "published";
214
224
  const locked = isPublished ? true : entry.locked;
215
- const items = [];
216
225
  const storageEntry = convertToStorageEntry({
217
226
  model,
218
227
  storageEntry: initialStorageEntry
@@ -222,158 +231,433 @@ const createEntriesStorageOperations = params => {
222
231
  * - update the current entry
223
232
  * - update the latest entry if the current entry is the latest one
224
233
  */
225
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
226
- locked,
227
- PK: partitionKey,
228
- SK: (0, _keys.createRevisionSortKey)(storageEntry),
229
- TYPE: createType(),
230
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
231
- GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
232
- })));
234
+
235
+ const entityBatch = entity.createEntityWriter({
236
+ put: [{
237
+ ...createEntryRevisionKeys(storageEntry),
238
+ data: {
239
+ ...storageEntry,
240
+ locked
241
+ }
242
+ }]
243
+ });
233
244
  if (isPublished) {
234
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
235
- locked,
236
- PK: partitionKey,
237
- SK: (0, _keys.createPublishedSortKey)(),
238
- TYPE: createPublishedType(),
239
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "P"),
240
- GSI1_SK: (0, _keys.createGSISortKey)(storageEntry)
241
- })));
245
+ entityBatch.put({
246
+ ...createEntryPublishedKeys(storageEntry),
247
+ data: {
248
+ ...storageEntry,
249
+ locked
250
+ }
251
+ });
242
252
  }
243
253
 
244
254
  /**
245
- * We need the latest entry to update it as well if neccessary.
255
+ * We need the latest entry to update it as well if necessary.
246
256
  */
247
257
  const latestStorageEntry = await getLatestRevisionByEntryId(model, entry);
248
- if (latestStorageEntry && latestStorageEntry.id === entry.id) {
249
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
250
- locked,
251
- PK: partitionKey,
252
- SK: (0, _keys.createLatestSortKey)(),
253
- TYPE: createLatestType(),
254
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
255
- GSI1_SK: (0, _keys.createGSISortKey)(entry)
256
- })));
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
+ }
257
295
  }
258
296
  try {
259
- await (0, _batchWrite.batchWriteAll)({
260
- table: entity.table,
261
- items
262
- });
297
+ await entityBatch.execute();
263
298
  dataLoaders.clearAll({
264
299
  model
265
300
  });
266
301
  return initialStorageEntry;
267
302
  } catch (ex) {
268
- 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", {
269
304
  error: ex,
270
305
  entry,
271
306
  latestStorageEntry
272
307
  });
273
308
  }
274
309
  };
275
- const deleteEntry = async (model, params) => {
276
- const {
277
- entry
278
- } = params;
279
- const queryAllParams = {
280
- entity,
281
- partitionKey: (0, _keys.createPartitionKey)({
282
- id: entry.id,
283
- locale: model.locale,
284
- tenant: model.tenant
285
- }),
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
+ */
316
+ const model = getStorageOperationsModel(initialModel);
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,
286
326
  options: {
287
327
  gte: " "
288
328
  }
289
- };
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
+
290
374
  let records = [];
291
375
  try {
292
- 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
+ });
293
385
  } catch (ex) {
294
- 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", {
295
387
  error: ex,
296
- entry
388
+ id: entry.id
297
389
  });
298
390
  }
299
- const items = records.map(item => {
300
- return entity.deleteBatch({
301
- PK: item.PK,
302
- 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
303
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
304
443
  });
444
+ let records = [];
305
445
  try {
306
- await (0, _batchWrite.batchWriteAll)({
307
- table: entity.table,
308
- 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
309
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();
310
468
  dataLoaders.clearAll({
311
469
  model
312
470
  });
313
471
  } catch (ex) {
314
- 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", {
315
473
  error: ex,
316
- partitionKey: queryAllParams.partitionKey,
317
- 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
318
547
  });
319
548
  }
320
549
  };
321
- const deleteRevision = async (model, params) => {
550
+ const deleteRevision = async (initialModel, params) => {
322
551
  const {
323
552
  entry,
324
553
  latestEntry,
325
554
  latestStorageEntry: initialLatestStorageEntry
326
555
  } = params;
327
- const partitionKey = (0, _keys.createPartitionKey)({
556
+ const model = getStorageOperationsModel(initialModel);
557
+ const partitionKey = createPartitionKey({
328
558
  id: entry.id,
329
- locale: model.locale,
330
559
  tenant: model.tenant
331
560
  });
332
- const items = [entity.deleteBatch({
333
- PK: partitionKey,
334
- SK: (0, _keys.createRevisionSortKey)(entry)
335
- })];
561
+ const entityBatch = entity.createEntityWriter({
562
+ delete: [{
563
+ PK: partitionKey,
564
+ SK: createRevisionSortKey(entry)
565
+ }]
566
+ });
336
567
  const publishedStorageEntry = await getPublishedRevisionByEntryId(model, entry);
337
568
 
338
569
  /**
339
570
  * If revision we are deleting is the published one as well, we need to delete those records as well.
340
571
  */
341
572
  if (publishedStorageEntry && entry.id === publishedStorageEntry.id) {
342
- items.push(entity.deleteBatch({
573
+ entityBatch.delete({
343
574
  PK: partitionKey,
344
- SK: (0, _keys.createPublishedSortKey)()
345
- }));
575
+ SK: createPublishedSortKey()
576
+ });
346
577
  }
347
578
  if (initialLatestStorageEntry) {
348
579
  const latestStorageEntry = convertToStorageEntry({
349
580
  storageEntry: initialLatestStorageEntry,
350
581
  model
351
582
  });
352
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, latestStorageEntry), {}, {
353
- PK: partitionKey,
354
- SK: (0, _keys.createLatestSortKey)(),
355
- TYPE: createLatestType(),
356
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
357
- GSI1_SK: (0, _keys.createGSISortKey)(latestStorageEntry)
358
- })));
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
+ });
359
598
  }
360
599
  try {
361
- await (0, _batchWrite.batchWriteAll)({
362
- table: entity.table,
363
- items
364
- });
600
+ await entityBatch.execute();
365
601
  dataLoaders.clearAll({
366
602
  model
367
603
  });
368
604
  } catch (ex) {
369
- throw new _error.default(ex.message, ex.code, {
605
+ throw new WebinyError(ex.message, ex.code, {
370
606
  error: ex,
371
607
  entry,
372
608
  latestEntry
373
609
  });
374
610
  }
375
611
  };
376
- const getLatestRevisionByEntryId = async (model, params) => {
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
+ };
659
+ const getLatestRevisionByEntryId = async (initialModel, params) => {
660
+ const model = getStorageOperationsModel(initialModel);
377
661
  const items = await dataLoaders.getLatestRevisionByEntryId({
378
662
  model,
379
663
  ids: [params.id]
@@ -387,7 +671,8 @@ const createEntriesStorageOperations = params => {
387
671
  model
388
672
  });
389
673
  };
390
- const getPublishedRevisionByEntryId = async (model, params) => {
674
+ const getPublishedRevisionByEntryId = async (initialModel, params) => {
675
+ const model = getStorageOperationsModel(initialModel);
391
676
  const items = await dataLoaders.getPublishedRevisionByEntryId({
392
677
  model,
393
678
  ids: [params.id]
@@ -401,7 +686,8 @@ const createEntriesStorageOperations = params => {
401
686
  model
402
687
  });
403
688
  };
404
- const getRevisionById = async (model, params) => {
689
+ const getRevisionById = async (initialModel, params) => {
690
+ const model = getStorageOperationsModel(initialModel);
405
691
  const items = await dataLoaders.getRevisionById({
406
692
  model,
407
693
  ids: [params.id]
@@ -415,7 +701,8 @@ const createEntriesStorageOperations = params => {
415
701
  model
416
702
  });
417
703
  };
418
- const getRevisions = async (model, params) => {
704
+ const getRevisions = async (initialModel, params) => {
705
+ const model = getStorageOperationsModel(initialModel);
419
706
  const items = await dataLoaders.getAllEntryRevisions({
420
707
  model,
421
708
  ids: [params.id]
@@ -427,7 +714,8 @@ const createEntriesStorageOperations = params => {
427
714
  });
428
715
  });
429
716
  };
430
- const getByIds = async (model, params) => {
717
+ const getByIds = async (initialModel, params) => {
718
+ const model = getStorageOperationsModel(initialModel);
431
719
  const items = await dataLoaders.getRevisionById({
432
720
  model,
433
721
  ids: params.ids
@@ -439,7 +727,8 @@ const createEntriesStorageOperations = params => {
439
727
  });
440
728
  });
441
729
  };
442
- const getLatestByIds = async (model, params) => {
730
+ const getLatestByIds = async (initialModel, params) => {
731
+ const model = getStorageOperationsModel(initialModel);
443
732
  const items = await dataLoaders.getLatestRevisionByEntryId({
444
733
  model,
445
734
  ids: params.ids
@@ -451,7 +740,8 @@ const createEntriesStorageOperations = params => {
451
740
  });
452
741
  });
453
742
  };
454
- const getPublishedByIds = async (model, params) => {
743
+ const getPublishedByIds = async (initialModel, params) => {
744
+ const model = getStorageOperationsModel(initialModel);
455
745
  const items = await dataLoaders.getPublishedRevisionByEntryId({
456
746
  model,
457
747
  ids: params.ids
@@ -463,53 +753,46 @@ const createEntriesStorageOperations = params => {
463
753
  });
464
754
  });
465
755
  };
466
- const getPreviousRevision = async (model, params) => {
756
+ const getPreviousRevision = async (initialModel, params) => {
757
+ const model = getStorageOperationsModel(initialModel);
467
758
  const {
468
759
  entryId,
469
760
  version
470
761
  } = params;
471
- const queryParams = {
472
- entity,
473
- partitionKey: (0, _keys.createPartitionKey)({
474
- tenant: model.tenant,
475
- locale: model.locale,
476
- id: entryId
477
- }),
762
+ const partitionKey = createPartitionKey({
763
+ tenant: model.tenant,
764
+ id: entryId
765
+ });
766
+ const unfilteredRevisions = await entity.queryAll({
767
+ partitionKey,
478
768
  options: {
479
- lt: `REV#${(0, _zeroPad.zeroPad)(version)}`,
480
- /**
481
- * We need to have extra checks because DynamoDB will return published or latest record if there is no REV# record.
482
- */
483
- filters: [{
484
- attr: "TYPE",
485
- eq: createType()
486
- }, {
487
- attr: "version",
488
- lt: version
489
- }],
769
+ beginsWith: `REV#`,
490
770
  reverse: true
491
771
  }
492
- };
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
+ }
493
780
  try {
494
- const result = await (0, _query.queryOne)(queryParams);
495
- const storageEntry = (0, _cleanup.cleanupItem)(entity, result);
496
- if (!storageEntry) {
497
- return null;
498
- }
499
781
  return convertFromStorageEntry({
500
- storageEntry,
782
+ storageEntry: storageEntry.data,
501
783
  model
502
784
  });
503
785
  } catch (ex) {
504
- 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,
505
788
  error: ex,
506
- partitionKey: queryParams.partitionKey,
507
- options: queryParams.options,
789
+ partitionKey,
508
790
  model
509
- }));
791
+ });
510
792
  }
511
793
  };
512
- const list = async (model, params) => {
794
+ const list = async (initialModel, params) => {
795
+ const model = getStorageOperationsModel(initialModel);
513
796
  const {
514
797
  limit: initialLimit = 10,
515
798
  where: initialWhere,
@@ -518,24 +801,24 @@ const createEntriesStorageOperations = params => {
518
801
  fields,
519
802
  search
520
803
  } = params;
521
- const limit = initialLimit <= 0 || initialLimit >= 10000 ? 10000 : initialLimit;
804
+ const limit = initialLimit <= 0 || initialLimit >= MAX_LIST_LIMIT ? MAX_LIST_LIMIT : initialLimit;
522
805
  const type = initialWhere.published ? "P" : "L";
523
- const queryAllParams = {
524
- entity,
525
- partitionKey: (0, _keys.createGSIPartitionKey)(model, type),
526
- options: {
527
- index: "GSI1",
528
- gte: " "
529
- }
806
+ const partitionKey = createGSIPartitionKey(model, type);
807
+ const options = {
808
+ index: "GSI1",
809
+ gte: " "
530
810
  };
531
811
  let storageEntries = [];
532
812
  try {
533
- storageEntries = await (0, _query.queryAll)(queryAllParams);
813
+ storageEntries = await entity.queryAll({
814
+ partitionKey,
815
+ options
816
+ });
534
817
  } catch (ex) {
535
- throw new _error.default(ex.message, "QUERY_ENTRIES_ERROR", {
818
+ throw new WebinyError(ex.message, "QUERY_ENTRIES_ERROR", {
536
819
  error: ex,
537
- partitionKey: queryAllParams.partitionKey,
538
- options: queryAllParams.options
820
+ partitionKey,
821
+ options
539
822
  });
540
823
  }
541
824
  if (storageEntries.length === 0) {
@@ -546,14 +829,16 @@ const createEntriesStorageOperations = params => {
546
829
  items: []
547
830
  };
548
831
  }
549
- const where = (0, _objectSpread2.default)({}, initialWhere);
832
+ const where = {
833
+ ...initialWhere
834
+ };
550
835
  delete where["published"];
551
836
  delete where["latest"];
552
837
  /**
553
838
  * We need an object containing field, transformers and paths.
554
839
  * Just build it here and pass on into other methods that require it to avoid mapping multiple times.
555
840
  */
556
- const modelFields = (0, _createFields.createFields)({
841
+ const modelFields = createFields({
557
842
  plugins,
558
843
  fields: model.fields
559
844
  });
@@ -565,7 +850,7 @@ const createEntriesStorageOperations = params => {
565
850
  */
566
851
  const records = await Promise.all(storageEntries.map(async storageEntry => {
567
852
  const entry = convertFromStorageEntry({
568
- storageEntry,
853
+ storageEntry: storageEntry.data,
569
854
  model
570
855
  });
571
856
  for (const field of model.fields) {
@@ -577,7 +862,7 @@ const createEntriesStorageOperations = params => {
577
862
  * Filter the read items via the code.
578
863
  * It will build the filters out of the where input and transform the values it is using.
579
864
  */
580
- const filteredItems = (0, _filtering.filter)({
865
+ const filteredItems = filter({
581
866
  items: records,
582
867
  where,
583
868
  plugins,
@@ -593,14 +878,14 @@ const createEntriesStorageOperations = params => {
593
878
  * Sorting is also done via the code.
594
879
  * It takes the sort input and sorts by it via the lodash sortBy method.
595
880
  */
596
- const sortedItems = (0, _filtering.sort)({
881
+ const sortedItems = sort({
597
882
  model,
598
883
  plugins,
599
884
  items: filteredItems,
600
885
  sort: sortBy,
601
886
  fields: modelFields
602
887
  });
603
- const start = parseInt((0, _cursor.decodeCursor)(after) || "0") || 0;
888
+ const start = parseInt(decodeCursor(after) || "0") || 0;
604
889
  const hasMoreItems = totalCount > start + limit;
605
890
  const end = limit > totalCount + start + limit ? undefined : start + limit;
606
891
  const slicedItems = sortedItems.slice(start, end);
@@ -608,110 +893,163 @@ const createEntriesStorageOperations = params => {
608
893
  * Although we do not need a cursor here, we will use it as such to keep it standardized.
609
894
  * Number is simply encoded.
610
895
  */
611
- const cursor = totalCount > start + limit ? (0, _cursor.encodeCursor)(`${start + limit}`) : null;
896
+ const cursor = encodeCursor(`${start + limit}`);
612
897
  return {
613
898
  hasMoreItems,
614
899
  totalCount,
615
900
  cursor,
616
- items: (0, _cleanup.cleanupItems)(entity, slicedItems)
901
+ items: slicedItems
617
902
  };
618
903
  };
619
- const get = async (model, params) => {
904
+ const get = async (initialModel, params) => {
905
+ const model = getStorageOperationsModel(initialModel);
620
906
  const {
621
907
  items
622
- } = await list(model, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), {}, {
908
+ } = await list(model, {
909
+ ...params,
623
910
  limit: 1
624
- }));
911
+ });
625
912
  return items.shift() || null;
626
913
  };
627
- const publish = async (model, params) => {
914
+ const publish = async (initialModel, params) => {
628
915
  const {
629
916
  entry,
630
917
  storageEntry: initialStorageEntry
631
918
  } = params;
632
- const partitionKey = (0, _keys.createPartitionKey)({
633
- id: entry.id,
634
- locale: model.locale,
635
- tenant: model.tenant
636
- });
919
+ const model = getStorageOperationsModel(initialModel);
637
920
 
638
921
  /**
639
- * We need the latest and published entries to see if something needs to be updated along side the publishing one.
922
+ * We need the latest and published entries to see if something needs to be updated alongside the publishing one.
640
923
  */
641
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
+ }
642
930
  const initialPublishedStorageEntry = await getPublishedRevisionByEntryId(model, entry);
643
931
  const storageEntry = convertToStorageEntry({
644
932
  model,
645
933
  storageEntry: initialStorageEntry
646
934
  });
647
- /**
648
- * We need to update:
649
- * - current entry revision sort key
650
- * - published sort key
651
- * - latest sort key - if entry updated is actually latest
652
- * - previous published entry to unpublished status - if any previously published entry
653
- */
654
- const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
655
- PK: partitionKey,
656
- SK: (0, _keys.createRevisionSortKey)(entry),
657
- TYPE: createType(),
658
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
659
- GSI1_SK: (0, _keys.createGSISortKey)(entry)
660
- })), entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
661
- PK: partitionKey,
662
- SK: (0, _keys.createPublishedSortKey)(),
663
- TYPE: createPublishedType(),
664
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "P"),
665
- GSI1_SK: (0, _keys.createGSISortKey)(entry)
666
- }))];
667
- if (initialLatestStorageEntry && entry.id === initialLatestStorageEntry.id) {
668
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
669
- PK: partitionKey,
670
- SK: (0, _keys.createLatestSortKey)(),
671
- TYPE: createLatestType(),
672
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
673
- GSI1_SK: (0, _keys.createGSISortKey)(entry)
674
- })));
675
- }
676
- if (initialPublishedStorageEntry && initialPublishedStorageEntry.id !== entry.id) {
677
- const publishedStorageEntry = convertToStorageEntry({
678
- 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,
679
985
  model
680
986
  });
681
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, publishedStorageEntry), {}, {
682
- PK: partitionKey,
683
- SK: (0, _keys.createRevisionSortKey)(publishedStorageEntry),
684
- TYPE: createType(),
685
- status: _types.CONTENT_ENTRY_STATUS.UNPUBLISHED,
686
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
687
- GSI1_SK: (0, _keys.createGSISortKey)(publishedStorageEntry)
688
- })));
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
+ }
689
1030
  }
690
1031
  try {
691
- await (0, _batchWrite.batchWriteAll)({
692
- table: entity.table,
693
- items
694
- });
1032
+ await entityBatch.execute();
695
1033
  dataLoaders.clearAll({
696
1034
  model
697
1035
  });
698
1036
  return initialStorageEntry;
699
1037
  } catch (ex) {
700
- 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", {
701
1039
  entry,
702
1040
  latestStorageEntry: initialLatestStorageEntry,
703
1041
  publishedStorageEntry: initialPublishedStorageEntry
704
1042
  });
705
1043
  }
706
1044
  };
707
- const unpublish = async (model, params) => {
1045
+ const unpublish = async (initialModel, params) => {
708
1046
  const {
709
1047
  entry,
710
1048
  storageEntry: initialStorageEntry
711
1049
  } = params;
712
- const partitionKey = (0, _keys.createPartitionKey)({
1050
+ const model = getStorageOperationsModel(initialModel);
1051
+ const partitionKey = createPartitionKey({
713
1052
  id: entry.id,
714
- locale: model.locale,
715
1053
  tenant: model.tenant
716
1054
  });
717
1055
  const storageEntry = convertToStorageEntry({
@@ -722,54 +1060,122 @@ const createEntriesStorageOperations = params => {
722
1060
  * We need to:
723
1061
  * - delete currently published entry
724
1062
  * - update current entry revision with new data
725
- * - update latest entry status - if entry being unpublished is latest
1063
+ * - update the latest entry status - if entry being unpublished is latest
726
1064
  */
727
- const items = [entity.deleteBatch({
728
- PK: partitionKey,
729
- SK: (0, _keys.createPublishedSortKey)()
730
- }), entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
731
- PK: partitionKey,
732
- SK: (0, _keys.createRevisionSortKey)(entry),
733
- TYPE: createType(),
734
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "A"),
735
- GSI1_SK: (0, _keys.createGSISortKey)(entry)
736
- }))];
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
+ });
737
1077
 
738
1078
  /**
739
- * We need the latest entry to see if something needs to be updated along side the unpublishing one.
1079
+ * We need the latest entry to see if something needs to be updated alongside the unpublishing one.
740
1080
  */
741
- const latestStorageEntry = await getLatestRevisionByEntryId(model, entry);
742
- if (latestStorageEntry && entry.id === latestStorageEntry.id) {
743
- items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
744
- PK: partitionKey,
745
- SK: (0, _keys.createLatestSortKey)(),
746
- TYPE: createLatestType(),
747
- GSI1_PK: (0, _keys.createGSIPartitionKey)(model, "L"),
748
- GSI1_SK: (0, _keys.createGSISortKey)(entry)
749
- })));
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
+ }
750
1119
  }
751
1120
  try {
752
- await (0, _batchWrite.batchWriteAll)({
753
- table: entity.table,
754
- items
755
- });
1121
+ await entityBatch.execute();
756
1122
  dataLoaders.clearAll({
757
1123
  model
758
1124
  });
759
1125
  return initialStorageEntry;
760
1126
  } catch (ex) {
761
- 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", {
762
1128
  entry,
763
1129
  storageEntry
764
1130
  });
765
1131
  }
766
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
+ };
767
1169
  return {
768
1170
  create,
769
1171
  createRevisionFrom,
770
1172
  update,
1173
+ move,
771
1174
  delete: deleteEntry,
1175
+ moveToBin,
1176
+ restoreFromBin,
772
1177
  deleteRevision,
1178
+ deleteMultipleEntries,
773
1179
  getPreviousRevision,
774
1180
  getPublishedByIds,
775
1181
  getLatestByIds,
@@ -781,7 +1187,10 @@ const createEntriesStorageOperations = params => {
781
1187
  getRevisions,
782
1188
  publish,
783
1189
  list,
784
- unpublish
1190
+ unpublish,
1191
+ dataLoaders,
1192
+ getUniqueFieldValues
785
1193
  };
786
1194
  };
787
- exports.createEntriesStorageOperations = createEntriesStorageOperations;
1195
+
1196
+ //# sourceMappingURL=index.js.map