@webiny/api-headless-cms-ddb 0.0.0-unstable.e6f0dc8ca7 → 0.0.0-unstable.e9333b3f0b

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 (267) hide show
  1. package/abstractions/CmsDdbDataLoaders.d.ts +5 -0
  2. package/abstractions/CmsDdbDataLoaders.js +5 -0
  3. package/abstractions/CmsDdbDataLoaders.js.map +1 -0
  4. package/abstractions/CmsDdbEntryEntity.d.ts +5 -0
  5. package/abstractions/CmsDdbEntryEntity.js +5 -0
  6. package/abstractions/CmsDdbEntryEntity.js.map +1 -0
  7. package/abstractions/CmsDdbGroupEntity.d.ts +5 -0
  8. package/abstractions/CmsDdbGroupEntity.js +5 -0
  9. package/abstractions/CmsDdbGroupEntity.js.map +1 -0
  10. package/abstractions/CmsDdbModelEntity.d.ts +5 -0
  11. package/abstractions/CmsDdbModelEntity.js +5 -0
  12. package/abstractions/CmsDdbModelEntity.js.map +1 -0
  13. package/abstractions/CmsDdbTable.d.ts +5 -0
  14. package/abstractions/CmsDdbTable.js +5 -0
  15. package/abstractions/CmsDdbTable.js.map +1 -0
  16. package/definitions/entry.js +7 -9
  17. package/definitions/entry.js.map +1 -1
  18. package/definitions/group.js +7 -9
  19. package/definitions/group.js.map +1 -1
  20. package/definitions/model.js +7 -9
  21. package/definitions/model.js.map +1 -1
  22. package/definitions/table.js +6 -7
  23. package/definitions/table.js.map +1 -1
  24. package/definitions/types.d.ts +4 -0
  25. package/definitions/types.js +0 -3
  26. package/index.d.ts +17 -2
  27. package/index.js +31 -88
  28. package/index.js.map +1 -1
  29. package/operations/entry/DdbCreateEntry.d.ts +16 -0
  30. package/operations/entry/DdbCreateEntry.js +77 -0
  31. package/operations/entry/DdbCreateEntry.js.map +1 -0
  32. package/operations/entry/DdbCreateEntryRevisionFrom.d.ts +16 -0
  33. package/operations/entry/DdbCreateEntryRevisionFrom.js +85 -0
  34. package/operations/entry/DdbCreateEntryRevisionFrom.js.map +1 -0
  35. package/operations/entry/DdbDeleteEntry.d.ts +16 -0
  36. package/operations/entry/DdbDeleteEntry.js +65 -0
  37. package/operations/entry/DdbDeleteEntry.js.map +1 -0
  38. package/operations/entry/DdbDeleteEntryRevision.d.ts +17 -0
  39. package/operations/entry/DdbDeleteEntryRevision.js +91 -0
  40. package/operations/entry/DdbDeleteEntryRevision.js.map +1 -0
  41. package/operations/entry/DdbDeleteMultipleEntries.d.ts +16 -0
  42. package/operations/entry/DdbDeleteMultipleEntries.js +59 -0
  43. package/operations/entry/DdbDeleteMultipleEntries.js.map +1 -0
  44. package/operations/entry/DdbGetEntriesByIds.d.ts +14 -0
  45. package/operations/entry/DdbGetEntriesByIds.js +31 -0
  46. package/operations/entry/DdbGetEntriesByIds.js.map +1 -0
  47. package/operations/entry/DdbGetEntry.d.ts +14 -0
  48. package/operations/entry/DdbGetEntry.js +27 -0
  49. package/operations/entry/DdbGetEntry.js.map +1 -0
  50. package/operations/entry/DdbGetLatestEntriesByIds.d.ts +14 -0
  51. package/operations/entry/DdbGetLatestEntriesByIds.js +31 -0
  52. package/operations/entry/DdbGetLatestEntriesByIds.js.map +1 -0
  53. package/operations/entry/DdbGetLatestRevisionByEntryId.d.ts +14 -0
  54. package/operations/entry/DdbGetLatestRevisionByEntryId.js +35 -0
  55. package/operations/entry/DdbGetLatestRevisionByEntryId.js.map +1 -0
  56. package/operations/entry/DdbGetPreviousRevision.d.ts +14 -0
  57. package/operations/entry/DdbGetPreviousRevision.js +53 -0
  58. package/operations/entry/DdbGetPreviousRevision.js.map +1 -0
  59. package/operations/entry/DdbGetPublishedEntriesByIds.d.ts +14 -0
  60. package/operations/entry/DdbGetPublishedEntriesByIds.js +31 -0
  61. package/operations/entry/DdbGetPublishedEntriesByIds.js.map +1 -0
  62. package/operations/entry/DdbGetPublishedRevisionByEntryId.d.ts +14 -0
  63. package/operations/entry/DdbGetPublishedRevisionByEntryId.js +35 -0
  64. package/operations/entry/DdbGetPublishedRevisionByEntryId.js.map +1 -0
  65. package/operations/entry/DdbGetRevisionById.d.ts +14 -0
  66. package/operations/entry/DdbGetRevisionById.js +34 -0
  67. package/operations/entry/DdbGetRevisionById.js.map +1 -0
  68. package/operations/entry/DdbGetRevisions.d.ts +14 -0
  69. package/operations/entry/DdbGetRevisions.js +33 -0
  70. package/operations/entry/DdbGetRevisions.js.map +1 -0
  71. package/operations/entry/DdbGetUniqueFieldValues.d.ts +12 -0
  72. package/operations/entry/DdbGetUniqueFieldValues.js +31 -0
  73. package/operations/entry/DdbGetUniqueFieldValues.js.map +1 -0
  74. package/operations/entry/DdbListEntries.d.ts +28 -0
  75. package/operations/entry/DdbListEntries.js +124 -0
  76. package/operations/entry/DdbListEntries.js.map +1 -0
  77. package/operations/entry/DdbMoveEntry.d.ts +14 -0
  78. package/operations/entry/DdbMoveEntry.js +57 -0
  79. package/operations/entry/DdbMoveEntry.js.map +1 -0
  80. package/operations/entry/DdbMoveToBin.d.ts +16 -0
  81. package/operations/entry/DdbMoveToBin.js +77 -0
  82. package/operations/entry/DdbMoveToBin.js.map +1 -0
  83. package/operations/entry/DdbPublishEntry.d.ts +18 -0
  84. package/operations/entry/DdbPublishEntry.js +162 -0
  85. package/operations/entry/DdbPublishEntry.js.map +1 -0
  86. package/operations/entry/DdbRestoreFromBin.d.ts +16 -0
  87. package/operations/entry/DdbRestoreFromBin.js +78 -0
  88. package/operations/entry/DdbRestoreFromBin.js.map +1 -0
  89. package/operations/entry/DdbUnpublishEntry.d.ts +17 -0
  90. package/operations/entry/DdbUnpublishEntry.js +112 -0
  91. package/operations/entry/DdbUnpublishEntry.js.map +1 -0
  92. package/operations/entry/DdbUpdateEntry.d.ts +17 -0
  93. package/operations/entry/DdbUpdateEntry.js +105 -0
  94. package/operations/entry/DdbUpdateEntry.js.map +1 -0
  95. package/operations/entry/dataLoader/DataLoaderCache.js +22 -26
  96. package/operations/entry/dataLoader/DataLoaderCache.js.map +1 -1
  97. package/operations/entry/dataLoader/constants.js +2 -1
  98. package/operations/entry/dataLoader/constants.js.map +1 -1
  99. package/operations/entry/dataLoader/createBatchScheduleFn.js +6 -15
  100. package/operations/entry/dataLoader/createBatchScheduleFn.js.map +1 -1
  101. package/operations/entry/dataLoader/getAllEntryRevisions.js +18 -29
  102. package/operations/entry/dataLoader/getAllEntryRevisions.js.map +1 -1
  103. package/operations/entry/dataLoader/getLatestRevisionByEntryId.js +31 -41
  104. package/operations/entry/dataLoader/getLatestRevisionByEntryId.js.map +1 -1
  105. package/operations/entry/dataLoader/getPublishedRevisionByEntryId.js +31 -41
  106. package/operations/entry/dataLoader/getPublishedRevisionByEntryId.js.map +1 -1
  107. package/operations/entry/dataLoader/getRevisionById.js +33 -47
  108. package/operations/entry/dataLoader/getRevisionById.js.map +1 -1
  109. package/operations/entry/dataLoader/index.js +8 -9
  110. package/operations/entry/dataLoader/index.js.map +1 -1
  111. package/operations/entry/dataLoader/types.js +0 -3
  112. package/operations/entry/dataLoaders.d.ts +7 -6
  113. package/operations/entry/dataLoaders.js +89 -99
  114. package/operations/entry/dataLoaders.js.map +1 -1
  115. package/operations/entry/feature.d.ts +4 -0
  116. package/operations/entry/feature.js +53 -0
  117. package/operations/entry/feature.js.map +1 -0
  118. package/operations/entry/keys.d.ts +6 -0
  119. package/operations/entry/keys.js +57 -77
  120. package/operations/entry/keys.js.map +1 -1
  121. package/operations/entry/storageEntryUtils.d.ts +8 -0
  122. package/operations/entry/storageEntryUtils.js +25 -0
  123. package/operations/entry/storageEntryUtils.js.map +1 -0
  124. package/operations/group/DdbCreateGroup.d.ts +12 -0
  125. package/operations/group/DdbCreateGroup.js +35 -0
  126. package/operations/group/DdbCreateGroup.js.map +1 -0
  127. package/operations/group/DdbDeleteGroup.d.ts +12 -0
  128. package/operations/group/DdbDeleteGroup.js +31 -0
  129. package/operations/group/DdbDeleteGroup.js.map +1 -0
  130. package/operations/group/DdbGetGroup.d.ts +12 -0
  131. package/operations/group/DdbGetGroup.js +31 -0
  132. package/operations/group/DdbGetGroup.js.map +1 -0
  133. package/operations/group/DdbListGroups.d.ts +14 -0
  134. package/operations/group/DdbListGroups.js +52 -0
  135. package/operations/group/DdbListGroups.js.map +1 -0
  136. package/operations/group/DdbUpdateGroup.d.ts +12 -0
  137. package/operations/group/DdbUpdateGroup.js +35 -0
  138. package/operations/group/DdbUpdateGroup.js.map +1 -0
  139. package/operations/group/feature.d.ts +4 -0
  140. package/operations/group/feature.js +19 -0
  141. package/operations/group/feature.js.map +1 -0
  142. package/operations/group/keys.d.ts +15 -0
  143. package/operations/group/keys.js +17 -0
  144. package/operations/group/keys.js.map +1 -0
  145. package/operations/model/DdbCreateModel.d.ts +12 -0
  146. package/operations/model/DdbCreateModel.js +37 -0
  147. package/operations/model/DdbCreateModel.js.map +1 -0
  148. package/operations/model/DdbDeleteModel.d.ts +12 -0
  149. package/operations/model/DdbDeleteModel.js +31 -0
  150. package/operations/model/DdbDeleteModel.js.map +1 -0
  151. package/operations/model/DdbGetModel.d.ts +12 -0
  152. package/operations/model/DdbGetModel.js +30 -0
  153. package/operations/model/DdbGetModel.js.map +1 -0
  154. package/operations/model/DdbListModels.d.ts +12 -0
  155. package/operations/model/DdbListModels.js +36 -0
  156. package/operations/model/DdbListModels.js.map +1 -0
  157. package/operations/model/DdbUpdateModel.d.ts +12 -0
  158. package/operations/model/DdbUpdateModel.js +36 -0
  159. package/operations/model/DdbUpdateModel.js.map +1 -0
  160. package/operations/model/feature.d.ts +4 -0
  161. package/operations/model/feature.js +19 -0
  162. package/operations/model/feature.js.map +1 -0
  163. package/operations/model/keys.d.ts +15 -0
  164. package/operations/model/keys.js +16 -0
  165. package/operations/model/keys.js.map +1 -0
  166. package/package.json +22 -24
  167. package/requestFeature.d.ts +17 -0
  168. package/requestFeature.js +19 -0
  169. package/requestFeature.js.map +1 -0
  170. package/types.d.ts +10 -50
  171. package/types.js +6 -5
  172. package/types.js.map +1 -1
  173. package/definitions/types.js.map +0 -1
  174. package/dynamoDb/index.d.ts +0 -2
  175. package/dynamoDb/index.js +0 -6
  176. package/dynamoDb/index.js.map +0 -1
  177. package/dynamoDb/path/locationFolderId.d.ts +0 -2
  178. package/dynamoDb/path/locationFolderId.js +0 -34
  179. package/dynamoDb/path/locationFolderId.js.map +0 -1
  180. package/dynamoDb/path/plainObject.d.ts +0 -2
  181. package/dynamoDb/path/plainObject.js +0 -26
  182. package/dynamoDb/path/plainObject.js.map +0 -1
  183. package/dynamoDb/transformValue/datetime.d.ts +0 -5
  184. package/dynamoDb/transformValue/datetime.js +0 -40
  185. package/dynamoDb/transformValue/datetime.js.map +0 -1
  186. package/operations/entry/dataLoader/types.js.map +0 -1
  187. package/operations/entry/filtering/createExpressions.d.ts +0 -27
  188. package/operations/entry/filtering/createExpressions.js +0 -172
  189. package/operations/entry/filtering/createExpressions.js.map +0 -1
  190. package/operations/entry/filtering/createFields.d.ts +0 -16
  191. package/operations/entry/filtering/createFields.js +0 -106
  192. package/operations/entry/filtering/createFields.js.map +0 -1
  193. package/operations/entry/filtering/extractSort.d.ts +0 -17
  194. package/operations/entry/filtering/extractSort.js +0 -84
  195. package/operations/entry/filtering/extractSort.js.map +0 -1
  196. package/operations/entry/filtering/filter.d.ts +0 -16
  197. package/operations/entry/filtering/filter.js +0 -153
  198. package/operations/entry/filtering/filter.js.map +0 -1
  199. package/operations/entry/filtering/fullTextSearch.d.ts +0 -14
  200. package/operations/entry/filtering/fullTextSearch.js +0 -42
  201. package/operations/entry/filtering/fullTextSearch.js.map +0 -1
  202. package/operations/entry/filtering/getValue.d.ts +0 -5
  203. package/operations/entry/filtering/getValue.js +0 -59
  204. package/operations/entry/filtering/getValue.js.map +0 -1
  205. package/operations/entry/filtering/index.d.ts +0 -2
  206. package/operations/entry/filtering/index.js +0 -4
  207. package/operations/entry/filtering/index.js.map +0 -1
  208. package/operations/entry/filtering/mapPlugins.d.ts +0 -8
  209. package/operations/entry/filtering/mapPlugins.js +0 -25
  210. package/operations/entry/filtering/mapPlugins.js.map +0 -1
  211. package/operations/entry/filtering/plugins/defaultFilterCreate.d.ts +0 -2
  212. package/operations/entry/filtering/plugins/defaultFilterCreate.js +0 -37
  213. package/operations/entry/filtering/plugins/defaultFilterCreate.js.map +0 -1
  214. package/operations/entry/filtering/plugins/index.d.ts +0 -1
  215. package/operations/entry/filtering/plugins/index.js +0 -9
  216. package/operations/entry/filtering/plugins/index.js.map +0 -1
  217. package/operations/entry/filtering/plugins/objectFilterCreate.d.ts +0 -2
  218. package/operations/entry/filtering/plugins/objectFilterCreate.js +0 -84
  219. package/operations/entry/filtering/plugins/objectFilterCreate.js.map +0 -1
  220. package/operations/entry/filtering/plugins/refFilterCreate.d.ts +0 -3
  221. package/operations/entry/filtering/plugins/refFilterCreate.js +0 -74
  222. package/operations/entry/filtering/plugins/refFilterCreate.js.map +0 -1
  223. package/operations/entry/filtering/plugins/searchableJsonFilterCreate.d.ts +0 -2
  224. package/operations/entry/filtering/plugins/searchableJsonFilterCreate.js +0 -62
  225. package/operations/entry/filtering/plugins/searchableJsonFilterCreate.js.map +0 -1
  226. package/operations/entry/filtering/sort.d.ts +0 -12
  227. package/operations/entry/filtering/sort.js +0 -67
  228. package/operations/entry/filtering/sort.js.map +0 -1
  229. package/operations/entry/filtering/systemFields.d.ts +0 -2
  230. package/operations/entry/filtering/systemFields.js +0 -131
  231. package/operations/entry/filtering/systemFields.js.map +0 -1
  232. package/operations/entry/filtering/transform.d.ts +0 -6
  233. package/operations/entry/filtering/transform.js +0 -11
  234. package/operations/entry/filtering/transform.js.map +0 -1
  235. package/operations/entry/filtering/types.d.ts +0 -40
  236. package/operations/entry/filtering/types.js +0 -3
  237. package/operations/entry/filtering/types.js.map +0 -1
  238. package/operations/entry/filtering/values.d.ts +0 -2
  239. package/operations/entry/filtering/values.js +0 -16
  240. package/operations/entry/filtering/values.js.map +0 -1
  241. package/operations/entry/filtering/where.d.ts +0 -5
  242. package/operations/entry/filtering/where.js +0 -28
  243. package/operations/entry/filtering/where.js.map +0 -1
  244. package/operations/entry/index.d.ts +0 -9
  245. package/operations/entry/index.js +0 -1197
  246. package/operations/entry/index.js.map +0 -1
  247. package/operations/group/index.d.ts +0 -8
  248. package/operations/group/index.js +0 -142
  249. package/operations/group/index.js.map +0 -1
  250. package/operations/model/index.d.ts +0 -7
  251. package/operations/model/index.js +0 -127
  252. package/operations/model/index.js.map +0 -1
  253. package/plugins/CmsEntryFieldFilterPathPlugin.d.ts +0 -23
  254. package/plugins/CmsEntryFieldFilterPathPlugin.js +0 -40
  255. package/plugins/CmsEntryFieldFilterPathPlugin.js.map +0 -1
  256. package/plugins/CmsEntryFieldFilterPlugin.d.ts +0 -43
  257. package/plugins/CmsEntryFieldFilterPlugin.js +0 -21
  258. package/plugins/CmsEntryFieldFilterPlugin.js.map +0 -1
  259. package/plugins/CmsEntryFieldSortingPlugin.d.ts +0 -42
  260. package/plugins/CmsEntryFieldSortingPlugin.js +0 -19
  261. package/plugins/CmsEntryFieldSortingPlugin.js.map +0 -1
  262. package/plugins/CmsFieldFilterValueTransformPlugin.d.ts +0 -11
  263. package/plugins/CmsFieldFilterValueTransformPlugin.js +0 -16
  264. package/plugins/CmsFieldFilterValueTransformPlugin.js.map +0 -1
  265. package/plugins/index.d.ts +0 -4
  266. package/plugins/index.js +0 -6
  267. package/plugins/index.js.map +0 -1
