@webiny/api-headless-cms-ddb 0.0.0-unstable.df7a8bb475 → 0.0.0-unstable.e1999482d0

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 (272) 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 +11 -5
  25. package/definitions/types.js +0 -3
  26. package/index.d.ts +10 -3
  27. package/index.js +40 -85
  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 +30 -0
  75. package/operations/entry/DdbListEntries.js +133 -0
  76. package/operations/entry/DdbListEntries.js.map +1 -0
  77. package/operations/entry/DdbMoveEntry.d.ts +16 -0
  78. package/operations/entry/DdbMoveEntry.js +63 -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.d.ts +1 -0
  96. package/operations/entry/dataLoader/DataLoaderCache.js +22 -29
  97. package/operations/entry/dataLoader/DataLoaderCache.js.map +1 -1
  98. package/operations/entry/dataLoader/constants.js +2 -1
  99. package/operations/entry/dataLoader/constants.js.map +1 -1
  100. package/operations/entry/dataLoader/createBatchScheduleFn.js +6 -15
  101. package/operations/entry/dataLoader/createBatchScheduleFn.js.map +1 -1
  102. package/operations/entry/dataLoader/getAllEntryRevisions.js +18 -26
  103. package/operations/entry/dataLoader/getAllEntryRevisions.js.map +1 -1
  104. package/operations/entry/dataLoader/getLatestRevisionByEntryId.js +32 -38
  105. package/operations/entry/dataLoader/getLatestRevisionByEntryId.js.map +1 -1
  106. package/operations/entry/dataLoader/getPublishedRevisionByEntryId.js +32 -38
  107. package/operations/entry/dataLoader/getPublishedRevisionByEntryId.js.map +1 -1
  108. package/operations/entry/dataLoader/getRevisionById.js +34 -44
  109. package/operations/entry/dataLoader/getRevisionById.js.map +1 -1
  110. package/operations/entry/dataLoader/index.js +8 -9
  111. package/operations/entry/dataLoader/index.js.map +1 -1
  112. package/operations/entry/dataLoader/types.d.ts +1 -0
  113. package/operations/entry/dataLoader/types.js +0 -3
  114. package/operations/entry/dataLoaders.d.ts +7 -6
  115. package/operations/entry/dataLoaders.js +89 -97
  116. package/operations/entry/dataLoaders.js.map +1 -1
  117. package/operations/entry/feature.d.ts +4 -0
  118. package/operations/entry/feature.js +55 -0
  119. package/operations/entry/feature.js.map +1 -0
  120. package/operations/entry/keys.d.ts +6 -0
  121. package/operations/entry/keys.js +57 -77
  122. package/operations/entry/keys.js.map +1 -1
  123. package/operations/entry/storageEntryUtils.d.ts +8 -0
  124. package/operations/entry/storageEntryUtils.js +25 -0
  125. package/operations/entry/storageEntryUtils.js.map +1 -0
  126. package/operations/group/DdbCreateGroup.d.ts +12 -0
  127. package/operations/group/DdbCreateGroup.js +35 -0
  128. package/operations/group/DdbCreateGroup.js.map +1 -0
  129. package/operations/group/DdbDeleteGroup.d.ts +12 -0
  130. package/operations/group/DdbDeleteGroup.js +31 -0
  131. package/operations/group/DdbDeleteGroup.js.map +1 -0
  132. package/operations/group/DdbGetGroup.d.ts +12 -0
  133. package/operations/group/DdbGetGroup.js +31 -0
  134. package/operations/group/DdbGetGroup.js.map +1 -0
  135. package/operations/group/DdbListGroups.d.ts +14 -0
  136. package/operations/group/DdbListGroups.js +52 -0
  137. package/operations/group/DdbListGroups.js.map +1 -0
  138. package/operations/group/DdbUpdateGroup.d.ts +12 -0
  139. package/operations/group/DdbUpdateGroup.js +35 -0
  140. package/operations/group/DdbUpdateGroup.js.map +1 -0
  141. package/operations/group/feature.d.ts +4 -0
  142. package/operations/group/feature.js +19 -0
  143. package/operations/group/feature.js.map +1 -0
  144. package/operations/group/keys.d.ts +15 -0
  145. package/operations/group/keys.js +17 -0
  146. package/operations/group/keys.js.map +1 -0
  147. package/operations/model/DdbCreateModel.d.ts +12 -0
  148. package/operations/model/DdbCreateModel.js +37 -0
  149. package/operations/model/DdbCreateModel.js.map +1 -0
  150. package/operations/model/DdbDeleteModel.d.ts +12 -0
  151. package/operations/model/DdbDeleteModel.js +31 -0
  152. package/operations/model/DdbDeleteModel.js.map +1 -0
  153. package/operations/model/DdbGetModel.d.ts +12 -0
  154. package/operations/model/DdbGetModel.js +30 -0
  155. package/operations/model/DdbGetModel.js.map +1 -0
  156. package/operations/model/DdbListModels.d.ts +12 -0
  157. package/operations/model/DdbListModels.js +36 -0
  158. package/operations/model/DdbListModels.js.map +1 -0
  159. package/operations/model/DdbUpdateModel.d.ts +12 -0
  160. package/operations/model/DdbUpdateModel.js +36 -0
  161. package/operations/model/DdbUpdateModel.js.map +1 -0
  162. package/operations/model/feature.d.ts +4 -0
  163. package/operations/model/feature.js +19 -0
  164. package/operations/model/feature.js.map +1 -0
  165. package/operations/model/keys.d.ts +15 -0
  166. package/operations/model/keys.js +16 -0
  167. package/operations/model/keys.js.map +1 -0
  168. package/package.json +26 -22
  169. package/types.d.ts +10 -48
  170. package/types.js +6 -5
  171. package/types.js.map +1 -1
  172. package/definitions/types.js.map +0 -1
  173. package/dynamoDb/index.d.ts +0 -2
  174. package/dynamoDb/index.js +0 -8
  175. package/dynamoDb/index.js.map +0 -1
  176. package/dynamoDb/path/locationFolderId.d.ts +0 -2
  177. package/dynamoDb/path/locationFolderId.js +0 -34
  178. package/dynamoDb/path/locationFolderId.js.map +0 -1
  179. package/dynamoDb/path/plainObject.d.ts +0 -2
  180. package/dynamoDb/path/plainObject.js +0 -26
  181. package/dynamoDb/path/plainObject.js.map +0 -1
  182. package/dynamoDb/storage/longText.d.ts +0 -10
  183. package/dynamoDb/storage/longText.js +0 -86
  184. package/dynamoDb/storage/longText.js.map +0 -1
  185. package/dynamoDb/storage/richText.d.ts +0 -2
  186. package/dynamoDb/storage/richText.js +0 -52
  187. package/dynamoDb/storage/richText.js.map +0 -1
  188. package/dynamoDb/transformValue/datetime.d.ts +0 -5
  189. package/dynamoDb/transformValue/datetime.js +0 -40
  190. package/dynamoDb/transformValue/datetime.js.map +0 -1
  191. package/operations/entry/dataLoader/types.js.map +0 -1
  192. package/operations/entry/filtering/createExpressions.d.ts +0 -26
  193. package/operations/entry/filtering/createExpressions.js +0 -175
  194. package/operations/entry/filtering/createExpressions.js.map +0 -1
  195. package/operations/entry/filtering/createFields.d.ts +0 -16
  196. package/operations/entry/filtering/createFields.js +0 -106
  197. package/operations/entry/filtering/createFields.js.map +0 -1
  198. package/operations/entry/filtering/extractSort.d.ts +0 -17
  199. package/operations/entry/filtering/extractSort.js +0 -84
  200. package/operations/entry/filtering/extractSort.js.map +0 -1
  201. package/operations/entry/filtering/filter.d.ts +0 -15
  202. package/operations/entry/filtering/filter.js +0 -150
  203. package/operations/entry/filtering/filter.js.map +0 -1
  204. package/operations/entry/filtering/fullTextSearch.d.ts +0 -14
  205. package/operations/entry/filtering/fullTextSearch.js +0 -42
  206. package/operations/entry/filtering/fullTextSearch.js.map +0 -1
  207. package/operations/entry/filtering/getValue.d.ts +0 -5
  208. package/operations/entry/filtering/getValue.js +0 -59
  209. package/operations/entry/filtering/getValue.js.map +0 -1
  210. package/operations/entry/filtering/index.d.ts +0 -2
  211. package/operations/entry/filtering/index.js +0 -4
  212. package/operations/entry/filtering/index.js.map +0 -1
  213. package/operations/entry/filtering/mapPlugins.d.ts +0 -8
  214. package/operations/entry/filtering/mapPlugins.js +0 -25
  215. package/operations/entry/filtering/mapPlugins.js.map +0 -1
  216. package/operations/entry/filtering/plugins/defaultFilterCreate.d.ts +0 -2
  217. package/operations/entry/filtering/plugins/defaultFilterCreate.js +0 -37
  218. package/operations/entry/filtering/plugins/defaultFilterCreate.js.map +0 -1
  219. package/operations/entry/filtering/plugins/index.d.ts +0 -1
  220. package/operations/entry/filtering/plugins/index.js +0 -9
  221. package/operations/entry/filtering/plugins/index.js.map +0 -1
  222. package/operations/entry/filtering/plugins/objectFilterCreate.d.ts +0 -2
  223. package/operations/entry/filtering/plugins/objectFilterCreate.js +0 -84
  224. package/operations/entry/filtering/plugins/objectFilterCreate.js.map +0 -1
  225. package/operations/entry/filtering/plugins/refFilterCreate.d.ts +0 -3
  226. package/operations/entry/filtering/plugins/refFilterCreate.js +0 -74
  227. package/operations/entry/filtering/plugins/refFilterCreate.js.map +0 -1
  228. package/operations/entry/filtering/plugins/searchableJsonFilterCreate.d.ts +0 -2
  229. package/operations/entry/filtering/plugins/searchableJsonFilterCreate.js +0 -57
  230. package/operations/entry/filtering/plugins/searchableJsonFilterCreate.js.map +0 -1
  231. package/operations/entry/filtering/sort.d.ts +0 -12
  232. package/operations/entry/filtering/sort.js +0 -67
  233. package/operations/entry/filtering/sort.js.map +0 -1
  234. package/operations/entry/filtering/systemFields.d.ts +0 -2
  235. package/operations/entry/filtering/systemFields.js +0 -116
  236. package/operations/entry/filtering/systemFields.js.map +0 -1
  237. package/operations/entry/filtering/transform.d.ts +0 -6
  238. package/operations/entry/filtering/transform.js +0 -11
  239. package/operations/entry/filtering/transform.js.map +0 -1
  240. package/operations/entry/filtering/types.d.ts +0 -40
  241. package/operations/entry/filtering/types.js +0 -3
  242. package/operations/entry/filtering/types.js.map +0 -1
  243. package/operations/entry/filtering/values.d.ts +0 -2
  244. package/operations/entry/filtering/values.js +0 -16
  245. package/operations/entry/filtering/values.js.map +0 -1
  246. package/operations/entry/filtering/where.d.ts +0 -5
  247. package/operations/entry/filtering/where.js +0 -28
  248. package/operations/entry/filtering/where.js.map +0 -1
  249. package/operations/entry/index.d.ts +0 -7
  250. package/operations/entry/index.js +0 -1196
  251. package/operations/entry/index.js.map +0 -1
  252. package/operations/group/index.d.ts +0 -9
  253. package/operations/group/index.js +0 -145
  254. package/operations/group/index.js.map +0 -1
  255. package/operations/model/index.d.ts +0 -7
  256. package/operations/model/index.js +0 -128
  257. package/operations/model/index.js.map +0 -1
  258. package/plugins/CmsEntryFieldFilterPathPlugin.d.ts +0 -23
  259. package/plugins/CmsEntryFieldFilterPathPlugin.js +0 -40
  260. package/plugins/CmsEntryFieldFilterPathPlugin.js.map +0 -1
  261. package/plugins/CmsEntryFieldFilterPlugin.d.ts +0 -43
  262. package/plugins/CmsEntryFieldFilterPlugin.js +0 -21
  263. package/plugins/CmsEntryFieldFilterPlugin.js.map +0 -1
  264. package/plugins/CmsEntryFieldSortingPlugin.d.ts +0 -42
  265. package/plugins/CmsEntryFieldSortingPlugin.js +0 -19
  266. package/plugins/CmsEntryFieldSortingPlugin.js.map +0 -1
  267. package/plugins/CmsFieldFilterValueTransformPlugin.d.ts +0 -11
  268. package/plugins/CmsFieldFilterValueTransformPlugin.js +0 -16
  269. package/plugins/CmsFieldFilterValueTransformPlugin.js.map +0 -1
  270. package/plugins/index.d.ts +0 -4
  271. package/plugins/index.js +0 -6
  272. package/plugins/index.js.map +0 -1
