@webiny/api-headless-cms-ddb-es 0.0.0-mt-2 → 0.0.0-unstable.06b2ede40f

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 (325) hide show
  1. package/configurations.d.ts +15 -8
  2. package/configurations.js +29 -12
  3. package/configurations.js.map +1 -0
  4. package/definitions/entry.d.ts +6 -5
  5. package/definitions/entry.js +111 -20
  6. package/definitions/entry.js.map +1 -0
  7. package/definitions/entryElasticsearch.d.ts +6 -5
  8. package/definitions/entryElasticsearch.js +11 -16
  9. package/definitions/entryElasticsearch.js.map +1 -0
  10. package/definitions/group.d.ts +6 -5
  11. package/definitions/group.js +8 -16
  12. package/definitions/group.js.map +1 -0
  13. package/definitions/model.d.ts +6 -5
  14. package/definitions/model.js +30 -16
  15. package/definitions/model.js.map +1 -0
  16. package/definitions/system.d.ts +6 -5
  17. package/definitions/system.js +8 -16
  18. package/definitions/system.js.map +1 -0
  19. package/definitions/table.d.ts +6 -6
  20. package/definitions/table.js +7 -6
  21. package/definitions/table.js.map +1 -0
  22. package/definitions/tableElasticsearch.d.ts +6 -6
  23. package/definitions/tableElasticsearch.js +7 -6
  24. package/definitions/tableElasticsearch.js.map +1 -0
  25. package/dynamoDb/index.d.ts +2 -1
  26. package/dynamoDb/index.js +10 -12
  27. package/dynamoDb/index.js.map +1 -0
  28. package/dynamoDb/storage/longText.d.ts +7 -4
  29. package/dynamoDb/storage/longText.js +75 -62
  30. package/dynamoDb/storage/longText.js.map +1 -0
  31. package/dynamoDb/storage/richText.d.ts +2 -8
  32. package/dynamoDb/storage/richText.js +49 -100
  33. package/dynamoDb/storage/richText.js.map +1 -0
  34. package/elasticsearch/createElasticsearchIndex.d.ts +9 -0
  35. package/elasticsearch/createElasticsearchIndex.js +40 -0
  36. package/elasticsearch/createElasticsearchIndex.js.map +1 -0
  37. package/elasticsearch/deleteElasticsearchIndex.d.ts +8 -0
  38. package/elasticsearch/deleteElasticsearchIndex.js +38 -0
  39. package/elasticsearch/deleteElasticsearchIndex.js.map +1 -0
  40. package/elasticsearch/index.d.ts +1 -1
  41. package/elasticsearch/index.js +3 -6
  42. package/elasticsearch/index.js.map +1 -0
  43. package/elasticsearch/indexing/dateTimeIndexing.d.ts +1 -1
  44. package/elasticsearch/indexing/dateTimeIndexing.js +21 -22
  45. package/elasticsearch/indexing/dateTimeIndexing.js.map +1 -0
  46. package/elasticsearch/indexing/defaultFieldIndexing.d.ts +1 -1
  47. package/elasticsearch/indexing/defaultFieldIndexing.js +17 -10
  48. package/elasticsearch/indexing/defaultFieldIndexing.js.map +1 -0
  49. package/elasticsearch/indexing/index.js +5 -11
  50. package/elasticsearch/indexing/index.js.map +1 -0
  51. package/elasticsearch/indexing/jsonIndexing.d.ts +2 -0
  52. package/elasticsearch/indexing/jsonIndexing.js +28 -0
  53. package/elasticsearch/indexing/jsonIndexing.js.map +1 -0
  54. package/elasticsearch/indexing/longTextIndexing.d.ts +5 -1
  55. package/elasticsearch/indexing/longTextIndexing.js +16 -10
  56. package/elasticsearch/indexing/longTextIndexing.js.map +1 -0
  57. package/elasticsearch/indexing/numberIndexing.d.ts +1 -1
  58. package/elasticsearch/indexing/numberIndexing.js +2 -9
  59. package/elasticsearch/indexing/numberIndexing.js.map +1 -0
  60. package/elasticsearch/indexing/objectIndexing.d.ts +10 -1
  61. package/elasticsearch/indexing/objectIndexing.js +37 -36
  62. package/elasticsearch/indexing/objectIndexing.js.map +1 -0
  63. package/elasticsearch/indexing/richTextIndexing.d.ts +1 -1
  64. package/elasticsearch/indexing/richTextIndexing.js +2 -5
  65. package/elasticsearch/indexing/richTextIndexing.js.map +1 -0
  66. package/elasticsearch/indices/base.d.ts +2 -0
  67. package/elasticsearch/indices/base.js +13 -0
  68. package/elasticsearch/indices/base.js.map +1 -0
  69. package/elasticsearch/indices/index.d.ts +1 -0
  70. package/elasticsearch/indices/index.js +14 -0
  71. package/elasticsearch/indices/index.js.map +1 -0
  72. package/elasticsearch/indices/japanese.d.ts +2 -0
  73. package/elasticsearch/indices/japanese.js +14 -0
  74. package/elasticsearch/indices/japanese.js.map +1 -0
  75. package/elasticsearch/search/index.d.ts +2 -2
  76. package/elasticsearch/search/index.js +6 -9
  77. package/elasticsearch/search/index.js.map +1 -0
  78. package/elasticsearch/search/refSearch.d.ts +2 -3
  79. package/elasticsearch/search/refSearch.js +24 -17
  80. package/elasticsearch/search/refSearch.js.map +1 -0
  81. package/elasticsearch/search/searchableJson.d.ts +2 -0
  82. package/elasticsearch/search/searchableJson.js +60 -0
  83. package/elasticsearch/search/searchableJson.js.map +1 -0
  84. package/elasticsearch/search/timeSearch.d.ts +2 -3
  85. package/elasticsearch/search/timeSearch.js +18 -16
  86. package/elasticsearch/search/timeSearch.js.map +1 -0
  87. package/helpers/entryIndexHelpers.d.ts +3 -3
  88. package/helpers/entryIndexHelpers.js +59 -58
  89. package/helpers/entryIndexHelpers.js.map +1 -0
  90. package/helpers/fieldIdentifier.d.ts +6 -0
  91. package/helpers/fieldIdentifier.js +43 -0
  92. package/helpers/fieldIdentifier.js.map +1 -0
  93. package/helpers/index.d.ts +1 -1
  94. package/helpers/index.js +11 -13
  95. package/helpers/index.js.map +1 -0
  96. package/index.d.ts +2 -1
  97. package/index.js +132 -78
  98. package/index.js.map +1 -0
  99. package/operations/entry/dataLoader/DataLoaderCache.d.ts +17 -0
  100. package/operations/entry/dataLoader/DataLoaderCache.js +41 -0
  101. package/operations/entry/dataLoader/DataLoaderCache.js.map +1 -0
  102. package/operations/entry/dataLoader/constants.d.ts +1 -0
  103. package/operations/entry/dataLoader/constants.js +10 -0
  104. package/operations/entry/dataLoader/constants.js.map +1 -0
  105. package/operations/entry/dataLoader/createBatchScheduleFn.d.ts +8 -0
  106. package/operations/entry/dataLoader/createBatchScheduleFn.js +25 -0
  107. package/operations/entry/dataLoader/createBatchScheduleFn.js.map +1 -0
  108. package/operations/entry/dataLoader/getAllEntryRevisions.d.ts +4 -0
  109. package/operations/entry/dataLoader/getAllEntryRevisions.js +45 -0
  110. package/operations/entry/dataLoader/getAllEntryRevisions.js.map +1 -0
  111. package/operations/entry/dataLoader/getLatestRevisionByEntryId.d.ts +4 -0
  112. package/operations/entry/dataLoader/getLatestRevisionByEntryId.js +52 -0
  113. package/operations/entry/dataLoader/getLatestRevisionByEntryId.js.map +1 -0
  114. package/operations/entry/dataLoader/getPublishedRevisionByEntryId.d.ts +4 -0
  115. package/operations/entry/dataLoader/getPublishedRevisionByEntryId.js +52 -0
  116. package/operations/entry/dataLoader/getPublishedRevisionByEntryId.js.map +1 -0
  117. package/operations/entry/dataLoader/getRevisionById.d.ts +4 -0
  118. package/operations/entry/dataLoader/getRevisionById.js +62 -0
  119. package/operations/entry/dataLoader/getRevisionById.js.map +1 -0
  120. package/operations/entry/dataLoader/index.d.ts +8 -0
  121. package/operations/entry/dataLoader/index.js +40 -0
  122. package/operations/entry/dataLoader/index.js.map +1 -0
  123. package/operations/entry/dataLoader/types.d.ts +6 -0
  124. package/operations/entry/dataLoader/types.js +7 -0
  125. package/operations/entry/dataLoader/types.js.map +1 -0
  126. package/operations/entry/dataLoaders.d.ts +21 -38
  127. package/operations/entry/dataLoaders.js +59 -290
  128. package/operations/entry/dataLoaders.js.map +1 -0
  129. package/operations/entry/elasticsearch/assignMinimumShouldMatchToQuery.d.ts +18 -0
  130. package/operations/entry/elasticsearch/assignMinimumShouldMatchToQuery.js +34 -0
  131. package/operations/entry/elasticsearch/assignMinimumShouldMatchToQuery.js.map +1 -0
  132. package/operations/entry/elasticsearch/body.d.ts +13 -0
  133. package/operations/entry/elasticsearch/body.js +142 -0
  134. package/operations/entry/elasticsearch/body.js.map +1 -0
  135. package/operations/entry/elasticsearch/fields.d.ts +10 -0
  136. package/operations/entry/elasticsearch/fields.js +315 -0
  137. package/operations/entry/elasticsearch/fields.js.map +1 -0
  138. package/operations/entry/elasticsearch/filtering/applyFiltering.d.ts +8 -0
  139. package/operations/entry/elasticsearch/filtering/applyFiltering.js +60 -0
  140. package/operations/entry/elasticsearch/filtering/applyFiltering.js.map +1 -0
  141. package/operations/entry/elasticsearch/filtering/exec.d.ts +17 -0
  142. package/operations/entry/elasticsearch/filtering/exec.js +175 -0
  143. package/operations/entry/elasticsearch/filtering/exec.js.map +1 -0
  144. package/operations/entry/elasticsearch/filtering/index.d.ts +1 -0
  145. package/operations/entry/elasticsearch/filtering/index.js +18 -0
  146. package/operations/entry/elasticsearch/filtering/index.js.map +1 -0
  147. package/operations/entry/elasticsearch/filtering/path.d.ts +16 -0
  148. package/operations/entry/elasticsearch/filtering/path.js +48 -0
  149. package/operations/entry/elasticsearch/filtering/path.js.map +1 -0
  150. package/operations/entry/elasticsearch/filtering/plugins/defaultFilterPlugin.d.ts +2 -0
  151. package/operations/entry/elasticsearch/filtering/plugins/defaultFilterPlugin.js +30 -0
  152. package/operations/entry/elasticsearch/filtering/plugins/defaultFilterPlugin.js.map +1 -0
  153. package/operations/entry/elasticsearch/filtering/plugins/index.d.ts +1 -0
  154. package/operations/entry/elasticsearch/filtering/plugins/index.js +15 -0
  155. package/operations/entry/elasticsearch/filtering/plugins/index.js.map +1 -0
  156. package/operations/entry/elasticsearch/filtering/plugins/objectFilterPlugin.d.ts +2 -0
  157. package/operations/entry/elasticsearch/filtering/plugins/objectFilterPlugin.js +69 -0
  158. package/operations/entry/elasticsearch/filtering/plugins/objectFilterPlugin.js.map +1 -0
  159. package/operations/entry/elasticsearch/filtering/plugins/refFilterPlugin.d.ts +2 -0
  160. package/operations/entry/elasticsearch/filtering/plugins/refFilterPlugin.js +57 -0
  161. package/operations/entry/elasticsearch/filtering/plugins/refFilterPlugin.js.map +1 -0
  162. package/operations/entry/elasticsearch/filtering/populated.d.ts +2 -0
  163. package/operations/entry/elasticsearch/filtering/populated.js +25 -0
  164. package/operations/entry/elasticsearch/filtering/populated.js.map +1 -0
  165. package/operations/entry/elasticsearch/filtering/values.d.ts +2 -0
  166. package/operations/entry/elasticsearch/filtering/values.js +24 -0
  167. package/operations/entry/elasticsearch/filtering/values.js.map +1 -0
  168. package/operations/entry/elasticsearch/fullTextSearch.d.ts +12 -0
  169. package/operations/entry/elasticsearch/fullTextSearch.js +91 -0
  170. package/operations/entry/elasticsearch/fullTextSearch.js.map +1 -0
  171. package/operations/entry/elasticsearch/fullTextSearchFields.d.ts +8 -0
  172. package/operations/entry/elasticsearch/fullTextSearchFields.js +27 -0
  173. package/operations/entry/elasticsearch/fullTextSearchFields.js.map +1 -0
  174. package/operations/entry/elasticsearch/initialQuery.d.ts +17 -0
  175. package/operations/entry/elasticsearch/initialQuery.js +106 -0
  176. package/operations/entry/elasticsearch/initialQuery.js.map +1 -0
  177. package/operations/entry/elasticsearch/keyword.d.ts +2 -0
  178. package/operations/entry/elasticsearch/keyword.js +36 -0
  179. package/operations/entry/elasticsearch/keyword.js.map +1 -0
  180. package/operations/entry/elasticsearch/logIgnoredEsResponseError.d.ts +9 -0
  181. package/operations/entry/elasticsearch/logIgnoredEsResponseError.js +26 -0
  182. package/operations/entry/elasticsearch/logIgnoredEsResponseError.js.map +1 -0
  183. package/operations/entry/elasticsearch/plugins/bodyModifier.d.ts +9 -0
  184. package/operations/entry/elasticsearch/plugins/bodyModifier.js +18 -0
  185. package/operations/entry/elasticsearch/plugins/bodyModifier.js.map +1 -0
  186. package/operations/entry/elasticsearch/plugins/operator.d.ts +8 -0
  187. package/operations/entry/elasticsearch/plugins/operator.js +39 -0
  188. package/operations/entry/elasticsearch/plugins/operator.js.map +1 -0
  189. package/operations/entry/elasticsearch/plugins/queryModifier.d.ts +9 -0
  190. package/operations/entry/elasticsearch/plugins/queryModifier.js +18 -0
  191. package/operations/entry/elasticsearch/plugins/queryModifier.js.map +1 -0
  192. package/operations/entry/elasticsearch/plugins/search.d.ts +7 -0
  193. package/{helpers/searchPluginsList.js → operations/entry/elasticsearch/plugins/search.js} +9 -9
  194. package/operations/entry/elasticsearch/plugins/search.js.map +1 -0
  195. package/operations/entry/elasticsearch/plugins/sortModifier.d.ts +9 -0
  196. package/operations/entry/elasticsearch/plugins/sortModifier.js +18 -0
  197. package/operations/entry/elasticsearch/plugins/sortModifier.js.map +1 -0
  198. package/operations/entry/elasticsearch/shouldIgnoreEsResponseError.d.ts +2 -0
  199. package/operations/entry/elasticsearch/shouldIgnoreEsResponseError.js +13 -0
  200. package/operations/entry/elasticsearch/shouldIgnoreEsResponseError.js.map +1 -0
  201. package/operations/entry/elasticsearch/sort.d.ts +12 -0
  202. package/operations/entry/elasticsearch/sort.js +89 -0
  203. package/operations/entry/elasticsearch/sort.js.map +1 -0
  204. package/operations/entry/elasticsearch/transformValueForSearch.d.ts +16 -0
  205. package/operations/entry/elasticsearch/transformValueForSearch.js +32 -0
  206. package/operations/entry/elasticsearch/transformValueForSearch.js.map +1 -0
  207. package/operations/entry/elasticsearch/types.d.ts +41 -0
  208. package/operations/entry/elasticsearch/types.js +7 -0
  209. package/operations/entry/elasticsearch/types.js.map +1 -0
  210. package/operations/entry/index.d.ts +6 -8
  211. package/operations/entry/index.js +1422 -693
  212. package/operations/entry/index.js.map +1 -0
  213. package/operations/entry/keys.js +2 -9
  214. package/operations/entry/keys.js.map +1 -0
  215. package/operations/entry/recordType.d.ts +3 -0
  216. package/operations/entry/recordType.js +20 -0
  217. package/operations/entry/recordType.js.map +1 -0
  218. package/operations/entry/transformations/convertEntryKeys.d.ts +8 -0
  219. package/operations/entry/transformations/convertEntryKeys.js +38 -0
  220. package/operations/entry/transformations/convertEntryKeys.js.map +1 -0
  221. package/operations/entry/transformations/index.d.ts +29 -0
  222. package/operations/entry/transformations/index.js +127 -0
  223. package/operations/entry/transformations/index.js.map +1 -0
  224. package/operations/entry/transformations/modifyEntryValues.d.ts +58 -0
  225. package/operations/entry/transformations/modifyEntryValues.js +28 -0
  226. package/operations/entry/transformations/modifyEntryValues.js.map +1 -0
  227. package/operations/entry/transformations/transformEntryKeys.d.ts +11 -0
  228. package/operations/entry/transformations/transformEntryKeys.js +27 -0
  229. package/operations/entry/transformations/transformEntryKeys.js.map +1 -0
  230. package/operations/entry/transformations/transformEntryToIndex.d.ts +10 -0
  231. package/operations/entry/transformations/transformEntryToIndex.js +31 -0
  232. package/operations/entry/transformations/transformEntryToIndex.js.map +1 -0
  233. package/operations/group/index.d.ts +5 -5
  234. package/operations/group/index.js +40 -63
  235. package/operations/group/index.js.map +1 -0
  236. package/operations/model/index.d.ts +5 -5
  237. package/operations/model/index.js +56 -78
  238. package/operations/model/index.js.map +1 -0
  239. package/operations/system/index.d.ts +4 -4
  240. package/operations/system/index.js +20 -31
  241. package/operations/system/index.js.map +1 -0
  242. package/operations/system/indexes.d.ts +10 -0
  243. package/operations/system/indexes.js +41 -0
  244. package/operations/system/indexes.js.map +1 -0
  245. package/package.json +38 -40
  246. package/plugins/CmsElasticsearchModelFieldPlugin.d.ts +69 -0
  247. package/plugins/CmsElasticsearchModelFieldPlugin.js +52 -0
  248. package/plugins/CmsElasticsearchModelFieldPlugin.js.map +1 -0
  249. package/plugins/CmsEntryElasticsearchBodyModifierPlugin.d.ts +9 -4
  250. package/plugins/CmsEntryElasticsearchBodyModifierPlugin.js +10 -13
  251. package/plugins/CmsEntryElasticsearchBodyModifierPlugin.js.map +1 -0
  252. package/plugins/CmsEntryElasticsearchFullTextSearchPlugin.d.ts +23 -0
  253. package/plugins/CmsEntryElasticsearchFullTextSearchPlugin.js +27 -0
  254. package/plugins/CmsEntryElasticsearchFullTextSearchPlugin.js.map +1 -0
  255. package/plugins/CmsEntryElasticsearchIndexPlugin.d.ts +4 -0
  256. package/plugins/CmsEntryElasticsearchIndexPlugin.js +13 -0
  257. package/plugins/CmsEntryElasticsearchIndexPlugin.js.map +1 -0
  258. package/plugins/CmsEntryElasticsearchQueryBuilderValueSearchPlugin.d.ts +32 -0
  259. package/plugins/CmsEntryElasticsearchQueryBuilderValueSearchPlugin.js +40 -0
  260. package/plugins/CmsEntryElasticsearchQueryBuilderValueSearchPlugin.js.map +1 -0
  261. package/plugins/CmsEntryElasticsearchQueryModifierPlugin.d.ts +6 -4
  262. package/plugins/CmsEntryElasticsearchQueryModifierPlugin.js +10 -13
  263. package/plugins/CmsEntryElasticsearchQueryModifierPlugin.js.map +1 -0
  264. package/plugins/CmsEntryElasticsearchSortModifierPlugin.d.ts +6 -4
  265. package/plugins/CmsEntryElasticsearchSortModifierPlugin.js +10 -13
  266. package/plugins/CmsEntryElasticsearchSortModifierPlugin.js.map +1 -0
  267. package/plugins/CmsEntryElasticsearchValuesModifier.d.ts +33 -0
  268. package/plugins/CmsEntryElasticsearchValuesModifier.js +50 -0
  269. package/plugins/CmsEntryElasticsearchValuesModifier.js.map +1 -0
  270. package/plugins/CmsEntryFilterPlugin.d.ts +49 -0
  271. package/plugins/CmsEntryFilterPlugin.js +22 -0
  272. package/plugins/CmsEntryFilterPlugin.js.map +1 -0
  273. package/plugins/index.d.ts +8 -0
  274. package/plugins/index.js +95 -0
  275. package/plugins/index.js.map +1 -0
  276. package/tasks/createIndexTaskPlugin.d.ts +2 -0
  277. package/tasks/createIndexTaskPlugin.js +67 -0
  278. package/tasks/createIndexTaskPlugin.js.map +1 -0
  279. package/types.d.ts +30 -52
  280. package/types.js +5 -23
  281. package/types.js.map +1 -0
  282. package/values/NoValueContainer.d.ts +5 -0
  283. package/values/NoValueContainer.js +20 -0
  284. package/values/NoValueContainer.js.map +1 -0
  285. package/definitions/settings.d.ts +0 -8
  286. package/definitions/settings.js +0 -62
  287. package/dynamoDb/storage/date.d.ts +0 -3
  288. package/dynamoDb/storage/date.js +0 -65
  289. package/helpers/createElasticsearchQueryBody.d.ts +0 -11
  290. package/helpers/createElasticsearchQueryBody.js +0 -375
  291. package/helpers/fields.d.ts +0 -77
  292. package/helpers/fields.js +0 -174
  293. package/helpers/operatorPluginsList.d.ts +0 -7
  294. package/helpers/operatorPluginsList.js +0 -30
  295. package/helpers/searchPluginsList.d.ts +0 -6
  296. package/helpers/transformValueForSearch.d.ts +0 -9
  297. package/helpers/transformValueForSearch.js +0 -26
  298. package/operations/entry/elasticsearchFields.d.ts +0 -2
  299. package/operations/entry/elasticsearchFields.js +0 -32
  300. package/operations/entry/fields.d.ts +0 -3
  301. package/operations/entry/fields.js +0 -60
  302. package/operations/settings/index.d.ts +0 -6
  303. package/operations/settings/index.js +0 -141
  304. package/operations/system/createElasticsearchTemplate.d.ts +0 -5
  305. package/operations/system/createElasticsearchTemplate.js +0 -62
  306. package/plugins/CmsEntryElasticsearchFieldPlugin.d.ts +0 -12
  307. package/plugins/CmsEntryElasticsearchFieldPlugin.js +0 -24
  308. package/upgrades/index.d.ts +0 -2
  309. package/upgrades/index.js +0 -16
  310. package/upgrades/utils.d.ts +0 -1
  311. package/upgrades/utils.js +0 -16
  312. package/upgrades/v5.0.0/cleanDatabaseRecord.d.ts +0 -6
  313. package/upgrades/v5.0.0/cleanDatabaseRecord.js +0 -16
  314. package/upgrades/v5.0.0/createOldVersionIndiceName.d.ts +0 -2
  315. package/upgrades/v5.0.0/createOldVersionIndiceName.js +0 -12
  316. package/upgrades/v5.0.0/entryValueFixer.d.ts +0 -4
  317. package/upgrades/v5.0.0/entryValueFixer.js +0 -124
  318. package/upgrades/v5.0.0/fieldFinder.d.ts +0 -6
  319. package/upgrades/v5.0.0/fieldFinder.js +0 -42
  320. package/upgrades/v5.0.0/helpers.d.ts +0 -4
  321. package/upgrades/v5.0.0/helpers.js +0 -57
  322. package/upgrades/v5.0.0/index.d.ts +0 -4
  323. package/upgrades/v5.0.0/index.js +0 -232
  324. package/upgrades/v5.8.0/index.d.ts +0 -4
  325. package/upgrades/v5.8.0/index.js +0 -426