@@ -1,1197 +0,0 @@
1
- import WebinyError from "@webiny/error";
2
- import { DataLoadersHandler } from "./dataLoaders.js";
3
- import { CONTENT_ENTRY_STATUS } from "@webiny/api-headless-cms/types/index.js";
4
- import { createEntryLatestKeys, createEntryPublishedKeys, createEntryRevisionKeys, createGSIPartitionKey, createPartitionKey, createPublishedSortKey, createRevisionSortKey } from "./keys.js";
5
- import { decodeCursor, encodeCursor } from "@webiny/utils";
6
- import { StorageOperationsCmsModelPlugin } from "@webiny/api-headless-cms";
7
- import { createFields } from "./filtering/createFields.js";
8
- import { filter, sort } from "./filtering/index.js";
9
- import { isDeletedEntryMetaField, isEntryLevelEntryMetaField, isRestoredEntryMetaField, pickEntryMetaFields } from "@webiny/api-headless-cms/constants.js";
10
- import { getBaseFieldType } from "@webiny/api-headless-cms/utils/getBaseFieldType.js";
11
- import { StorageTransformRegistry } from "@webiny/api-headless-cms/exports/api/cms/storage.js";
12
- const convertToStorageEntry = params => {
13
- const {
14
- model,
15
- storageEntry
16
- } = params;
17
- const values = model.convertValueKeyToStorage({
18
- fields: model.fields,
19
- values: storageEntry.values
20
- });
21
- return {
22
- ...storageEntry,
23
- values
24
- };
25
- };
26
- const convertFromStorageEntry = params => {
27
- const {
28
- model,
29
- storageEntry
30
- } = params;
31
- const values = model.convertValueKeyFromStorage({
32
- fields: model.fields,
33
- values: storageEntry.values
34
- });
35
- return {
36
- ...storageEntry,
37
- values
38
- };
39
- };
40
- const MAX_LIST_LIMIT = 1000000;
41
- export const createEntriesStorageOperations = params => {
42
- const {
43
- entity,
44
- container,
45
- plugins
46
- } = params;
47
- const storageTransformRegistry = container.resolve(StorageTransformRegistry);
48
- let storageOperationsCmsModelPlugin;
49
- const getStorageOperationsCmsModelPlugin = () => {
50
- if (storageOperationsCmsModelPlugin) {
51
- return storageOperationsCmsModelPlugin;
52
- }
53
- storageOperationsCmsModelPlugin = plugins.oneByType(StorageOperationsCmsModelPlugin.type);
54
- return storageOperationsCmsModelPlugin;
55
- };
56
- const getStorageOperationsModel = model => {
57
- const plugin = getStorageOperationsCmsModelPlugin();
58
- return plugin.getModel(model);
59
- };
60
- const dataLoaders = new DataLoadersHandler({
61
- entity
62
- });
63
- const createStorageTransformCallable = model => {
64
- return (field, value) => {
65
- const fieldType = getBaseFieldType(field);
66
- const storageTransform = storageTransformRegistry.get(fieldType);
67
- if (!storageTransform) {
68
- return value;
69
- }
70
- return storageTransform.fromStorage({
71
- model,
72
- field,
73
- value,
74
- getStorageTransform(fieldType) {
75
- return storageTransformRegistry.get(fieldType) || storageTransformRegistry.get("*");
76
- }
77
- });
78
- };
79
- };
80
- const create = async (initialModel, params) => {
81
- const {
82
- entry,
83
- storageEntry: initialStorageEntry
84
- } = params;
85
- const model = getStorageOperationsModel(initialModel);
86
- const isPublished = entry.status === "published";
87
- const locked = isPublished ? true : entry.locked;
88
- const storageEntry = convertToStorageEntry({
89
- model,
90
- storageEntry: initialStorageEntry
91
- });
92
- const storageEntryRevisionKeys = createEntryRevisionKeys(entry);
93
- const storageEntryLatestKeys = createEntryLatestKeys(entry);
94
- /**
95
- * We need to:
96
- * - create new main entry item
97
- * - create new or update the latest entry item
98
- */
99
- const entityBatch = entity.createEntityWriter({
100
- put: [{
101
- ...storageEntryRevisionKeys,
102
- data: {
103
- ...storageEntry,
104
- locked
105
- }
106
- }, {
107
- ...storageEntryLatestKeys,
108
- data: {
109
- ...storageEntry,
110
- locked
111
- }
112
- }]
113
- });
114
-
115
- /**
116
- * We need to create published entry if
117
- */
118
- if (isPublished) {
119
- const storageEntryPublishedKeys = createEntryPublishedKeys(storageEntry);
120
- entityBatch.put({
121
- ...storageEntryPublishedKeys,
122
- data: {
123
- ...storageEntry,
124
- locked
125
- }
126
- });
127
- }
128
- try {
129
- await entityBatch.execute();
130
- dataLoaders.clearAll({
131
- model
132
- });
133
- } catch (ex) {
134
- throw new WebinyError(ex.message || "Could not insert data into the DynamoDB.", ex.code || "CREATE_ENTRY_ERROR", {
135
- error: ex,
136
- entry
137
- });
138
- }
139
- return initialStorageEntry;
140
- };
141
- const createRevisionFrom = async (initialModel, params) => {
142
- const {
143
- entry,
144
- storageEntry: initialStorageEntry
145
- } = params;
146
- const model = getStorageOperationsModel(initialModel);
147
- const storageEntry = convertToStorageEntry({
148
- storageEntry: initialStorageEntry,
149
- model
150
- });
151
-
152
- /**
153
- * We need to:
154
- * - create the main entry item
155
- * - update the latest entry item to the current one
156
- * - if the entry's status was set to "published":
157
- * - update the published entry item to the current one
158
- * - unpublish previously published revision (if any)
159
- */
160
- const entityBatch = entity.createEntityWriter({
161
- put: [{
162
- ...createEntryRevisionKeys(storageEntry),
163
- data: {
164
- ...storageEntry
165
- }
166
- }, {
167
- ...createEntryLatestKeys(storageEntry),
168
- data: {
169
- ...storageEntry
170
- }
171
- }]
172
- });
173
- const isPublished = entry.status === "published";
174
- if (isPublished) {
175
- entityBatch.put({
176
- ...createEntryPublishedKeys(storageEntry),
177
- data: {
178
- ...storageEntry
179
- }
180
- });
181
-
182
- // Unpublish previously published revision (if any).
183
- const [publishedRevisionStorageEntry] = await dataLoaders.getPublishedRevisionByEntryId({
184
- model,
185
- ids: [entry.id]
186
- });
187
- if (publishedRevisionStorageEntry) {
188
- entityBatch.put({
189
- ...createEntryRevisionKeys(publishedRevisionStorageEntry),
190
- data: {
191
- ...publishedRevisionStorageEntry,
192
- status: CONTENT_ENTRY_STATUS.UNPUBLISHED
193
- }
194
- });
195
- }
196
- }
197
- try {
198
- await entityBatch.execute();
199
- dataLoaders.clearAll({
200
- model
201
- });
202
- } catch (ex) {
203
- throw new WebinyError(ex.message || "Could not create revision from given entry.", ex.code || "CREATE_REVISION_ERROR", {
204
- error: ex,
205
- entry,
206
- storageEntry
207
- });
208
- }
209
- /**
210
- * There are no modifications on the entry created so just return the data.
211
- */
212
- return initialStorageEntry;
213
- };
214
- const update = async (initialModel, params) => {
215
- const {
216
- entry,
217
- storageEntry: initialStorageEntry
218
- } = params;
219
- const model = getStorageOperationsModel(initialModel);
220
- const isPublished = entry.status === "published";
221
- const locked = isPublished ? true : entry.locked;
222
- const storageEntry = convertToStorageEntry({
223
- model,
224
- storageEntry: initialStorageEntry
225
- });
226
- /**
227
- * We need to:
228
- * - update the current entry
229
- * - update the latest entry if the current entry is the latest one
230
- */
231
-
232
- const entityBatch = entity.createEntityWriter({
233
- put: [{
234
- ...createEntryRevisionKeys(storageEntry),
235
- data: {
236
- ...storageEntry,
237
- locked
238
- }
239
- }]
240
- });
241
- if (isPublished) {
242
- entityBatch.put({
243
- ...createEntryPublishedKeys(storageEntry),
244
- data: {
245
- ...storageEntry,
246
- locked
247
- }
248
- });
249
- }
250
-
251
- /**
252
- * We need the latest entry to update it as well if necessary.
253
- */
254
- const latestStorageEntry = await getLatestRevisionByEntryId(model, entry);
255
- if (latestStorageEntry) {
256
- const updatingLatestRevision = latestStorageEntry.id === entry.id;
257
- if (updatingLatestRevision) {
258
- entityBatch.put({
259
- ...createEntryLatestKeys(storageEntry),
260
- data: {
261
- ...storageEntry,
262
- locked
263
- }
264
- });
265
- } else {
266
- /**
267
- * If not updating latest revision, we still want to update the latest revision's
268
- * entry-level meta fields to match the current revision's entry-level meta fields.
269
- */
270
- const updatedEntryLevelMetaFields = pickEntryMetaFields(entry, isEntryLevelEntryMetaField);
271
-
272
- /**
273
- * First we update the regular DynamoDB table. Two updates are needed:
274
- * - one for the actual revision record
275
- * - one for the latest record
276
- */
277
- entityBatch.put({
278
- ...createEntryRevisionKeys(latestStorageEntry),
279
- data: {
280
- ...latestStorageEntry,
281
- ...updatedEntryLevelMetaFields
282
- }
283
- });
284
- entityBatch.put({
285
- ...createEntryLatestKeys(latestStorageEntry),
286
- data: {
287
- ...latestStorageEntry,
288
- ...updatedEntryLevelMetaFields
289
- }
290
- });
291
- }
292
- }
293
- try {
294
- await entityBatch.execute();
295
- dataLoaders.clearAll({
296
- model
297
- });
298
- return initialStorageEntry;
299
- } catch (ex) {
300
- throw new WebinyError(ex.message || "Could not update entry.", ex.code || "UPDATE_ERROR", {
301
- error: ex,
302
- entry,
303
- latestStorageEntry
304
- });
305
- }
306
- };
307
- const move = async (initialModel, id, folderId) => {
308
- /**
309
- * We need to:
310
- * - load all the revisions of the entry, including published and latest
311
- * - update all the revisions (published and latest ) of the entry with new folderId
312
- */
313
- const model = getStorageOperationsModel(initialModel);
314
- /**
315
- * First we need to load all the revisions and published / latest entry.
316
- */
317
- const partitionKey = createPartitionKey({
318
- id,
319
- tenant: model.tenant
320
- });
321
- const records = await entity.queryAll({
322
- partitionKey,
323
- options: {
324
- gte: " "
325
- }
326
- });
327
- /**
328
- * Then create the batch writes for the DynamoDB, with the updated folderId.
329
- */
330
- const entityBatch = entity.createEntityWriter({
331
- put: records.map(item => {
332
- return {
333
- ...item,
334
- data: {
335
- ...item.data,
336
- location: {
337
- ...item.data.location,
338
- folderId
339
- }
340
- }
341
- };
342
- })
343
- });
344
-
345
- /**
346
- * And finally write it...
347
- */
348
- try {
349
- await entityBatch.execute();
350
- } catch (ex) {
351
- throw WebinyError.from(ex, {
352
- message: "Could not move records to a new folder.",
353
- data: {
354
- id,
355
- folderId
356
- }
357
- });
358
- }
359
- };
360
- const moveToBin = async (initialModel, params) => {
361
- const {
362
- entry,
363
- storageEntry: initialStorageEntry
364
- } = params;
365
- const model = getStorageOperationsModel(initialModel);
366
-
367
- /**
368
- * First we need to load all the revisions and published / latest entries.
369
- */
370
-
371
- let records = [];
372
- try {
373
- records = await entity.queryAll({
374
- partitionKey: createPartitionKey({
375
- id: entry.id,
376
- tenant: model.tenant
377
- }),
378
- options: {
379
- gte: " "
380
- }
381
- });
382
- } catch (ex) {
383
- throw new WebinyError(ex.message || "Could not load all records.", ex.code || "LOAD_ALL_RECORDS_ERROR", {
384
- error: ex,
385
- id: entry.id
386
- });
387
- }
388
- if (records.length === 0) {
389
- return;
390
- }
391
- const storageEntry = convertToStorageEntry({
392
- model,
393
- storageEntry: initialStorageEntry
394
- });
395
-
396
- /**
397
- * Let's pick the `deleted` meta fields from the storage entry.
398
- */
399
- const updatedDeletedMetaFields = pickEntryMetaFields(storageEntry, isDeletedEntryMetaField);
400
-
401
- /**
402
- * Then create the batch writes for the DynamoDB, with the updated data.
403
- */
404
- const entityBatch = entity.createEntityWriter({
405
- put: records.map(record => {
406
- return {
407
- ...record,
408
- data: {
409
- ...record.data,
410
- ...updatedDeletedMetaFields,
411
- wbyDeleted: storageEntry.wbyDeleted,
412
- location: storageEntry.location,
413
- binOriginalFolderId: storageEntry.binOriginalFolderId
414
- }
415
- };
416
- })
417
- });
418
- /**
419
- * And finally write it...
420
- */
421
- try {
422
- await entityBatch.execute();
423
- dataLoaders.clearAll({
424
- model
425
- });
426
- } catch (ex) {
427
- throw new WebinyError(ex.message || "Could not move the entry to the bin.", ex.code || "MOVE_ENTRY_TO_BIN_ERROR", {
428
- error: ex,
429
- entry,
430
- storageEntry
431
- });
432
- }
433
- };
434
- const deleteEntry = async (initialModel, params) => {
435
- const {
436
- entry
437
- } = params;
438
- const id = entry.id || entry.entryId;
439
- const model = getStorageOperationsModel(initialModel);
440
- const partitionKey = createPartitionKey({
441
- id,
442
- tenant: model.tenant
443
- });
444
- let records = [];
445
- try {
446
- records = await entity.queryAll({
447
- partitionKey,
448
- options: {
449
- gte: " "
450
- }
451
- });
452
- } catch (ex) {
453
- throw new WebinyError(ex.message || "Could not load all records.", ex.code || "LOAD_ALL_RECORDS_ERROR", {
454
- error: ex,
455
- id
456
- });
457
- }
458
- const entityBatch = entity.createEntityWriter({
459
- delete: records.map(item => {
460
- return {
461
- PK: item.PK,
462
- SK: item.SK
463
- };
464
- })
465
- });
466
- try {
467
- await entityBatch.execute();
468
- dataLoaders.clearAll({
469
- model
470
- });
471
- } catch (ex) {
472
- throw new WebinyError(ex.message || "Could not delete the entry.", ex.code || "DELETE_ENTRY_ERROR", {
473
- error: ex,
474
- partitionKey,
475
- id
476
- });
477
- }
478
- };
479
- const restoreFromBin = async (initialModel, params) => {
480
- const {
481
- entry,
482
- storageEntry: initialStorageEntry
483
- } = params;
484
- const model = getStorageOperationsModel(initialModel);
485
-
486
- /**
487
- * First we need to load all the revisions and published / latest entries.
488
- */
489
- let records = [];
490
- try {
491
- records = await entity.queryAll({
492
- partitionKey: createPartitionKey({
493
- id: entry.id,
494
- tenant: model.tenant
495
- }),
496
- options: {
497
- gte: " "
498
- }
499
- });
500
- } catch (ex) {
501
- throw new WebinyError(ex.message || "Could not load all records.", ex.code || "LOAD_ALL_RECORDS_ERROR", {
502
- error: ex,
503
- id: entry.id
504
- });
505
- }
506
- if (records.length === 0) {
507
- return initialStorageEntry;
508
- }
509
- const storageEntry = convertToStorageEntry({
510
- model,
511
- storageEntry: initialStorageEntry
512
- });
513
-
514
- /**
515
- * Let's pick the `restored` meta fields from the storage entry.
516
- */
517
- const updatedRestoredMetaFields = pickEntryMetaFields(storageEntry, isRestoredEntryMetaField);
518
- const entityBatch = entity.createEntityWriter({
519
- put: records.map(record => {
520
- return {
521
- ...record,
522
- data: {
523
- ...record.data,
524
- ...updatedRestoredMetaFields,
525
- wbyDeleted: storageEntry.wbyDeleted,
526
- location: storageEntry.location,
527
- binOriginalFolderId: storageEntry.binOriginalFolderId
528
- }
529
- };
530
- })
531
- });
532
-
533
- /**
534
- * And finally write it...
535
- */
536
- try {
537
- await entityBatch.execute();
538
- dataLoaders.clearAll({
539
- model
540
- });
541
- return initialStorageEntry;
542
- } catch (ex) {
543
- throw new WebinyError(ex.message || "Could not restore the entry from the bin.", ex.code || "RESTORE_ENTRY_ERROR", {
544
- error: ex,
545
- entry,
546
- storageEntry
547
- });
548
- }
549
- };
550
- const deleteRevision = async (initialModel, params) => {
551
- const {
552
- entry,
553
- latestEntry,
554
- latestStorageEntry: initialLatestStorageEntry
555
- } = params;
556
- const model = getStorageOperationsModel(initialModel);
557
- const partitionKey = createPartitionKey({
558
- id: entry.id,
559
- tenant: model.tenant
560
- });
561
- const entityBatch = entity.createEntityWriter({
562
- delete: [{
563
- PK: partitionKey,
564
- SK: createRevisionSortKey(entry)
565
- }]
566
- });
567
- const publishedStorageEntry = await getPublishedRevisionByEntryId(model, entry);
568
-
569
- /**
570
- * If revision we are deleting is the published one as well, we need to delete those records as well.
571
- */
572
- if (publishedStorageEntry && entry.id === publishedStorageEntry.id) {
573
- entityBatch.delete({
574
- PK: partitionKey,
575
- SK: createPublishedSortKey()
576
- });
577
- }
578
- if (initialLatestStorageEntry) {
579
- const latestStorageEntry = convertToStorageEntry({
580
- storageEntry: initialLatestStorageEntry,
581
- model
582
- });
583
- entityBatch.put({
584
- ...createEntryLatestKeys(latestStorageEntry),
585
- data: {
586
- ...latestStorageEntry
587
- }
588
- });
589
-
590
- // Do an update on the latest revision. We need to update the latest revision's
591
- // entry-level meta fields to match the previous revision's entry-level meta fields.
592
- entityBatch.put({
593
- ...createEntryRevisionKeys(latestStorageEntry),
594
- data: {
595
- ...latestStorageEntry
596
- }
597
- });
598
- }
599
- try {
600
- await entityBatch.execute();
601
- dataLoaders.clearAll({
602
- model
603
- });
604
- } catch (ex) {
605
- throw new WebinyError(ex.message, ex.code, {
606
- error: ex,
607
- entry,
608
- latestEntry
609
- });
610
- }
611
- };
612
- const deleteMultipleEntries = async (initialModel, params) => {
613
- const {
614
- entries
615
- } = params;
616
- const model = getStorageOperationsModel(initialModel);
617
- /**
618
- * First we need all the revisions of the entries we want to delete.
619
- */
620
- const revisions = await dataLoaders.getAllEntryRevisions({
621
- model,
622
- ids: entries
623
- });
624
- /**
625
- * Then we need to construct the queries for all the revisions and entries.
626
- */
627
-
628
- const entityBatch = entity.createEntityWriter();
629
- for (const id of entries) {
630
- const partitionKey = createPartitionKey({
631
- id,
632
- tenant: model.tenant
633
- });
634
- entityBatch.delete({
635
- PK: partitionKey,
636
- SK: "L"
637
- });
638
- entityBatch.delete({
639
- PK: partitionKey,
640
- SK: "P"
641
- });
642
- }
643
- /**
644
- * Exact revisions of all the entries
645
- */
646
- for (const revision of revisions) {
647
- entityBatch.delete({
648
- PK: createPartitionKey({
649
- id: revision.id,
650
- tenant: model.tenant
651
- }),
652
- SK: createRevisionSortKey({
653
- version: revision.version
654
- })
655
- });
656
- }
657
- await entityBatch.execute();
658
- };
659
- const getLatestRevisionByEntryId = async (initialModel, params) => {
660
- const model = getStorageOperationsModel(initialModel);
661
- const items = await dataLoaders.getLatestRevisionByEntryId({
662
- model,
663
- ids: [params.id]
664
- });
665
- const item = items.shift() || null;
666
- if (!item) {
667
- return null;
668
- }
669
- return convertFromStorageEntry({
670
- storageEntry: item,
671
- model
672
- });
673
- };
674
- const getPublishedRevisionByEntryId = async (initialModel, params) => {
675
- const model = getStorageOperationsModel(initialModel);
676
- const items = await dataLoaders.getPublishedRevisionByEntryId({
677
- model,
678
- ids: [params.id]
679
- });
680
- const item = items.shift() || null;
681
- if (!item) {
682
- return null;
683
- }
684
- return convertFromStorageEntry({
685
- storageEntry: item,
686
- model
687
- });
688
- };
689
- const getRevisionById = async (initialModel, params) => {
690
- const model = getStorageOperationsModel(initialModel);
691
- const items = await dataLoaders.getRevisionById({
692
- model,
693
- ids: [params.id]
694
- });
695
- const item = items.shift() || null;
696
- if (!item) {
697
- return null;
698
- }
699
- return convertFromStorageEntry({
700
- storageEntry: item,
701
- model
702
- });
703
- };
704
- const getRevisions = async (initialModel, params) => {
705
- const model = getStorageOperationsModel(initialModel);
706
- const items = await dataLoaders.getAllEntryRevisions({
707
- model,
708
- ids: [params.id]
709
- });
710
- return items.map(item => {
711
- return convertFromStorageEntry({
712
- storageEntry: item,
713
- model
714
- });
715
- });
716
- };
717
- const getByIds = async (initialModel, params) => {
718
- const model = getStorageOperationsModel(initialModel);
719
- const items = await dataLoaders.getRevisionById({
720
- model,
721
- ids: params.ids
722
- });
723
- return items.map(item => {
724
- return convertFromStorageEntry({
725
- storageEntry: item,
726
- model
727
- });
728
- });
729
- };
730
- const getLatestByIds = async (initialModel, params) => {
731
- const model = getStorageOperationsModel(initialModel);
732
- const items = await dataLoaders.getLatestRevisionByEntryId({
733
- model,
734
- ids: params.ids
735
- });
736
- return items.map(item => {
737
- return convertFromStorageEntry({
738
- storageEntry: item,
739
- model
740
- });
741
- });
742
- };
743
- const getPublishedByIds = async (initialModel, params) => {
744
- const model = getStorageOperationsModel(initialModel);
745
- const items = await dataLoaders.getPublishedRevisionByEntryId({
746
- model,
747
- ids: params.ids
748
- });
749
- return items.map(item => {
750
- return convertFromStorageEntry({
751
- storageEntry: item,
752
- model
753
- });
754
- });
755
- };
756
- const getPreviousRevision = async (initialModel, params) => {
757
- const model = getStorageOperationsModel(initialModel);
758
- const {
759
- entryId,
760
- version
761
- } = params;
762
- const partitionKey = createPartitionKey({
763
- tenant: model.tenant,
764
- id: entryId
765
- });
766
- const unfilteredRevisions = await entity.queryAll({
767
- partitionKey,
768
- options: {
769
- beginsWith: `REV#`,
770
- reverse: true
771
- }
772
- });
773
- const filteredRevisions = unfilteredRevisions.filter(item => {
774
- return item.data.version < version;
775
- });
776
- const storageEntry = filteredRevisions[0];
777
- if (!storageEntry) {
778
- return null;
779
- }
780
- try {
781
- return convertFromStorageEntry({
782
- storageEntry: storageEntry.data,
783
- model
784
- });
785
- } catch (ex) {
786
- throw new WebinyError(ex.message || "Could not get previous version of given entry.", ex.code || "GET_PREVIOUS_VERSION_ERROR", {
787
- ...params,
788
- error: ex,
789
- partitionKey,
790
- model
791
- });
792
- }
793
- };
794
- const list = async (initialModel, params) => {
795
- const model = getStorageOperationsModel(initialModel);
796
- const {
797
- limit: initialLimit = 10,
798
- where: initialWhere,
799
- after,
800
- sort: sortBy,
801
- fields,
802
- search
803
- } = params;
804
- const limit = initialLimit <= 0 || initialLimit >= MAX_LIST_LIMIT ? MAX_LIST_LIMIT : initialLimit;
805
- const type = initialWhere.published ? "P" : "L";
806
- const partitionKey = createGSIPartitionKey(model, type);
807
- const options = {
808
- index: "GSI1",
809
- gte: " "
810
- };
811
- let storageEntries = [];
812
- try {
813
- storageEntries = await entity.queryAll({
814
- partitionKey,
815
- options
816
- });
817
- } catch (ex) {
818
- throw new WebinyError(ex.message, "QUERY_ENTRIES_ERROR", {
819
- error: ex,
820
- partitionKey,
821
- options
822
- });
823
- }
824
- if (storageEntries.length === 0) {
825
- return {
826
- hasMoreItems: false,
827
- totalCount: 0,
828
- cursor: null,
829
- items: []
830
- };
831
- }
832
- const where = {
833
- ...initialWhere
834
- };
835
- delete where["published"];
836
- delete where["latest"];
837
- /**
838
- * We need an object containing field, transformers and paths.
839
- * Just build it here and pass on into other methods that require it to avoid mapping multiple times.
840
- */
841
- const modelFields = createFields({
842
- plugins,
843
- fields: model.fields
844
- });
845
- const fromStorage = createStorageTransformCallable(model);
846
- /**
847
- * Let's transform records from storage ones to regular ones, so we do not need to do it later.
848
- *
849
- * This is always being done, but at least its in parallel.
850
- */
851
- const records = await Promise.all(storageEntries.map(async storageEntry => {
852
- const entry = convertFromStorageEntry({
853
- storageEntry: storageEntry.data,
854
- model
855
- });
856
- for (const field of model.fields) {
857
- entry.values[field.fieldId] = await fromStorage(field, entry.values[field.fieldId]);
858
- }
859
- return entry;
860
- }));
861
- /**
862
- * Filter the read items via the code.
863
- * It will build the filters out of the where input and transform the values it is using.
864
- */
865
- const filteredItems = filter({
866
- items: records,
867
- where,
868
- plugins,
869
- fields: modelFields,
870
- fullTextSearch: {
871
- term: search,
872
- fields: fields || []
873
- },
874
- container
875
- });
876
- const totalCount = filteredItems.length;
877
-
878
- /**
879
- * Sorting is also done via the code.
880
- * It takes the sort input and sorts by it via the lodash sortBy method.
881
- */
882
- const sortedItems = sort({
883
- model,
884
- plugins,
885
- items: filteredItems,
886
- sort: sortBy,
887
- fields: modelFields
888
- });
889
- const start = parseInt(decodeCursor(after) || "0") || 0;
890
- const hasMoreItems = totalCount > start + limit;
891
- const end = limit > totalCount + start + limit ? undefined : start + limit;
892
- const slicedItems = sortedItems.slice(start, end);
893
- /**
894
- * Although we do not need a cursor here, we will use it as such to keep it standardized.
895
- * Number is simply encoded.
896
- */
897
- const cursor = encodeCursor(`${start + limit}`);
898
- return {
899
- hasMoreItems,
900
- totalCount,
901
- cursor,
902
- items: slicedItems
903
- };
904
- };
905
- const get = async (initialModel, params) => {
906
- const model = getStorageOperationsModel(initialModel);
907
- const {
908
- items
909
- } = await list(model, {
910
- ...params,
911
- limit: 1
912
- });
913
- return items.shift() || null;
914
- };
915
- const publish = async (initialModel, params) => {
916
- const {
917
- entry,
918
- storageEntry: initialStorageEntry
919
- } = params;
920
- const model = getStorageOperationsModel(initialModel);
921
-
922
- /**
923
- * We need the latest and published entries to see if something needs to be updated alongside the publishing one.
924
- */
925
- const initialLatestStorageEntry = await getLatestRevisionByEntryId(model, entry);
926
- if (!initialLatestStorageEntry) {
927
- throw new WebinyError(`Could not publish entry. Could not load latest ("L") record.`, "PUBLISH_ERROR", {
928
- entry
929
- });
930
- }
931
- const initialPublishedStorageEntry = await getPublishedRevisionByEntryId(model, entry);
932
- const storageEntry = convertToStorageEntry({
933
- model,
934
- storageEntry: initialStorageEntry
935
- });
936
-
937
- // 1. Update REV# and P records with new data.
938
- const entityBatch = entity.createEntityWriter({
939
- put: [{
940
- ...createEntryRevisionKeys(storageEntry),
941
- data: {
942
- ...storageEntry
943
- }
944
- }, {
945
- ...createEntryPublishedKeys(storageEntry),
946
- data: {
947
- ...storageEntry
948
- }
949
- }]
950
- });
951
-
952
- // 2. When it comes to the latest record, we need to perform a couple of different
953
- // updates, based on whether the entry being published is the latest revision or not.
954
- const publishedRevisionId = initialPublishedStorageEntry?.id;
955
- const publishingLatestRevision = entry.id === initialLatestStorageEntry.id;
956
- if (publishingLatestRevision) {
957
- // 2.1 If we're publishing the latest revision, we first need to update the L record.
958
- entityBatch.put({
959
- ...createEntryLatestKeys(storageEntry),
960
- data: {
961
- ...storageEntry
962
- }
963
- });
964
-
965
- // 2.2 Additionally, if we have a previously published entry, we need to mark it as unpublished.
966
- if (publishedRevisionId && publishedRevisionId !== entry.id) {
967
- const publishedStorageEntry = convertToStorageEntry({
968
- storageEntry: initialPublishedStorageEntry,
969
- model
970
- });
971
- entityBatch.put({
972
- ...createEntryRevisionKeys(publishedStorageEntry),
973
- data: {
974
- ...publishedStorageEntry,
975
- status: CONTENT_ENTRY_STATUS.UNPUBLISHED
976
- }
977
- });
978
- }
979
- } else {
980
- // 2.3 If the published revision is not the latest one, the situation is a bit
981
- // more complex. We first need to update the L and REV# records with the new
982
- // values of *only entry-level* meta fields.
983
- const updatedEntryLevelMetaFields = pickEntryMetaFields(entry, isEntryLevelEntryMetaField);
984
- const latestStorageEntry = convertToStorageEntry({
985
- storageEntry: initialLatestStorageEntry,
986
- model
987
- });
988
-
989
- // 2.3.1 Update L record. Apart from updating the entry-level meta fields, we also need
990
- // to change the status from "published" to "unpublished" (if the status is set to "published").
991
- let latestRevisionStatus = latestStorageEntry.status;
992
- if (latestRevisionStatus === CONTENT_ENTRY_STATUS.PUBLISHED) {
993
- latestRevisionStatus = CONTENT_ENTRY_STATUS.UNPUBLISHED;
994
- }
995
- const latestStorageEntryFields = {
996
- ...latestStorageEntry,
997
- ...updatedEntryLevelMetaFields,
998
- status: latestRevisionStatus
999
- };
1000
- entityBatch.put({
1001
- ...createEntryLatestKeys(latestStorageEntryFields),
1002
- data: {
1003
- ...latestStorageEntryFields
1004
- }
1005
- });
1006
-
1007
- // 2.3.2 Update REV# record.
1008
- entityBatch.put({
1009
- ...createEntryRevisionKeys(latestStorageEntryFields),
1010
- data: {
1011
- ...latestStorageEntryFields
1012
- }
1013
- });
1014
-
1015
- // 2.3.3 Finally, if we got a published entry, but it wasn't the latest one, we need to take
1016
- // an extra step and mark it as unpublished.
1017
- const publishedRevisionDifferentFromLatest = publishedRevisionId && publishedRevisionId !== latestStorageEntry.id;
1018
- if (publishedRevisionDifferentFromLatest) {
1019
- const publishedStorageEntry = convertToStorageEntry({
1020
- storageEntry: initialPublishedStorageEntry,
1021
- model
1022
- });
1023
- entityBatch.put({
1024
- ...createEntryRevisionKeys(publishedStorageEntry),
1025
- data: {
1026
- ...publishedStorageEntry,
1027
- status: CONTENT_ENTRY_STATUS.UNPUBLISHED
1028
- }
1029
- });
1030
- }
1031
- }
1032
- try {
1033
- await entityBatch.execute();
1034
- dataLoaders.clearAll({
1035
- model
1036
- });
1037
- return initialStorageEntry;
1038
- } catch (ex) {
1039
- throw new WebinyError(ex.message || "Could not execute the publishing batch.", ex.code || "PUBLISH_ERROR", {
1040
- entry,
1041
- latestStorageEntry: initialLatestStorageEntry,
1042
- publishedStorageEntry: initialPublishedStorageEntry
1043
- });
1044
- }
1045
- };
1046
- const unpublish = async (initialModel, params) => {
1047
- const {
1048
- entry,
1049
- storageEntry: initialStorageEntry
1050
- } = params;
1051
- const model = getStorageOperationsModel(initialModel);
1052
- const partitionKey = createPartitionKey({
1053
- id: entry.id,
1054
- tenant: model.tenant
1055
- });
1056
- const storageEntry = convertToStorageEntry({
1057
- storageEntry: initialStorageEntry,
1058
- model
1059
- });
1060
- /**
1061
- * We need to:
1062
- * - delete currently published entry
1063
- * - update current entry revision with new data
1064
- * - update the latest entry status - if entry being unpublished is latest
1065
- */
1066
- const entityBatch = entity.createEntityWriter({
1067
- delete: [{
1068
- PK: partitionKey,
1069
- SK: createPublishedSortKey()
1070
- }],
1071
- put: [{
1072
- ...createEntryRevisionKeys(storageEntry),
1073
- data: {
1074
- ...storageEntry
1075
- }
1076
- }]
1077
- });
1078
-
1079
- /**
1080
- * We need the latest entry to see if something needs to be updated alongside the unpublishing one.
1081
- */
1082
- const initialLatestStorageEntry = await getLatestRevisionByEntryId(model, entry);
1083
- if (initialLatestStorageEntry) {
1084
- const unpublishingLatestRevision = entry.id === initialLatestStorageEntry.id;
1085
- if (unpublishingLatestRevision) {
1086
- entityBatch.put({
1087
- ...createEntryLatestKeys(storageEntry),
1088
- data: {
1089
- ...storageEntry
1090
- }
1091
- });
1092
- } else {
1093
- const latestStorageEntry = convertToStorageEntry({
1094
- storageEntry: initialLatestStorageEntry,
1095
- model
1096
- });
1097
-
1098
- // If the unpublished revision is not the latest one, we still need to
1099
- // update the latest record with the new values of entry-level meta fields.
1100
- const updatedEntryLevelMetaFields = pickEntryMetaFields(entry, isEntryLevelEntryMetaField);
1101
-
1102
- // 1. Update actual revision record.
1103
- entityBatch.put({
1104
- ...createEntryRevisionKeys(latestStorageEntry),
1105
- data: {
1106
- ...latestStorageEntry,
1107
- ...updatedEntryLevelMetaFields
1108
- }
1109
- });
1110
-
1111
- // 2. Update latest record.
1112
- entityBatch.put({
1113
- ...createEntryLatestKeys(latestStorageEntry),
1114
- data: {
1115
- ...latestStorageEntry,
1116
- ...updatedEntryLevelMetaFields
1117
- }
1118
- });
1119
- }
1120
- }
1121
- try {
1122
- await entityBatch.execute();
1123
- dataLoaders.clearAll({
1124
- model
1125
- });
1126
- return initialStorageEntry;
1127
- } catch (ex) {
1128
- throw new WebinyError(ex.message || "Could not execute unpublish batch.", ex.code || "UNPUBLISH_ERROR", {
1129
- entry,
1130
- storageEntry
1131
- });
1132
- }
1133
- };
1134
- const getUniqueFieldValues = async (model, params) => {
1135
- const {
1136
- where,
1137
- fieldId
1138
- } = params;
1139
- const field = model.fields.find(f => f.fieldId === fieldId);
1140
- if (!field) {
1141
- throw new WebinyError(`Could not find field with given "fieldId" value.`, "FIELD_NOT_FOUND", {
1142
- fieldId
1143
- });
1144
- }
1145
- const {
1146
- items
1147
- } = await list(model, {
1148
- where,
1149
- limit: MAX_LIST_LIMIT
1150
- });
1151
- const result = {};
1152
- for (const item of items) {
1153
- const fieldValue = item.values[field.fieldId];
1154
- if (!fieldValue) {
1155
- continue;
1156
- }
1157
- const values = Array.isArray(fieldValue) ? fieldValue : [fieldValue];
1158
- if (values.length === 0) {
1159
- continue;
1160
- }
1161
- for (const value of values) {
1162
- result[value] = {
1163
- value,
1164
- count: (result[value]?.count || 0) + 1
1165
- };
1166
- }
1167
- }
1168
- return Object.values(result).sort((a, b) => a.value > b.value ? 1 : b.value > a.value ? -1 : 0).sort((a, b) => b.count - a.count);
1169
- };
1170
- return {
1171
- create,
1172
- createRevisionFrom,
1173
- update,
1174
- move,
1175
- delete: deleteEntry,
1176
- moveToBin,
1177
- restoreFromBin,
1178
- deleteRevision,
1179
- deleteMultipleEntries,
1180
- getPreviousRevision,
1181
- getPublishedByIds,
1182
- getLatestByIds,
1183
- getByIds,
1184
- getRevisionById,
1185
- getPublishedRevisionByEntryId,
1186
- getLatestRevisionByEntryId,
1187
- get,
1188
- getRevisions,
1189
- publish,
1190
- list,
1191
- unpublish,
1192
- dataLoaders,
1193
- getUniqueFieldValues
1194
- };
1195
- };
1196
-
1197
- //# sourceMappingURL=index.js.map