@@ -1,1196 +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, StorageTransformPlugin } 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
- const convertToStorageEntry = params => {
12
- const {
13
- model,
14
- storageEntry
15
- } = params;
16
- const values = model.convertValueKeyToStorage({
17
- fields: model.fields,
18
- values: storageEntry.values
19
- });
20
- return {
21
- ...storageEntry,
22
- values
23
- };
24
- };
25
- const convertFromStorageEntry = params => {
26
- const {
27
- model,
28
- storageEntry
29
- } = params;
30
- const values = model.convertValueKeyFromStorage({
31
- fields: model.fields,
32
- values: storageEntry.values
33
- });
34
- return {
35
- ...storageEntry,
36
- values
37
- };
38
- };
39
- const MAX_LIST_LIMIT = 1000000;
40
- export const createEntriesStorageOperations = params => {
41
- const {
42
- entity,
43
- plugins
44
- } = params;
45
- let storageOperationsCmsModelPlugin;
46
- const getStorageOperationsCmsModelPlugin = () => {
47
- if (storageOperationsCmsModelPlugin) {
48
- return storageOperationsCmsModelPlugin;
49
- }
50
- storageOperationsCmsModelPlugin = plugins.oneByType(StorageOperationsCmsModelPlugin.type);
51
- return storageOperationsCmsModelPlugin;
52
- };
53
- const getStorageOperationsModel = model => {
54
- const plugin = getStorageOperationsCmsModelPlugin();
55
- return plugin.getModel(model);
56
- };
57
- const dataLoaders = new DataLoadersHandler({
58
- entity
59
- });
60
- const createStorageTransformCallable = model => {
61
- // Cache StorageTransformPlugin to optimize execution.
62
- const storageTransformPlugins = plugins.byType(StorageTransformPlugin.type).reduce((collection, plugin) => {
63
- collection[plugin.fieldType] = plugin;
64
- return collection;
65
- }, {});
66
- return (field, value) => {
67
- const fieldType = getBaseFieldType(field);
68
- const plugin = storageTransformPlugins[fieldType];
69
- if (!plugin) {
70
- return value;
71
- }
72
- return plugin.fromStorage({
73
- model,
74
- field,
75
- value,
76
- getStoragePlugin(fieldType) {
77
- return storageTransformPlugins[fieldType] || storageTransformPlugins["*"];
78
- },
79
- plugins
80
- });
81
- };
82
- };
83
- const create = async (initialModel, params) => {
84
- const {
85
- entry,
86
- storageEntry: initialStorageEntry
87
- } = params;
88
- const model = getStorageOperationsModel(initialModel);
89
- const isPublished = entry.status === "published";
90
- const locked = isPublished ? true : entry.locked;
91
- const storageEntry = convertToStorageEntry({
92
- model,
93
- storageEntry: initialStorageEntry
94
- });
95
- const storageEntryRevisionKeys = createEntryRevisionKeys(entry);
96
- const storageEntryLatestKeys = createEntryLatestKeys(entry);
97
- /**
98
- * We need to:
99
- * - create new main entry item
100
- * - create new or update the latest entry item
101
- */
102
- const entityBatch = entity.createEntityWriter({
103
- put: [{
104
- ...storageEntryRevisionKeys,
105
- data: {
106
- ...storageEntry,
107
- locked
108
- }
109
- }, {
110
- ...storageEntryLatestKeys,
111
- data: {
112
- ...storageEntry,
113
- locked
114
- }
115
- }]
116
- });
117
-
118
- /**
119
- * We need to create published entry if
120
- */
121
- if (isPublished) {
122
- const storageEntryPublishedKeys = createEntryPublishedKeys(storageEntry);
123
- entityBatch.put({
124
- ...storageEntryPublishedKeys,
125
- data: {
126
- ...storageEntry,
127
- locked
128
- }
129
- });
130
- }
131
- try {
132
- await entityBatch.execute();
133
- dataLoaders.clearAll({
134
- model
135
- });
136
- } catch (ex) {
137
- throw new WebinyError(ex.message || "Could not insert data into the DynamoDB.", ex.code || "CREATE_ENTRY_ERROR", {
138
- error: ex,
139
- entry
140
- });
141
- }
142
- return initialStorageEntry;
143
- };
144
- const createRevisionFrom = async (initialModel, params) => {
145
- const {
146
- entry,
147
- storageEntry: initialStorageEntry
148
- } = params;
149
- const model = getStorageOperationsModel(initialModel);
150
- const storageEntry = convertToStorageEntry({
151
- storageEntry: initialStorageEntry,
152
- model
153
- });
154
-
155
- /**
156
- * We need to:
157
- * - create the main entry item
158
- * - update the latest entry item to the current one
159
- * - if the entry's status was set to "published":
160
- * - update the published entry item to the current one
161
- * - unpublish previously published revision (if any)
162
- */
163
- const entityBatch = entity.createEntityWriter({
164
- put: [{
165
- ...createEntryRevisionKeys(storageEntry),
166
- data: {
167
- ...storageEntry
168
- }
169
- }, {
170
- ...createEntryLatestKeys(storageEntry),
171
- data: {
172
- ...storageEntry
173
- }
174
- }]
175
- });
176
- const isPublished = entry.status === "published";
177
- if (isPublished) {
178
- entityBatch.put({
179
- ...createEntryPublishedKeys(storageEntry),
180
- data: {
181
- ...storageEntry
182
- }
183
- });
184
-
185
- // Unpublish previously published revision (if any).
186
- const [publishedRevisionStorageEntry] = await dataLoaders.getPublishedRevisionByEntryId({
187
- model,
188
- ids: [entry.id]
189
- });
190
- if (publishedRevisionStorageEntry) {
191
- entityBatch.put({
192
- ...createEntryRevisionKeys(publishedRevisionStorageEntry),
193
- data: {
194
- ...publishedRevisionStorageEntry,
195
- status: CONTENT_ENTRY_STATUS.UNPUBLISHED
196
- }
197
- });
198
- }
199
- }
200
- try {
201
- await entityBatch.execute();
202
- dataLoaders.clearAll({
203
- model
204
- });
205
- } catch (ex) {
206
- throw new WebinyError(ex.message || "Could not create revision from given entry.", ex.code || "CREATE_REVISION_ERROR", {
207
- error: ex,
208
- entry,
209
- storageEntry
210
- });
211
- }
212
- /**
213
- * There are no modifications on the entry created so just return the data.
214
- */
215
- return initialStorageEntry;
216
- };
217
- const update = async (initialModel, params) => {
218
- const {
219
- entry,
220
- storageEntry: initialStorageEntry
221
- } = params;
222
- const model = getStorageOperationsModel(initialModel);
223
- const isPublished = entry.status === "published";
224
- const locked = isPublished ? true : entry.locked;
225
- const storageEntry = convertToStorageEntry({
226
- model,
227
- storageEntry: initialStorageEntry
228
- });
229
- /**
230
- * We need to:
231
- * - update the current entry
232
- * - update the latest entry if the current entry is the latest one
233
- */
234
-
235
- const entityBatch = entity.createEntityWriter({
236
- put: [{
237
- ...createEntryRevisionKeys(storageEntry),
238
- data: {
239
- ...storageEntry,
240
- locked
241
- }
242
- }]
243
- });
244
- if (isPublished) {
245
- entityBatch.put({
246
- ...createEntryPublishedKeys(storageEntry),
247
- data: {
248
- ...storageEntry,
249
- locked
250
- }
251
- });
252
- }
253
-
254
- /**
255
- * We need the latest entry to update it as well if necessary.
256
- */
257
- const latestStorageEntry = await getLatestRevisionByEntryId(model, entry);
258
- if (latestStorageEntry) {
259
- const updatingLatestRevision = latestStorageEntry.id === entry.id;
260
- if (updatingLatestRevision) {
261
- entityBatch.put({
262
- ...createEntryLatestKeys(storageEntry),
263
- data: {
264
- ...storageEntry,
265
- locked
266
- }
267
- });
268
- } else {
269
- /**
270
- * If not updating latest revision, we still want to update the latest revision's
271
- * entry-level meta fields to match the current revision's entry-level meta fields.
272
- */
273
- const updatedEntryLevelMetaFields = pickEntryMetaFields(entry, isEntryLevelEntryMetaField);
274
-
275
- /**
276
- * First we update the regular DynamoDB table. Two updates are needed:
277
- * - one for the actual revision record
278
- * - one for the latest record
279
- */
280
- entityBatch.put({
281
- ...createEntryRevisionKeys(latestStorageEntry),
282
- data: {
283
- ...latestStorageEntry,
284
- ...updatedEntryLevelMetaFields
285
- }
286
- });
287
- entityBatch.put({
288
- ...createEntryLatestKeys(latestStorageEntry),
289
- data: {
290
- ...latestStorageEntry,
291
- ...updatedEntryLevelMetaFields
292
- }
293
- });
294
- }
295
- }
296
- try {
297
- await entityBatch.execute();
298
- dataLoaders.clearAll({
299
- model
300
- });
301
- return initialStorageEntry;
302
- } catch (ex) {
303
- throw new WebinyError(ex.message || "Could not update entry.", ex.code || "UPDATE_ERROR", {
304
- error: ex,
305
- entry,
306
- latestStorageEntry
307
- });
308
- }
309
- };
310
- const move = async (initialModel, id, folderId) => {
311
- /**
312
- * We need to:
313
- * - load all the revisions of the entry, including published and latest
314
- * - update all the revisions (published and latest ) of the entry with new folderId
315
- */
316
- const model = getStorageOperationsModel(initialModel);
317
- /**
318
- * First we need to load all the revisions and published / latest entry.
319
- */
320
- const partitionKey = createPartitionKey({
321
- id,
322
- tenant: model.tenant
323
- });
324
- const records = await entity.queryAll({
325
- partitionKey,
326
- options: {
327
- gte: " "
328
- }
329
- });
330
- /**
331
- * Then create the batch writes for the DynamoDB, with the updated folderId.
332
- */
333
- const entityBatch = entity.createEntityWriter({
334
- put: records.map(item => {
335
- return {
336
- ...item,
337
- data: {
338
- ...item.data,
339
- location: {
340
- ...item.data.location,
341
- folderId
342
- }
343
- }
344
- };
345
- })
346
- });
347
-
348
- /**
349
- * And finally write it...
350
- */
351
- try {
352
- await entityBatch.execute();
353
- } catch (ex) {
354
- throw WebinyError.from(ex, {
355
- message: "Could not move records to a new folder.",
356
- data: {
357
- id,
358
- folderId
359
- }
360
- });
361
- }
362
- };
363
- const moveToBin = async (initialModel, params) => {
364
- const {
365
- entry,
366
- storageEntry: initialStorageEntry
367
- } = params;
368
- const model = getStorageOperationsModel(initialModel);
369
-
370
- /**
371
- * First we need to load all the revisions and published / latest entries.
372
- */
373
-
374
- let records = [];
375
- try {
376
- records = await entity.queryAll({
377
- partitionKey: createPartitionKey({
378
- id: entry.id,
379
- tenant: model.tenant
380
- }),
381
- options: {
382
- gte: " "
383
- }
384
- });
385
- } catch (ex) {
386
- throw new WebinyError(ex.message || "Could not load all records.", ex.code || "LOAD_ALL_RECORDS_ERROR", {
387
- error: ex,
388
- id: entry.id
389
- });
390
- }
391
- if (records.length === 0) {
392
- return;
393
- }
394
- const storageEntry = convertToStorageEntry({
395
- model,
396
- storageEntry: initialStorageEntry
397
- });
398
-
399
- /**
400
- * Let's pick the `deleted` meta fields from the storage entry.
401
- */
402
- const updatedDeletedMetaFields = pickEntryMetaFields(storageEntry, isDeletedEntryMetaField);
403
-
404
- /**
405
- * Then create the batch writes for the DynamoDB, with the updated data.
406
- */
407
- const entityBatch = entity.createEntityWriter({
408
- put: records.map(record => {
409
- return {
410
- ...record,
411
- data: {
412
- ...record.data,
413
- ...updatedDeletedMetaFields,
414
- wbyDeleted: storageEntry.wbyDeleted,
415
- location: storageEntry.location,
416
- binOriginalFolderId: storageEntry.binOriginalFolderId
417
- }
418
- };
419
- })
420
- });
421
- /**
422
- * And finally write it...
423
- */
424
- try {
425
- await entityBatch.execute();
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
- });
875
- const totalCount = filteredItems.length;
876
-
877
- /**
878
- * Sorting is also done via the code.
879
- * It takes the sort input and sorts by it via the lodash sortBy method.
880
- */
881
- const sortedItems = sort({
882
- model,
883
- plugins,
884
- items: filteredItems,
885
- sort: sortBy,
886
- fields: modelFields
887
- });
888
- const start = parseInt(decodeCursor(after) || "0") || 0;
889
- const hasMoreItems = totalCount > start + limit;
890
- const end = limit > totalCount + start + limit ? undefined : start + limit;
891
- const slicedItems = sortedItems.slice(start, end);
892
- /**
893
- * Although we do not need a cursor here, we will use it as such to keep it standardized.
894
- * Number is simply encoded.
895
- */
896
- const cursor = encodeCursor(`${start + limit}`);
897
- return {
898
- hasMoreItems,
899
- totalCount,
900
- cursor,
901
- items: slicedItems
902
- };
903
- };
904
- const get = async (initialModel, params) => {
905
- const model = getStorageOperationsModel(initialModel);
906
- const {
907
- items
908
- } = await list(model, {
909
- ...params,
910
- limit: 1
911
- });
912
- return items.shift() || null;
913
- };
914
- const publish = async (initialModel, params) => {
915
- const {
916
- entry,
917
- storageEntry: initialStorageEntry
918
- } = params;
919
- const model = getStorageOperationsModel(initialModel);
920
-
921
- /**
922
- * We need the latest and published entries to see if something needs to be updated alongside the publishing one.
923
- */
924
- const initialLatestStorageEntry = await getLatestRevisionByEntryId(model, entry);
925
- if (!initialLatestStorageEntry) {
926
- throw new WebinyError(`Could not publish entry. Could not load latest ("L") record.`, "PUBLISH_ERROR", {
927
- entry
928
- });
929
- }
930
- const initialPublishedStorageEntry = await getPublishedRevisionByEntryId(model, entry);
931
- const storageEntry = convertToStorageEntry({
932
- model,
933
- storageEntry: initialStorageEntry
934
- });
935
-
936
- // 1. Update REV# and P records with new data.
937
- const entityBatch = entity.createEntityWriter({
938
- put: [{
939
- ...createEntryRevisionKeys(storageEntry),
940
- data: {
941
- ...storageEntry
942
- }
943
- }, {
944
- ...createEntryPublishedKeys(storageEntry),
945
- data: {
946
- ...storageEntry
947
- }
948
- }]
949
- });
950
-
951
- // 2. When it comes to the latest record, we need to perform a couple of different
952
- // updates, based on whether the entry being published is the latest revision or not.
953
- const publishedRevisionId = initialPublishedStorageEntry?.id;
954
- const publishingLatestRevision = entry.id === initialLatestStorageEntry.id;
955
- if (publishingLatestRevision) {
956
- // 2.1 If we're publishing the latest revision, we first need to update the L record.
957
- entityBatch.put({
958
- ...createEntryLatestKeys(storageEntry),
959
- data: {
960
- ...storageEntry
961
- }
962
- });
963
-
964
- // 2.2 Additionally, if we have a previously published entry, we need to mark it as unpublished.
965
- if (publishedRevisionId && publishedRevisionId !== entry.id) {
966
- const publishedStorageEntry = convertToStorageEntry({
967
- storageEntry: initialPublishedStorageEntry,
968
- model
969
- });
970
- entityBatch.put({
971
- ...createEntryRevisionKeys(publishedStorageEntry),
972
- data: {
973
- ...publishedStorageEntry,
974
- status: CONTENT_ENTRY_STATUS.UNPUBLISHED
975
- }
976
- });
977
- }
978
- } else {
979
- // 2.3 If the published revision is not the latest one, the situation is a bit
980
- // more complex. We first need to update the L and REV# records with the new
981
- // values of *only entry-level* meta fields.
982
- const updatedEntryLevelMetaFields = pickEntryMetaFields(entry, isEntryLevelEntryMetaField);
983
- const latestStorageEntry = convertToStorageEntry({
984
- storageEntry: initialLatestStorageEntry,
985
- model
986
- });
987
-
988
- // 2.3.1 Update L record. Apart from updating the entry-level meta fields, we also need
989
- // to change the status from "published" to "unpublished" (if the status is set to "published").
990
- let latestRevisionStatus = latestStorageEntry.status;
991
- if (latestRevisionStatus === CONTENT_ENTRY_STATUS.PUBLISHED) {
992
- latestRevisionStatus = CONTENT_ENTRY_STATUS.UNPUBLISHED;
993
- }
994
- const latestStorageEntryFields = {
995
- ...latestStorageEntry,
996
- ...updatedEntryLevelMetaFields,
997
- status: latestRevisionStatus
998
- };
999
- entityBatch.put({
1000
- ...createEntryLatestKeys(latestStorageEntryFields),
1001
- data: {
1002
- ...latestStorageEntryFields
1003
- }
1004
- });
1005
-
1006
- // 2.3.2 Update REV# record.
1007
- entityBatch.put({
1008
- ...createEntryRevisionKeys(latestStorageEntryFields),
1009
- data: {
1010
- ...latestStorageEntryFields
1011
- }
1012
- });
1013
-
1014
- // 2.3.3 Finally, if we got a published entry, but it wasn't the latest one, we need to take
1015
- // an extra step and mark it as unpublished.
1016
- const publishedRevisionDifferentFromLatest = publishedRevisionId && publishedRevisionId !== latestStorageEntry.id;
1017
- if (publishedRevisionDifferentFromLatest) {
1018
- const publishedStorageEntry = convertToStorageEntry({
1019
- storageEntry: initialPublishedStorageEntry,
1020
- model
1021
- });
1022
- entityBatch.put({
1023
- ...createEntryRevisionKeys(publishedStorageEntry),
1024
- data: {
1025
- ...publishedStorageEntry,
1026
- status: CONTENT_ENTRY_STATUS.UNPUBLISHED
1027
- }
1028
- });
1029
- }
1030
- }
1031
- try {
1032
- await entityBatch.execute();
1033
- dataLoaders.clearAll({
1034
- model
1035
- });
1036
- return initialStorageEntry;
1037
- } catch (ex) {
1038
- throw new WebinyError(ex.message || "Could not execute the publishing batch.", ex.code || "PUBLISH_ERROR", {
1039
- entry,
1040
- latestStorageEntry: initialLatestStorageEntry,
1041
- publishedStorageEntry: initialPublishedStorageEntry
1042
- });
1043
- }
1044
- };
1045
- const unpublish = async (initialModel, params) => {
1046
- const {
1047
- entry,
1048
- storageEntry: initialStorageEntry
1049
- } = params;
1050
- const model = getStorageOperationsModel(initialModel);
1051
- const partitionKey = createPartitionKey({
1052
- id: entry.id,
1053
- tenant: model.tenant
1054
- });
1055
- const storageEntry = convertToStorageEntry({
1056
- storageEntry: initialStorageEntry,
1057
- model
1058
- });
1059
- /**
1060
- * We need to:
1061
- * - delete currently published entry
1062
- * - update current entry revision with new data
1063
- * - update the latest entry status - if entry being unpublished is latest
1064
- */
1065
- const entityBatch = entity.createEntityWriter({
1066
- delete: [{
1067
- PK: partitionKey,
1068
- SK: createPublishedSortKey()
1069
- }],
1070
- put: [{
1071
- ...createEntryRevisionKeys(storageEntry),
1072
- data: {
1073
- ...storageEntry
1074
- }
1075
- }]
1076
- });
1077
-
1078
- /**
1079
- * We need the latest entry to see if something needs to be updated alongside the unpublishing one.
1080
- */
1081
- const initialLatestStorageEntry = await getLatestRevisionByEntryId(model, entry);
1082
- if (initialLatestStorageEntry) {
1083
- const unpublishingLatestRevision = entry.id === initialLatestStorageEntry.id;
1084
- if (unpublishingLatestRevision) {
1085
- entityBatch.put({
1086
- ...createEntryLatestKeys(storageEntry),
1087
- data: {
1088
- ...storageEntry
1089
- }
1090
- });
1091
- } else {
1092
- const latestStorageEntry = convertToStorageEntry({
1093
- storageEntry: initialLatestStorageEntry,
1094
- model
1095
- });
1096
-
1097
- // If the unpublished revision is not the latest one, we still need to
1098
- // update the latest record with the new values of entry-level meta fields.
1099
- const updatedEntryLevelMetaFields = pickEntryMetaFields(entry, isEntryLevelEntryMetaField);
1100
-
1101
- // 1. Update actual revision record.
1102
- entityBatch.put({
1103
- ...createEntryRevisionKeys(latestStorageEntry),
1104
- data: {
1105
- ...latestStorageEntry,
1106
- ...updatedEntryLevelMetaFields
1107
- }
1108
- });
1109
-
1110
- // 2. Update latest record.
1111
- entityBatch.put({
1112
- ...createEntryLatestKeys(latestStorageEntry),
1113
- data: {
1114
- ...latestStorageEntry,
1115
- ...updatedEntryLevelMetaFields
1116
- }
1117
- });
1118
- }
1119
- }
1120
- try {
1121
- await entityBatch.execute();
1122
- dataLoaders.clearAll({
1123
- model
1124
- });
1125
- return initialStorageEntry;
1126
- } catch (ex) {
1127
- throw new WebinyError(ex.message || "Could not execute unpublish batch.", ex.code || "UNPUBLISH_ERROR", {
1128
- entry,
1129
- storageEntry
1130
- });
1131
- }
1132
- };
1133
- const getUniqueFieldValues = async (model, params) => {
1134
- const {
1135
- where,
1136
- fieldId
1137
- } = params;
1138
- const field = model.fields.find(f => f.fieldId === fieldId);
1139
- if (!field) {
1140
- throw new WebinyError(`Could not find field with given "fieldId" value.`, "FIELD_NOT_FOUND", {
1141
- fieldId
1142
- });
1143
- }
1144
- const {
1145
- items
1146
- } = await list(model, {
1147
- where,
1148
- limit: MAX_LIST_LIMIT
1149
- });
1150
- const result = {};
1151
- for (const item of items) {
1152
- const fieldValue = item.values[field.fieldId];
1153
- if (!fieldValue) {
1154
- continue;
1155
- }
1156
- const values = Array.isArray(fieldValue) ? fieldValue : [fieldValue];
1157
- if (values.length === 0) {
1158
- continue;
1159
- }
1160
- for (const value of values) {
1161
- result[value] = {
1162
- value,
1163
- count: (result[value]?.count || 0) + 1
1164
- };
1165
- }
1166
- }
1167
- 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);
1168
- };
1169
- return {
1170
- create,
1171
- createRevisionFrom,
1172
- update,
1173
- move,
1174
- delete: deleteEntry,
1175
- moveToBin,
1176
- restoreFromBin,
1177
- deleteRevision,
1178
- deleteMultipleEntries,
1179
- getPreviousRevision,
1180
- getPublishedByIds,
1181
- getLatestByIds,
1182
- getByIds,
1183
- getRevisionById,
1184
- getPublishedRevisionByEntryId,
1185
- getLatestRevisionByEntryId,
1186
- get,
1187
- getRevisions,
1188
- publish,
1189
- list,
1190
- unpublish,
1191
- dataLoaders,
1192
- getUniqueFieldValues
1193
- };
1194
- };
1195
-
1196
- //# sourceMappingURL=index.js.map