@webiny/api-page-builder-so-ddb-es 0.0.0-ee-vpcs.549378cf03

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 (186) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +30 -0
  3. package/configurations.d.ts +9 -0
  4. package/configurations.js +49 -0
  5. package/configurations.js.map +1 -0
  6. package/definitions/blockCategoryEntity.d.ts +9 -0
  7. package/definitions/blockCategoryEntity.js +61 -0
  8. package/definitions/blockCategoryEntity.js.map +1 -0
  9. package/definitions/categoryEntity.d.ts +9 -0
  10. package/definitions/categoryEntity.js +61 -0
  11. package/definitions/categoryEntity.js.map +1 -0
  12. package/definitions/menuEntity.d.ts +9 -0
  13. package/definitions/menuEntity.js +61 -0
  14. package/definitions/menuEntity.js.map +1 -0
  15. package/definitions/pageBlockEntity.d.ts +9 -0
  16. package/definitions/pageBlockEntity.js +64 -0
  17. package/definitions/pageBlockEntity.js.map +1 -0
  18. package/definitions/pageElasticsearchEntity.d.ts +9 -0
  19. package/definitions/pageElasticsearchEntity.js +40 -0
  20. package/definitions/pageElasticsearchEntity.js.map +1 -0
  21. package/definitions/pageElementEntity.d.ts +9 -0
  22. package/definitions/pageElementEntity.js +67 -0
  23. package/definitions/pageElementEntity.js.map +1 -0
  24. package/definitions/pageEntity.d.ts +9 -0
  25. package/definitions/pageEntity.js +97 -0
  26. package/definitions/pageEntity.js.map +1 -0
  27. package/definitions/settingsEntity.d.ts +9 -0
  28. package/definitions/settingsEntity.js +77 -0
  29. package/definitions/settingsEntity.js.map +1 -0
  30. package/definitions/systemEntity.d.ts +9 -0
  31. package/definitions/systemEntity.js +40 -0
  32. package/definitions/systemEntity.js.map +1 -0
  33. package/definitions/table.d.ts +9 -0
  34. package/definitions/table.js +24 -0
  35. package/definitions/table.js.map +1 -0
  36. package/definitions/tableElasticsearch.d.ts +9 -0
  37. package/definitions/tableElasticsearch.js +24 -0
  38. package/definitions/tableElasticsearch.js.map +1 -0
  39. package/elasticsearch/createElasticsearchIndex.d.ts +9 -0
  40. package/elasticsearch/createElasticsearchIndex.js +60 -0
  41. package/elasticsearch/createElasticsearchIndex.js.map +1 -0
  42. package/elasticsearch/indices/base.d.ts +2 -0
  43. package/elasticsearch/indices/base.js +15 -0
  44. package/elasticsearch/indices/base.js.map +1 -0
  45. package/elasticsearch/indices/index.d.ts +1 -0
  46. package/elasticsearch/indices/index.js +16 -0
  47. package/elasticsearch/indices/index.js.map +1 -0
  48. package/elasticsearch/indices/japanese.d.ts +2 -0
  49. package/elasticsearch/indices/japanese.js +16 -0
  50. package/elasticsearch/indices/japanese.js.map +1 -0
  51. package/index.d.ts +2 -0
  52. package/index.js +233 -0
  53. package/index.js.map +1 -0
  54. package/operations/blockCategory/dataLoader.d.ts +16 -0
  55. package/operations/blockCategory/dataLoader.js +79 -0
  56. package/operations/blockCategory/dataLoader.js.map +1 -0
  57. package/operations/blockCategory/fields.d.ts +2 -0
  58. package/operations/blockCategory/fields.js +28 -0
  59. package/operations/blockCategory/fields.js.map +1 -0
  60. package/operations/blockCategory/index.d.ts +8 -0
  61. package/operations/blockCategory/index.js +208 -0
  62. package/operations/blockCategory/index.js.map +1 -0
  63. package/operations/blockCategory/keys.d.ts +9 -0
  64. package/operations/blockCategory/keys.js +25 -0
  65. package/operations/blockCategory/keys.js.map +1 -0
  66. package/operations/category/dataLoader.d.ts +16 -0
  67. package/operations/category/dataLoader.js +79 -0
  68. package/operations/category/dataLoader.js.map +1 -0
  69. package/operations/category/fields.d.ts +2 -0
  70. package/operations/category/fields.js +28 -0
  71. package/operations/category/fields.js.map +1 -0
  72. package/operations/category/index.d.ts +8 -0
  73. package/operations/category/index.js +208 -0
  74. package/operations/category/index.js.map +1 -0
  75. package/operations/category/keys.d.ts +9 -0
  76. package/operations/category/keys.js +25 -0
  77. package/operations/category/keys.js.map +1 -0
  78. package/operations/menu/fields.d.ts +2 -0
  79. package/operations/menu/fields.js +27 -0
  80. package/operations/menu/fields.js.map +1 -0
  81. package/operations/menu/index.d.ts +8 -0
  82. package/operations/menu/index.js +217 -0
  83. package/operations/menu/index.js.map +1 -0
  84. package/operations/pageBlock/dataLoader.d.ts +16 -0
  85. package/operations/pageBlock/dataLoader.js +79 -0
  86. package/operations/pageBlock/dataLoader.js.map +1 -0
  87. package/operations/pageBlock/fields.d.ts +2 -0
  88. package/operations/pageBlock/fields.js +25 -0
  89. package/operations/pageBlock/fields.js.map +1 -0
  90. package/operations/pageBlock/index.d.ts +8 -0
  91. package/operations/pageBlock/index.js +208 -0
  92. package/operations/pageBlock/index.js.map +1 -0
  93. package/operations/pageBlock/keys.d.ts +9 -0
  94. package/operations/pageBlock/keys.js +25 -0
  95. package/operations/pageBlock/keys.js.map +1 -0
  96. package/operations/pageElement/fields.d.ts +2 -0
  97. package/operations/pageElement/fields.js +19 -0
  98. package/operations/pageElement/fields.js.map +1 -0
  99. package/operations/pageElement/index.d.ts +8 -0
  100. package/operations/pageElement/index.js +207 -0
  101. package/operations/pageElement/index.js.map +1 -0
  102. package/operations/pages/elasticsearchQueryBody.d.ts +12 -0
  103. package/operations/pages/elasticsearchQueryBody.js +237 -0
  104. package/operations/pages/elasticsearchQueryBody.js.map +1 -0
  105. package/operations/pages/fields.d.ts +4 -0
  106. package/operations/pages/fields.js +54 -0
  107. package/operations/pages/fields.js.map +1 -0
  108. package/operations/pages/helpers.d.ts +35 -0
  109. package/operations/pages/helpers.js +83 -0
  110. package/operations/pages/helpers.js.map +1 -0
  111. package/operations/pages/index.d.ts +11 -0
  112. package/operations/pages/index.js +829 -0
  113. package/operations/pages/index.js.map +1 -0
  114. package/operations/pages/keys.d.ts +24 -0
  115. package/operations/pages/keys.js +79 -0
  116. package/operations/pages/keys.js.map +1 -0
  117. package/operations/settings/index.d.ts +6 -0
  118. package/operations/settings/index.js +178 -0
  119. package/operations/settings/index.js.map +1 -0
  120. package/operations/system/index.d.ts +6 -0
  121. package/operations/system/index.js +104 -0
  122. package/operations/system/index.js.map +1 -0
  123. package/package.json +65 -0
  124. package/plugins/definitions/BlockCategoryDynamoDbElasticFieldPlugin.d.ts +4 -0
  125. package/plugins/definitions/BlockCategoryDynamoDbElasticFieldPlugin.js +17 -0
  126. package/plugins/definitions/BlockCategoryDynamoDbElasticFieldPlugin.js.map +1 -0
  127. package/plugins/definitions/CategoryDynamoDbElasticFieldPlugin.d.ts +4 -0
  128. package/plugins/definitions/CategoryDynamoDbElasticFieldPlugin.js +17 -0
  129. package/plugins/definitions/CategoryDynamoDbElasticFieldPlugin.js.map +1 -0
  130. package/plugins/definitions/IndexPageDataPlugin.d.ts +17 -0
  131. package/plugins/definitions/IndexPageDataPlugin.js +32 -0
  132. package/plugins/definitions/IndexPageDataPlugin.js.map +1 -0
  133. package/plugins/definitions/MenuDynamoDbElasticFieldPlugin.d.ts +4 -0
  134. package/plugins/definitions/MenuDynamoDbElasticFieldPlugin.js +17 -0
  135. package/plugins/definitions/MenuDynamoDbElasticFieldPlugin.js.map +1 -0
  136. package/plugins/definitions/PageBlockDynamoDbFieldPlugin.d.ts +4 -0
  137. package/plugins/definitions/PageBlockDynamoDbFieldPlugin.js +17 -0
  138. package/plugins/definitions/PageBlockDynamoDbFieldPlugin.js.map +1 -0
  139. package/plugins/definitions/PageDynamoDbElasticsearchFieldPlugin.d.ts +4 -0
  140. package/plugins/definitions/PageDynamoDbElasticsearchFieldPlugin.js +17 -0
  141. package/plugins/definitions/PageDynamoDbElasticsearchFieldPlugin.js.map +1 -0
  142. package/plugins/definitions/PageElasticsearchBodyModifierPlugin.d.ts +4 -0
  143. package/plugins/definitions/PageElasticsearchBodyModifierPlugin.js +17 -0
  144. package/plugins/definitions/PageElasticsearchBodyModifierPlugin.js.map +1 -0
  145. package/plugins/definitions/PageElasticsearchFieldPlugin.d.ts +4 -0
  146. package/plugins/definitions/PageElasticsearchFieldPlugin.js +17 -0
  147. package/plugins/definitions/PageElasticsearchFieldPlugin.js.map +1 -0
  148. package/plugins/definitions/PageElasticsearchIndexPlugin.d.ts +4 -0
  149. package/plugins/definitions/PageElasticsearchIndexPlugin.js +17 -0
  150. package/plugins/definitions/PageElasticsearchIndexPlugin.js.map +1 -0
  151. package/plugins/definitions/PageElasticsearchQueryModifierPlugin.d.ts +4 -0
  152. package/plugins/definitions/PageElasticsearchQueryModifierPlugin.js +17 -0
  153. package/plugins/definitions/PageElasticsearchQueryModifierPlugin.js.map +1 -0
  154. package/plugins/definitions/PageElasticsearchSortModifierPlugin.d.ts +4 -0
  155. package/plugins/definitions/PageElasticsearchSortModifierPlugin.js +17 -0
  156. package/plugins/definitions/PageElasticsearchSortModifierPlugin.js.map +1 -0
  157. package/plugins/definitions/PageElementDynamoDbElasticFieldPlugin.d.ts +4 -0
  158. package/plugins/definitions/PageElementDynamoDbElasticFieldPlugin.js +17 -0
  159. package/plugins/definitions/PageElementDynamoDbElasticFieldPlugin.js.map +1 -0
  160. package/plugins/definitions/SearchLatestPagesPlugin.d.ts +4 -0
  161. package/plugins/definitions/SearchLatestPagesPlugin.js +17 -0
  162. package/plugins/definitions/SearchLatestPagesPlugin.js.map +1 -0
  163. package/plugins/definitions/SearchPagesPlugin.d.ts +23 -0
  164. package/plugins/definitions/SearchPagesPlugin.js +39 -0
  165. package/plugins/definitions/SearchPagesPlugin.js.map +1 -0
  166. package/plugins/definitions/SearchPublishedPagesPlugin.d.ts +4 -0
  167. package/plugins/definitions/SearchPublishedPagesPlugin.js +17 -0
  168. package/plugins/definitions/SearchPublishedPagesPlugin.js.map +1 -0
  169. package/types.d.ts +39 -0
  170. package/types.js +20 -0
  171. package/types.js.map +1 -0
  172. package/upgrades/index.d.ts +2 -0
  173. package/upgrades/index.js +14 -0
  174. package/upgrades/index.js.map +1 -0
  175. package/upgrades/v5.15.0/category.d.ts +2 -0
  176. package/upgrades/v5.15.0/category.js +73 -0
  177. package/upgrades/v5.15.0/category.js.map +1 -0
  178. package/upgrades/v5.15.0/index.d.ts +4 -0
  179. package/upgrades/v5.15.0/index.js +52 -0
  180. package/upgrades/v5.15.0/index.js.map +1 -0
  181. package/upgrades/v5.15.0/menu.d.ts +2 -0
  182. package/upgrades/v5.15.0/menu.js +73 -0
  183. package/upgrades/v5.15.0/menu.js.map +1 -0
  184. package/upgrades/v5.15.0/pageElement.d.ts +2 -0
  185. package/upgrades/v5.15.0/pageElement.js +73 -0
  186. package/upgrades/v5.15.0/pageElement.js.map +1 -0
