@webiny/api-headless-cms-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 (228) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +35 -0
  3. package/configurations.d.ts +12 -0
  4. package/configurations.js +43 -0
  5. package/configurations.js.map +1 -0
  6. package/definitions/entry.d.ts +8 -0
  7. package/definitions/entry.js +90 -0
  8. package/definitions/entry.js.map +1 -0
  9. package/definitions/entryElasticsearch.d.ts +8 -0
  10. package/definitions/entryElasticsearch.js +42 -0
  11. package/definitions/entryElasticsearch.js.map +1 -0
  12. package/definitions/group.d.ts +8 -0
  13. package/definitions/group.js +70 -0
  14. package/definitions/group.js.map +1 -0
  15. package/definitions/model.d.ts +8 -0
  16. package/definitions/model.js +97 -0
  17. package/definitions/model.js.map +1 -0
  18. package/definitions/settings.d.ts +8 -0
  19. package/definitions/settings.js +58 -0
  20. package/definitions/settings.js.map +1 -0
  21. package/definitions/system.d.ts +8 -0
  22. package/definitions/system.js +46 -0
  23. package/definitions/system.js.map +1 -0
  24. package/definitions/table.d.ts +8 -0
  25. package/definitions/table.js +24 -0
  26. package/definitions/table.js.map +1 -0
  27. package/definitions/tableElasticsearch.d.ts +8 -0
  28. package/definitions/tableElasticsearch.js +24 -0
  29. package/definitions/tableElasticsearch.js.map +1 -0
  30. package/dynamoDb/index.d.ts +2 -0
  31. package/dynamoDb/index.js +16 -0
  32. package/dynamoDb/index.js.map +1 -0
  33. package/dynamoDb/storage/date.d.ts +3 -0
  34. package/dynamoDb/storage/date.js +99 -0
  35. package/dynamoDb/storage/date.js.map +1 -0
  36. package/dynamoDb/storage/longText.d.ts +10 -0
  37. package/dynamoDb/storage/longText.js +108 -0
  38. package/dynamoDb/storage/longText.js.map +1 -0
  39. package/dynamoDb/storage/richText.d.ts +7 -0
  40. package/dynamoDb/storage/richText.js +128 -0
  41. package/dynamoDb/storage/richText.js.map +1 -0
  42. package/elasticsearch/createElasticsearchIndex.d.ts +9 -0
  43. package/elasticsearch/createElasticsearchIndex.js +60 -0
  44. package/elasticsearch/createElasticsearchIndex.js.map +1 -0
  45. package/elasticsearch/deleteElasticsearchIndex.d.ts +8 -0
  46. package/elasticsearch/deleteElasticsearchIndex.js +43 -0
  47. package/elasticsearch/deleteElasticsearchIndex.js.map +1 -0
  48. package/elasticsearch/index.d.ts +2 -0
  49. package/elasticsearch/index.js +16 -0
  50. package/elasticsearch/index.js.map +1 -0
  51. package/elasticsearch/indexing/dateTimeIndexing.d.ts +3 -0
  52. package/elasticsearch/indexing/dateTimeIndexing.js +103 -0
  53. package/elasticsearch/indexing/dateTimeIndexing.js.map +1 -0
  54. package/elasticsearch/indexing/defaultFieldIndexing.d.ts +3 -0
  55. package/elasticsearch/indexing/defaultFieldIndexing.js +47 -0
  56. package/elasticsearch/indexing/defaultFieldIndexing.js.map +1 -0
  57. package/elasticsearch/indexing/index.d.ts +2 -0
  58. package/elasticsearch/indexing/index.js +24 -0
  59. package/elasticsearch/indexing/index.js.map +1 -0
  60. package/elasticsearch/indexing/longTextIndexing.d.ts +7 -0
  61. package/elasticsearch/indexing/longTextIndexing.js +52 -0
  62. package/elasticsearch/indexing/longTextIndexing.js.map +1 -0
  63. package/elasticsearch/indexing/numberIndexing.d.ts +3 -0
  64. package/elasticsearch/indexing/numberIndexing.js +48 -0
  65. package/elasticsearch/indexing/numberIndexing.js.map +1 -0
  66. package/elasticsearch/indexing/objectIndexing.d.ts +12 -0
  67. package/elasticsearch/indexing/objectIndexing.js +223 -0
  68. package/elasticsearch/indexing/objectIndexing.js.map +1 -0
  69. package/elasticsearch/indexing/richTextIndexing.d.ts +3 -0
  70. package/elasticsearch/indexing/richTextIndexing.js +34 -0
  71. package/elasticsearch/indexing/richTextIndexing.js.map +1 -0
  72. package/elasticsearch/indices/base.d.ts +2 -0
  73. package/elasticsearch/indices/base.js +15 -0
  74. package/elasticsearch/indices/base.js.map +1 -0
  75. package/elasticsearch/indices/index.d.ts +1 -0
  76. package/elasticsearch/indices/index.js +16 -0
  77. package/elasticsearch/indices/index.js.map +1 -0
  78. package/elasticsearch/indices/japanese.d.ts +2 -0
  79. package/elasticsearch/indices/japanese.js +16 -0
  80. package/elasticsearch/indices/japanese.js.map +1 -0
  81. package/elasticsearch/search/index.d.ts +3 -0
  82. package/elasticsearch/search/index.js +14 -0
  83. package/elasticsearch/search/index.js.map +1 -0
  84. package/elasticsearch/search/refSearch.d.ts +2 -0
  85. package/elasticsearch/search/refSearch.js +35 -0
  86. package/elasticsearch/search/refSearch.js.map +1 -0
  87. package/elasticsearch/search/timeSearch.d.ts +2 -0
  88. package/elasticsearch/search/timeSearch.js +33 -0
  89. package/elasticsearch/search/timeSearch.js.map +1 -0
  90. package/helpers/entryIndexHelpers.d.ts +18 -0
  91. package/helpers/entryIndexHelpers.js +204 -0
  92. package/helpers/entryIndexHelpers.js.map +1 -0
  93. package/helpers/index.d.ts +1 -0
  94. package/helpers/index.js +18 -0
  95. package/helpers/index.js.map +1 -0
  96. package/index.d.ts +3 -0
  97. package/index.js +275 -0
  98. package/index.js.map +1 -0
  99. package/operations/entry/dataLoaders.d.ts +51 -0
  100. package/operations/entry/dataLoaders.js +365 -0
  101. package/operations/entry/dataLoaders.js.map +1 -0
  102. package/operations/entry/elasticsearch/assignMinimumShouldMatchToQuery.d.ts +18 -0
  103. package/operations/entry/elasticsearch/assignMinimumShouldMatchToQuery.js +34 -0
  104. package/operations/entry/elasticsearch/assignMinimumShouldMatchToQuery.js.map +1 -0
  105. package/operations/entry/elasticsearch/body.d.ts +13 -0
  106. package/operations/entry/elasticsearch/body.js +162 -0
  107. package/operations/entry/elasticsearch/body.js.map +1 -0
  108. package/operations/entry/elasticsearch/fields.d.ts +9 -0
  109. package/operations/entry/elasticsearch/fields.js +248 -0
  110. package/operations/entry/elasticsearch/fields.js.map +1 -0
  111. package/operations/entry/elasticsearch/filtering/applyFiltering.d.ts +8 -0
  112. package/operations/entry/elasticsearch/filtering/applyFiltering.js +66 -0
  113. package/operations/entry/elasticsearch/filtering/applyFiltering.js.map +1 -0
  114. package/operations/entry/elasticsearch/filtering/exec.d.ts +17 -0
  115. package/operations/entry/elasticsearch/filtering/exec.js +215 -0
  116. package/operations/entry/elasticsearch/filtering/exec.js.map +1 -0
  117. package/operations/entry/elasticsearch/filtering/index.d.ts +1 -0
  118. package/operations/entry/elasticsearch/filtering/index.js +18 -0
  119. package/operations/entry/elasticsearch/filtering/index.js.map +1 -0
  120. package/operations/entry/elasticsearch/filtering/path.d.ts +15 -0
  121. package/operations/entry/elasticsearch/filtering/path.js +52 -0
  122. package/operations/entry/elasticsearch/filtering/path.js.map +1 -0
  123. package/operations/entry/elasticsearch/filtering/plugins/defaultFilterPlugin.d.ts +2 -0
  124. package/operations/entry/elasticsearch/filtering/plugins/defaultFilterPlugin.js +35 -0
  125. package/operations/entry/elasticsearch/filtering/plugins/defaultFilterPlugin.js.map +1 -0
  126. package/operations/entry/elasticsearch/filtering/plugins/index.d.ts +1 -0
  127. package/operations/entry/elasticsearch/filtering/plugins/index.js +18 -0
  128. package/operations/entry/elasticsearch/filtering/plugins/index.js.map +1 -0
  129. package/operations/entry/elasticsearch/filtering/plugins/objectFilterPlugin.d.ts +2 -0
  130. package/operations/entry/elasticsearch/filtering/plugins/objectFilterPlugin.js +78 -0
  131. package/operations/entry/elasticsearch/filtering/plugins/objectFilterPlugin.js.map +1 -0
  132. package/operations/entry/elasticsearch/filtering/plugins/refFilterPlugin.d.ts +2 -0
  133. package/operations/entry/elasticsearch/filtering/plugins/refFilterPlugin.js +58 -0
  134. package/operations/entry/elasticsearch/filtering/plugins/refFilterPlugin.js.map +1 -0
  135. package/operations/entry/elasticsearch/filtering/populated.d.ts +2 -0
  136. package/operations/entry/elasticsearch/filtering/populated.js +30 -0
  137. package/operations/entry/elasticsearch/filtering/populated.js.map +1 -0
  138. package/operations/entry/elasticsearch/filtering/values.d.ts +2 -0
  139. package/operations/entry/elasticsearch/filtering/values.js +28 -0
  140. package/operations/entry/elasticsearch/filtering/values.js.map +1 -0
  141. package/operations/entry/elasticsearch/fullTextSearch.d.ts +9 -0
  142. package/operations/entry/elasticsearch/fullTextSearch.js +34 -0
  143. package/operations/entry/elasticsearch/fullTextSearch.js.map +1 -0
  144. package/operations/entry/elasticsearch/fullTextSearchFields.d.ts +8 -0
  145. package/operations/entry/elasticsearch/fullTextSearchFields.js +52 -0
  146. package/operations/entry/elasticsearch/fullTextSearchFields.js.map +1 -0
  147. package/operations/entry/elasticsearch/initialQuery.d.ts +17 -0
  148. package/operations/entry/elasticsearch/initialQuery.js +117 -0
  149. package/operations/entry/elasticsearch/initialQuery.js.map +1 -0
  150. package/operations/entry/elasticsearch/keyword.d.ts +2 -0
  151. package/operations/entry/elasticsearch/keyword.js +38 -0
  152. package/operations/entry/elasticsearch/keyword.js.map +1 -0
  153. package/operations/entry/elasticsearch/plugins/bodyModifier.d.ts +9 -0
  154. package/operations/entry/elasticsearch/plugins/bodyModifier.js +19 -0
  155. package/operations/entry/elasticsearch/plugins/bodyModifier.js.map +1 -0
  156. package/operations/entry/elasticsearch/plugins/operator.d.ts +8 -0
  157. package/operations/entry/elasticsearch/plugins/operator.js +45 -0
  158. package/operations/entry/elasticsearch/plugins/operator.js.map +1 -0
  159. package/operations/entry/elasticsearch/plugins/queryModifier.d.ts +9 -0
  160. package/operations/entry/elasticsearch/plugins/queryModifier.js +19 -0
  161. package/operations/entry/elasticsearch/plugins/queryModifier.js.map +1 -0
  162. package/operations/entry/elasticsearch/plugins/search.d.ts +7 -0
  163. package/operations/entry/elasticsearch/plugins/search.js +30 -0
  164. package/operations/entry/elasticsearch/plugins/search.js.map +1 -0
  165. package/operations/entry/elasticsearch/plugins/sortModifier.d.ts +9 -0
  166. package/operations/entry/elasticsearch/plugins/sortModifier.js +19 -0
  167. package/operations/entry/elasticsearch/plugins/sortModifier.js.map +1 -0
  168. package/operations/entry/elasticsearch/sort.d.ts +12 -0
  169. package/operations/entry/elasticsearch/sort.js +92 -0
  170. package/operations/entry/elasticsearch/sort.js.map +1 -0
  171. package/operations/entry/elasticsearch/transformValueForSearch.d.ts +16 -0
  172. package/operations/entry/elasticsearch/transformValueForSearch.js +34 -0
  173. package/operations/entry/elasticsearch/transformValueForSearch.js.map +1 -0
  174. package/operations/entry/elasticsearch/types.d.ts +41 -0
  175. package/operations/entry/elasticsearch/types.js +5 -0
  176. package/operations/entry/elasticsearch/types.js.map +1 -0
  177. package/operations/entry/elasticsearchFields.d.ts +2 -0
  178. package/operations/entry/elasticsearchFields.js +38 -0
  179. package/operations/entry/elasticsearchFields.js.map +1 -0
  180. package/operations/entry/index.d.ts +11 -0
  181. package/operations/entry/index.js +1251 -0
  182. package/operations/entry/index.js.map +1 -0
  183. package/operations/entry/keys.d.ts +12 -0
  184. package/operations/entry/keys.js +40 -0
  185. package/operations/entry/keys.js.map +1 -0
  186. package/operations/entry/recordType.d.ts +3 -0
  187. package/operations/entry/recordType.js +24 -0
  188. package/operations/entry/recordType.js.map +1 -0
  189. package/operations/group/index.d.ts +8 -0
  190. package/operations/group/index.js +195 -0
  191. package/operations/group/index.js.map +1 -0
  192. package/operations/model/index.d.ts +8 -0
  193. package/operations/model/index.js +204 -0
  194. package/operations/model/index.js.map +1 -0
  195. package/operations/settings/index.d.ts +6 -0
  196. package/operations/settings/index.js +135 -0
  197. package/operations/settings/index.js.map +1 -0
  198. package/operations/system/index.d.ts +6 -0
  199. package/operations/system/index.js +99 -0
  200. package/operations/system/index.js.map +1 -0
  201. package/package.json +68 -0
  202. package/plugins/CmsEntryElasticsearchBodyModifierPlugin.d.ts +20 -0
  203. package/plugins/CmsEntryElasticsearchBodyModifierPlugin.js +24 -0
  204. package/plugins/CmsEntryElasticsearchBodyModifierPlugin.js.map +1 -0
  205. package/plugins/CmsEntryElasticsearchFieldPlugin.d.ts +12 -0
  206. package/plugins/CmsEntryElasticsearchFieldPlugin.js +24 -0
  207. package/plugins/CmsEntryElasticsearchFieldPlugin.js.map +1 -0
  208. package/plugins/CmsEntryElasticsearchIndexPlugin.d.ts +4 -0
  209. package/plugins/CmsEntryElasticsearchIndexPlugin.js +17 -0
  210. package/plugins/CmsEntryElasticsearchIndexPlugin.js.map +1 -0
  211. package/plugins/CmsEntryElasticsearchQueryBuilderValueSearchPlugin.d.ts +30 -0
  212. package/plugins/CmsEntryElasticsearchQueryBuilderValueSearchPlugin.js +43 -0
  213. package/plugins/CmsEntryElasticsearchQueryBuilderValueSearchPlugin.js.map +1 -0
  214. package/plugins/CmsEntryElasticsearchQueryModifierPlugin.d.ts +17 -0
  215. package/plugins/CmsEntryElasticsearchQueryModifierPlugin.js +24 -0
  216. package/plugins/CmsEntryElasticsearchQueryModifierPlugin.js.map +1 -0
  217. package/plugins/CmsEntryElasticsearchSortModifierPlugin.d.ts +17 -0
  218. package/plugins/CmsEntryElasticsearchSortModifierPlugin.js +24 -0
  219. package/plugins/CmsEntryElasticsearchSortModifierPlugin.js.map +1 -0
  220. package/plugins/CmsEntryFilterPlugin.d.ts +49 -0
  221. package/plugins/CmsEntryFilterPlugin.js +31 -0
  222. package/plugins/CmsEntryFilterPlugin.js.map +1 -0
  223. package/plugins/index.d.ts +6 -0
  224. package/plugins/index.js +83 -0
  225. package/plugins/index.js.map +1 -0
  226. package/types.d.ts +164 -0
  227. package/types.js +47 -0
  228. package/types.js.map +1 -0
