@webiny/api-headless-cms-ddb-es 6.3.0 → 6.4.0-beta.1

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