@@ -0,0 +1,829 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.createPageStorageOperations = void 0;
9
+
10
+ var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
11
+
12
+ var _omit = _interopRequireDefault(require("lodash/omit"));
13
+
14
+ var _error = _interopRequireDefault(require("@webiny/error"));
15
+
16
+ var _cleanup = require("@webiny/db-dynamodb/utils/cleanup");
17
+
18
+ var _configurations = require("../../configurations");
19
+
20
+ var _apiElasticsearch = require("@webiny/api-elasticsearch");
21
+
22
+ var _elasticsearchQueryBody = require("./elasticsearchQueryBody");
23
+
24
+ var _SearchLatestPagesPlugin = require("../../plugins/definitions/SearchLatestPagesPlugin");
25
+
26
+ var _SearchPublishedPagesPlugin = require("../../plugins/definitions/SearchPublishedPagesPlugin");
27
+
28
+ var _query = require("@webiny/db-dynamodb/utils/query");
29
+
30
+ var _batchWrite = require("@webiny/db-dynamodb/utils/batchWrite");
31
+
32
+ var _helpers = require("./helpers");
33
+
34
+ var _keys = require("./keys");
35
+
36
+ var _sort = require("@webiny/db-dynamodb/utils/sort");
37
+
38
+ var _PageDynamoDbElasticsearchFieldPlugin = require("../../plugins/definitions/PageDynamoDbElasticsearchFieldPlugin");
39
+
40
+ /**
41
+ * This function removes attributes that were once present in the Page record, which we no longer need.
42
+ */
43
+ function removePageAttributes(item) {
44
+ return (0, _omit.default)(item, ["home", "notFound", "visibility"]);
45
+ }
46
+
47
+ const createPageStorageOperations = params => {
48
+ const {
49
+ entity,
50
+ esEntity,
51
+ elasticsearch,
52
+ plugins
53
+ } = params;
54
+
55
+ const create = async params => {
56
+ const {
57
+ page
58
+ } = params;
59
+ const versionKeys = {
60
+ PK: (0, _keys.createPartitionKey)(page),
61
+ SK: (0, _keys.createSortKey)(page)
62
+ };
63
+ const latestKeys = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, versionKeys), {}, {
64
+ SK: (0, _keys.createLatestSortKey)()
65
+ });
66
+ const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, page), versionKeys), {}, {
67
+ TYPE: (0, _keys.createBasicType)()
68
+ })), entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, page), latestKeys), {}, {
69
+ TYPE: (0, _keys.createLatestType)()
70
+ }))];
71
+ const esData = (0, _helpers.getESLatestPageData)(plugins, page);
72
+
73
+ try {
74
+ await (0, _batchWrite.batchWriteAll)({
75
+ table: entity.table,
76
+ items: items
77
+ });
78
+ await esEntity.put((0, _objectSpread2.default)({
79
+ index: _configurations.configurations.es(page).index,
80
+ data: esData
81
+ }, latestKeys));
82
+ return page;
83
+ } catch (ex) {
84
+ throw new _error.default(ex.message || "Could not create new page.", ex.code || "CREATE_PAGE_ERROR", {
85
+ versionKeys,
86
+ latestKeys,
87
+ page
88
+ });
89
+ }
90
+ };
91
+
92
+ const createFrom = async params => {
93
+ const {
94
+ page,
95
+ latestPage,
96
+ original
97
+ } = params;
98
+ const versionKeys = {
99
+ PK: (0, _keys.createPartitionKey)(page),
100
+ SK: (0, _keys.createSortKey)(page)
101
+ };
102
+ const latestKeys = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, versionKeys), {}, {
103
+ SK: (0, _keys.createLatestSortKey)()
104
+ });
105
+ const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, page), {}, {
106
+ TYPE: (0, _keys.createBasicType)()
107
+ }, versionKeys)), entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, page), {}, {
108
+ TYPE: (0, _keys.createLatestType)()
109
+ }, latestKeys))];
110
+ const esData = (0, _helpers.getESLatestPageData)(plugins, page);
111
+
112
+ try {
113
+ await (0, _batchWrite.batchWriteAll)({
114
+ table: entity.table,
115
+ items
116
+ });
117
+ await esEntity.put((0, _objectSpread2.default)({
118
+ index: _configurations.configurations.es(page).index,
119
+ data: esData
120
+ }, latestKeys));
121
+ return page;
122
+ } catch (ex) {
123
+ throw new _error.default(ex.message || "Could not create new page from existing page.", ex.code || "CREATE_PAGE_FROM_ERROR", {
124
+ versionKeys,
125
+ latestKeys,
126
+ latestPage,
127
+ original,
128
+ page
129
+ });
130
+ }
131
+ };
132
+
133
+ const update = async params => {
134
+ const {
135
+ original,
136
+ page
137
+ } = params;
138
+ const keys = {
139
+ PK: (0, _keys.createPartitionKey)(page),
140
+ SK: (0, _keys.createSortKey)(page)
141
+ };
142
+ const latestKeys = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, keys), {}, {
143
+ SK: (0, _keys.createLatestSortKey)()
144
+ });
145
+ const latestPageResult = await entity.get(latestKeys);
146
+ const latestPage = (0, _cleanup.cleanupItem)(entity, latestPageResult ? latestPageResult.Item : null);
147
+ const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, page), {}, {
148
+ TYPE: (0, _keys.createBasicType)()
149
+ }, keys))];
150
+ const esData = (0, _helpers.getESLatestPageData)(plugins, page);
151
+
152
+ if (latestPage && latestPage.id === page.id) {
153
+ /**
154
+ * We also update the regular record.
155
+ */
156
+ items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, page), {}, {
157
+ TYPE: (0, _keys.createLatestType)()
158
+ }, latestKeys)));
159
+ }
160
+ /**
161
+ * Unfortunately we cannot push regular and es record in the batch write because they are two separate tables.
162
+ */
163
+
164
+
165
+ try {
166
+ await (0, _batchWrite.batchWriteAll)({
167
+ table: entity.table,
168
+ items
169
+ });
170
+ await esEntity.put((0, _objectSpread2.default)({
171
+ index: _configurations.configurations.es(page).index,
172
+ data: esData
173
+ }, latestKeys));
174
+ return page;
175
+ } catch (ex) {
176
+ throw new _error.default(ex.message || "Could not update existing page.", ex.code || "UPDATE_PAGE_ERROR", {
177
+ original,
178
+ page,
179
+ latestPage,
180
+ latestKeys,
181
+ keys
182
+ });
183
+ }
184
+ };
185
+ /**
186
+ * In case of delete, we must delete records:
187
+ * - revision
188
+ * - path if published
189
+ * Update:
190
+ * - latest
191
+ */
192
+
193
+
194
+ const deleteOne = async params => {
195
+ const {
196
+ page,
197
+ latestPage,
198
+ publishedPage
199
+ } = params;
200
+ const partitionKey = (0, _keys.createPartitionKey)(page);
201
+ const items = [entity.deleteBatch({
202
+ PK: partitionKey,
203
+ SK: (0, _keys.createSortKey)(page)
204
+ })];
205
+ const esItems = [];
206
+
207
+ if (publishedPage && publishedPage.id === page.id) {
208
+ items.push(entity.deleteBatch({
209
+ PK: partitionKey,
210
+ SK: (0, _keys.createPublishedSortKey)()
211
+ }));
212
+ items.push(entity.deleteBatch({
213
+ PK: (0, _keys.createPathPartitionKey)(page),
214
+ SK: (0, _keys.createPathSortKey)(page)
215
+ }));
216
+ esItems.push(esEntity.deleteBatch({
217
+ PK: partitionKey,
218
+ SK: (0, _keys.createPublishedSortKey)()
219
+ }));
220
+ }
221
+
222
+ let previousLatestPage = null;
223
+
224
+ if (latestPage && latestPage.id === page.id) {
225
+ const previousLatestRecord = await (0, _query.queryOne)({
226
+ entity,
227
+ partitionKey,
228
+ options: {
229
+ lt: (0, _keys.createSortKey)(latestPage),
230
+ reverse: true
231
+ }
232
+ });
233
+
234
+ if (previousLatestRecord) {
235
+ items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, previousLatestRecord), {}, {
236
+ TYPE: (0, _keys.createLatestType)(),
237
+ PK: partitionKey,
238
+ SK: (0, _keys.createLatestSortKey)()
239
+ })));
240
+ esItems.push(esEntity.putBatch({
241
+ PK: partitionKey,
242
+ SK: (0, _keys.createLatestSortKey)(),
243
+ index: _configurations.configurations.es(page).index,
244
+ data: (0, _helpers.getESLatestPageData)(plugins, previousLatestRecord)
245
+ }));
246
+ previousLatestPage = (0, _cleanup.cleanupItem)(entity, previousLatestRecord);
247
+ }
248
+ }
249
+
250
+ try {
251
+ await (0, _batchWrite.batchWriteAll)({
252
+ table: entity.table,
253
+ items
254
+ });
255
+ } catch (ex) {
256
+ throw new _error.default(ex.message || "Could not batch write all the page records.", ex.code || "BATCH_WRITE_RECORDS_ERROR");
257
+ }
258
+
259
+ if (esItems.length === 0) {
260
+ return [page, previousLatestPage];
261
+ }
262
+
263
+ try {
264
+ await (0, _batchWrite.batchWriteAll)({
265
+ table: entity.table,
266
+ items: esItems
267
+ });
268
+ } catch (ex) {
269
+ throw new _error.default(ex.message || "Could not batch write all the page Elasticsearch records.", ex.code || "BATCH_WRITE_ELASTICSEARCH_RECORDS_ERROR");
270
+ }
271
+
272
+ return [page, previousLatestPage];
273
+ };
274
+ /**
275
+ * In case of deleteAll, we must delete records:
276
+ * - latest
277
+ * - published
278
+ * - path if published
279
+ * - revision
280
+ * - es latest
281
+ * - es published
282
+ */
283
+
284
+
285
+ const deleteAll = async params => {
286
+ const {
287
+ page
288
+ } = params;
289
+ const partitionKey = (0, _keys.createPartitionKey)(page);
290
+ const queryAllParams = {
291
+ entity,
292
+ partitionKey,
293
+ options: {
294
+ gte: " "
295
+ }
296
+ };
297
+ let revisions;
298
+
299
+ try {
300
+ revisions = await (0, _query.queryAll)(queryAllParams);
301
+ } catch (ex) {
302
+ throw new _error.default(ex.message || "Could not query for all revisions of the page.", ex.code || "LIST_REVISIONS_ERROR", {
303
+ params: queryAllParams
304
+ });
305
+ }
306
+ /**
307
+ * We need to go through all possible entries and delete them.
308
+ * Also, delete the published entry path record.
309
+ */
310
+
311
+
312
+ const items = [];
313
+ let publishedPathEntryDeleted = false;
314
+
315
+ for (const revision of revisions) {
316
+ if (revision.status === "published" && !publishedPathEntryDeleted) {
317
+ publishedPathEntryDeleted = true;
318
+ items.push(entity.deleteBatch({
319
+ PK: (0, _keys.createPathPartitionKey)(page),
320
+ SK: revision.path
321
+ }));
322
+ }
323
+
324
+ items.push(entity.deleteBatch({
325
+ PK: revision.PK,
326
+ SK: revision.SK
327
+ }));
328
+ }
329
+
330
+ const esItems = [esEntity.deleteBatch({
331
+ PK: partitionKey,
332
+ SK: (0, _keys.createLatestSortKey)()
333
+ })];
334
+ /**
335
+ * Delete published record if it is published.
336
+ */
337
+
338
+ if (publishedPathEntryDeleted) {
339
+ esItems.push(esEntity.deleteBatch({
340
+ PK: partitionKey,
341
+ SK: (0, _keys.createPublishedSortKey)()
342
+ }));
343
+ }
344
+
345
+ try {
346
+ await (0, _batchWrite.batchWriteAll)({
347
+ table: entity.table,
348
+ items
349
+ });
350
+ } catch (ex) {
351
+ throw new _error.default(ex.message || "Could not delete all the page records.", ex.code || "DELETE_RECORDS_ERROR");
352
+ }
353
+
354
+ try {
355
+ await (0, _batchWrite.batchWriteAll)({
356
+ table: entity.table,
357
+ items: esItems
358
+ });
359
+ } catch (ex) {
360
+ throw new _error.default(ex.message || "Could not delete all the page Elasticsearch records.", ex.code || "DELETE_ELASTICSEARCH_RECORDS_ERROR");
361
+ }
362
+
363
+ return [page];
364
+ };
365
+
366
+ const publish = async params => {
367
+ const {
368
+ page,
369
+ latestPage,
370
+ publishedPage
371
+ } = params;
372
+ /**
373
+ * Update the given revision of the page.
374
+ */
375
+
376
+ const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, page), {}, {
377
+ TYPE: (0, _keys.createBasicType)(),
378
+ PK: (0, _keys.createPartitionKey)(page),
379
+ SK: (0, _keys.createSortKey)(page)
380
+ }))];
381
+ const esItems = [];
382
+ /**
383
+ * If we are publishing the latest revision, let's also update the latest revision entry's
384
+ * status in ES. Also, if we are publishing the latest revision, we need to update the latest
385
+ * page revision entry in ES.
386
+ */
387
+
388
+ if (latestPage.id === page.id) {
389
+ items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, page), {}, {
390
+ TYPE: (0, _keys.createLatestType)(),
391
+ PK: (0, _keys.createPartitionKey)(page),
392
+ SK: (0, _keys.createLatestSortKey)()
393
+ })));
394
+ esItems.push(esEntity.putBatch({
395
+ PK: (0, _keys.createPartitionKey)(page),
396
+ SK: (0, _keys.createLatestSortKey)(),
397
+ index: _configurations.configurations.es(page).index,
398
+ data: (0, _helpers.getESLatestPageData)(plugins, page)
399
+ }));
400
+ }
401
+ /**
402
+ * If we have already published revision of this page:
403
+ * - set existing published page revision to unpublished
404
+ * - remove old published path if paths are different
405
+ */
406
+
407
+
408
+ if (publishedPage) {
409
+ items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, publishedPage), {}, {
410
+ status: "unpublished",
411
+ PK: (0, _keys.createPartitionKey)(publishedPage),
412
+ SK: (0, _keys.createSortKey)(publishedPage)
413
+ })));
414
+ /**
415
+ * Remove old published path if required.
416
+ */
417
+
418
+ if (publishedPage.path !== page.path) {
419
+ items.push(entity.deleteBatch({
420
+ PK: (0, _keys.createPathPartitionKey)(page),
421
+ SK: publishedPage.path
422
+ }));
423
+ }
424
+ }
425
+
426
+ esItems.push(esEntity.putBatch({
427
+ PK: (0, _keys.createPartitionKey)(page),
428
+ SK: (0, _keys.createPublishedSortKey)(),
429
+ index: _configurations.configurations.es(page).index,
430
+ data: (0, _helpers.getESPublishedPageData)(plugins, page)
431
+ }));
432
+ /**
433
+ * Update or insert published path.
434
+ */
435
+
436
+ items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, page), {}, {
437
+ TYPE: (0, _keys.createPublishedPathType)(),
438
+ PK: (0, _keys.createPathPartitionKey)(page),
439
+ SK: (0, _keys.createPathSortKey)(page)
440
+ })));
441
+ /**
442
+ * Update or insert published page.
443
+ */
444
+
445
+ items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, page), {}, {
446
+ TYPE: (0, _keys.createPublishedType)(),
447
+ PK: (0, _keys.createPartitionKey)(page),
448
+ SK: (0, _keys.createPublishedSortKey)()
449
+ })));
450
+
451
+ try {
452
+ await (0, _batchWrite.batchWriteAll)({
453
+ table: entity.table,
454
+ items
455
+ });
456
+ } catch (ex) {
457
+ throw new _error.default(ex.message || "Could not update all the page records when publishing.", ex.code || "UPDATE_RECORDS_ERROR");
458
+ }
459
+ /**
460
+ * No point in continuing if there are no items in Elasticsearch data
461
+ */
462
+
463
+
464
+ if (esItems.length === 0) {
465
+ return page;
466
+ }
467
+
468
+ try {
469
+ await (0, _batchWrite.batchWriteAll)({
470
+ table: esEntity.table,
471
+ items: esItems
472
+ });
473
+ } catch (ex) {
474
+ throw new _error.default(ex.message || "Could not update all the page Elasticsearch records when publishing.", ex.code || "UPDATE_ELASTICSEARCH_RECORDS_ERROR");
475
+ }
476
+
477
+ return page;
478
+ };
479
+
480
+ const unpublish = async params => {
481
+ const {
482
+ page,
483
+ latestPage
484
+ } = params;
485
+ const items = [entity.deleteBatch({
486
+ PK: (0, _keys.createPartitionKey)(page),
487
+ SK: (0, _keys.createPublishedSortKey)()
488
+ }), entity.deleteBatch({
489
+ PK: (0, _keys.createPathPartitionKey)(page),
490
+ SK: (0, _keys.createPathSortKey)(page)
491
+ }), entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, page), {}, {
492
+ TYPE: (0, _keys.createBasicType)(),
493
+ PK: (0, _keys.createPartitionKey)(page),
494
+ SK: (0, _keys.createSortKey)(page)
495
+ }))];
496
+ const esItems = [];
497
+ /*
498
+ * If we are unpublishing the latest revision, let's also update the latest revision entry's status in ES.
499
+ */
500
+
501
+ if (latestPage.id === page.id) {
502
+ items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, page), {}, {
503
+ TYPE: (0, _keys.createLatestType)(),
504
+ PK: (0, _keys.createPartitionKey)(page),
505
+ SK: (0, _keys.createLatestSortKey)()
506
+ })));
507
+ esItems.push(esEntity.putBatch({
508
+ PK: (0, _keys.createPartitionKey)(page),
509
+ SK: (0, _keys.createLatestSortKey)(),
510
+ index: _configurations.configurations.es(page).index,
511
+ data: (0, _helpers.getESLatestPageData)(plugins, page)
512
+ }));
513
+ }
514
+
515
+ esItems.push(esEntity.deleteBatch({
516
+ PK: (0, _keys.createPartitionKey)(page),
517
+ SK: (0, _keys.createPublishedSortKey)()
518
+ }));
519
+
520
+ try {
521
+ await (0, _batchWrite.batchWriteAll)({
522
+ table: entity.table,
523
+ items
524
+ });
525
+ } catch (ex) {
526
+ throw new _error.default(ex.message || "Could not update all the page records when unpublishing.", ex.code || "UPDATE_RECORDS_ERROR");
527
+ }
528
+ /**
529
+ * No need to go further if no Elasticsearch items to be applied.
530
+ */
531
+
532
+
533
+ if (esItems.length === 0) {
534
+ return page;
535
+ }
536
+
537
+ try {
538
+ await (0, _batchWrite.batchWriteAll)({
539
+ table: esEntity.table,
540
+ items: esItems
541
+ });
542
+ } catch (ex) {
543
+ throw new _error.default(ex.message || "Could not update all the page Elasticsearch records when unpublishing.", ex.code || "UPDATE_ELASTICSEARCH_RECORDS_ERROR");
544
+ }
545
+
546
+ return page;
547
+ };
548
+
549
+ const get = async params => {
550
+ const {
551
+ where
552
+ } = params;
553
+ const {
554
+ pid,
555
+ id,
556
+ path,
557
+ published
558
+ } = where;
559
+ let {
560
+ version
561
+ } = where;
562
+ /**
563
+ * In case of having full ID and not having version we can take the version from the id.
564
+ */
565
+
566
+ if (id && id.includes("#") && !version) {
567
+ version = Number(id.split("#").pop());
568
+ }
569
+
570
+ let partitionKey = null;
571
+ let sortKey;
572
+
573
+ if (path) {
574
+ partitionKey = (0, _keys.createPathPartitionKey)(where);
575
+ sortKey = path;
576
+ } else if (published) {
577
+ sortKey = (0, _keys.createPublishedSortKey)();
578
+ } else if (version) {
579
+ sortKey = (0, _keys.createSortKey)({
580
+ version
581
+ });
582
+ } else {
583
+ sortKey = (0, _keys.createLatestSortKey)();
584
+ }
585
+ /**
586
+ * If partition key is still undefined, create one with id or pid
587
+ */
588
+
589
+
590
+ if (!partitionKey) {
591
+ partitionKey = (0, _keys.createPartitionKey)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, where), {}, {
592
+ id: pid || id
593
+ }));
594
+ }
595
+
596
+ const keys = {
597
+ PK: partitionKey,
598
+ SK: sortKey
599
+ };
600
+
601
+ try {
602
+ const result = await entity.get(keys);
603
+
604
+ if (!result || !result.Item) {
605
+ return null;
606
+ }
607
+
608
+ return (0, _cleanup.cleanupItem)(entity, result.Item);
609
+ } catch (ex) {
610
+ throw new _error.default(ex.message || "Could not load page by given params.", ex.code || "GET_PAGE_ERROR", {
611
+ where,
612
+ keys
613
+ });
614
+ }
615
+ };
616
+
617
+ const list = async params => {
618
+ /**
619
+ * We do not allow loading both published and latest at the same time.
620
+ * @see PageStorageOperationsListWhere
621
+ */
622
+ if (params.where.published && params.where.latest) {
623
+ throw new _error.default("Both published and latest cannot be defined at the same time.", "MALFORMED_WHERE_ERROR", {
624
+ where: params.where
625
+ });
626
+ }
627
+
628
+ const {
629
+ after: previousCursor = null,
630
+ limit: initialLimit
631
+ } = params;
632
+ const limit = (0, _apiElasticsearch.createLimit)(initialLimit, 50);
633
+ const body = (0, _elasticsearchQueryBody.createElasticsearchQueryBody)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), {}, {
634
+ where: (0, _objectSpread2.default)({}, params.where),
635
+ limit,
636
+ after: previousCursor,
637
+ plugins
638
+ }));
639
+ let searchPlugins = [];
640
+
641
+ if (params.where.published) {
642
+ searchPlugins = plugins.byType(_SearchPublishedPagesPlugin.SearchPublishedPagesPlugin.type);
643
+ } else if (params.where.latest) {
644
+ searchPlugins = plugins.byType(_SearchLatestPagesPlugin.SearchLatestPagesPlugin.type);
645
+ } else {
646
+ throw new _error.default("Only published or latest can be listed. Missing where condition.", "MALFORMED_WHERE_ERROR", {
647
+ where: params.where
648
+ });
649
+ }
650
+
651
+ for (const plugin of searchPlugins) {
652
+ /**
653
+ * Apply query modifications
654
+ */
655
+ plugin.modifyQuery({
656
+ query: body.query,
657
+ args: params,
658
+ plugins
659
+ });
660
+ /**
661
+ * Apply sort modifications
662
+ */
663
+
664
+ plugin.modifySort({
665
+ sort: body.sort,
666
+ args: params,
667
+ plugins
668
+ });
669
+ }
670
+
671
+ let response;
672
+
673
+ const esConfig = _configurations.configurations.es(params.where);
674
+
675
+ try {
676
+ response = await elasticsearch.search((0, _objectSpread2.default)((0, _objectSpread2.default)({}, esConfig), {}, {
677
+ body
678
+ }));
679
+ } catch (ex) {
680
+ /**
681
+ * Do not throw the error if Elasticsearch index does not exist.
682
+ * In some CRUDs we try to get list of pages but index was not created yet.
683
+ */
684
+ if (ex.message === "index_not_found_exception") {
685
+ return {
686
+ items: [],
687
+ meta: {
688
+ hasMoreItems: false,
689
+ totalCount: 0,
690
+ cursor: null
691
+ }
692
+ };
693
+ }
694
+
695
+ throw new _error.default(ex.message || "Could not load pages by given Elasticsearch body.", ex.code || "LIST_PAGES_ERROR", {
696
+ body
697
+ });
698
+ }
699
+
700
+ const {
701
+ hits,
702
+ total
703
+ } = response.body.hits;
704
+ const items = hits.map(item => item._source).map(item => removePageAttributes(item));
705
+ const hasMoreItems = items.length > limit;
706
+
707
+ if (hasMoreItems) {
708
+ /**
709
+ * Remove the last item from results, we don't want to include it.
710
+ */
711
+ items.pop();
712
+ }
713
+ /**
714
+ * Cursor is the `sort` value of the last item in the array.
715
+ * https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html#search-after
716
+ */
717
+
718
+
719
+ const cursor = items.length > 0 && hasMoreItems ? (0, _apiElasticsearch.encodeCursor)(hits[items.length - 1].sort) || null : null;
720
+ return {
721
+ items,
722
+ meta: {
723
+ hasMoreItems,
724
+ totalCount: total.value,
725
+ cursor
726
+ }
727
+ };
728
+ };
729
+
730
+ const listTags = async params => {
731
+ const {
732
+ where
733
+ } = params;
734
+ const tenant = where.tenant;
735
+ const body = (0, _elasticsearchQueryBody.createElasticsearchQueryBody)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), {}, {
736
+ where: {
737
+ locale: where.locale,
738
+ search: undefined,
739
+ tenant
740
+ },
741
+ sort: [],
742
+ after: null,
743
+ limit: 100000,
744
+ plugins
745
+ }));
746
+
747
+ const esConfig = _configurations.configurations.es(where);
748
+
749
+ try {
750
+ const response = await elasticsearch.search((0, _objectSpread2.default)((0, _objectSpread2.default)({}, esConfig), {}, {
751
+ body: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, body), {}, {
752
+ sort: undefined,
753
+ limit: undefined,
754
+ size: 0,
755
+ aggs: {
756
+ tags: {
757
+ terms: {
758
+ field: "tags.keyword",
759
+ include: `.*${where.search}.*`,
760
+ size: 10
761
+ }
762
+ }
763
+ }
764
+ })
765
+ }));
766
+ const tags = response.body.aggregations["tags"];
767
+
768
+ if (!tags || Array.isArray(tags.buckets) === false) {
769
+ return [];
770
+ }
771
+
772
+ return tags.buckets.map(item => item.key);
773
+ } catch (ex) {
774
+ throw new _error.default(ex.message || "Could not list tags by given parameters.", ex.code || "LIST_TAGS_ERROR", {
775
+ body,
776
+ where
777
+ });
778
+ }
779
+ };
780
+
781
+ const listRevisions = async params => {
782
+ const {
783
+ where,
784
+ sort
785
+ } = params;
786
+ const queryAllParams = {
787
+ entity,
788
+ partitionKey: (0, _keys.createPartitionKey)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, where), {}, {
789
+ id: where.pid
790
+ })),
791
+ options: {
792
+ beginsWith: "REV#",
793
+ reverse: false
794
+ }
795
+ };
796
+ let items = [];
797
+
798
+ try {
799
+ items = await (0, _query.queryAll)(queryAllParams);
800
+ } catch (ex) {
801
+ throw new _error.default(ex.message || "Could not load all the revisions from requested page.", ex.code || "LOAD_PAGE_REVISIONS_ERROR", {
802
+ params
803
+ });
804
+ }
805
+
806
+ const fields = plugins.byType(_PageDynamoDbElasticsearchFieldPlugin.PageDynamoDbElasticsearchFieldPlugin.type);
807
+ return (0, _sort.sortItems)({
808
+ items: items.map(item => removePageAttributes(item)),
809
+ fields,
810
+ sort
811
+ });
812
+ };
813
+
814
+ return {
815
+ create,
816
+ createFrom,
817
+ update,
818
+ delete: deleteOne,
819
+ deleteAll: deleteAll,
820
+ publish,
821
+ unpublish,
822
+ get,
823
+ list,
824
+ listRevisions,
825
+ listTags
826
+ };
827
+ };
828
+
829
+ exports.createPageStorageOperations = createPageStorageOperations;