@@ -1,89 +1,41 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
- exports.createPublishedType = exports.createLatestType = exports.createEntriesStorageOperations = void 0;
9
-
10
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
-
7
+ exports.createEntriesStorageOperations = void 0;
8
+ var _error = _interopRequireDefault(require("@webiny/error"));
12
9
  var _types = require("@webiny/api-headless-cms/types");
13
-
14
10
  var _helpers = require("../../helpers");
15
-
16
- var _configurations = _interopRequireDefault(require("../../configurations"));
17
-
18
- var _error = _interopRequireDefault(require("@webiny/error"));
19
-
20
- var _lodash = _interopRequireDefault(require("lodash.clonedeep"));
21
-
22
- var _lodash2 = _interopRequireDefault(require("lodash.omit"));
23
-
24
- var _compression = require("@webiny/api-elasticsearch/compression");
25
-
26
- var _batchWrite = require("@webiny/db-dynamodb/utils/batchWrite");
27
-
11
+ var _configurations = require("../../configurations");
12
+ var _dbDynamodb = require("@webiny/db-dynamodb");
28
13
  var _dataLoaders = require("./dataLoaders");
29
-
30
14
  var _keys = require("./keys");
31
-
32
- var _query = require("@webiny/db-dynamodb/utils/query");
33
-
34
- var _limit = require("@webiny/api-elasticsearch/limit");
35
-
36
- var _cursors = require("@webiny/api-elasticsearch/cursors");
37
-
38
- var _get = require("@webiny/db-dynamodb/utils/get");
39
-
15
+ var _apiElasticsearch = require("@webiny/api-elasticsearch");
40
16
  var _utils = require("@webiny/utils");