@@ -0,0 +1,1251 @@
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.createEntriesStorageOperations = void 0;
9
+
10
+ var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
11
+
12
+ var _cloneDeep = _interopRequireDefault(require("lodash/cloneDeep"));
13
+
14
+ var _error = _interopRequireDefault(require("@webiny/error"));
15
+
16
+ var _types = require("@webiny/api-headless-cms/types");
17
+
18
+ var _helpers = require("../../helpers");
19
+
20
+ var _configurations = require("../../configurations");
21
+
22
+ var _batchWrite = require("@webiny/db-dynamodb/utils/batchWrite");
23
+
24
+ var _dataLoaders = require("./dataLoaders");
25
+
26
+ var _keys = require("./keys");
27
+
28
+ var _query = require("@webiny/db-dynamodb/utils/query");
29
+
30
+ var _apiElasticsearch = require("@webiny/api-elasticsearch");
31
+
32
+ var _get = require("@webiny/db-dynamodb/utils/get");
33
+
34
+ var _utils = require("@webiny/utils");
35
+
36
+ var _cleanup = require("@webiny/db-dynamodb/utils/cleanup");
37
+
38
+ var _body = require("./elasticsearch/body");
39
+
40
+ var _recordType = require("./recordType");
41
+
42
+ const getEntryData = input => {
43
+ const output = (0, _objectSpread2.default)({}, input);
44
+ delete output["PK"];
45
+ delete output["SK"];
46
+ delete output["published"];
47
+ delete output["latest"];
48
+ return output;
49
+ };
50
+
51
+ const getESLatestEntryData = async (plugins, entry) => {
52
+ return (0, _apiElasticsearch.compress)(plugins, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, getEntryData(entry)), {}, {
53
+ latest: true,
54
+ TYPE: (0, _recordType.createLatestRecordType)(),
55
+ __type: (0, _recordType.createLatestRecordType)()
56
+ }));
57
+ };
58
+
59
+ const getESPublishedEntryData = async (plugins, entry) => {
60
+ return (0, _apiElasticsearch.compress)(plugins, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, getEntryData(entry)), {}, {
61
+ published: true,
62
+ TYPE: (0, _recordType.createPublishedRecordType)(),
63
+ __type: (0, _recordType.createPublishedRecordType)()
64
+ }));
65
+ };
66
+
67
+ const convertEntryKeysToStorage = params => {
68
+ const {
69
+ model,
70
+ entry
71
+ } = params;
72
+ const values = model.convertValueKeyToStorage({
73
+ fields: model.fields,
74
+ values: entry.values
75
+ });
76
+ return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, entry), {}, {
77
+ values
78
+ });
79
+ };
80
+
81
+ const convertEntryKeysFromStorage = params => {
82
+ const {
83
+ model,
84
+ entry
85
+ } = params;
86
+ const values = model.convertValueKeyFromStorage({
87
+ fields: model.fields,
88
+ values: entry.values
89
+ });
90
+ return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, entry), {}, {
91
+ values
92
+ });
93
+ };
94
+
95
+ const createEntriesStorageOperations = params => {
96
+ const {
97
+ entity,
98
+ esEntity,
99
+ elasticsearch,
100
+ plugins
101
+ } = params;
102
+ const dataLoaders = new _dataLoaders.DataLoadersHandler({
103
+ entity
104
+ });
105
+
106
+ const create = async (model, params) => {
107
+ const {
108
+ entry: initialEntry,
109
+ storageEntry: initialStorageEntry
110
+ } = params;
111
+ const isPublished = initialEntry.status === "published";
112
+ const locked = isPublished ? true : initialEntry.locked;
113
+ const entry = convertEntryKeysToStorage({
114
+ model,
115
+ entry: initialEntry
116
+ });
117
+ const storageEntry = convertEntryKeysToStorage({
118
+ model,
119
+ entry: initialStorageEntry
120
+ });
121
+ const esEntry = (0, _helpers.prepareEntryToIndex)({
122
+ plugins,
123
+ model,
124
+ entry: (0, _cloneDeep.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, entry), {}, {
125
+ locked
126
+ })),
127
+ storageEntry: (0, _cloneDeep.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
128
+ locked
129
+ }))
130
+ });
131
+
132
+ const {
133
+ index: esIndex
134
+ } = _configurations.configurations.es({
135
+ model
136
+ });
137
+
138
+ const esLatestData = await getESLatestEntryData(plugins, esEntry);
139
+ const esPublishedData = await getESPublishedEntryData(plugins, esEntry);
140
+ const revisionKeys = {
141
+ PK: (0, _keys.createPartitionKey)({
142
+ id: entry.id,
143
+ locale: model.locale,
144
+ tenant: model.tenant
145
+ }),
146
+ SK: (0, _keys.createRevisionSortKey)(entry)
147
+ };
148
+ const latestKeys = {
149
+ PK: (0, _keys.createPartitionKey)({
150
+ id: entry.id,
151
+ locale: model.locale,
152
+ tenant: model.tenant
153
+ }),
154
+ SK: (0, _keys.createLatestSortKey)()
155
+ };
156
+ const publishedKeys = {
157
+ PK: (0, _keys.createPartitionKey)({
158
+ id: entry.id,
159
+ locale: model.locale,
160
+ tenant: model.tenant
161
+ }),
162
+ SK: (0, _keys.createPublishedSortKey)()
163
+ };
164
+ const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
165
+ locked
166
+ }, revisionKeys), {}, {
167
+ TYPE: (0, _recordType.createRecordType)()
168
+ })), entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
169
+ locked
170
+ }, latestKeys), {}, {
171
+ TYPE: (0, _recordType.createLatestRecordType)()
172
+ }))];
173
+
174
+ if (isPublished) {
175
+ items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
176
+ locked
177
+ }, publishedKeys), {}, {
178
+ TYPE: (0, _recordType.createPublishedRecordType)()
179
+ })));
180
+ }
181
+
182
+ try {
183
+ await (0, _batchWrite.batchWriteAll)({
184
+ table: entity.table,
185
+ items
186
+ });
187
+ dataLoaders.clearAll({
188
+ model
189
+ });
190
+ } catch (ex) {
191
+ throw new _error.default(ex.message || "Could not insert entry data into the DynamoDB table.", ex.code || "CREATE_ENTRY_ERROR", {
192
+ error: ex,
193
+ entry,
194
+ storageEntry
195
+ });
196
+ }
197
+
198
+ const esItems = [esEntity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, latestKeys), {}, {
199
+ index: esIndex,
200
+ data: esLatestData
201
+ }))];
202
+
203
+ if (isPublished) {
204
+ esItems.push(esEntity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, publishedKeys), {}, {
205
+ index: esIndex,
206
+ data: esPublishedData
207
+ })));
208
+ }
209
+
210
+ try {
211
+ await (0, _batchWrite.batchWriteAll)({
212
+ table: esEntity.table,
213
+ items: esItems
214
+ });
215
+ } catch (ex) {
216
+ throw new _error.default(ex.message || "Could not insert entry data into the Elasticsearch DynamoDB table.", ex.code || "CREATE_ES_ENTRY_ERROR", {
217
+ error: ex,
218
+ entry,
219
+ esEntry
220
+ });
221
+ }
222
+
223
+ return initialStorageEntry;
224
+ };
225
+
226
+ const createRevisionFrom = async (model, params) => {
227
+ const {
228
+ entry: initialEntry,
229
+ storageEntry: initialStorageEntry
230
+ } = params;
231
+ const entry = convertEntryKeysToStorage({
232
+ model,
233
+ entry: initialEntry
234
+ });
235
+ const storageEntry = convertEntryKeysToStorage({
236
+ model,
237
+ entry: initialStorageEntry
238
+ });
239
+ const revisionKeys = {
240
+ PK: (0, _keys.createPartitionKey)({
241
+ id: entry.id,
242
+ locale: model.locale,
243
+ tenant: model.tenant
244
+ }),
245
+ SK: (0, _keys.createRevisionSortKey)(entry)
246
+ };
247
+ const latestKeys = {
248
+ PK: (0, _keys.createPartitionKey)({
249
+ id: entry.id,
250
+ locale: model.locale,
251
+ tenant: model.tenant
252
+ }),
253
+ SK: (0, _keys.createLatestSortKey)()
254
+ };
255
+ const esEntry = (0, _helpers.prepareEntryToIndex)({
256
+ plugins,
257
+ model,
258
+ entry: (0, _cloneDeep.default)(entry),
259
+ storageEntry: (0, _cloneDeep.default)(storageEntry)
260
+ });
261
+ const esLatestData = await getESLatestEntryData(plugins, esEntry);
262
+ const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
263
+ TYPE: (0, _recordType.createRecordType)()
264
+ }, revisionKeys)), entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
265
+ TYPE: (0, _recordType.createLatestRecordType)()
266
+ }, latestKeys))];
267
+
268
+ const {
269
+ index
270
+ } = _configurations.configurations.es({
271
+ model
272
+ });
273
+
274
+ try {
275
+ await (0, _batchWrite.batchWriteAll)({
276
+ table: entity.table,
277
+ items
278
+ });
279
+ dataLoaders.clearAll({
280
+ model
281
+ });
282
+ } catch (ex) {
283
+ throw new _error.default(ex.message || "Could not create revision from given entry in the DynamoDB table.", ex.code || "CREATE_REVISION_ERROR", {
284
+ error: ex,
285
+ entry,
286
+ storageEntry
287
+ });
288
+ }
289
+ /**
290
+ * Update the "latest" entry item in the Elasticsearch
291
+ */
292
+
293
+
294
+ try {
295
+ await esEntity.put((0, _objectSpread2.default)((0, _objectSpread2.default)({}, latestKeys), {}, {
296
+ index,
297
+ data: esLatestData
298
+ }));
299
+ } catch (ex) {
300
+ throw new _error.default(ex.message || "Could not update latest entry in the DynamoDB Elasticsearch table.", ex.code || "CREATE_REVISION_ERROR", {
301
+ error: ex,
302
+ entry
303
+ });
304
+ }
305
+ /**
306
+ * There are no modifications on the entry created so just return the data.
307
+ */
308
+
309
+
310
+ return initialStorageEntry;
311
+ };
312
+
313
+ const update = async (model, params) => {
314
+ const {
315
+ entry: initialEntry,
316
+ storageEntry: initialStorageEntry
317
+ } = params;
318
+ const entry = convertEntryKeysToStorage({
319
+ model,
320
+ entry: initialEntry
321
+ });
322
+ const storageEntry = convertEntryKeysToStorage({
323
+ model,
324
+ entry: initialStorageEntry
325
+ });
326
+ const isPublished = entry.status === "published";
327
+ const locked = isPublished ? true : entry.locked;
328
+ const revisionKeys = {
329
+ PK: (0, _keys.createPartitionKey)({
330
+ id: entry.id,
331
+ locale: model.locale,
332
+ tenant: model.tenant
333
+ }),
334
+ SK: (0, _keys.createRevisionSortKey)(entry)
335
+ };
336
+ const latestKeys = {
337
+ PK: (0, _keys.createPartitionKey)({
338
+ id: entry.id,
339
+ locale: model.locale,
340
+ tenant: model.tenant
341
+ }),
342
+ SK: (0, _keys.createLatestSortKey)()
343
+ };
344
+ const publishedKeys = {
345
+ PK: (0, _keys.createPartitionKey)({
346
+ id: entry.id,
347
+ locale: model.locale,
348
+ tenant: model.tenant
349
+ }),
350
+ SK: (0, _keys.createPublishedSortKey)()
351
+ };
352
+ /**
353
+ * We need the latest entry to check if it needs to be updated.
354
+ */
355
+
356
+ const [latestStorageEntry] = await dataLoaders.getLatestRevisionByEntryId({
357
+ model,
358
+ ids: [entry.id]
359
+ });
360
+ const [publishedStorageEntry] = await dataLoaders.getPublishedRevisionByEntryId({
361
+ model,
362
+ ids: [entry.id]
363
+ });
364
+ const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
365
+ locked
366
+ }, revisionKeys), {}, {
367
+ TYPE: (0, _recordType.createRecordType)()
368
+ }))];
369
+
370
+ if (isPublished) {
371
+ items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
372
+ locked
373
+ }, publishedKeys), {}, {
374
+ TYPE: (0, _recordType.createPublishedRecordType)()
375
+ })));
376
+ }
377
+
378
+ const esItems = [];
379
+
380
+ const {
381
+ index: esIndex
382
+ } = _configurations.configurations.es({
383
+ model
384
+ });
385
+ /**
386
+ * Variable for the elasticsearch entry so we do not convert it more than once
387
+ */
388
+
389
+
390
+ let esEntry = undefined;
391
+ /**
392
+ * If the latest entry is the one being updated, we need to create a new latest entry records.
393
+ */
394
+
395
+ let elasticsearchLatestData = null;
396
+
397
+ if ((latestStorageEntry === null || latestStorageEntry === void 0 ? void 0 : latestStorageEntry.id) === entry.id) {
398
+ /**
399
+ * First we update the regular DynamoDB table
400
+ */
401
+ items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), latestKeys), {}, {
402
+ TYPE: (0, _keys.createLatestSortKey)()
403
+ })));
404
+ /**
405
+ * And then update the Elasticsearch table to propagate changes to the Elasticsearch
406
+ */
407
+
408
+ esEntry = (0, _helpers.prepareEntryToIndex)({
409
+ plugins,
410
+ model,
411
+ entry: (0, _cloneDeep.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, entry), {}, {
412
+ locked
413
+ })),
414
+ storageEntry: (0, _cloneDeep.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
415
+ locked
416
+ }))
417
+ });
418
+ elasticsearchLatestData = await getESLatestEntryData(plugins, esEntry);
419
+ esItems.push(esEntity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, latestKeys), {}, {
420
+ index: esIndex,
421
+ data: elasticsearchLatestData
422
+ })));
423
+ }
424
+
425
+ let elasticsearchPublishedData = null;
426
+
427
+ if (isPublished && (publishedStorageEntry === null || publishedStorageEntry === void 0 ? void 0 : publishedStorageEntry.id) === entry.id) {
428
+ if (!elasticsearchLatestData) {
429
+ /**
430
+ * And then update the Elasticsearch table to propagate changes to the Elasticsearch
431
+ */
432
+ if (!esEntry) {
433
+ esEntry = (0, _helpers.prepareEntryToIndex)({
434
+ plugins,
435
+ model,
436
+ entry: (0, _cloneDeep.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, entry), {}, {
437
+ locked
438
+ })),
439
+ storageEntry: (0, _cloneDeep.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
440
+ locked
441
+ }))
442
+ });
443
+ }
444
+
445
+ elasticsearchPublishedData = await getESPublishedEntryData(plugins, esEntry);
446
+ } else {
447
+ elasticsearchPublishedData = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, elasticsearchLatestData), {}, {
448
+ published: true,
449
+ TYPE: (0, _recordType.createPublishedRecordType)(),
450
+ __type: (0, _recordType.createPublishedRecordType)()
451
+ });
452
+ delete elasticsearchPublishedData.latest;
453
+ }
454
+
455
+ esItems.push(esEntity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, publishedKeys), {}, {
456
+ index: esIndex,
457
+ data: elasticsearchPublishedData
458
+ })));
459
+ }
460
+
461
+ try {
462
+ await (0, _batchWrite.batchWriteAll)({
463
+ table: entity.table,
464
+ items
465
+ });
466
+ dataLoaders.clearAll({
467
+ model
468
+ });
469
+ } catch (ex) {
470
+ throw new _error.default(ex.message || "Could not update entry DynamoDB records.", ex.code || "UPDATE_ENTRY_ERROR", {
471
+ error: ex,
472
+ entry,
473
+ storageEntry
474
+ });
475
+ }
476
+
477
+ if (esItems.length === 0) {
478
+ return initialStorageEntry;
479
+ }
480
+
481
+ try {
482
+ await (0, _batchWrite.batchWriteAll)({
483
+ table: esEntity.table,
484
+ items: esItems
485
+ });
486
+ } catch (ex) {
487
+ throw new _error.default(ex.message || "Could not update entry DynamoDB Elasticsearch records.", ex.code || "UPDATE_ES_ENTRY_ERROR", {
488
+ error: ex,
489
+ entry
490
+ });
491
+ }
492
+
493
+ return initialStorageEntry;
494
+ };
495
+
496
+ const deleteEntry = async (model, params) => {
497
+ const {
498
+ entry
499
+ } = params;
500
+ const partitionKey = (0, _keys.createPartitionKey)({
501
+ id: entry.id,
502
+ locale: model.locale,
503
+ tenant: model.tenant
504
+ });
505
+ const items = await (0, _query.queryAll)({
506
+ entity,
507
+ partitionKey,
508
+ options: {
509
+ gte: " "
510
+ }
511
+ });
512
+ const esItems = await (0, _query.queryAll)({
513
+ entity: esEntity,
514
+ partitionKey,
515
+ options: {
516
+ gte: " "
517
+ }
518
+ });
519
+ const deleteItems = items.map(item => {
520
+ return entity.deleteBatch({
521
+ PK: item.PK,
522
+ SK: item.SK
523
+ });
524
+ });
525
+ const deleteEsItems = esItems.map(item => {
526
+ return esEntity.deleteBatch({
527
+ PK: item.PK,
528
+ SK: item.SK
529
+ });
530
+ });
531
+
532
+ try {
533
+ await (0, _batchWrite.batchWriteAll)({
534
+ table: entity.table,
535
+ items: deleteItems
536
+ });
537
+ dataLoaders.clearAll({
538
+ model
539
+ });
540
+ } catch (ex) {
541
+ throw new _error.default(ex.message || "Could not delete entry records from DynamoDB table.", ex.code || "DELETE_ENTRY_ERROR", {
542
+ error: ex,
543
+ entry
544
+ });
545
+ }
546
+
547
+ try {
548
+ await (0, _batchWrite.batchWriteAll)({
549
+ table: esEntity.table,
550
+ items: deleteEsItems
551
+ });
552
+ } catch (ex) {
553
+ throw new _error.default(ex.message || "Could not delete entry records from DynamoDB Elasticsearch table.", ex.code || "DELETE_ENTRY_ERROR", {
554
+ error: ex,
555
+ entry
556
+ });
557
+ }
558
+ };
559
+
560
+ const deleteRevision = async (model, params) => {
561
+ const {
562
+ entry,
563
+ latestEntry,
564
+ latestStorageEntry
565
+ } = params;
566
+ const partitionKey = (0, _keys.createPartitionKey)({
567
+ id: entry.id,
568
+ locale: model.locale,
569
+ tenant: model.tenant
570
+ });
571
+
572
+ const {
573
+ index
574
+ } = _configurations.configurations.es({
575
+ model
576
+ });
577
+ /**
578
+ * We need published entry to delete it if necessary.
579
+ */
580
+
581
+
582
+ const [publishedStorageEntry] = await dataLoaders.getPublishedRevisionByEntryId({
583
+ model,
584
+ ids: [entry.id]
585
+ });
586
+ /**
587
+ * We need to delete all existing records of the given entry revision.
588
+ */
589
+
590
+ const items = [
591
+ /**
592
+ * Delete records of given entry revision.
593
+ */
594
+ entity.deleteBatch({
595
+ PK: partitionKey,
596
+ SK: (0, _keys.createRevisionSortKey)(entry)
597
+ })];
598
+ const esItems = [];
599
+ /**
600
+ * If revision we are deleting is the published one as well, we need to delete those records as well.
601
+ */
602
+
603
+ if ((publishedStorageEntry === null || publishedStorageEntry === void 0 ? void 0 : publishedStorageEntry.id) === entry.id) {
604
+ items.push(entity.deleteBatch({
605
+ PK: partitionKey,
606
+ SK: (0, _keys.createPublishedSortKey)()
607
+ }));
608
+ esItems.push(entity.deleteBatch({
609
+ PK: partitionKey,
610
+ SK: (0, _keys.createPublishedSortKey)()
611
+ }));
612
+ }
613
+
614
+ if (latestEntry && latestStorageEntry) {
615
+ const esEntry = (0, _helpers.prepareEntryToIndex)({
616
+ plugins,
617
+ model,
618
+ entry: (0, _cloneDeep.default)(latestEntry),
619
+ storageEntry: (0, _cloneDeep.default)(latestStorageEntry)
620
+ });
621
+ const esLatestData = await getESLatestEntryData(plugins, esEntry);
622
+ /**
623
+ * In the end we need to set the new latest entry
624
+ */
625
+
626
+ items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, latestStorageEntry), {}, {
627
+ PK: partitionKey,
628
+ SK: (0, _keys.createLatestSortKey)(),
629
+ TYPE: (0, _recordType.createLatestRecordType)()
630
+ })));
631
+ esItems.push(esEntity.putBatch({
632
+ PK: partitionKey,
633
+ SK: (0, _keys.createLatestSortKey)(),
634
+ index,
635
+ data: esLatestData
636
+ }));
637
+ }
638
+
639
+ try {
640
+ await (0, _batchWrite.batchWriteAll)({
641
+ table: entity.table,
642
+ items
643
+ });
644
+ dataLoaders.clearAll({
645
+ model
646
+ });
647
+ } catch (ex) {
648
+ throw new _error.default(ex.message || "Could not batch write entry records to DynamoDB table.", ex.code || "DELETE_REVISION_ERROR", {
649
+ error: ex,
650
+ entry,
651
+ latestEntry,
652
+ latestStorageEntry
653
+ });
654
+ }
655
+
656
+ if (esItems.length === 0) {
657
+ return;
658
+ }
659
+
660
+ try {
661
+ await (0, _batchWrite.batchWriteAll)({
662
+ table: esEntity.table,
663
+ items: esItems
664
+ });
665
+ } catch (ex) {
666
+ throw new _error.default(ex.message || "Could not batch write entry records to DynamoDB Elasticsearch table.", ex.code || "DELETE_REVISION_ERROR", {
667
+ error: ex,
668
+ entry,
669
+ latestEntry,
670
+ latestStorageEntry
671
+ });
672
+ }
673
+ };
674
+
675
+ const list = async (model, params) => {
676
+ var _response, _response$body;
677
+
678
+ const limit = (0, _apiElasticsearch.createLimit)(params.limit, 50);
679
+
680
+ const {
681
+ index
682
+ } = _configurations.configurations.es({
683
+ model
684
+ });
685
+
686
+ try {
687
+ const result = await elasticsearch.indices.exists({
688
+ index
689
+ });
690
+
691
+ if (!(result !== null && result !== void 0 && result.body)) {
692
+ return {
693
+ hasMoreItems: false,
694
+ totalCount: 0,
695
+ cursor: null,
696
+ items: []
697
+ };
698
+ }
699
+ } catch (ex) {
700
+ throw new _error.default("Could not determine if Elasticsearch index exists.", "ELASTICSEARCH_INDEX_CHECK_ERROR", {
701
+ error: ex,
702
+ index
703
+ });
704
+ }
705
+
706
+ const body = (0, _body.createElasticsearchBody)({
707
+ model,
708
+ params: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), {}, {
709
+ limit,
710
+ after: (0, _apiElasticsearch.decodeCursor)(params.after)
711
+ }),
712
+ plugins
713
+ });
714
+ let response;
715
+
716
+ try {
717
+ response = await elasticsearch.search({
718
+ index,
719
+ body
720
+ });
721
+ } catch (ex) {
722
+ throw new _error.default(ex.message, ex.code || "ELASTICSEARCH_ERROR", {
723
+ error: ex,
724
+ index,
725
+ body,
726
+ model
727
+ });
728
+ }
729
+
730
+ const {
731
+ hits,
732
+ total
733
+ } = ((_response = response) === null || _response === void 0 ? void 0 : (_response$body = _response.body) === null || _response$body === void 0 ? void 0 : _response$body.hits) || {};
734
+ const items = (0, _helpers.extractEntriesFromIndex)({
735
+ plugins,
736
+ model,
737
+ entries: hits.map(item => item._source)
738
+ }).map(item => {
739
+ return convertEntryKeysFromStorage({
740
+ model,
741
+ entry: item
742
+ });
743
+ });
744
+ const hasMoreItems = items.length > limit;
745
+
746
+ if (hasMoreItems) {
747
+ /**
748
+ * Remove the last item from results, we don't want to include it.
749
+ */
750
+ items.pop();
751
+ }
752
+ /**
753
+ * Cursor is the `sort` value of the last item in the array.
754
+ * https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html#search-after
755
+ */
756
+
757
+
758
+ const cursor = items.length > 0 ? (0, _apiElasticsearch.encodeCursor)(hits[items.length - 1].sort) || null : null;
759
+ return {
760
+ hasMoreItems,
761
+ totalCount: total.value,
762
+ cursor,
763
+ items
764
+ };
765
+ };
766
+
767
+ const get = async (model, params) => {
768
+ const {
769
+ items
770
+ } = await list(model, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), {}, {
771
+ limit: 1
772
+ }));
773
+ return items.shift() || null;
774
+ };
775
+
776
+ const publish = async (model, params) => {
777
+ const {
778
+ entry: initialEntry,
779
+ storageEntry: initialStorageEntry
780
+ } = params;
781
+ const entry = convertEntryKeysToStorage({
782
+ model,
783
+ entry: initialEntry
784
+ });
785
+ const storageEntry = convertEntryKeysToStorage({
786
+ model,
787
+ entry: initialStorageEntry
788
+ });
789
+ /**
790
+ * We need currently published entry to check if need to remove it.
791
+ */
792
+
793
+ const [publishedStorageEntry] = await dataLoaders.getPublishedRevisionByEntryId({
794
+ model,
795
+ ids: [entry.id]
796
+ });
797
+ const revisionKeys = {
798
+ PK: (0, _keys.createPartitionKey)({
799
+ id: entry.id,
800
+ locale: model.locale,
801
+ tenant: model.tenant
802
+ }),
803
+ SK: (0, _keys.createRevisionSortKey)(entry)
804
+ };
805
+ const latestKeys = {
806
+ PK: (0, _keys.createPartitionKey)({
807
+ id: entry.id,
808
+ locale: model.locale,
809
+ tenant: model.tenant
810
+ }),
811
+ SK: (0, _keys.createLatestSortKey)()
812
+ };
813
+ const publishedKeys = {
814
+ PK: (0, _keys.createPartitionKey)({
815
+ id: entry.id,
816
+ locale: model.locale,
817
+ tenant: model.tenant
818
+ }),
819
+ SK: (0, _keys.createPublishedSortKey)()
820
+ };
821
+ let latestEsEntry = null;
822
+
823
+ try {
824
+ latestEsEntry = await (0, _get.get)({
825
+ entity: esEntity,
826
+ keys: latestKeys
827
+ });
828
+ } catch (ex) {
829
+ throw new _error.default(ex.message || "Could not read Elasticsearch latest or published data.", ex.code || "PUBLISH_BATCH_READ", {
830
+ error: ex,
831
+ latestKeys: latestKeys,
832
+ publishedKeys: publishedKeys
833
+ });
834
+ }
835
+
836
+ const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), revisionKeys), {}, {
837
+ TYPE: (0, _recordType.createRecordType)()
838
+ }))];
839
+ const esItems = [];
840
+
841
+ const {
842
+ index
843
+ } = _configurations.configurations.es({
844
+ model
845
+ });
846
+
847
+ if (publishedStorageEntry && publishedStorageEntry.id !== entry.id) {
848
+ /**
849
+ * If there is a `published` entry already, we need to set it to `unpublished`. We need to
850
+ * execute two updates: update the previously published entry's status and the published entry record.
851
+ * DynamoDB does not support `batchUpdate` - so here we load the previously published
852
+ * entry's data to update its status within a batch operation. If, hopefully,
853
+ * they introduce a true update batch operation, remove this `read` call.
854
+ */
855
+ const [previouslyPublishedEntry] = await dataLoaders.getRevisionById({
856
+ model,
857
+ ids: [publishedStorageEntry.id]
858
+ });
859
+ items.push(
860
+ /**
861
+ * Update currently published entry (unpublish it)
862
+ */
863
+ entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, previouslyPublishedEntry), {}, {
864
+ status: _types.CONTENT_ENTRY_STATUS.UNPUBLISHED,
865
+ savedOn: entry.savedOn,
866
+ TYPE: (0, _recordType.createRecordType)(),
867
+ PK: (0, _keys.createPartitionKey)(publishedStorageEntry),
868
+ SK: (0, _keys.createRevisionSortKey)(publishedStorageEntry)
869
+ })));
870
+ }
871
+ /**
872
+ * Update the helper item in DB with the new published entry
873
+ */
874
+
875
+
876
+ items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), publishedKeys), {}, {
877
+ TYPE: (0, _recordType.createPublishedRecordType)()
878
+ })));
879
+ /**
880
+ * We need the latest entry to check if it needs to be updated as well in the Elasticsearch.
881
+ */
882
+
883
+ const [latestStorageEntry] = await dataLoaders.getLatestRevisionByEntryId({
884
+ model,
885
+ ids: [entry.id]
886
+ });
887
+
888
+ if ((latestStorageEntry === null || latestStorageEntry === void 0 ? void 0 : latestStorageEntry.id) === entry.id) {
889
+ items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), latestKeys)));
890
+ }
891
+ /**
892
+ * If we are publishing the latest revision, let's also update the latest revision's status in ES.
893
+ */
894
+
895
+
896
+ if (latestEsEntry && (latestStorageEntry === null || latestStorageEntry === void 0 ? void 0 : latestStorageEntry.id) === entry.id) {
897
+ /**
898
+ * Need to decompress the data from Elasticsearch DynamoDB table.
899
+ *
900
+ * No need to transform it for the storage because it was fetched directly from the Elasticsearch table, where it sits transformed.
901
+ */
902
+ const latestEsEntryDataDecompressed = await (0, _apiElasticsearch.decompress)(plugins, latestEsEntry.data);
903
+ esItems.push(esEntity.putBatch({
904
+ index,
905
+ PK: (0, _keys.createPartitionKey)(latestEsEntryDataDecompressed),
906
+ SK: (0, _keys.createLatestSortKey)(),
907
+ data: await getESLatestEntryData(plugins, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, latestEsEntryDataDecompressed), {}, {
908
+ status: _types.CONTENT_ENTRY_STATUS.PUBLISHED,
909
+ locked: true,
910
+ savedOn: entry.savedOn,
911
+ publishedOn: entry.publishedOn
912
+ }))
913
+ }));
914
+ }
915
+
916
+ const preparedEntryData = (0, _helpers.prepareEntryToIndex)({
917
+ plugins,
918
+ model,
919
+ entry: (0, _cloneDeep.default)(entry),
920
+ storageEntry: (0, _cloneDeep.default)(storageEntry)
921
+ });
922
+ /**
923
+ * Update the published revision entry in ES.
924
+ */
925
+
926
+ const esPublishedData = await getESPublishedEntryData(plugins, preparedEntryData);
927
+ esItems.push(esEntity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, publishedKeys), {}, {
928
+ index,
929
+ data: esPublishedData
930
+ })));
931
+ /**
932
+ * Finally, execute regular table batch.
933
+ */
934
+
935
+ try {
936
+ await (0, _batchWrite.batchWriteAll)({
937
+ table: entity.table,
938
+ items
939
+ });
940
+ dataLoaders.clearAll({
941
+ model
942
+ });
943
+ } catch (ex) {
944
+ throw new _error.default(ex.message || "Could not store publish entry records in DynamoDB table.", ex.code || "PUBLISH_ERROR", {
945
+ error: ex,
946
+ entry,
947
+ latestStorageEntry,
948
+ publishedStorageEntry
949
+ });
950
+ }
951
+ /**
952
+ * And Elasticsearch table batch.
953
+ */
954
+
955
+
956
+ try {
957
+ await (0, _batchWrite.batchWriteAll)({
958
+ table: esEntity.table,
959
+ items: esItems
960
+ });
961
+ } catch (ex) {
962
+ throw new _error.default(ex.message || "Could not store publish entry records in DynamoDB Elasticsearch table.", ex.code || "PUBLISH_ES_ERROR", {
963
+ error: ex,
964
+ entry,
965
+ latestStorageEntry,
966
+ publishedStorageEntry
967
+ });
968
+ }
969
+
970
+ return initialStorageEntry;
971
+ };
972
+
973
+ const unpublish = async (model, params) => {
974
+ const {
975
+ entry: initialEntry,
976
+ storageEntry: initialStorageEntry
977
+ } = params;
978
+ const entry = convertEntryKeysToStorage({
979
+ model,
980
+ entry: initialEntry
981
+ });
982
+ const storageEntry = convertEntryKeysToStorage({
983
+ model,
984
+ entry: initialStorageEntry
985
+ });
986
+ /**
987
+ * We need the latest entry to check if it needs to be updated.
988
+ */
989
+
990
+ const [latestStorageEntry] = await dataLoaders.getLatestRevisionByEntryId({
991
+ model,
992
+ ids: [entry.id]
993
+ });
994
+ const partitionKey = (0, _keys.createPartitionKey)({
995
+ id: entry.id,
996
+ locale: model.locale,
997
+ tenant: model.tenant
998
+ });
999
+ const items = [entity.deleteBatch({
1000
+ PK: partitionKey,
1001
+ SK: (0, _keys.createPublishedSortKey)()
1002
+ }), entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
1003
+ PK: partitionKey,
1004
+ SK: (0, _keys.createRevisionSortKey)(entry),
1005
+ TYPE: (0, _recordType.createRecordType)()
1006
+ }))];
1007
+ const esItems = [esEntity.deleteBatch({
1008
+ PK: partitionKey,
1009
+ SK: (0, _keys.createPublishedSortKey)()
1010
+ })];
1011
+ /**
1012
+ * If we are unpublishing the latest revision, let's also update the latest revision entry's status in ES.
1013
+ */
1014
+
1015
+ if ((latestStorageEntry === null || latestStorageEntry === void 0 ? void 0 : latestStorageEntry.id) === entry.id) {
1016
+ const {
1017
+ index
1018
+ } = _configurations.configurations.es({
1019
+ model
1020
+ });
1021
+
1022
+ const preparedEntryData = (0, _helpers.prepareEntryToIndex)({
1023
+ plugins,
1024
+ model,
1025
+ entry: (0, _cloneDeep.default)(entry),
1026
+ storageEntry: (0, _cloneDeep.default)(storageEntry)
1027
+ });
1028
+ const esLatestData = await getESLatestEntryData(plugins, preparedEntryData);
1029
+ esItems.push(esEntity.putBatch({
1030
+ PK: partitionKey,
1031
+ SK: (0, _keys.createLatestSortKey)(),
1032
+ index,
1033
+ data: esLatestData
1034
+ }));
1035
+ }
1036
+ /**
1037
+ * Finally, execute regular table batch.
1038
+ */
1039
+
1040
+
1041
+ try {
1042
+ await (0, _batchWrite.batchWriteAll)({
1043
+ table: entity.table,
1044
+ items
1045
+ });
1046
+ dataLoaders.clearAll({
1047
+ model
1048
+ });
1049
+ } catch (ex) {
1050
+ throw new _error.default(ex.message || "Could not store unpublished entry records in DynamoDB table.", ex.code || "UNPUBLISH_ERROR", {
1051
+ entry,
1052
+ storageEntry
1053
+ });
1054
+ }
1055
+ /**
1056
+ * And Elasticsearch table batch.
1057
+ */
1058
+
1059
+
1060
+ try {
1061
+ await (0, _batchWrite.batchWriteAll)({
1062
+ table: esEntity.table,
1063
+ items: esItems
1064
+ });
1065
+ } catch (ex) {
1066
+ throw new _error.default(ex.message || "Could not store unpublished entry records in DynamoDB Elasticsearch table.", ex.code || "UNPUBLISH_ERROR", {
1067
+ entry,
1068
+ storageEntry
1069
+ });
1070
+ }
1071
+
1072
+ return initialStorageEntry;
1073
+ };
1074
+
1075
+ const getLatestRevisionByEntryId = async (model, params) => {
1076
+ const [entry] = await dataLoaders.getLatestRevisionByEntryId({
1077
+ model,
1078
+ ids: [params.id]
1079
+ });
1080
+
1081
+ if (!entry) {
1082
+ return null;
1083
+ }
1084
+
1085
+ return convertEntryKeysFromStorage({
1086
+ model,
1087
+ entry
1088
+ });
1089
+ };
1090
+
1091
+ const getPublishedRevisionByEntryId = async (model, params) => {
1092
+ const [entry] = await dataLoaders.getPublishedRevisionByEntryId({
1093
+ model,
1094
+ ids: [params.id]
1095
+ });
1096
+
1097
+ if (!entry) {
1098
+ return null;
1099
+ }
1100
+
1101
+ return convertEntryKeysFromStorage({
1102
+ model,
1103
+ entry
1104
+ });
1105
+ };
1106
+
1107
+ const getRevisionById = async (model, params) => {
1108
+ const [entry] = await dataLoaders.getRevisionById({
1109
+ model,
1110
+ ids: [params.id]
1111
+ });
1112
+
1113
+ if (!entry) {
1114
+ return null;
1115
+ }
1116
+
1117
+ return convertEntryKeysFromStorage({
1118
+ model,
1119
+ entry
1120
+ });
1121
+ };
1122
+
1123
+ const getRevisions = async (model, params) => {
1124
+ const entries = await dataLoaders.getAllEntryRevisions({
1125
+ model,
1126
+ ids: [params.id]
1127
+ });
1128
+ return entries.map(entry => {
1129
+ return convertEntryKeysFromStorage({
1130
+ model,
1131
+ entry
1132
+ });
1133
+ });
1134
+ };
1135
+
1136
+ const getByIds = async (model, params) => {
1137
+ const entries = await dataLoaders.getRevisionById({
1138
+ model,
1139
+ ids: params.ids
1140
+ });
1141
+ return entries.map(entry => {
1142
+ return convertEntryKeysFromStorage({
1143
+ model,
1144
+ entry
1145
+ });
1146
+ });
1147
+ };
1148
+
1149
+ const getLatestByIds = async (model, params) => {
1150
+ const entries = await dataLoaders.getLatestRevisionByEntryId({
1151
+ model,
1152
+ ids: params.ids
1153
+ });
1154
+ return entries.map(entry => {
1155
+ return convertEntryKeysFromStorage({
1156
+ model,
1157
+ entry
1158
+ });
1159
+ });
1160
+ };
1161
+
1162
+ const getPublishedByIds = async (model, params) => {
1163
+ const entries = await dataLoaders.getPublishedRevisionByEntryId({
1164
+ model,
1165
+ ids: params.ids
1166
+ });
1167
+ return entries.map(entry => {
1168
+ return convertEntryKeysFromStorage({
1169
+ model,
1170
+ entry
1171
+ });
1172
+ });
1173
+ };
1174
+
1175
+ const getPreviousRevision = async (model, params) => {
1176
+ const {
1177
+ tenant,
1178
+ locale
1179
+ } = model;
1180
+ const {
1181
+ entryId,
1182
+ version
1183
+ } = params;
1184
+ const queryParams = {
1185
+ entity,
1186
+ partitionKey: (0, _keys.createPartitionKey)({
1187
+ tenant,
1188
+ locale,
1189
+ id: entryId
1190
+ }),
1191
+ options: {
1192
+ lt: `REV#${(0, _utils.zeroPad)(version)}`,
1193
+
1194
+ /**
1195
+ * We need to have extra checks because DynamoDB will return published or latest record if there is no REV# record.
1196
+ */
1197
+ filters: [{
1198
+ attr: "TYPE",
1199
+ eq: (0, _recordType.createRecordType)()
1200
+ }, {
1201
+ attr: "version",
1202
+ lt: version
1203
+ }],
1204
+ reverse: true
1205
+ }
1206
+ };
1207
+
1208
+ try {
1209
+ const result = await (0, _query.queryOne)(queryParams);
1210
+ const entry = (0, _cleanup.cleanupItem)(entity, result);
1211
+
1212
+ if (!entry) {
1213
+ return null;
1214
+ }
1215
+
1216
+ return convertEntryKeysFromStorage({
1217
+ entry,
1218
+ model
1219
+ });
1220
+ } catch (ex) {
1221
+ 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), {}, {
1222
+ error: ex,
1223
+ partitionKey: queryParams.partitionKey,
1224
+ options: queryParams.options,
1225
+ model
1226
+ }));
1227
+ }
1228
+ };
1229
+
1230
+ return {
1231
+ create,
1232
+ createRevisionFrom,
1233
+ update,
1234
+ delete: deleteEntry,
1235
+ deleteRevision,
1236
+ get,
1237
+ publish,
1238
+ unpublish,
1239
+ list,
1240
+ getLatestRevisionByEntryId,
1241
+ getPublishedRevisionByEntryId,
1242
+ getRevisionById,
1243
+ getRevisions,
1244
+ getByIds,
1245
+ getLatestByIds,
1246
+ getPublishedByIds,
1247
+ getPreviousRevision
1248
+ };
1249
+ };
1250
+
1251
+ exports.createEntriesStorageOperations = createEntriesStorageOperations;