41
-
42
- var _cleanup = require("@webiny/db-dynamodb/utils/cleanup");
43
-
44
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
45
-
46
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
47
-
48
- const createType = () => {
49
- return "cms.entry";
50
- };
51
-
52
- const createLatestType = () => {
53
- return `${createType()}.l`;
54
- };
55
-
56
- exports.createLatestType = createLatestType;
57
-
58
- const createPublishedType = () => {
59
- return `${createType()}.p`;
60
- };
61
-
62
- exports.createPublishedType = createPublishedType;
63
-
64
- const getEntryData = entry => {
65
- return _objectSpread(_objectSpread({}, (0, _lodash2.default)(entry, ["PK", "SK", "published", "latest"])), {}, {
66
- TYPE: createType(),
67
- __type: createType()
17
+ var _body = require("./elasticsearch/body");
18
+ var _logIgnoredEsResponseError = require("./elasticsearch/logIgnoredEsResponseError");
19
+ var _shouldIgnoreEsResponseError = require("./elasticsearch/shouldIgnoreEsResponseError");
20
+ var _recordType = require("./recordType");
21
+ var _apiHeadlessCms = require("@webiny/api-headless-cms");
22
+ var _transformations = require("./transformations");
23
+ var _convertEntryKeys = require("./transformations/convertEntryKeys");
24
+ var _constants = require("@webiny/api-headless-cms/constants");
25
+ const convertToStorageEntry = params => {
26
+ const {
27
+ model,
28
+ storageEntry
29
+ } = params;
30
+ const values = model.convertValueKeyToStorage({
31
+ fields: model.fields,
32
+ values: storageEntry.values
68
33
  });
34
+ return {
35
+ ...storageEntry,
36
+ values
37
+ };
69
38
  };
70
-
71
- const getESLatestEntryData = async (plugins, entry) => {
72
- return (0, _compression.compress)(plugins, _objectSpread(_objectSpread({}, getEntryData(entry)), {}, {
73
- latest: true,
74
- TYPE: createLatestType(),
75
- __type: createLatestType()
76
- }));
77
- };
78
-
79
- const getESPublishedEntryData = async (plugins, entry) => {
80
- return (0, _compression.compress)(plugins, _objectSpread(_objectSpread({}, getEntryData(entry)), {}, {
81
- published: true,
82
- TYPE: createPublishedType(),
83
- __type: createPublishedType()
84
- }));
85
- };
86
-
87
39
  const createEntriesStorageOperations = params => {
88
40
  const {
89
41
  entity,
@@ -91,48 +43,95 @@ const createEntriesStorageOperations = params => {
91
43
  elasticsearch,
92
44
  plugins
93
45
  } = params;
46
+ let storageOperationsCmsModelPlugin;
47
+ const getStorageOperationsCmsModelPlugin = () => {
48
+ if (storageOperationsCmsModelPlugin) {
49
+ return storageOperationsCmsModelPlugin;
50
+ }
51
+ storageOperationsCmsModelPlugin = plugins.oneByType(_apiHeadlessCms.StorageOperationsCmsModelPlugin.type);
52
+ return storageOperationsCmsModelPlugin;
53
+ };
54
+ const getStorageOperationsModel = model => {
55
+ const plugin = getStorageOperationsCmsModelPlugin();
56
+ return plugin.getModel(model);
57
+ };
94
58
  const dataLoaders = new _dataLoaders.DataLoadersHandler({
95
59
  entity
96
60
  });
97
-
98
- const create = async (model, params) => {
61
+ const create = async (initialModel, params) => {
99
62
  const {
100
- entry,
101
- storageEntry
63
+ entry: initialEntry,
64
+ storageEntry: initialStorageEntry
102
65
  } = params;
103
- const esEntry = (0, _helpers.prepareEntryToIndex)({
66
+ const model = getStorageOperationsModel(initialModel);
67
+ const isPublished = initialEntry.status === "published";
68
+ const locked = isPublished ? true : initialEntry.locked;
69
+ initialEntry.locked = locked;
70
+ initialStorageEntry.locked = locked;
71
+ const transformer = (0, _transformations.createTransformer)({
104
72
  plugins,
105
73
  model,
106
- entry: (0, _lodash.default)(entry),
107
- storageEntry: (0, _lodash.default)(storageEntry)
74
+ entry: initialEntry,
75
+ storageEntry: initialStorageEntry
108
76
  });
109
-
77
+ const {
78
+ entry,
79
+ storageEntry
80
+ } = transformer.transformEntryKeys();
81
+ const esEntry = transformer.transformToIndex();
110
82
  const {
111
83
  index: esIndex
112
- } = _configurations.default.es({
84
+ } = _configurations.configurations.es({
113
85
  model
114
86
  });
115
-
116
- const esLatestData = await getESLatestEntryData(plugins, esEntry);
117
87
  const revisionKeys = {
118
- PK: (0, _keys.createPartitionKey)(entry),
88
+ PK: (0, _keys.createPartitionKey)({
89
+ id: entry.id,
90
+ locale: model.locale,
91
+ tenant: model.tenant
92
+ }),
119
93
  SK: (0, _keys.createRevisionSortKey)(entry)
120
94
  };
121
95
  const latestKeys = {
122
- PK: (0, _keys.createPartitionKey)(entry),
96
+ PK: (0, _keys.createPartitionKey)({
97
+ id: entry.id,
98
+ locale: model.locale,
99
+ tenant: model.tenant
100
+ }),
123
101
  SK: (0, _keys.createLatestSortKey)()
124
102
  };
125
- const items = [entity.putBatch(_objectSpread(_objectSpread(_objectSpread({}, storageEntry), revisionKeys), {}, {
126
- TYPE: createType()
127
- })), entity.putBatch(_objectSpread(_objectSpread(_objectSpread({}, storageEntry), latestKeys), {}, {
128
- TYPE: createLatestType()
129
- }))];
130
-
131
- try {
132
- await (0, _batchWrite.batchWriteAll)({
133
- table: entity.table,
134
- items
103
+ const publishedKeys = {
104
+ PK: (0, _keys.createPartitionKey)({
105
+ id: entry.id,
106
+ locale: model.locale,
107
+ tenant: model.tenant
108
+ }),
109
+ SK: (0, _keys.createPublishedSortKey)()
110
+ };
111
+ const entityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
112
+ entity,
113
+ put: [{
114
+ ...storageEntry,
115
+ locked,
116
+ ...revisionKeys,
117
+ TYPE: (0, _recordType.createRecordType)()
118
+ }, {
119
+ ...storageEntry,
120
+ locked,
121
+ ...latestKeys,
122
+ TYPE: (0, _recordType.createLatestRecordType)()
123
+ }]
124
+ });
125
+ if (isPublished) {
126
+ entityBatch.put({
127
+ ...storageEntry,
128
+ locked,
129
+ ...publishedKeys,
130
+ TYPE: (0, _recordType.createPublishedRecordType)()
135
131
  });
132
+ }
133
+ try {
134
+ await entityBatch.execute();
136
135
  dataLoaders.clearAll({
137
136
  model
138
137
  });
@@ -143,12 +142,25 @@ const createEntriesStorageOperations = params => {
143
142
  storageEntry
144
143
  });
145
144
  }
146
-
147
- try {
148
- await esEntity.put(_objectSpread(_objectSpread({}, latestKeys), {}, {
145
+ const esLatestData = await transformer.getElasticsearchLatestEntryData();
146
+ const elasticsearchEntityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
147
+ entity: esEntity,
148
+ put: [{
149
+ ...latestKeys,
149
150
  index: esIndex,
150
151
  data: esLatestData
151
- }));
152
+ }]
153
+ });
154
+ if (isPublished) {
155
+ const esPublishedData = await transformer.getElasticsearchPublishedEntryData();
156
+ elasticsearchEntityBatch.put({
157
+ ...publishedKeys,
158
+ index: esIndex,
159
+ data: esPublishedData
160
+ });
161
+ }
162
+ try {
163
+ await elasticsearchEntityBatch.execute();
152
164
  } catch (ex) {
153
165
  throw new _error.default(ex.message || "Could not insert entry data into the Elasticsearch DynamoDB table.", ex.code || "CREATE_ES_ENTRY_ERROR", {
154
166
  error: ex,
@@ -156,391 +168,1064 @@ const createEntriesStorageOperations = params => {
156
168
  esEntry
157
169
  });
158
170
  }
159
-
160
- return storageEntry;
171
+ return initialStorageEntry;
161
172
  };
162
-
163
- const createRevisionFrom = async (model, params) => {
173
+ const createRevisionFrom = async (initialModel, params) => {
174
+ const {
175
+ entry: initialEntry,
176
+ storageEntry: initialStorageEntry
177
+ } = params;
178
+ const model = getStorageOperationsModel(initialModel);
179
+ const transformer = (0, _transformations.createTransformer)({
180
+ plugins,
181
+ model,
182
+ entry: initialEntry,
183
+ storageEntry: initialStorageEntry
184
+ });
164
185
  const {
165
- originalEntry,
166
186
  entry,
167
187
  storageEntry
168
- } = params;
188
+ } = transformer.transformEntryKeys();
169
189
  const revisionKeys = {
170
- PK: (0, _keys.createPartitionKey)(entry),
190
+ PK: (0, _keys.createPartitionKey)({
191
+ id: entry.id,
192
+ locale: model.locale,
193
+ tenant: model.tenant
194
+ }),
171
195
  SK: (0, _keys.createRevisionSortKey)(entry)
172
196
  };
173
197
  const latestKeys = {
174
- PK: (0, _keys.createPartitionKey)(entry),
198
+ PK: (0, _keys.createPartitionKey)({
199
+ id: entry.id,
200
+ locale: model.locale,
201
+ tenant: model.tenant
202
+ }),
175
203
  SK: (0, _keys.createLatestSortKey)()
176
204
  };
177
- const esEntry = (0, _helpers.prepareEntryToIndex)({
178
- plugins,
179
- model,
180
- entry: (0, _lodash.default)(entry),
181
- storageEntry: (0, _lodash.default)(storageEntry)
182
- });
183
- const esLatestData = await getESLatestEntryData(plugins, esEntry);
184
- const items = [entity.putBatch(_objectSpread(_objectSpread({}, storageEntry), {}, {
185
- TYPE: createType()
186
- }, revisionKeys)), entity.putBatch(_objectSpread(_objectSpread({}, storageEntry), {}, {
187
- TYPE: createLatestType()
188
- }, latestKeys))];
205
+ const publishedKeys = {
206
+ PK: (0, _keys.createPartitionKey)({
207
+ id: entry.id,
208
+ locale: model.locale,
209
+ tenant: model.tenant
210
+ }),
211
+ SK: (0, _keys.createPublishedSortKey)()
212
+ };
189
213
 
190
- const {
191
- index
192
- } = _configurations.default.es({
193
- model
214
+ // We'll need this flag below.
215
+ const isPublished = entry.status === "published";
216
+ const esLatestData = await transformer.getElasticsearchLatestEntryData();
217
+ const entityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
218
+ entity,
219
+ put: [{
220
+ ...storageEntry,
221
+ TYPE: (0, _recordType.createRecordType)(),
222
+ ...revisionKeys
223
+ }, {
224
+ ...storageEntry,
225
+ TYPE: (0, _recordType.createLatestRecordType)(),
226
+ ...latestKeys
227
+ }]
194
228
  });
229
+ if (isPublished) {
230
+ entityBatch.put({
231
+ ...storageEntry,
232
+ TYPE: (0, _recordType.createPublishedRecordType)(),
233
+ ...publishedKeys
234
+ });
195
235
 
196
- try {
197
- await (0, _batchWrite.batchWriteAll)({
198
- table: entity.table,
199
- items
236
+ // Unpublish previously published revision (if any).
237
+ const [publishedRevisionStorageEntry] = await dataLoaders.getPublishedRevisionByEntryId({
238
+ model,
239
+ ids: [entry.id]
200
240
  });
241
+ if (publishedRevisionStorageEntry) {
242
+ entityBatch.put({
243
+ ...publishedRevisionStorageEntry,
244
+ PK: (0, _keys.createPartitionKey)({
245
+ id: publishedRevisionStorageEntry.id,
246
+ locale: model.locale,
247
+ tenant: model.tenant
248
+ }),
249
+ SK: (0, _keys.createRevisionSortKey)(publishedRevisionStorageEntry),
250
+ TYPE: (0, _recordType.createRecordType)(),
251
+ status: _types.CONTENT_ENTRY_STATUS.UNPUBLISHED
252
+ });
253
+ }
254
+ }
255
+ try {
256
+ await entityBatch.execute();
201
257
  dataLoaders.clearAll({
202
258
  model
203
259
  });
204
260
  } catch (ex) {
205
261
  throw new _error.default(ex.message || "Could not create revision from given entry in the DynamoDB table.", ex.code || "CREATE_REVISION_ERROR", {
206
262
  error: ex,
207
- originalEntry,
208
263
  entry,
209
264
  storageEntry
210
265
  });
211
266
  }
212
- /**
213
- * Update the "latest" entry item in the Elasticsearch
214
- */
215
-
216
-
217
- try {
218
- await esEntity.put(_objectSpread(_objectSpread({}, latestKeys), {}, {
219
- index,
267
+ const {
268
+ index: esIndex
269
+ } = _configurations.configurations.es({
270
+ model
271
+ });
272
+ const elasticsearchEntityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
273
+ entity: esEntity,
274
+ put: [{
275
+ ...latestKeys,
276
+ index: esIndex,
220
277
  data: esLatestData
221
- }));
278
+ }]
279
+ });
280
+ if (isPublished) {
281
+ const esPublishedData = await transformer.getElasticsearchPublishedEntryData();
282
+ elasticsearchEntityBatch.put({
283
+ ...publishedKeys,
284
+ index: esIndex,
285
+ data: esPublishedData
286
+ });
287
+ }
288
+ try {
289
+ await elasticsearchEntityBatch.execute();
222
290
  } catch (ex) {
223
291
  throw new _error.default(ex.message || "Could not update latest entry in the DynamoDB Elasticsearch table.", ex.code || "CREATE_REVISION_ERROR", {
224
292
  error: ex,
225
- originalEntry,
226
293
  entry
227
294
  });
228
295
  }
229
296
  /**
230
297
  * There are no modifications on the entry created so just return the data.
231
298
  */
232
-
233
-
234
- return storageEntry;
299
+ return initialStorageEntry;
235
300
  };
236
-
237
- const update = async (model, params) => {
301
+ const update = async (initialModel, params) => {
302
+ const {
303
+ entry: initialEntry,
304
+ storageEntry: initialStorageEntry
305
+ } = params;
306
+ const model = getStorageOperationsModel(initialModel);
307
+ const transformer = (0, _transformations.createTransformer)({
308
+ plugins,
309
+ model,
310
+ entry: initialEntry,
311
+ storageEntry: initialStorageEntry
312
+ });
238
313
  const {
239
- originalEntry,
240
314
  entry,
241
315
  storageEntry
242
- } = params;
316
+ } = transformer.transformEntryKeys();
317
+ const isPublished = entry.status === "published";
318
+ const locked = isPublished ? true : entry.locked;
243
319
  const revisionKeys = {
244
- PK: (0, _keys.createPartitionKey)(entry),
320
+ PK: (0, _keys.createPartitionKey)({
321
+ id: entry.id,
322
+ locale: model.locale,
323
+ tenant: model.tenant
324
+ }),
245
325
  SK: (0, _keys.createRevisionSortKey)(entry)
246
326
  };
247
327
  const latestKeys = {
248
- PK: (0, _keys.createPartitionKey)(entry),
328
+ PK: (0, _keys.createPartitionKey)({
329
+ id: entry.id,
330
+ locale: model.locale,
331
+ tenant: model.tenant
332
+ }),
249
333
  SK: (0, _keys.createLatestSortKey)()
250
334
  };
335
+ const publishedKeys = {
336
+ PK: (0, _keys.createPartitionKey)({
337
+ id: entry.id,
338
+ locale: model.locale,
339
+ tenant: model.tenant
340
+ }),
341
+ SK: (0, _keys.createPublishedSortKey)()
342
+ };
343
+
251
344
  /**
252
345
  * We need the latest entry to check if it needs to be updated.
253
346
  */
254
-
255
347
  const [latestStorageEntry] = await dataLoaders.getLatestRevisionByEntryId({
256
348
  model,
257
- ids: [originalEntry.id]
349
+ ids: [entry.id]
350
+ });
351
+ const [publishedStorageEntry] = await dataLoaders.getPublishedRevisionByEntryId({
352
+ model,
353
+ ids: [entry.id]
354
+ });
355
+ const entityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
356
+ entity,
357
+ put: [{
358
+ ...storageEntry,
359
+ locked,
360
+ ...revisionKeys,
361
+ TYPE: (0, _recordType.createRecordType)()
362
+ }]
363
+ });
364
+ if (isPublished) {
365
+ entityBatch.put({
366
+ ...storageEntry,
367
+ locked,
368
+ ...publishedKeys,
369
+ TYPE: (0, _recordType.createPublishedRecordType)()
370
+ });
371
+ }
372
+ const elasticsearchEntityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
373
+ entity: esEntity
374
+ });
375
+ const {
376
+ index: esIndex
377
+ } = _configurations.configurations.es({
378
+ model
258
379
  });
259
- const items = [entity.putBatch(_objectSpread(_objectSpread(_objectSpread({}, storageEntry), revisionKeys), {}, {
260
- TYPE: createType()
261
- }))];
380
+
262
381
  /**
263
382
  * If the latest entry is the one being updated, we need to create a new latest entry records.
264
383
  */
384
+ if (latestStorageEntry) {
385
+ const updatingLatestRevision = latestStorageEntry.id === entry.id;
386
+ if (updatingLatestRevision) {
387
+ /**
388
+ * First we update the regular DynamoDB table.
389
+ */
390
+ entityBatch.put({
391
+ ...storageEntry,
392
+ ...latestKeys,
393
+ TYPE: (0, _recordType.createLatestRecordType)()
394
+ });
265
395
 
266
- let elasticsearchLatestData = null;
396
+ /**
397
+ * And then update the Elasticsearch table to propagate changes to the Elasticsearch
398
+ */
399
+ const elasticsearchLatestData = await transformer.getElasticsearchLatestEntryData();
400
+ elasticsearchEntityBatch.put({
401
+ ...latestKeys,
402
+ index: esIndex,
403
+ data: elasticsearchLatestData
404
+ });
405
+ } else {
406
+ /**
407
+ * If not updating latest revision, we still want to update the latest revision's
408
+ * entry-level meta fields to match the current revision's entry-level meta fields.
409
+ */
410
+ const updatedEntryLevelMetaFields = (0, _constants.pickEntryMetaFields)(entry, _constants.isEntryLevelEntryMetaField);
411
+ const updatedLatestStorageEntry = {
412
+ ...latestStorageEntry,
413
+ ...latestKeys,
414
+ ...updatedEntryLevelMetaFields
415
+ };
267
416
 
268
- if (latestStorageEntry.id === originalEntry.id) {
269
- /**
270
- * First we update the regular DynamoDB table
271
- */
272
- items.push(entity.putBatch(_objectSpread(_objectSpread(_objectSpread({}, storageEntry), latestKeys), {}, {
273
- TYPE: (0, _keys.createLatestSortKey)()
274
- })));
275
- /**
276
- * And then update the Elasticsearch table to propagate changes to the Elasticsearch
277
- */
417
+ /**
418
+ * First we update the regular DynamoDB table. Two updates are needed:
419
+ * - one for the actual revision record
420
+ * - one for the latest record
421
+ */
422
+ entityBatch.put({
423
+ ...updatedLatestStorageEntry,
424
+ PK: (0, _keys.createPartitionKey)({
425
+ id: latestStorageEntry.id,
426
+ locale: model.locale,
427
+ tenant: model.tenant
428
+ }),
429
+ SK: (0, _keys.createRevisionSortKey)(latestStorageEntry),
430
+ TYPE: (0, _recordType.createRecordType)()
431
+ });
432
+ entityBatch.put({
433
+ ...updatedLatestStorageEntry,
434
+ TYPE: (0, _recordType.createLatestRecordType)()
435
+ });
278
436
 
279
- const esEntry = (0, _helpers.prepareEntryToIndex)({
280
- plugins,
281
- model,
282
- entry: (0, _lodash.default)(entry),
283
- storageEntry: (0, _lodash.default)(storageEntry)
437
+ /**
438
+ * Update the Elasticsearch table to propagate changes to the Elasticsearch.
439
+ */
440
+ const latestEsEntry = await (0, _dbDynamodb.getClean)({
441
+ entity: esEntity,
442
+ keys: latestKeys
443
+ });
444
+ if (latestEsEntry) {
445
+ const latestEsEntryDataDecompressed = await (0, _apiElasticsearch.decompress)(plugins, latestEsEntry.data);
446
+ const updatedLatestEntry = await (0, _apiElasticsearch.compress)(plugins, {
447
+ ...latestEsEntryDataDecompressed,
448
+ ...updatedEntryLevelMetaFields
449
+ });
450
+ elasticsearchEntityBatch.put({
451
+ ...latestKeys,
452
+ index: esIndex,
453
+ data: updatedLatestEntry
454
+ });
455
+ }
456
+ }
457
+ }
458
+ if (isPublished && publishedStorageEntry?.id === entry.id) {
459
+ const elasticsearchPublishedData = await transformer.getElasticsearchPublishedEntryData();
460
+ elasticsearchEntityBatch.put({
461
+ ...publishedKeys,
462
+ index: esIndex,
463
+ data: elasticsearchPublishedData
284
464
  });
285
- elasticsearchLatestData = await getESLatestEntryData(plugins, esEntry);
286
465
  }
287
-
288
466
  try {
289
- await (0, _batchWrite.batchWriteAll)({
290
- table: entity.table,
291
- items
292
- });
467
+ await entityBatch.execute();
293
468
  dataLoaders.clearAll({
294
469
  model
295
470
  });
296
471
  } catch (ex) {
297
472
  throw new _error.default(ex.message || "Could not update entry DynamoDB records.", ex.code || "UPDATE_ENTRY_ERROR", {
298
473
  error: ex,
299
- originalEntry,
300
474
  entry,
301
475
  storageEntry
302
476
  });
303
477
  }
304
-
305
- if (!elasticsearchLatestData) {
306
- return storageEntry;
307
- }
308
-
309
- const {
310
- index: esIndex
311
- } = _configurations.default.es({
312
- model
313
- });
314
-
315
478
  try {
316
- await esEntity.put(_objectSpread(_objectSpread({}, latestKeys), {}, {
317
- index: esIndex,
318
- data: elasticsearchLatestData
319
- }));
479
+ await elasticsearchEntityBatch.execute();
320
480
  } catch (ex) {
321
- throw new _error.default(ex.message || "Could not update entry DynamoDB Elasticsearch record.", ex.code || "UPDATE_ES_ENTRY_ERROR", {
481
+ throw new _error.default(ex.message || "Could not update entry DynamoDB Elasticsearch records.", ex.code || "UPDATE_ES_ENTRY_ERROR", {
322
482
  error: ex,
323
- originalEntry,
324
483
  entry
325
484
  });
326
485
  }
327
-
328
- return storageEntry;
486
+ return initialStorageEntry;
329
487
  };
330
-
331
- const deleteEntry = async (model, params) => {
332
- const {
333
- entry
334
- } = params;
335
- const partitionKey = (0, _keys.createPartitionKey)(entry);
336
- const items = await (0, _query.queryAll)({
337
- entity,
338
- partitionKey,
339
- options: {
340
- gte: " "
341
- }
488
+ const move = async (initialModel, id, folderId) => {
489
+ const model = getStorageOperationsModel(initialModel);
490
+ const partitionKey = (0, _keys.createPartitionKey)({
491
+ id,
492
+ locale: model.locale,
493
+ tenant: model.tenant
342
494
  });
343
- const esItems = await (0, _query.queryAll)({
344
- entity: esEntity,
495
+ /**
496
+ * First we need to fetch all the records in the regular DynamoDB table.
497
+ */
498
+ const queryAllParams = {
499
+ entity,
345
500
  partitionKey,
346
501
  options: {
347
502
  gte: " "
348
503
  }
504
+ };
505
+ const latestSortKey = (0, _keys.createLatestSortKey)();
506
+ const publishedSortKey = (0, _keys.createPublishedSortKey)();
507
+ const records = await (0, _dbDynamodb.queryAll)(queryAllParams);
508
+ /**
509
+ * Then update the folderId in each record and prepare it to be stored.
510
+ */
511
+ let latestRecord = undefined;
512
+ let publishedRecord = undefined;
513
+ const entityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
514
+ entity
349
515
  });
350
- const deleteItems = items.map(item => {
351
- return entity.deleteBatch({
352
- PK: item.PK,
353
- SK: item.SK
354
- });
355
- });
356
- const deleteEsItems = esItems.map(item => {
357
- return esEntity.deleteBatch({
358
- PK: item.PK,
359
- SK: item.SK
516
+ for (const record of records) {
517
+ entityBatch.put({
518
+ ...record,
519
+ location: {
520
+ ...record?.location,
521
+ folderId
522
+ }
360
523
  });
361
- });
362
524
 
525
+ /**
526
+ * We need to get the published and latest records, so we can update the Elasticsearch.
527
+ */
528
+ if (record.SK === publishedSortKey) {
529
+ publishedRecord = record;
530
+ } else if (record.SK === latestSortKey) {
531
+ latestRecord = record;
532
+ }
533
+ }
363
534
  try {
364
- await (0, _batchWrite.batchWriteAll)({
365
- table: entity.table,
366
- items: deleteItems
367
- });
535
+ await entityBatch.execute();
368
536
  dataLoaders.clearAll({
369
537
  model
370
538
  });
371
539
  } catch (ex) {
372
- throw new _error.default(ex.message || "Could not delete entry records from DynamoDB table.", ex.code || "DELETE_ENTRY_ERROR", {
540
+ throw new _error.default(ex.message || "Could not move all entry records from in the DynamoDB table.", ex.code || "MOVE_ENTRY_ERROR", {
373
541
  error: ex,
374
- entry
542
+ id
375
543
  });
376
544
  }
377
-
545
+ const esGetItems = [];
546
+ if (publishedRecord) {
547
+ esGetItems.push(esEntity.getBatch({
548
+ PK: partitionKey,
549
+ SK: publishedSortKey
550
+ }));
551
+ }
552
+ if (latestRecord) {
553
+ esGetItems.push(esEntity.getBatch({
554
+ PK: partitionKey,
555
+ SK: latestSortKey
556
+ }));
557
+ }
558
+ if (esGetItems.length === 0) {
559
+ return;
560
+ }
561
+ const esRecords = await (0, _dbDynamodb.batchReadAll)({
562
+ table: esEntity.table,
563
+ items: esGetItems
564
+ });
565
+ const esItems = (await Promise.all(esRecords.map(async record => {
566
+ if (!record) {
567
+ return null;
568
+ }
569
+ return {
570
+ ...record,
571
+ data: await (0, _apiElasticsearch.decompress)(plugins, record.data)
572
+ };
573
+ }))).filter(Boolean);
574
+ if (esItems.length === 0) {
575
+ return;
576
+ }
577
+ const elasticsearchEntityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
578
+ entity: esEntity,
579
+ put: await Promise.all(esItems.map(async item => {
580
+ return {
581
+ ...item,
582
+ data: await (0, _apiElasticsearch.compress)(plugins, {
583
+ ...item.data,
584
+ location: {
585
+ ...item.data?.location,
586
+ folderId
587
+ }
588
+ })
589
+ };
590
+ }))
591
+ });
378
592
  try {
379
- await (0, _batchWrite.batchWriteAll)({
380
- table: esEntity.table,
381
- items: deleteEsItems
382
- });
593
+ await elasticsearchEntityBatch.execute();
383
594
  } catch (ex) {
384
- throw new _error.default(ex.message || "Could not delete entry records from DynamoDB Elasticsearch table.", ex.code || "DELETE_ENTRY_ERROR", {
595
+ throw new _error.default(ex.message || "Could not move entry DynamoDB Elasticsearch records.", ex.code || "MOVE_ES_ENTRY_ERROR", {
385
596
  error: ex,
386
- entry
597
+ partitionKey
387
598
  });
388
599
  }
389
600
  };
390
-
391
- const deleteRevision = async (model, params) => {
601
+ const moveToBin = async (initialModel, params) => {
392
602
  const {
393
- entryToDelete,
394
- entryToSetAsLatest,
395
- storageEntryToSetAsLatest
603
+ entry: initialEntry,
604
+ storageEntry: initialStorageEntry
396
605
  } = params;
397
- const partitionKey = (0, _keys.createPartitionKey)(entryToDelete);
398
-
606
+ const model = getStorageOperationsModel(initialModel);
607
+ const transformer = (0, _transformations.createTransformer)({
608
+ plugins,
609
+ model,
610
+ entry: initialEntry,
611
+ storageEntry: initialStorageEntry
612
+ });
399
613
  const {
400
- index
401
- } = _configurations.default.es({
402
- model
614
+ entry,
615
+ storageEntry
616
+ } = transformer.transformEntryKeys();
617
+ const partitionKey = (0, _keys.createPartitionKey)({
618
+ id: entry.id,
619
+ locale: model.locale,
620
+ tenant: model.tenant
403
621
  });
622
+
404
623
  /**
405
- * We need published entry to delete it if necessary.
624
+ * First we need to fetch all the records in the regular DynamoDB table.
406
625
  */
626
+ const queryAllParams = {
627
+ entity,
628
+ partitionKey,
629
+ options: {
630
+ gte: " "
631
+ }
632
+ };
633
+ const latestSortKey = (0, _keys.createLatestSortKey)();
634
+ const publishedSortKey = (0, _keys.createPublishedSortKey)();
635
+ const records = await (0, _dbDynamodb.queryAll)(queryAllParams);
407
636
 
408
-
409
- const [publishedStorageEntry] = await dataLoaders.getPublishedRevisionByEntryId({
410
- model,
411
- ids: [entryToDelete.id]
412
- });
413
637
  /**
414
- * We need to delete all existing records of the given entry revision.
638
+ * Let's pick the `deleted` meta fields from the entry.
415
639
  */
640
+ const updatedEntryMetaFields = (0, _constants.pickEntryMetaFields)(entry, _constants.isDeletedEntryMetaField);
416
641
 
417
- const items = [
418
642
  /**
419
- * Delete records of given entry revision.
643
+ * Then update all the records with data received.
420
644
  */
421
- entity.deleteBatch({
422
- PK: partitionKey,
423
- SK: (0, _keys.createRevisionSortKey)(entryToDelete)
424
- })];
425
- const esItems = [];
645
+ let latestRecord = undefined;
646
+ let publishedRecord = undefined;
647
+ const entityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
648
+ entity
649
+ });
650
+ for (const record of records) {
651
+ entityBatch.put({
652
+ ...record,
653
+ ...updatedEntryMetaFields,
654
+ wbyDeleted: storageEntry.wbyDeleted,
655
+ location: storageEntry.location,
656
+ binOriginalFolderId: storageEntry.binOriginalFolderId
657
+ });
658
+
659
+ /**
660
+ * We need to get the published and latest records, so we can update the Elasticsearch.
661
+ */
662
+ if (record.SK === publishedSortKey) {
663
+ publishedRecord = record;
664
+ } else if (record.SK === latestSortKey) {
665
+ latestRecord = record;
666
+ }
667
+ }
668
+
426
669
  /**
427
- * If revision we are deleting is the published one as well, we need to delete those records as well.
670
+ * We write the records back to the primary DynamoDB table.
428
671
  */
429
-
430
- if (publishedStorageEntry && entryToDelete.id === publishedStorageEntry.id) {
431
- items.push(entity.deleteBatch({
432
- PK: partitionKey,
433
- SK: (0, _keys.createPublishedSortKey)()
672
+ try {
673
+ await entityBatch.execute();
674
+ dataLoaders.clearAll({
675
+ model
676
+ });
677
+ } catch (ex) {
678
+ throw new _error.default(ex.message || "Could mark as deleted all entry records from in the DynamoDB table.", ex.code || "MOVE_ENTRY_TO_BIN_ERROR", {
679
+ error: ex,
680
+ entry,
681
+ storageEntry
682
+ });
683
+ }
684
+
685
+ /**
686
+ * We need to get the published and latest records from Elasticsearch.
687
+ */
688
+ const esGetItems = [];
689
+ if (publishedRecord) {
690
+ esGetItems.push(esEntity.getBatch({
691
+ PK: partitionKey,
692
+ SK: publishedSortKey
434
693
  }));
435
- esItems.push(entity.deleteBatch({
694
+ }
695
+ if (latestRecord) {
696
+ esGetItems.push(esEntity.getBatch({
436
697
  PK: partitionKey,
437
- SK: (0, _keys.createPublishedSortKey)()
698
+ SK: latestSortKey
438
699
  }));
439
700
  }
701
+ if (esGetItems.length === 0) {
702
+ return;
703
+ }
704
+ const esRecords = await (0, _dbDynamodb.batchReadAll)({
705
+ table: esEntity.table,
706
+ items: esGetItems
707
+ });
708
+ const esItems = (await Promise.all(esRecords.map(async record => {
709
+ if (!record) {
710
+ return null;
711
+ }
712
+ return {
713
+ ...record,
714
+ data: await (0, _apiElasticsearch.decompress)(plugins, record.data)
715
+ };
716
+ }))).filter(Boolean);
717
+ if (esItems.length === 0) {
718
+ return;
719
+ }
440
720
 
441
- if (entryToSetAsLatest) {
442
- const esEntry = (0, _helpers.prepareEntryToIndex)({
443
- plugins,
444
- model,
445
- entry: (0, _lodash.default)(entryToSetAsLatest),
446
- storageEntry: (0, _lodash.default)(storageEntryToSetAsLatest)
721
+ /**
722
+ * We update all ES records with data received.
723
+ */
724
+ const elasticsearchEntityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
725
+ entity: esEntity
726
+ });
727
+ for (const item of esItems) {
728
+ elasticsearchEntityBatch.put({
729
+ ...item,
730
+ data: await (0, _apiElasticsearch.compress)(plugins, {
731
+ ...item.data,
732
+ ...updatedEntryMetaFields,
733
+ wbyDeleted: entry.wbyDeleted,
734
+ location: entry.location,
735
+ binOriginalFolderId: entry.binOriginalFolderId
736
+ })
737
+ });
738
+ }
739
+
740
+ /**
741
+ * We write the records back to the primary DynamoDB Elasticsearch table.
742
+ */
743
+ try {
744
+ await elasticsearchEntityBatch.execute();
745
+ } catch (ex) {
746
+ throw new _error.default(ex.message || "Could not mark as deleted entry records from DynamoDB Elasticsearch table.", ex.code || "MOVE_ENTRY_TO_BIN_ERROR", {
747
+ error: ex,
748
+ entry,
749
+ storageEntry
447
750
  });
448
- const esLatestData = await getESLatestEntryData(plugins, esEntry);
751
+ }
752
+ };
753
+ const restoreFromBin = async (initialModel, params) => {
754
+ const {
755
+ entry: initialEntry,
756
+ storageEntry: initialStorageEntry
757
+ } = params;
758
+ const model = getStorageOperationsModel(initialModel);
759
+ const transformer = (0, _transformations.createTransformer)({
760
+ plugins,
761
+ model,
762
+ entry: initialEntry,
763
+ storageEntry: initialStorageEntry
764
+ });
765
+ const {
766
+ entry,
767
+ storageEntry
768
+ } = transformer.transformEntryKeys();
769
+
770
+ /**
771
+ * Let's pick the `restored` meta fields from the storage entry.
772
+ */
773
+ const updatedEntryMetaFields = (0, _constants.pickEntryMetaFields)(entry, _constants.isRestoredEntryMetaField);
774
+ const partitionKey = (0, _keys.createPartitionKey)({
775
+ id: entry.id,
776
+ locale: model.locale,
777
+ tenant: model.tenant
778
+ });
779
+
780
+ /**
781
+ * First we need to fetch all the records in the regular DynamoDB table.
782
+ */
783
+ const queryAllParams = {
784
+ entity,
785
+ partitionKey,
786
+ options: {
787
+ gte: " "
788
+ }
789
+ };
790
+ const latestSortKey = (0, _keys.createLatestSortKey)();
791
+ const publishedSortKey = (0, _keys.createPublishedSortKey)();
792
+ const records = await (0, _dbDynamodb.queryAll)(queryAllParams);
793
+
794
+ /**
795
+ * Then update all the records with data received.
796
+ */
797
+ let latestRecord = undefined;
798
+ let publishedRecord = undefined;
799
+ const entityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
800
+ entity
801
+ });
802
+ for (const record of records) {
803
+ entityBatch.put({
804
+ ...record,
805
+ ...updatedEntryMetaFields,
806
+ wbyDeleted: storageEntry.wbyDeleted,
807
+ location: storageEntry.location,
808
+ binOriginalFolderId: storageEntry.binOriginalFolderId
809
+ });
810
+
449
811
  /**
450
- * In the end we need to set the new latest entry
812
+ * We need to get the published and latest records, so we can update the Elasticsearch.
451
813
  */
814
+ if (record.SK === publishedSortKey) {
815
+ publishedRecord = record;
816
+ } else if (record.SK === latestSortKey) {
817
+ latestRecord = record;
818
+ }
819
+ }
820
+
821
+ /**
822
+ * We write the records back to the primary DynamoDB table.
823
+ */
824
+ try {
825
+ await entityBatch.execute();
826
+ dataLoaders.clearAll({
827
+ model
828
+ });
829
+ } catch (ex) {
830
+ throw new _error.default(ex.message || "Could not restore all entry records from in the DynamoDB table.", ex.code || "RESTORE_ENTRY_ERROR", {
831
+ error: ex,
832
+ entry,
833
+ storageEntry
834
+ });
835
+ }
452
836
 
453
- items.push(entity.putBatch(_objectSpread(_objectSpread({}, storageEntryToSetAsLatest), {}, {
837
+ /**
838
+ * We need to get the published and latest records from Elasticsearch.
839
+ */
840
+ const esGetItems = [];
841
+ if (publishedRecord) {
842
+ esGetItems.push(esEntity.getBatch({
454
843
  PK: partitionKey,
455
- SK: (0, _keys.createLatestSortKey)(),
456
- TYPE: createLatestType()
457
- })));
458
- esItems.push(esEntity.putBatch({
844
+ SK: publishedSortKey
845
+ }));
846
+ }
847
+ if (latestRecord) {
848
+ esGetItems.push(esEntity.getBatch({
459
849
  PK: partitionKey,
460
- SK: (0, _keys.createLatestSortKey)(),
461
- index,
462
- data: esLatestData
850
+ SK: latestSortKey
463
851
  }));
464
852
  }
853
+ const esRecords = await (0, _dbDynamodb.batchReadAll)({
854
+ table: esEntity.table,
855
+ items: esGetItems
856
+ });
857
+ const esItems = (await Promise.all(esRecords.map(async record => {
858
+ if (!record) {
859
+ return null;
860
+ }
861
+ return {
862
+ ...record,
863
+ data: await (0, _apiElasticsearch.decompress)(plugins, record.data)
864
+ };
865
+ }))).filter(Boolean);
866
+ if (esItems.length === 0) {
867
+ return initialStorageEntry;
868
+ }
465
869
 
870
+ /**
871
+ * We update all ES records with data received.
872
+ */
873
+ const elasticsearchEntityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
874
+ entity: esEntity
875
+ });
876
+ for (const item of esItems) {
877
+ elasticsearchEntityBatch.put({
878
+ ...item,
879
+ data: await (0, _apiElasticsearch.compress)(plugins, {
880
+ ...item.data,
881
+ ...updatedEntryMetaFields,
882
+ wbyDeleted: entry.wbyDeleted,
883
+ location: entry.location,
884
+ binOriginalFolderId: entry.binOriginalFolderId
885
+ })
886
+ });
887
+ }
888
+
889
+ /**
890
+ * We write the records back to the primary DynamoDB Elasticsearch table.
891
+ */
466
892
  try {
467
- await (0, _batchWrite.batchWriteAll)({
468
- table: entity.table,
469
- items
893
+ await elasticsearchEntityBatch.execute();
894
+ } catch (ex) {
895
+ throw new _error.default(ex.message || "Could not restore entry records from DynamoDB Elasticsearch table.", ex.code || "RESTORE_ENTRY_ERROR", {
896
+ error: ex,
897
+ entry,
898
+ storageEntry
470
899
  });
900
+ }
901
+ return initialStorageEntry;
902
+ };
903
+ const deleteEntry = async (initialModel, params) => {
904
+ const {
905
+ entry
906
+ } = params;
907
+ const id = entry.id || entry.entryId;
908
+ const model = getStorageOperationsModel(initialModel);
909
+ const partitionKey = (0, _keys.createPartitionKey)({
910
+ id,
911
+ locale: model.locale,
912
+ tenant: model.tenant
913
+ });
914
+ const items = await (0, _dbDynamodb.queryAll)({
915
+ entity,
916
+ partitionKey,
917
+ options: {
918
+ gte: " "
919
+ }
920
+ });
921
+ const esItems = await (0, _dbDynamodb.queryAll)({
922
+ entity: esEntity,
923
+ partitionKey,
924
+ options: {
925
+ gte: " "
926
+ }
927
+ });
928
+ const entityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
929
+ entity,
930
+ delete: items.map(item => {
931
+ return {
932
+ PK: item.PK,
933
+ SK: item.SK
934
+ };
935
+ })
936
+ });
937
+ const elasticsearchEntityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
938
+ entity: esEntity,
939
+ delete: esItems.map(item => {
940
+ return {
941
+ PK: item.PK,
942
+ SK: item.SK
943
+ };
944
+ })
945
+ });
946
+ try {
947
+ await entityBatch.execute();
471
948
  dataLoaders.clearAll({
472
949
  model
473
950
  });
474
951
  } catch (ex) {
475
- throw new _error.default(ex.message || "Could not batch write entry records to DynamoDB table.", ex.code || "DELETE_REVISION_ERROR", {
952
+ throw new _error.default(ex.message || "Could not destroy entry records from DynamoDB table.", ex.code || "DELETE_ENTRY_ERROR", {
953
+ error: ex,
954
+ id
955
+ });
956
+ }
957
+ try {
958
+ await elasticsearchEntityBatch.execute();
959
+ } catch (ex) {
960
+ throw new _error.default(ex.message || "Could not destroy entry records from DynamoDB Elasticsearch table.", ex.code || "DELETE_ENTRY_ERROR", {
476
961
  error: ex,
477
- entryToDelete,
478
- entryToSetAsLatest,
479
- storageEntryToSetAsLatest
962
+ id
480
963
  });
481
964
  }
965
+ };
966
+ const deleteRevision = async (initialModel, params) => {
967
+ const {
968
+ entry,
969
+ latestEntry,
970
+ latestStorageEntry: initialLatestStorageEntry
971
+ } = params;
972
+ const model = getStorageOperationsModel(initialModel);
973
+ const partitionKey = (0, _keys.createPartitionKey)({
974
+ id: entry.id,
975
+ locale: model.locale,
976
+ tenant: model.tenant
977
+ });
978
+ const {
979
+ index
980
+ } = _configurations.configurations.es({
981
+ model
982
+ });
983
+ /**
984
+ * We need published entry to delete it if necessary.
985
+ */
986
+ const [publishedStorageEntry] = await dataLoaders.getPublishedRevisionByEntryId({
987
+ model,
988
+ ids: [entry.id]
989
+ });
990
+ /**
991
+ * We need to delete all existing records of the given entry revision.
992
+ */
993
+ const entityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
994
+ entity,
995
+ delete: [{
996
+ PK: partitionKey,
997
+ SK: (0, _keys.createRevisionSortKey)(entry)
998
+ }]
999
+ });
1000
+ const elasticsearchEntityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
1001
+ entity: esEntity
1002
+ });
482
1003
 
483
- if (esItems.length === 0) {
484
- return;
1004
+ /**
1005
+ * If revision we are deleting is the published one as well, we need to delete those records as well.
1006
+ */
1007
+ if (publishedStorageEntry?.id === entry.id) {
1008
+ entityBatch.delete({
1009
+ PK: partitionKey,
1010
+ SK: (0, _keys.createPublishedSortKey)()
1011
+ });
1012
+ elasticsearchEntityBatch.delete({
1013
+ PK: partitionKey,
1014
+ SK: (0, _keys.createPublishedSortKey)()
1015
+ });
485
1016
  }
1017
+ if (latestEntry && initialLatestStorageEntry) {
1018
+ const latestStorageEntry = convertToStorageEntry({
1019
+ storageEntry: initialLatestStorageEntry,
1020
+ model
1021
+ });
1022
+
1023
+ /**
1024
+ * In the end we need to set the new latest entry.
1025
+ */
1026
+ entityBatch.put({
1027
+ ...latestStorageEntry,
1028
+ PK: partitionKey,
1029
+ SK: (0, _keys.createLatestSortKey)(),
1030
+ TYPE: (0, _recordType.createLatestRecordType)()
1031
+ });
486
1032
 
1033
+ /**
1034
+ * Also perform an update on the actual revision. This is needed
1035
+ * because of updates on the entry-level meta fields.
1036
+ */
1037
+ entityBatch.put({
1038
+ ...latestStorageEntry,
1039
+ PK: (0, _keys.createPartitionKey)({
1040
+ id: initialLatestStorageEntry.id,
1041
+ locale: model.locale,
1042
+ tenant: model.tenant
1043
+ }),
1044
+ SK: (0, _keys.createRevisionSortKey)(initialLatestStorageEntry),
1045
+ TYPE: (0, _recordType.createRecordType)()
1046
+ });
1047
+ const latestTransformer = (0, _transformations.createTransformer)({
1048
+ plugins,
1049
+ model,
1050
+ entry: latestEntry,
1051
+ storageEntry: initialLatestStorageEntry
1052
+ });
1053
+ const esLatestData = await latestTransformer.getElasticsearchLatestEntryData();
1054
+ elasticsearchEntityBatch.put({
1055
+ PK: partitionKey,
1056
+ SK: (0, _keys.createLatestSortKey)(),
1057
+ index,
1058
+ data: esLatestData
1059
+ });
1060
+ }
487
1061
  try {
488
- await (0, _batchWrite.batchWriteAll)({
489
- table: esEntity.table,
490
- items: esItems
1062
+ await entityBatch.execute();
1063
+ dataLoaders.clearAll({
1064
+ model
1065
+ });
1066
+ } catch (ex) {
1067
+ throw new _error.default(ex.message || "Could not batch write entry records to DynamoDB table.", ex.code || "DELETE_REVISION_ERROR", {
1068
+ error: ex,
1069
+ entry,
1070
+ latestEntry,
1071
+ initialLatestStorageEntry
491
1072
  });
1073
+ }
1074
+ try {
1075
+ await elasticsearchEntityBatch.execute();
492
1076
  } catch (ex) {
493
1077
  throw new _error.default(ex.message || "Could not batch write entry records to DynamoDB Elasticsearch table.", ex.code || "DELETE_REVISION_ERROR", {
494
1078
  error: ex,
495
- entryToDelete,
496
- entryToSetAsLatest,
497
- storageEntryToSetAsLatest
1079
+ entry,
1080
+ latestEntry,
1081
+ initialLatestStorageEntry
498
1082
  });
499
1083
  }
500
1084
  };
501
-
502
- const list = async (model, params) => {
503
- const limit = (0, _limit.createLimit)(params.limit, 50);
504
- const body = (0, _helpers.createElasticsearchQueryBody)({
1085
+ const deleteMultipleEntries = async (initialModel, params) => {
1086
+ const {
1087
+ entries
1088
+ } = params;
1089
+ const model = getStorageOperationsModel(initialModel);
1090
+ /**
1091
+ * First we need all the revisions of the entries we want to delete.
1092
+ */
1093
+ const revisions = await dataLoaders.getAllEntryRevisions({
505
1094
  model,
506
- args: _objectSpread(_objectSpread({}, params), {}, {
507
- limit
508
- }),
509
- plugins,
510
- parentPath: "values"
1095
+ ids: entries
511
1096
  });
512
- let response;
1097
+ /**
1098
+ * Then we need to construct the queries for all the revisions and entries.
1099
+ */
1100
+
1101
+ const entityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
1102
+ entity
1103
+ });
1104
+ const elasticsearchEntityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
1105
+ entity: esEntity
1106
+ });
1107
+ for (const id of entries) {
1108
+ /**
1109
+ * Latest item.
1110
+ */
1111
+ entityBatch.delete({
1112
+ PK: (0, _keys.createPartitionKey)({
1113
+ id,
1114
+ locale: model.locale,
1115
+ tenant: model.tenant
1116
+ }),
1117
+ SK: "L"
1118
+ });
1119
+ elasticsearchEntityBatch.delete({
1120
+ PK: (0, _keys.createPartitionKey)({
1121
+ id,
1122
+ locale: model.locale,
1123
+ tenant: model.tenant
1124
+ }),
1125
+ SK: "L"
1126
+ });
513
1127
 
1128
+ /**
1129
+ * Published item.
1130
+ */
1131
+ entityBatch.delete({
1132
+ PK: (0, _keys.createPartitionKey)({
1133
+ id,
1134
+ locale: model.locale,
1135
+ tenant: model.tenant
1136
+ }),
1137
+ SK: "P"
1138
+ });
1139
+ elasticsearchEntityBatch.delete({
1140
+ PK: (0, _keys.createPartitionKey)({
1141
+ id,
1142
+ locale: model.locale,
1143
+ tenant: model.tenant
1144
+ }),
1145
+ SK: "P"
1146
+ });
1147
+ }
1148
+ /**
1149
+ * Exact revisions of all the entries
1150
+ */
1151
+ for (const revision of revisions) {
1152
+ entityBatch.delete({
1153
+ PK: (0, _keys.createPartitionKey)({
1154
+ id: revision.id,
1155
+ locale: model.locale,
1156
+ tenant: model.tenant
1157
+ }),
1158
+ SK: (0, _keys.createRevisionSortKey)({
1159
+ version: revision.version
1160
+ })
1161
+ });
1162
+ }
1163
+ await entityBatch.execute();
1164
+ await elasticsearchEntityBatch.execute();
1165
+ };
1166
+ const list = async (initialModel, params) => {
1167
+ const model = getStorageOperationsModel(initialModel);
1168
+ const limit = (0, _apiElasticsearch.createLimit)(params.limit, 50);
514
1169
  const {
515
1170
  index
516
- } = _configurations.default.es({
1171
+ } = _configurations.configurations.es({
517
1172
  model
518
1173
  });
519
-
1174
+ const body = (0, _body.createElasticsearchBody)({
1175
+ model,
1176
+ params: {
1177
+ ...params,
1178
+ limit,
1179
+ after: (0, _apiElasticsearch.decodeCursor)(params.after)
1180
+ },
1181
+ plugins
1182
+ });
1183
+ let response;
520
1184
  try {
521
1185
  response = await elasticsearch.search({
522
1186
  index,
523
1187
  body
524
1188
  });
525
- } catch (ex) {
526
- throw new _error.default(ex.message, ex.code || "ELASTICSEARCH_ERROR", {
527
- error: ex,
1189
+ } catch (error) {
1190
+ /**
1191
+ * We will silently ignore the `index_not_found_exception` error and return an empty result set.
1192
+ * This is because the index might not exist yet, and we don't want to throw an error.
1193
+ */
1194
+ if ((0, _shouldIgnoreEsResponseError.shouldIgnoreEsResponseError)(error)) {
1195
+ (0, _logIgnoredEsResponseError.logIgnoredEsResponseError)({
1196
+ error,
1197
+ model,
1198
+ indexName: index
1199
+ });
1200
+ return {
1201
+ hasMoreItems: false,
1202
+ totalCount: 0,
1203
+ cursor: null,
1204
+ items: []
1205
+ };
1206
+ }
1207
+ throw new _error.default(error.message, error.code || "ELASTICSEARCH_ERROR", {
1208
+ error,
528
1209
  index,
529
- body
1210
+ body,
1211
+ model
530
1212
  });
531
1213
  }
532
-
533
1214
  const {
534
1215
  hits,
535
1216
  total
536
- } = response.body.hits;
1217
+ } = response?.body?.hits || {};
537
1218
  const items = (0, _helpers.extractEntriesFromIndex)({
538
1219
  plugins,
539
1220
  model,
540
1221
  entries: hits.map(item => item._source)
1222
+ }).map(item => {
1223
+ return (0, _convertEntryKeys.convertEntryKeysFromStorage)({
1224
+ model,
1225
+ entry: item
1226
+ });
541
1227
  });
542
1228
  const hasMoreItems = items.length > limit;
543
-
544
1229
  if (hasMoreItems) {
545
1230
  /**
546
1231
  * Remove the last item from results, we don't want to include it.
@@ -551,9 +1236,7 @@ const createEntriesStorageOperations = params => {
551
1236
  * Cursor is the `sort` value of the last item in the array.
552
1237
  * https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html#search-after
553
1238
  */
554
-
555
-
556
- const cursor = items.length > 0 ? (0, _cursors.encodeCursor)(hits[items.length - 1].sort) : null;
1239
+ const cursor = items.length > 0 ? (0, _apiElasticsearch.encodeCursor)(hits[items.length - 1].sort) || null : null;
557
1240
  return {
558
1241
  hasMoreItems,
559
1242
  totalCount: total.value,
@@ -561,158 +1244,270 @@ const createEntriesStorageOperations = params => {
561
1244
  items
562
1245
  };
563
1246
  };
564
-
565
- const get = async (model, params) => {
1247
+ const get = async (initialModel, params) => {
1248
+ const model = getStorageOperationsModel(initialModel);
566
1249
  const {
567
1250
  items
568
- } = await list(model, _objectSpread(_objectSpread({}, params), {}, {
1251
+ } = await list(model, {
1252
+ ...params,
569
1253
  limit: 1
570
- }));
571
-
572
- if (items.length === 0) {
573
- return null;
574
- }
575
-
576
- return items.shift();
1254
+ });
1255
+ return items.shift() || null;
577
1256
  };
578
-
579
- const publish = async (model, params) => {
1257
+ const publish = async (initialModel, params) => {
580
1258
  const {
581
- entry,
582
- storageEntry
1259
+ entry: initialEntry,
1260
+ storageEntry: initialStorageEntry
583
1261
  } = params;
584
- /**
585
- * We need currently published entry to check if need to remove it.
586
- */
587
-
588
- const [publishedStorageEntry] = await dataLoaders.getPublishedRevisionByEntryId({
1262
+ const model = getStorageOperationsModel(initialModel);
1263
+ const transformer = (0, _transformations.createTransformer)({
1264
+ plugins,
589
1265
  model,
590
- ids: [entry.id]
1266
+ entry: initialEntry,
1267
+ storageEntry: initialStorageEntry
591
1268
  });
1269
+ const {
1270
+ entry,
1271
+ storageEntry
1272
+ } = transformer.transformEntryKeys();
592
1273
  const revisionKeys = {
593
- PK: (0, _keys.createPartitionKey)(entry),
1274
+ PK: (0, _keys.createPartitionKey)({
1275
+ id: entry.id,
1276
+ locale: model.locale,
1277
+ tenant: model.tenant
1278
+ }),
594
1279
  SK: (0, _keys.createRevisionSortKey)(entry)
595
1280
  };
596
1281
  const latestKeys = {
597
- PK: (0, _keys.createPartitionKey)(entry),
1282
+ PK: (0, _keys.createPartitionKey)({
1283
+ id: entry.id,
1284
+ locale: model.locale,
1285
+ tenant: model.tenant
1286
+ }),
598
1287
  SK: (0, _keys.createLatestSortKey)()
599
1288
  };
600
1289
  const publishedKeys = {
601
- PK: (0, _keys.createPartitionKey)(entry),
1290
+ PK: (0, _keys.createPartitionKey)({
1291
+ id: entry.id,
1292
+ locale: model.locale,
1293
+ tenant: model.tenant
1294
+ }),
602
1295
  SK: (0, _keys.createPublishedSortKey)()
603
1296
  };
604
1297
  let latestEsEntry = null;
605
-
606
1298
  try {
607
- latestEsEntry = await (0, _get.get)({
1299
+ latestEsEntry = await (0, _dbDynamodb.getClean)({
608
1300
  entity: esEntity,
609
1301
  keys: latestKeys
610
1302
  });
611
1303
  } catch (ex) {
612
- throw new _error.default(ex.message || "Could not read Elasticsearch latest or published data.", ex.code || "PUBLISH_BATCH_READ", {
1304
+ throw new _error.default(ex.message || "Could not read Elasticsearch latest data.", ex.code || "PUBLISH_LATEST_READ", {
613
1305
  error: ex,
614
1306
  latestKeys: latestKeys,
615
1307
  publishedKeys: publishedKeys
616
1308
  });
617
1309
  }
1310
+ if (!latestEsEntry) {
1311
+ throw new _error.default(`Could not publish entry. Could not load latest ("L") record (ES table).`, "PUBLISH_ERROR", {
1312
+ entry
1313
+ });
1314
+ }
1315
+
1316
+ /**
1317
+ * We need the latest entry to check if it needs to be updated as well in the Elasticsearch.
1318
+ */
1319
+ const [latestStorageEntry] = await dataLoaders.getLatestRevisionByEntryId({
1320
+ model,
1321
+ ids: [entry.id]
1322
+ });
1323
+ if (!latestStorageEntry) {
1324
+ throw new _error.default(`Could not publish entry. Could not load latest ("L") record.`, "PUBLISH_ERROR", {
1325
+ entry
1326
+ });
1327
+ }
618
1328
 
619
- const items = [entity.putBatch(_objectSpread(_objectSpread(_objectSpread({}, storageEntry), revisionKeys), {}, {
620
- TYPE: createType()
621
- }))];
622
- const esItems = [];
1329
+ /**
1330
+ * We need currently published entry to check if need to remove it.
1331
+ */
1332
+ const [publishedStorageEntry] = await dataLoaders.getPublishedRevisionByEntryId({
1333
+ model,
1334
+ ids: [entry.id]
1335
+ });
623
1336
 
1337
+ // 1. Update REV# and P records with new data.
1338
+ const entityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
1339
+ entity,
1340
+ put: [{
1341
+ ...storageEntry,
1342
+ ...revisionKeys,
1343
+ TYPE: (0, _recordType.createRecordType)()
1344
+ }, {
1345
+ ...storageEntry,
1346
+ ...publishedKeys,
1347
+ TYPE: (0, _recordType.createPublishedRecordType)()
1348
+ }]
1349
+ });
1350
+ const elasticsearchEntityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
1351
+ entity: esEntity
1352
+ });
624
1353
  const {
625
- index
626
- } = _configurations.default.es({
1354
+ index: esIndex
1355
+ } = _configurations.configurations.es({
627
1356
  model
628
1357
  });
629
1358
 
630
- if (publishedStorageEntry) {
631
- /**
632
- * If there is a `published` entry already, we need to set it to `unpublished`. We need to
633
- * execute two updates: update the previously published entry's status and the published entry record.
634
- * DynamoDB does not support `batchUpdate` - so here we load the previously published
635
- * entry's data to update its status within a batch operation. If, hopefully,
636
- * they introduce a true update batch operation, remove this `read` call.
637
- */
638
- const [previouslyPublishedEntry] = await dataLoaders.getRevisionById({
639
- model,
640
- ids: [publishedStorageEntry.id]
1359
+ // 2. When it comes to the latest record, we need to perform a couple of different
1360
+ // updates, based on whether the entry being published is the latest revision or not.
1361
+ const publishedRevisionId = publishedStorageEntry?.id;
1362
+ const publishingLatestRevision = latestStorageEntry?.id === entry.id;
1363
+ if (publishingLatestRevision) {
1364
+ // 2.1 If we're publishing the latest revision, we first need to update the L record.
1365
+ entityBatch.put({
1366
+ ...storageEntry,
1367
+ ...latestKeys
641
1368
  });
642
- items.push(
643
- /**
644
- * Update currently published entry (unpublish it)
645
- */
646
- entity.putBatch(_objectSpread(_objectSpread({}, previouslyPublishedEntry), {}, {
647
- status: _types.CONTENT_ENTRY_STATUS.UNPUBLISHED,
648
- savedOn: entry.savedOn,
649
- TYPE: createType(),
650
- PK: (0, _keys.createPartitionKey)(publishedStorageEntry),
651
- SK: (0, _keys.createRevisionSortKey)(publishedStorageEntry)
652
- })));
1369
+
1370
+ // 2.2 Additionally, if we have a previously published entry, we need to mark it as unpublished.
1371
+ // Note that we need to take re-publishing into account (same published revision being
1372
+ // published again), in which case the below code does not apply. This is because the
1373
+ // required updates were already applied above.
1374
+ if (publishedStorageEntry) {
1375
+ const isRepublishing = publishedStorageEntry.id === entry.id;
1376
+ if (!isRepublishing) {
1377
+ /**
1378
+ * Update currently published entry (unpublish it)
1379
+ */
1380
+ entityBatch.put({
1381
+ ...publishedStorageEntry,
1382
+ status: _types.CONTENT_ENTRY_STATUS.UNPUBLISHED,
1383
+ TYPE: (0, _recordType.createRecordType)(),
1384
+ PK: (0, _keys.createPartitionKey)(publishedStorageEntry),
1385
+ SK: (0, _keys.createRevisionSortKey)(publishedStorageEntry)
1386
+ });
1387
+ }
1388
+ }
1389
+ } else {
1390
+ // 2.3 If the published revision is not the latest one, the situation is a bit
1391
+ // more complex. We first need to update the L and REV# records with the new
1392
+ // values of *only entry-level* meta fields.
1393
+ const updatedEntryLevelMetaFields = (0, _constants.pickEntryMetaFields)(entry, _constants.isEntryLevelEntryMetaField);
1394
+
1395
+ // 2.4 Update L record. Apart from updating the entry-level meta fields, we also need
1396
+ // to change the status from "published" to "unpublished" (if the status is set to "published").
1397
+ let latestRevisionStatus = latestStorageEntry.status;
1398
+ if (latestRevisionStatus === _types.CONTENT_ENTRY_STATUS.PUBLISHED) {
1399
+ latestRevisionStatus = _types.CONTENT_ENTRY_STATUS.UNPUBLISHED;
1400
+ }
1401
+ const latestStorageEntryFields = {
1402
+ ...latestStorageEntry,
1403
+ ...updatedEntryLevelMetaFields,
1404
+ status: latestRevisionStatus
1405
+ };
1406
+ entityBatch.put({
1407
+ ...latestStorageEntryFields,
1408
+ PK: (0, _keys.createPartitionKey)(latestStorageEntry),
1409
+ SK: (0, _keys.createLatestSortKey)(),
1410
+ TYPE: (0, _recordType.createLatestRecordType)()
1411
+ });
1412
+
1413
+ // 2.5 Update REV# record.
1414
+ entityBatch.put({
1415
+ ...latestStorageEntryFields,
1416
+ PK: (0, _keys.createPartitionKey)(latestStorageEntry),
1417
+ SK: (0, _keys.createRevisionSortKey)(latestStorageEntry),
1418
+ TYPE: (0, _recordType.createRecordType)()
1419
+ });
1420
+
1421
+ // 2.6 Additionally, if we have a previously published entry, we need to mark it as unpublished.
1422
+ // Note that we need to take re-publishing into account (same published revision being
1423
+ // published again), in which case the below code does not apply. This is because the
1424
+ // required updates were already applied above.
1425
+ if (publishedStorageEntry) {
1426
+ const isRepublishing = publishedStorageEntry.id === entry.id;
1427
+ const publishedRevisionDifferentFromLatest = publishedRevisionId !== latestStorageEntry.id;
1428
+ if (!isRepublishing && publishedRevisionDifferentFromLatest) {
1429
+ entityBatch.put({
1430
+ ...publishedStorageEntry,
1431
+ PK: (0, _keys.createPartitionKey)(publishedStorageEntry),
1432
+ SK: (0, _keys.createRevisionSortKey)(publishedStorageEntry),
1433
+ TYPE: (0, _recordType.createRecordType)(),
1434
+ status: _types.CONTENT_ENTRY_STATUS.UNPUBLISHED
1435
+ });
1436
+ }
1437
+ }
653
1438
  }
654
- /**
655
- * Update the helper item in DB with the new published entry
656
- */
657
1439
 
1440
+ // 3. Update records in ES -> DDB table.
658
1441
 
659
- items.push(entity.putBatch(_objectSpread(_objectSpread(_objectSpread({}, storageEntry), publishedKeys), {}, {
660
- TYPE: createPublishedType()
661
- })));
662
1442
  /**
663
- * We need the latest entry to check if it neds to be updated as well in the Elasticsearch.
1443
+ * Update the published revision entry in ES.
664
1444
  */
665
-
666
- const [latestStorageEntry] = await dataLoaders.getLatestRevisionByEntryId({
667
- model,
668
- ids: [entry.id]
1445
+ const esPublishedData = await transformer.getElasticsearchPublishedEntryData();
1446
+ elasticsearchEntityBatch.put({
1447
+ ...publishedKeys,
1448
+ index: esIndex,
1449
+ data: esPublishedData
669
1450
  });
1451
+
670
1452
  /**
671
- * If we are publishing the latest revision, let's also update the latest revision's status in ES.
1453
+ * Need to decompress the data from Elasticsearch DynamoDB table.
1454
+ *
1455
+ * No need to transform it for the storage because it was fetched
1456
+ * directly from the Elasticsearch table, where it sits transformed.
672
1457
  */
1458
+ const latestEsEntryDataDecompressed = await (0, _apiElasticsearch.decompress)(plugins, latestEsEntry.data);
1459
+ if (publishingLatestRevision) {
1460
+ const updatedMetaFields = (0, _constants.pickEntryMetaFields)(entry);
1461
+ const latestTransformer = (0, _transformations.createTransformer)({
1462
+ plugins,
1463
+ model,
1464
+ transformedToIndex: {
1465
+ ...latestEsEntryDataDecompressed,
1466
+ status: _types.CONTENT_ENTRY_STATUS.PUBLISHED,
1467
+ locked: true,
1468
+ ...updatedMetaFields
1469
+ }
1470
+ });
1471
+ elasticsearchEntityBatch.put({
1472
+ index: esIndex,
1473
+ PK: (0, _keys.createPartitionKey)(latestEsEntryDataDecompressed),
1474
+ SK: (0, _keys.createLatestSortKey)(),
1475
+ data: await latestTransformer.getElasticsearchLatestEntryData()
1476
+ });
1477
+ } else {
1478
+ const updatedEntryLevelMetaFields = (0, _constants.pickEntryMetaFields)(entry, _constants.isEntryLevelEntryMetaField);
673
1479
 
674
- if (latestStorageEntry && latestStorageEntry.id === entry.id) {
675
1480
  /**
676
- * Need to decompress the data from Elasticsearch DynamoDB table.
1481
+ * Update the Elasticsearch table to propagate changes to the Elasticsearch.
677
1482
  */
678
- const latestEsEntryDataDecompressed = await (0, _compression.decompress)(plugins, latestEsEntry.data);
679
- esItems.push(esEntity.putBatch({
680
- index,
681
- PK: (0, _keys.createPartitionKey)(latestEsEntryDataDecompressed),
682
- SK: (0, _keys.createLatestSortKey)(),
683
- data: _objectSpread(_objectSpread({}, latestEsEntryDataDecompressed), {}, {
684
- status: _types.CONTENT_ENTRY_STATUS.PUBLISHED,
685
- locked: true,
686
- savedOn: entry.savedOn,
687
- publishedOn: entry.publishedOn
688
- })
689
- }));
1483
+ const latestEsEntry = await (0, _dbDynamodb.getClean)({
1484
+ entity: esEntity,
1485
+ keys: latestKeys
1486
+ });
1487
+ if (latestEsEntry) {
1488
+ const latestEsEntryDataDecompressed = await (0, _apiElasticsearch.decompress)(plugins, latestEsEntry.data);
1489
+ let latestRevisionStatus = latestEsEntryDataDecompressed.status;
1490
+ if (latestRevisionStatus === _types.CONTENT_ENTRY_STATUS.PUBLISHED) {
1491
+ latestRevisionStatus = _types.CONTENT_ENTRY_STATUS.UNPUBLISHED;
1492
+ }
1493
+ const updatedLatestEntry = await (0, _apiElasticsearch.compress)(plugins, {
1494
+ ...latestEsEntryDataDecompressed,
1495
+ ...updatedEntryLevelMetaFields,
1496
+ status: latestRevisionStatus
1497
+ });
1498
+ elasticsearchEntityBatch.put({
1499
+ ...latestKeys,
1500
+ index: esIndex,
1501
+ data: updatedLatestEntry
1502
+ });
1503
+ }
690
1504
  }
691
1505
 
692
- const preparedEntryData = (0, _helpers.prepareEntryToIndex)({
693
- plugins,
694
- model,
695
- entry: (0, _lodash.default)(entry),
696
- storageEntry: (0, _lodash.default)(storageEntry)
697
- });
698
- /**
699
- * Update the published revision entry in ES.
700
- */
701
-
702
- const esLatestData = await getESPublishedEntryData(plugins, preparedEntryData);
703
- esItems.push(esEntity.putBatch(_objectSpread(_objectSpread({}, publishedKeys), {}, {
704
- index,
705
- data: esLatestData
706
- })));
707
1506
  /**
708
1507
  * Finally, execute regular table batch.
709
1508
  */
710
-
711
1509
  try {
712
- await (0, _batchWrite.batchWriteAll)({
713
- table: entity.table,
714
- items
715
- });
1510
+ await entityBatch.execute();
716
1511
  dataLoaders.clearAll({
717
1512
  model
718
1513
  });
@@ -727,13 +1522,8 @@ const createEntriesStorageOperations = params => {
727
1522
  /**
728
1523
  * And Elasticsearch table batch.
729
1524
  */
730
-
731
-
732
1525
  try {
733
- await (0, _batchWrite.batchWriteAll)({
734
- table: esEntity.table,
735
- items: esItems
736
- });
1526
+ await elasticsearchEntityBatch.execute();
737
1527
  } catch (ex) {
738
1528
  throw new _error.default(ex.message || "Could not store publish entry records in DynamoDB Elasticsearch table.", ex.code || "PUBLISH_ES_ERROR", {
739
1529
  error: ex,
@@ -742,71 +1532,87 @@ const createEntriesStorageOperations = params => {
742
1532
  publishedStorageEntry
743
1533
  });
744
1534
  }
745
-
746
- return storageEntry;
1535
+ return initialStorageEntry;
747
1536
  };
748
-
749
- const unpublish = async (model, params) => {
1537
+ const unpublish = async (initialModel, params) => {
1538
+ const {
1539
+ entry: initialEntry,
1540
+ storageEntry: initialStorageEntry
1541
+ } = params;
1542
+ const model = getStorageOperationsModel(initialModel);
1543
+ const transformer = (0, _transformations.createTransformer)({
1544
+ plugins,
1545
+ model,
1546
+ entry: initialEntry,
1547
+ storageEntry: initialStorageEntry
1548
+ });
750
1549
  const {
751
1550
  entry,
752
1551
  storageEntry
753
- } = params;
1552
+ } = await transformer.transformEntryKeys();
1553
+
754
1554
  /**
755
1555
  * We need the latest entry to check if it needs to be updated.
756
1556
  */
757
-
758
1557
  const [latestStorageEntry] = await dataLoaders.getLatestRevisionByEntryId({
759
1558
  model,
760
1559
  ids: [entry.id]
761
1560
  });
762
- const partitionKey = (0, _keys.createPartitionKey)(entry);
763
- const items = [entity.deleteBatch({
764
- PK: partitionKey,
765
- SK: (0, _keys.createPublishedSortKey)()
766
- }), entity.putBatch(_objectSpread(_objectSpread({}, storageEntry), {}, {
767
- PK: partitionKey,
768
- SK: (0, _keys.createRevisionSortKey)(entry),
769
- TYPE: createType()
770
- }))];
771
- const esItems = [esEntity.deleteBatch({
772
- PK: partitionKey,
773
- SK: (0, _keys.createPublishedSortKey)()
774
- })];
1561
+ const partitionKey = (0, _keys.createPartitionKey)({
1562
+ id: entry.id,
1563
+ locale: model.locale,
1564
+ tenant: model.tenant
1565
+ });
1566
+ const entityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
1567
+ entity,
1568
+ put: [{
1569
+ ...storageEntry,
1570
+ PK: partitionKey,
1571
+ SK: (0, _keys.createRevisionSortKey)(entry),
1572
+ TYPE: (0, _recordType.createRecordType)()
1573
+ }],
1574
+ delete: [{
1575
+ PK: partitionKey,
1576
+ SK: (0, _keys.createPublishedSortKey)()
1577
+ }]
1578
+ });
1579
+ const elasticsearchEntityBatch = (0, _dbDynamodb.createEntityWriteBatch)({
1580
+ entity: esEntity,
1581
+ delete: [{
1582
+ PK: partitionKey,
1583
+ SK: (0, _keys.createPublishedSortKey)()
1584
+ }]
1585
+ });
1586
+
775
1587
  /**
776
- * If we are unpublishing the latest revision, let's also update the latest revision entry's status in ES.
1588
+ * If we are unpublishing the latest revision, let's also update the latest revision entry's status in both DynamoDB tables.
777
1589
  */
778
-
779
- if (latestStorageEntry.id === entry.id) {
1590
+ if (latestStorageEntry?.id === entry.id) {
780
1591
  const {
781
1592
  index
782
- } = _configurations.default.es({
1593
+ } = _configurations.configurations.es({
783
1594
  model
784
1595
  });
785
-
786
- const preparedEntryData = (0, _helpers.prepareEntryToIndex)({
787
- plugins,
788
- model,
789
- entry: (0, _lodash.default)(entry),
790
- storageEntry: (0, _lodash.default)(storageEntry)
1596
+ entityBatch.put({
1597
+ ...storageEntry,
1598
+ PK: partitionKey,
1599
+ SK: (0, _keys.createLatestSortKey)(),
1600
+ TYPE: (0, _recordType.createLatestRecordType)()
791
1601
  });
792
- const esLatestData = await getESLatestEntryData(plugins, preparedEntryData);
793
- esItems.push(esEntity.putBatch({
1602
+ const esLatestData = await transformer.getElasticsearchLatestEntryData();
1603
+ elasticsearchEntityBatch.put({
794
1604
  PK: partitionKey,
795
1605
  SK: (0, _keys.createLatestSortKey)(),
796
1606
  index,
797
1607
  data: esLatestData
798
- }));
1608
+ });
799
1609
  }
1610
+
800
1611
  /**
801
1612
  * Finally, execute regular table batch.
802
1613
  */
803
-
804
-
805
1614
  try {
806
- await (0, _batchWrite.batchWriteAll)({
807
- table: entity.table,
808
- items
809
- });
1615
+ await entityBatch.execute();
810
1616
  dataLoaders.clearAll({
811
1617
  model
812
1618
  });
@@ -819,267 +1625,112 @@ const createEntriesStorageOperations = params => {
819
1625
  /**
820
1626
  * And Elasticsearch table batch.
821
1627
  */
822
-
823
-
824
1628
  try {
825
- await (0, _batchWrite.batchWriteAll)({
826
- table: esEntity.table,
827
- items: esItems
828
- });
1629
+ await elasticsearchEntityBatch.execute();
829
1630
  } catch (ex) {
830
1631
  throw new _error.default(ex.message || "Could not store unpublished entry records in DynamoDB Elasticsearch table.", ex.code || "UNPUBLISH_ERROR", {
831
1632
  entry,
832
1633
  storageEntry
833
1634
  });
834
1635
  }
835
-
836
- return storageEntry;
837
- };
838
-
839
- const requestReview = async (model, params) => {
840
- const {
841
- entry,
842
- storageEntry,
843
- originalEntry
844
- } = params;
845
- /**
846
- * We need the latest entry to check if it needs to be updated.
847
- */
848
-
849
- const [latestStorageEntry] = await dataLoaders.getLatestRevisionByEntryId({
850
- model,
851
- ids: [entry.id]
852
- });
853
- const partitionKey = (0, _keys.createPartitionKey)(entry);
854
- /**
855
- * If we updated the latest version, then make sure the changes are propagated to ES too.
856
- */
857
-
858
- let esLatestData = null;
859
-
860
- const {
861
- index
862
- } = _configurations.default.es({
863
- model
864
- });
865
-
866
- if (latestStorageEntry && latestStorageEntry.id === entry.id) {
867
- const preparedEntryData = (0, _helpers.prepareEntryToIndex)({
868
- plugins,
869
- model,
870
- entry: (0, _lodash.default)(entry),
871
- storageEntry: (0, _lodash.default)(storageEntry)
872
- });
873
- esLatestData = await getESLatestEntryData(plugins, preparedEntryData);
874
- }
875
-
876
- try {
877
- await entity.put(_objectSpread(_objectSpread({}, storageEntry), {}, {
878
- PK: partitionKey,
879
- SK: (0, _keys.createRevisionSortKey)(entry),
880
- TYPE: createType()
881
- }));
882
- dataLoaders.clearAll({
883
- model
884
- });
885
- } catch (ex) {
886
- throw new _error.default(ex.message || "Could not store request review entry record into DynamoDB table.", ex.code || "REQUEST_REVIEW_ERROR", {
887
- entry,
888
- latestStorageEntry,
889
- originalEntry
890
- });
891
- }
892
- /**
893
- * No need to proceed further if nothing to put into Elasticsearch.
894
- */
895
-
896
-
897
- if (!esLatestData) {
898
- return storageEntry;
899
- }
900
-
901
- try {
902
- await esEntity.put({
903
- PK: partitionKey,
904
- SK: (0, _keys.createLatestSortKey)(),
905
- index,
906
- data: esLatestData
907
- });
908
- } catch (ex) {
909
- throw new _error.default(ex.message || "Could not store request review entry record into DynamoDB Elasticsearch table.", ex.code || "REQUEST_REVIEW_ERROR", {
910
- entry,
911
- latestStorageEntry,
912
- originalEntry
913
- });
914
- }
915
-
916
- return storageEntry;
1636
+ return initialStorageEntry;
917
1637
  };
918
-
919
- const requestChanges = async (model, params) => {
920
- const {
921
- entry,
922
- storageEntry,
923
- originalEntry
924
- } = params;
925
- /**
926
- * We need the latest entry to check if it needs to be updated.
927
- */
928
-
929
- const [latestStorageEntry] = await dataLoaders.getLatestRevisionByEntryId({
1638
+ const getLatestRevisionByEntryId = async (initialModel, params) => {
1639
+ const model = getStorageOperationsModel(initialModel);
1640
+ const [entry] = await dataLoaders.getLatestRevisionByEntryId({
930
1641
  model,
931
- ids: [entry.id]
932
- });
933
- const partitionKey = (0, _keys.createPartitionKey)(entry);
934
- const items = [entity.putBatch(_objectSpread(_objectSpread({}, storageEntry), {}, {
935
- PK: partitionKey,
936
- SK: (0, _keys.createRevisionSortKey)(entry),
937
- TYPE: createType()
938
- }))];
939
- /**
940
- * If we updated the latest version, then make sure the changes are propagated to ES too.
941
- */
942
-
943
- const {
944
- index
945
- } = _configurations.default.es({
946
- model
1642
+ ids: [params.id]
947
1643
  });
948
-
949
- let esLatestData = null;
950
-
951
- if (latestStorageEntry && latestStorageEntry.id === entry.id) {
952
- items.push(entity.putBatch(_objectSpread(_objectSpread({}, storageEntry), {}, {
953
- PK: partitionKey,
954
- SK: (0, _keys.createLatestSortKey)(),
955
- TYPE: createLatestType()
956
- })));
957
- const preparedEntryData = (0, _helpers.prepareEntryToIndex)({
958
- plugins,
959
- model,
960
- entry: (0, _lodash.default)(entry),
961
- storageEntry: (0, _lodash.default)(storageEntry)
962
- });
963
- esLatestData = await getESLatestEntryData(plugins, preparedEntryData);
964
- }
965
-
966
- try {
967
- await (0, _batchWrite.batchWriteAll)({
968
- table: entity.table,
969
- items
970
- });
971
- dataLoaders.clearAll({
972
- model
973
- });
974
- } catch (ex) {
975
- throw new _error.default(ex.message || "Could not store request changes entry record into DynamoDB table.", ex.code || "REQUEST_CHANGES_ERROR", {
976
- entry,
977
- latestStorageEntry,
978
- originalEntry
979
- });
980
- }
981
- /**
982
- * No need to proceed further if nothing to put into Elasticsearch.
983
- */
984
-
985
-
986
- if (!esLatestData) {
987
- return storageEntry;
988
- }
989
-
990
- try {
991
- await esEntity.put({
992
- PK: partitionKey,
993
- SK: (0, _keys.createLatestSortKey)(),
994
- index,
995
- data: esLatestData
996
- });
997
- } catch (ex) {
998
- throw new _error.default(ex.message || "Could not store request changes entry record into DynamoDB Elasticsearch table.", ex.code || "REQUEST_CHANGES_ERROR", {
999
- entry,
1000
- latestStorageEntry,
1001
- originalEntry
1002
- });
1644
+ if (!entry) {
1645
+ return null;
1003
1646
  }
1004
-
1005
- return storageEntry;
1006
- };
1007
-
1008
- const getAllRevisionsByIds = async (model, params) => {
1009
- return await dataLoaders.getAllEntryRevisions({
1647
+ return (0, _convertEntryKeys.convertEntryKeysFromStorage)({
1010
1648
  model,
1011
- ids: params.ids
1649
+ entry
1012
1650
  });
1013
1651
  };
1014
-
1015
- const getLatestRevisionByEntryId = async (model, params) => {
1016
- const result = await dataLoaders.getLatestRevisionByEntryId({
1652
+ const getPublishedRevisionByEntryId = async (initialModel, params) => {
1653
+ const model = getStorageOperationsModel(initialModel);
1654
+ const [entry] = await dataLoaders.getPublishedRevisionByEntryId({
1017
1655
  model,
1018
1656
  ids: [params.id]
1019
1657
  });
1020
-
1021
- if (result.length === 0) {
1658
+ if (!entry) {
1022
1659
  return null;
1023
1660
  }
1024
-
1025
- return result.shift();
1026
- };
1027
-
1028
- const getPublishedRevisionByEntryId = async (model, params) => {
1029
- const result = await dataLoaders.getPublishedRevisionByEntryId({
1661
+ return (0, _convertEntryKeys.convertEntryKeysFromStorage)({
1030
1662
  model,
1031
- ids: [params.id]
1663
+ entry
1032
1664
  });
1033
-
1034
- if (result.length === 0) {
1035
- return null;
1036
- }
1037
-
1038
- return result.shift();
1039
1665
  };
1040
-
1041
- const getRevisionById = async (model, params) => {
1042
- const result = await dataLoaders.getRevisionById({
1666
+ const getRevisionById = async (initialModel, params) => {
1667
+ const model = getStorageOperationsModel(initialModel);
1668
+ const [entry] = await dataLoaders.getRevisionById({
1043
1669
  model,
1044
1670
  ids: [params.id]
1045
1671
  });
1046
-
1047
- if (result.length === 0) {
1672
+ if (!entry) {
1048
1673
  return null;
1049
1674
  }
1050
-
1051
- return result.shift();
1675
+ return (0, _convertEntryKeys.convertEntryKeysFromStorage)({
1676
+ model,
1677
+ entry
1678
+ });
1052
1679
  };
1053
-
1054
- const getRevisions = async (model, params) => {
1055
- return await dataLoaders.getAllEntryRevisions({
1680
+ const getRevisions = async (initialModel, params) => {
1681
+ const model = getStorageOperationsModel(initialModel);
1682
+ const entries = await dataLoaders.getAllEntryRevisions({
1056
1683
  model,
1057
1684
  ids: [params.id]
1058
1685
  });
1686
+ return entries.map(entry => {
1687
+ return (0, _convertEntryKeys.convertEntryKeysFromStorage)({
1688
+ model,
1689
+ entry
1690
+ });
1691
+ });
1059
1692
  };
1060
-
1061
- const getByIds = async (model, params) => {
1062
- return dataLoaders.getRevisionById({
1693
+ const getByIds = async (initialModel, params) => {
1694
+ const model = getStorageOperationsModel(initialModel);
1695
+ const entries = await dataLoaders.getRevisionById({
1063
1696
  model,
1064
1697
  ids: params.ids
1065
1698
  });
1699
+ return entries.map(entry => {
1700
+ return (0, _convertEntryKeys.convertEntryKeysFromStorage)({
1701
+ model,
1702
+ entry
1703
+ });
1704
+ });
1066
1705
  };
1067
-
1068
- const getLatestByIds = async (model, params) => {
1069
- return dataLoaders.getLatestRevisionByEntryId({
1706
+ const getLatestByIds = async (initialModel, params) => {
1707
+ const model = getStorageOperationsModel(initialModel);
1708
+ const entries = await dataLoaders.getLatestRevisionByEntryId({
1070
1709
  model,
1071
1710
  ids: params.ids
1072
1711
  });
1712
+ return entries.map(entry => {
1713
+ return (0, _convertEntryKeys.convertEntryKeysFromStorage)({
1714
+ model,
1715
+ entry
1716
+ });
1717
+ });
1073
1718
  };
1074
-
1075
- const getPublishedByIds = async (model, params) => {
1076
- return dataLoaders.getPublishedRevisionByEntryId({
1719
+ const getPublishedByIds = async (initialModel, params) => {
1720
+ const model = getStorageOperationsModel(initialModel);
1721
+ const entries = await dataLoaders.getPublishedRevisionByEntryId({
1077
1722
  model,
1078
1723
  ids: params.ids
1079
1724
  });
1725
+ return entries.map(entry => {
1726
+ return (0, _convertEntryKeys.convertEntryKeysFromStorage)({
1727
+ model,
1728
+ entry
1729
+ });
1730
+ });
1080
1731
  };
1081
-
1082
- const getPreviousRevision = async (model, params) => {
1732
+ const getPreviousRevision = async (initialModel, params) => {
1733
+ const model = getStorageOperationsModel(initialModel);
1083
1734
  const {
1084
1735
  tenant,
1085
1736
  locale
@@ -1097,13 +1748,12 @@ const createEntriesStorageOperations = params => {
1097
1748
  }),
1098
1749
  options: {
1099
1750
  lt: `REV#${(0, _utils.zeroPad)(version)}`,
1100
-
1101
1751
  /**
1102
1752
  * We need to have extra checks because DynamoDB will return published or latest record if there is no REV# record.
1103
1753
  */
1104
1754
  filters: [{
1105
1755
  attr: "TYPE",
1106
- eq: createType()
1756
+ eq: (0, _recordType.createRecordType)()
1107
1757
  }, {
1108
1758
  attr: "version",
1109
1759
  lt: version
@@ -1111,33 +1761,109 @@ const createEntriesStorageOperations = params => {
1111
1761
  reverse: true
1112
1762
  }
1113
1763
  };
1114
-
1115
1764
  try {
1116
- const result = await (0, _query.queryOne)(queryParams);
1117
- return (0, _cleanup.cleanupItem)(entity, result);
1765
+ const result = await (0, _dbDynamodb.queryOne)(queryParams);
1766
+ const entry = (0, _dbDynamodb.cleanupItem)(entity, result);
1767
+ if (!entry) {
1768
+ return null;
1769
+ }
1770
+ return (0, _convertEntryKeys.convertEntryKeysFromStorage)({
1771
+ entry,
1772
+ model
1773
+ });
1118
1774
  } catch (ex) {
1119
- throw new _error.default(ex.message || "Could not get previous version of given entry.", ex.code || "GET_PREVIOUS_VERSION_ERROR", _objectSpread(_objectSpread({}, params), {}, {
1775
+ throw new _error.default(ex.message || "Could not get previous version of given entry.", ex.code || "GET_PREVIOUS_VERSION_ERROR", {
1776
+ ...params,
1120
1777
  error: ex,
1121
1778
  partitionKey: queryParams.partitionKey,
1122
1779
  options: queryParams.options,
1123
1780
  model
1124
- }));
1781
+ });
1125
1782
  }
1126
1783
  };
1127
-
1784
+ const getUniqueFieldValues = async (model, params) => {
1785
+ const {
1786
+ where,
1787
+ fieldId
1788
+ } = params;
1789
+ const {
1790
+ index
1791
+ } = _configurations.configurations.es({
1792
+ model
1793
+ });
1794
+ const initialBody = (0, _body.createElasticsearchBody)({
1795
+ model,
1796
+ params: {
1797
+ limit: 1,
1798
+ where
1799
+ },
1800
+ plugins
1801
+ });
1802
+ const field = model.fields.find(f => f.fieldId === fieldId);
1803
+ if (!field) {
1804
+ throw new _error.default(`Could not find field with given "fieldId" value.`, "FIELD_NOT_FOUND", {
1805
+ fieldId
1806
+ });
1807
+ }
1808
+ const body = {
1809
+ ...initialBody,
1810
+ /**
1811
+ * We do not need any hits returned, we only need the aggregations.
1812
+ */
1813
+ size: 0,
1814
+ aggregations: {
1815
+ getUniqueFieldValues: {
1816
+ terms: {
1817
+ field: `values.${field.storageId}.keyword`,
1818
+ size: 1000000
1819
+ }
1820
+ }
1821
+ }
1822
+ };
1823
+ let response = undefined;
1824
+ try {
1825
+ response = await elasticsearch.search({
1826
+ index,
1827
+ body
1828
+ });
1829
+ } catch (error) {
1830
+ if ((0, _shouldIgnoreEsResponseError.shouldIgnoreEsResponseError)(error)) {
1831
+ (0, _logIgnoredEsResponseError.logIgnoredEsResponseError)({
1832
+ error,
1833
+ model,
1834
+ indexName: index
1835
+ });
1836
+ return [];
1837
+ }
1838
+ throw new _error.default(error.message || "Error in the Elasticsearch query.", error.code || "ELASTICSEARCH_ERROR", {
1839
+ error,
1840
+ index,
1841
+ model,
1842
+ body
1843
+ });
1844
+ }
1845
+ const buckets = response.body.aggregations["getUniqueFieldValues"]?.buckets || [];
1846
+ return buckets.map(file => {
1847
+ return {
1848
+ value: file.key,
1849
+ count: file.doc_count
1850
+ };
1851
+ });
1852
+ };
1128
1853
  return {
1129
1854
  create,
1130
1855
  createRevisionFrom,
1131
1856
  update,
1857
+ move,
1132
1858
  delete: deleteEntry,
1859
+ moveToBin,
1860
+ restoreFromBin,
1133
1861
  deleteRevision,
1862
+ deleteMultipleEntries,
1134
1863
  get,
1135
1864
  publish,
1136
1865
  unpublish,
1137
- requestReview,
1138
- requestChanges,
1139
1866
  list,
1140
- getAllRevisionsByIds,
1141
1867
  getLatestRevisionByEntryId,
1142
1868
  getPublishedRevisionByEntryId,
1143
1869
  getRevisionById,
@@ -1145,8 +1871,11 @@ const createEntriesStorageOperations = params => {
1145
1871
  getByIds,
1146
1872
  getLatestByIds,
1147
1873
  getPublishedByIds,
1148
- getPreviousRevision
1874
+ getPreviousRevision,
1875
+ getUniqueFieldValues,
1876
+ dataLoaders
1149
1877
  };
1150
1878
  };
1879
+ exports.createEntriesStorageOperations = createEntriesStorageOperations;
1151
1880
 
1152
- exports.createEntriesStorageOperations = createEntriesStorageOperations;
1881
+ //# sourceMappingURL=index.js.map