@webiny/api-headless-cms-ddb-es 5.35.0 → 5.35.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/index.js +8 -6
  2. package/index.js.map +1 -1
  3. package/operations/entry/dataLoader/DataLoaderCache.d.ts +17 -0
  4. package/operations/entry/dataLoader/DataLoaderCache.js +43 -0
  5. package/operations/entry/dataLoader/DataLoaderCache.js.map +1 -0
  6. package/operations/entry/dataLoader/constants.d.ts +1 -0
  7. package/operations/entry/dataLoader/constants.js +9 -0
  8. package/operations/entry/dataLoader/constants.js.map +1 -0
  9. package/operations/entry/dataLoader/createBatchScheduleFn.d.ts +8 -0
  10. package/operations/entry/dataLoader/createBatchScheduleFn.js +23 -0
  11. package/operations/entry/dataLoader/createBatchScheduleFn.js.map +1 -0
  12. package/operations/entry/dataLoader/getAllEntryRevisions.d.ts +4 -0
  13. package/operations/entry/dataLoader/getAllEntryRevisions.js +43 -0
  14. package/operations/entry/dataLoader/getAllEntryRevisions.js.map +1 -0
  15. package/operations/entry/dataLoader/getLatestRevisionByEntryId.d.ts +4 -0
  16. package/operations/entry/dataLoader/getLatestRevisionByEntryId.js +53 -0
  17. package/operations/entry/dataLoader/getLatestRevisionByEntryId.js.map +1 -0
  18. package/operations/entry/dataLoader/getPublishedRevisionByEntryId.d.ts +4 -0
  19. package/operations/entry/dataLoader/getPublishedRevisionByEntryId.js +53 -0
  20. package/operations/entry/dataLoader/getPublishedRevisionByEntryId.js.map +1 -0
  21. package/operations/entry/dataLoader/getRevisionById.d.ts +4 -0
  22. package/operations/entry/dataLoader/getRevisionById.js +63 -0
  23. package/operations/entry/dataLoader/getRevisionById.js.map +1 -0
  24. package/operations/entry/dataLoader/index.d.ts +8 -0
  25. package/operations/entry/dataLoader/index.js +38 -0
  26. package/operations/entry/dataLoader/index.js.map +1 -0
  27. package/operations/entry/dataLoader/types.d.ts +6 -0
  28. package/operations/entry/dataLoader/types.js +5 -0
  29. package/operations/entry/dataLoader/types.js.map +1 -0
  30. package/operations/entry/dataLoaders.d.ts +15 -36
  31. package/operations/entry/dataLoaders.js +43 -241
  32. package/operations/entry/dataLoaders.js.map +1 -1
  33. package/operations/entry/index.d.ts +1 -1
  34. package/operations/entry/index.js +88 -4
  35. package/operations/entry/index.js.map +1 -1
  36. package/package.json +33 -33
  37. package/types.d.ts +10 -1
  38. package/types.js.map +1 -1
  39. package/plugins/CmsEntryElasticsearchFieldPlugin.d.ts +0 -12
  40. package/plugins/CmsEntryElasticsearchFieldPlugin.js +0 -24
  41. package/plugins/CmsEntryElasticsearchFieldPlugin.js.map +0 -1
@@ -1,51 +1,30 @@
1
- import { CmsEntry, CmsModel } from "@webiny/api-headless-cms/types";
1
+ import { CmsModel, CmsStorageEntry } from "@webiny/api-headless-cms/types";
2
2
  import { Entity } from "dynamodb-toolbox";
3
- export interface GetAllEntryRevisionsParams {
3
+ import { DataLoadersHandlerInterface, DataLoadersHandlerInterfaceClearAllParams } from "../../types";
4
+ interface DataLoaderParams {
5
+ model: Pick<CmsModel, "tenant" | "locale" | "modelId">;
4
6
  ids: readonly string[];
5
- model: CmsModel;
6
7
  }
7
- export interface GetRevisionByIdParams {
8
- ids: readonly string[];
9
- model: CmsModel;
10
- }
11
- export interface GetPublishedRevisionByEntryIdParams {
12
- ids: readonly string[];
13
- model: CmsModel;
14
- }
15
- export interface GetLatestRevisionByEntryIdParams {
16
- ids: readonly string[];
17
- model: CmsModel;
18
- }
19
- interface ClearLoaderParams {
20
- model: CmsModel;
21
- entry?: CmsEntry;
22
- }
23
- export interface DataLoadersHandlerParams {
8
+ interface DataLoadersHandlerParams {
24
9
  entity: Entity<any>;
25
10
  }
26
- export declare class DataLoadersHandler {
27
- private readonly loaders;
11
+ export interface ClearAllParams {
12
+ model: Pick<CmsModel, "tenant" | "locale" | "modelId">;
13
+ }
14
+ export declare class DataLoadersHandler implements DataLoadersHandlerInterface {
28
15
  private readonly entity;
16
+ private readonly cache;
29
17
  constructor(params: DataLoadersHandlerParams);
30
- getAllEntryRevisions(params: GetAllEntryRevisionsParams): Promise<CmsEntry[]>;
31
- clearAllEntryRevisions(params: ClearLoaderParams): void;
32
- getRevisionById(params: GetRevisionByIdParams): Promise<CmsEntry[]>;
33
- clearRevisionById(params: ClearLoaderParams): void;
34
- getPublishedRevisionByEntryId(params: GetPublishedRevisionByEntryIdParams): Promise<CmsEntry[]>;
35
- clearPublishedRevisionByEntryId(params: ClearLoaderParams): void;
36
- getLatestRevisionByEntryId(params: GetLatestRevisionByEntryIdParams): Promise<CmsEntry[]>;
37
- clearLatestRevisionByEntryId(params: ClearLoaderParams): void;
18
+ getAllEntryRevisions(params: DataLoaderParams): Promise<CmsStorageEntry[]>;
19
+ getRevisionById(params: DataLoaderParams): Promise<CmsStorageEntry[]>;
20
+ getPublishedRevisionByEntryId(params: DataLoaderParams): Promise<CmsStorageEntry[]>;
21
+ getLatestRevisionByEntryId(params: DataLoaderParams): Promise<CmsStorageEntry[]>;
38
22
  /**
39
23
  * TODO @ts-refactor
40
24
  * Maybe pass on the generics to DataLoader definition?
41
25
  */
42
26
  private getLoader;
43
27
  private loadMany;
44
- clearAll(params: Omit<ClearLoaderParams, "entry">): void;
45
- /**
46
- * Helper to clear the cache for certain data loader.
47
- * If entry is passed then clear target key only.
48
- */
49
- private clear;
28
+ clearAll(params?: DataLoadersHandlerInterfaceClearAllParams): void;
50
29
  }
51
30
  export {};
@@ -7,246 +7,75 @@ Object.defineProperty(exports, "__esModule", {
7
7
  exports.DataLoadersHandler = void 0;
8
8
  var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
9
9
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
10
- var _dataloader = _interopRequireDefault(require("dataloader"));
11
10
  var _error = _interopRequireDefault(require("@webiny/error"));
12
- var _query = require("@webiny/db-dynamodb/utils/query");
13
- var _keys = require("./keys");
14
- var _cleanup = require("@webiny/db-dynamodb/utils/cleanup");
11
+ var _DataLoaderCache = require("./dataLoader/DataLoaderCache");
12
+ var _dataLoader = require("./dataLoader");
15
13
  var _utils = require("@webiny/utils");
16
- var _batchRead = require("@webiny/db-dynamodb/utils/batchRead");
17
- const getAllEntryRevisions = params => {
18
- const {
19
- entity,
20
- model
21
- } = params;
22
- const {
23
- tenant,
24
- locale
25
- } = model;
26
- return new _dataloader.default(async ids => {
27
- const results = {};
28
- for (const id of ids) {
29
- const queryAllParams = {
30
- entity,
31
- partitionKey: (0, _keys.createPartitionKey)({
32
- tenant,
33
- locale,
34
- id
35
- }),
36
- options: {
37
- beginsWith: "REV#"
38
- }
39
- };
40
- const items = await (0, _query.queryAll)(queryAllParams);
41
- results[id] = (0, _cleanup.cleanupItems)(entity, items);
42
- }
43
- return ids.map(id => {
44
- return results[id] || [];
45
- });
46
- });
47
- };
48
- const getRevisionById = params => {
49
- const {
50
- entity,
51
- model
52
- } = params;
53
- const {
54
- locale,
55
- tenant
56
- } = model;
57
- return new _dataloader.default(async ids => {
58
- const queries = ids.reduce((collection, id) => {
59
- const partitionKey = (0, _keys.createPartitionKey)({
60
- tenant,
61
- locale,
62
- id
63
- });
64
- const {
65
- version
66
- } = (0, _utils.parseIdentifier)(id);
67
- if (!version) {
68
- return collection;
69
- }
70
- const sortKey = (0, _keys.createRevisionSortKey)({
71
- version
72
- });
73
- const keys = `${partitionKey}__${sortKey}`;
74
- if (collection[keys]) {
75
- return collection;
76
- }
77
- collection[keys] = entity.getBatch({
78
- PK: partitionKey,
79
- SK: sortKey
80
- });
81
- return collection;
82
- /**
83
- * We use any because there is no type for the return type.
84
- */
85
- }, {});
86
- const records = await (0, _batchRead.batchReadAll)({
87
- table: entity.table,
88
- items: Object.values(queries)
89
- });
90
- const items = (0, _cleanup.cleanupItems)(entity, records);
91
- return ids.map(id => {
92
- return items.filter(item => {
93
- return id === item.id;
94
- });
95
- });
96
- });
97
- };
98
- const getPublishedRevisionByEntryId = params => {
99
- const {
100
- entity,
101
- model
102
- } = params;
103
- const {
104
- locale,
105
- tenant
106
- } = model;
107
- const publishedKey = (0, _keys.createPublishedSortKey)();
108
- return new _dataloader.default(async ids => {
109
- const queries = ids.reduce((collection, id) => {
110
- const partitionKey = (0, _keys.createPartitionKey)({
111
- tenant,
112
- locale,
113
- id
114
- });
115
- if (collection[partitionKey]) {
116
- return collection;
117
- }
118
- collection[partitionKey] = entity.getBatch({
119
- PK: partitionKey,
120
- SK: publishedKey
121
- });
122
- return collection;
123
- /**
124
- * We use any because there is no type for the return type.
125
- */
126
- }, {});
127
- const records = await (0, _batchRead.batchReadAll)({
128
- table: entity.table,
129
- items: Object.values(queries)
130
- });
131
- const items = (0, _cleanup.cleanupItems)(entity, records);
132
- return ids.map(id => {
133
- const {
134
- id: entryId
135
- } = (0, _utils.parseIdentifier)(id);
136
- return items.filter(item => {
137
- return entryId === item.entryId;
138
- });
139
- });
140
- });
141
- };
142
- const getLatestRevisionByEntryId = params => {
143
- const {
144
- entity,
145
- model
146
- } = params;
147
- const {
148
- locale,
149
- tenant
150
- } = model;
151
- const latestKey = (0, _keys.createLatestSortKey)();
152
- return new _dataloader.default(async ids => {
153
- const queries = ids.reduce((collection, id) => {
154
- const partitionKey = (0, _keys.createPartitionKey)({
155
- tenant,
156
- locale,
157
- id
158
- });
159
- if (collection[partitionKey]) {
160
- return collection;
161
- }
162
- collection[partitionKey] = entity.getBatch({
163
- PK: partitionKey,
164
- SK: latestKey
165
- });
166
- return collection;
167
- /**
168
- * We use any because there is no type for the return type.
169
- */
170
- }, {});
171
- const records = await (0, _batchRead.batchReadAll)({
172
- table: entity.table,
173
- items: Object.values(queries)
174
- });
175
- const items = (0, _cleanup.cleanupItems)(entity, records);
176
- return ids.map(id => {
177
- const {
178
- id: entryId
179
- } = (0, _utils.parseIdentifier)(id);
180
- return items.filter(item => {
181
- return entryId === item.entryId;
182
- });
183
- });
184
- });
185
- };
186
- const dataLoaders = {
187
- getAllEntryRevisions,
188
- getRevisionById,
189
- getPublishedRevisionByEntryId,
190
- getLatestRevisionByEntryId
191
- };
192
- const loaderNames = Object.keys(dataLoaders);
193
14
  class DataLoadersHandler {
194
15
  constructor(params) {
195
- (0, _defineProperty2.default)(this, "loaders", new Map());
196
16
  (0, _defineProperty2.default)(this, "entity", void 0);
17
+ (0, _defineProperty2.default)(this, "cache", new _DataLoaderCache.DataLoaderCache());
197
18
  this.entity = params.entity;
198
19
  }
199
20
  async getAllEntryRevisions(params) {
200
- return await this.loadMany("getAllEntryRevisions", params, params.ids);
201
- }
202
- clearAllEntryRevisions(params) {
203
- this.clear("getAllEntryRevisions", params);
21
+ const ids = params.ids.map(id => {
22
+ const {
23
+ id: entryId
24
+ } = (0, _utils.parseIdentifier)(id);
25
+ return entryId;
26
+ });
27
+ return await this.loadMany("getAllEntryRevisions", params, ids);
204
28
  }
205
29
  async getRevisionById(params) {
206
30
  return await this.loadMany("getRevisionById", params, params.ids);
207
31
  }
208
- clearRevisionById(params) {
209
- this.clear("getRevisionById", params);
210
- }
211
32
  async getPublishedRevisionByEntryId(params) {
212
- return await this.loadMany("getPublishedRevisionByEntryId", params, params.ids);
213
- }
214
- clearPublishedRevisionByEntryId(params) {
215
- this.clear("getPublishedRevisionByEntryId", params);
33
+ const ids = params.ids.map(id => {
34
+ const {
35
+ id: entryId
36
+ } = (0, _utils.parseIdentifier)(id);
37
+ return entryId;
38
+ });
39
+ return await this.loadMany("getPublishedRevisionByEntryId", params, ids);
216
40
  }
217
41
  async getLatestRevisionByEntryId(params) {
218
- return await this.loadMany("getLatestRevisionByEntryId", params, params.ids);
219
- }
220
- clearLatestRevisionByEntryId(params) {
221
- this.clear("getLatestRevisionByEntryId", params);
42
+ const ids = params.ids.map(id => {
43
+ const {
44
+ id: entryId
45
+ } = (0, _utils.parseIdentifier)(id);
46
+ return entryId;
47
+ });
48
+ return await this.loadMany("getLatestRevisionByEntryId", params, ids);
222
49
  }
50
+
223
51
  /**
224
52
  * TODO @ts-refactor
225
53
  * Maybe pass on the generics to DataLoader definition?
226
54
  */
227
55
  getLoader(name, params) {
228
- if (!dataLoaders[name]) {
229
- throw new _error.default("Unknown data loader.", "UNKNOWN_DATA_LOADER", {
230
- name
231
- });
232
- }
233
56
  const {
234
57
  model
235
58
  } = params;
236
- const {
237
- tenant,
238
- locale
239
- } = model;
240
- const loaderKey = `${name}-${tenant}-${locale}-${model.modelId}`;
241
- if (!this.loaders.has(loaderKey)) {
242
- this.loaders.set(loaderKey, dataLoaders[name]((0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), {}, {
243
- entity: this.entity
244
- })));
59
+ const cacheParams = {
60
+ tenant: model.tenant,
61
+ locale: model.locale,
62
+ name
63
+ };
64
+ let loader = this.cache.getDataLoader(cacheParams);
65
+ if (loader) {
66
+ return loader;
245
67
  }
246
- return this.loaders.get(loaderKey);
68
+ const factory = (0, _dataLoader.getDataLoaderFactory)(name);
69
+ loader = factory({
70
+ entity: this.entity,
71
+ tenant: model.tenant,
72
+ locale: model.locale
73
+ });
74
+ this.cache.setDataLoader(cacheParams, loader);
75
+ return loader;
247
76
  }
248
77
  async loadMany(loader, params, ids) {
249
- let results;
78
+ let results = [];
250
79
  try {
251
80
  results = await this.getLoader(loader, params).loadMany(ids);
252
81
  if (Array.isArray(results) === true) {
@@ -280,34 +109,7 @@ class DataLoadersHandler {
280
109
  });
281
110
  }
282
111
  clearAll(params) {
283
- for (const name of loaderNames) {
284
- const loader = this.getLoader(name, params);
285
- loader.clearAll();
286
- }
287
- }
288
- /**
289
- * Helper to clear the cache for certain data loader.
290
- * If entry is passed then clear target key only.
291
- */
292
- clear(name, params) {
293
- const {
294
- entry
295
- } = params;
296
- const loader = this.getLoader(name, params);
297
- if (!entry) {
298
- loader.clearAll();
299
- return;
300
- }
301
- loader.clear(entry.id);
302
- const {
303
- tenant,
304
- locale
305
- } = params.model;
306
- loader.clear((0, _keys.createPartitionKey)({
307
- tenant,
308
- locale,
309
- id: entry.id
310
- }));
112
+ this.cache.clearAll(params === null || params === void 0 ? void 0 : params.model);
311
113
  }
312
114
  }
313
115
  exports.DataLoadersHandler = DataLoadersHandler;
@@ -1 +1 @@
1
- {"version":3,"names":["getAllEntryRevisions","params","entity","model","tenant","locale","DataLoader","ids","results","id","queryAllParams","partitionKey","createPartitionKey","options","beginsWith","items","queryAll","cleanupItems","map","getRevisionById","queries","reduce","collection","version","parseIdentifier","sortKey","createRevisionSortKey","keys","getBatch","PK","SK","records","batchReadAll","table","Object","values","filter","item","getPublishedRevisionByEntryId","publishedKey","createPublishedSortKey","entryId","getLatestRevisionByEntryId","latestKey","createLatestSortKey","dataLoaders","loaderNames","DataLoadersHandler","constructor","Map","loadMany","clearAllEntryRevisions","clear","clearRevisionById","clearPublishedRevisionByEntryId","clearLatestRevisionByEntryId","getLoader","name","WebinyError","loaderKey","modelId","loaders","has","set","get","loader","Array","isArray","acc","res","message","code","data","JSON","stringify","push","ex","error","clearAll","entry"],"sources":["dataLoaders.ts"],"sourcesContent":["import DataLoader from \"dataloader\";\nimport WebinyError from \"@webiny/error\";\nimport { CmsEntry, CmsModel } from \"@webiny/api-headless-cms/types\";\nimport { Entity } from \"dynamodb-toolbox\";\nimport { queryAll, QueryAllParams } from \"@webiny/db-dynamodb/utils/query\";\nimport {\n createLatestSortKey,\n createPartitionKey,\n createPublishedSortKey,\n createRevisionSortKey\n} from \"./keys\";\nimport { cleanupItems } from \"@webiny/db-dynamodb/utils/cleanup\";\nimport { parseIdentifier } from \"@webiny/utils\";\nimport { batchReadAll } from \"@webiny/db-dynamodb/utils/batchRead\";\n\nconst getAllEntryRevisions = (params: LoaderParams) => {\n const { entity, model } = params;\n const { tenant, locale } = model;\n return new DataLoader<string, CmsEntry[]>(async (ids: readonly string[]) => {\n const results: Record<string, CmsEntry[]> = {};\n for (const id of ids) {\n const queryAllParams: QueryAllParams = {\n entity,\n partitionKey: createPartitionKey({\n tenant,\n locale,\n id\n }),\n options: {\n beginsWith: \"REV#\"\n }\n };\n const items = await queryAll<CmsEntry>(queryAllParams);\n\n results[id] = cleanupItems(entity, items);\n }\n\n return ids.map(id => {\n return results[id] || [];\n });\n });\n};\n\nconst getRevisionById = (params: LoaderParams) => {\n const { entity, model } = params;\n const { locale, tenant } = model;\n\n return new DataLoader<string, CmsEntry[]>(async (ids: readonly string[]) => {\n const queries = ids.reduce((collection, id) => {\n const partitionKey = createPartitionKey({\n tenant,\n locale,\n id\n });\n const { version } = parseIdentifier(id);\n if (!version) {\n return collection;\n }\n const sortKey = createRevisionSortKey({\n version\n });\n const keys = `${partitionKey}__${sortKey}`;\n if (collection[keys]) {\n return collection;\n }\n\n collection[keys] = entity.getBatch({\n PK: partitionKey,\n SK: sortKey\n });\n\n return collection;\n /**\n * We use any because there is no type for the return type.\n */\n }, {} as Record<string, any>);\n\n const records = await batchReadAll<CmsEntry>({\n table: entity.table,\n items: Object.values(queries)\n });\n const items = cleanupItems(entity, records);\n\n return ids.map(id => {\n return items.filter(item => {\n return id === item.id;\n });\n });\n });\n};\n\nconst getPublishedRevisionByEntryId = (params: LoaderParams) => {\n const { entity, model } = params;\n const { locale, tenant } = model;\n\n const publishedKey = createPublishedSortKey();\n\n return new DataLoader<string, CmsEntry[]>(async (ids: readonly string[]) => {\n const queries = ids.reduce((collection, id) => {\n const partitionKey = createPartitionKey({\n tenant,\n locale,\n id\n });\n if (collection[partitionKey]) {\n return collection;\n }\n collection[partitionKey] = entity.getBatch({\n PK: partitionKey,\n SK: publishedKey\n });\n return collection;\n /**\n * We use any because there is no type for the return type.\n */\n }, {} as Record<string, any>);\n\n const records = await batchReadAll<CmsEntry>({\n table: entity.table,\n items: Object.values(queries)\n });\n const items = cleanupItems(entity, records);\n\n return ids.map(id => {\n const { id: entryId } = parseIdentifier(id);\n return items.filter(item => {\n return entryId === item.entryId;\n });\n });\n });\n};\n\nconst getLatestRevisionByEntryId = (params: LoaderParams) => {\n const { entity, model } = params;\n const { locale, tenant } = model;\n\n const latestKey = createLatestSortKey();\n\n return new DataLoader<string, CmsEntry[]>(async (ids: readonly string[]) => {\n const queries = ids.reduce((collection, id) => {\n const partitionKey = createPartitionKey({\n tenant,\n locale,\n id\n });\n if (collection[partitionKey]) {\n return collection;\n }\n collection[partitionKey] = entity.getBatch({\n PK: partitionKey,\n SK: latestKey\n });\n return collection;\n /**\n * We use any because there is no type for the return type.\n */\n }, {} as Record<string, any>);\n\n const records = await batchReadAll<CmsEntry>({\n table: entity.table,\n items: Object.values(queries)\n });\n const items = cleanupItems(entity, records);\n\n return ids.map(id => {\n const { id: entryId } = parseIdentifier(id);\n return items.filter(item => {\n return entryId === item.entryId;\n });\n });\n });\n};\n\nconst dataLoaders: Record<Loaders, any> = {\n getAllEntryRevisions,\n getRevisionById,\n getPublishedRevisionByEntryId,\n getLatestRevisionByEntryId\n};\n\nexport interface GetAllEntryRevisionsParams {\n ids: readonly string[];\n model: CmsModel;\n}\n\nexport interface GetRevisionByIdParams {\n ids: readonly string[];\n model: CmsModel;\n}\n\nexport interface GetPublishedRevisionByEntryIdParams {\n ids: readonly string[];\n model: CmsModel;\n}\n\nexport interface GetLatestRevisionByEntryIdParams {\n ids: readonly string[];\n model: CmsModel;\n}\n\ninterface LoaderParams {\n entity: Entity<any>;\n model: CmsModel;\n}\n\ninterface GetLoaderParams {\n model: CmsModel;\n}\n\ninterface ClearLoaderParams {\n model: CmsModel;\n entry?: CmsEntry;\n}\n\ntype Loaders =\n | \"getAllEntryRevisions\"\n | \"getRevisionById\"\n | \"getPublishedRevisionByEntryId\"\n | \"getLatestRevisionByEntryId\";\n\nconst loaderNames = Object.keys(dataLoaders) as Loaders[];\n\nexport interface DataLoadersHandlerParams {\n entity: Entity<any>;\n}\nexport class DataLoadersHandler {\n private readonly loaders: Map<string, DataLoader<any, any>> = new Map();\n private readonly entity: Entity<any>;\n\n public constructor(params: DataLoadersHandlerParams) {\n this.entity = params.entity;\n }\n\n public async getAllEntryRevisions(params: GetAllEntryRevisionsParams): Promise<CmsEntry[]> {\n return await this.loadMany(\"getAllEntryRevisions\", params, params.ids);\n }\n\n public clearAllEntryRevisions(params: ClearLoaderParams): void {\n this.clear(\"getAllEntryRevisions\", params);\n }\n\n public async getRevisionById(params: GetRevisionByIdParams): Promise<CmsEntry[]> {\n return await this.loadMany(\"getRevisionById\", params, params.ids);\n }\n\n public clearRevisionById(params: ClearLoaderParams): void {\n this.clear(\"getRevisionById\", params);\n }\n\n public async getPublishedRevisionByEntryId(\n params: GetPublishedRevisionByEntryIdParams\n ): Promise<CmsEntry[]> {\n return await this.loadMany(\"getPublishedRevisionByEntryId\", params, params.ids);\n }\n public clearPublishedRevisionByEntryId(params: ClearLoaderParams): void {\n this.clear(\"getPublishedRevisionByEntryId\", params);\n }\n\n public async getLatestRevisionByEntryId(\n params: GetLatestRevisionByEntryIdParams\n ): Promise<CmsEntry[]> {\n return await this.loadMany(\"getLatestRevisionByEntryId\", params, params.ids);\n }\n\n public clearLatestRevisionByEntryId(params: ClearLoaderParams): void {\n this.clear(\"getLatestRevisionByEntryId\", params);\n }\n /**\n * TODO @ts-refactor\n * Maybe pass on the generics to DataLoader definition?\n */\n private getLoader(name: Loaders, params: GetLoaderParams): DataLoader<any, any> {\n if (!dataLoaders[name]) {\n throw new WebinyError(\"Unknown data loader.\", \"UNKNOWN_DATA_LOADER\", {\n name\n });\n }\n const { model } = params;\n const { tenant, locale } = model;\n const loaderKey = `${name}-${tenant}-${locale}-${model.modelId}`;\n if (!this.loaders.has(loaderKey)) {\n this.loaders.set(\n loaderKey,\n dataLoaders[name]({\n ...params,\n entity: this.entity\n })\n );\n }\n return this.loaders.get(loaderKey) as DataLoader<any, any>;\n }\n\n private async loadMany(\n loader: Loaders,\n params: GetLoaderParams,\n ids: readonly string[]\n ): Promise<CmsEntry[]> {\n let results;\n try {\n results = await this.getLoader(loader, params).loadMany(ids);\n if (Array.isArray(results) === true) {\n return results.reduce((acc, res) => {\n if (Array.isArray(res) === false) {\n if (res && res.message) {\n throw new WebinyError(res.message, res.code, {\n ...res,\n data: JSON.stringify(res.data || {})\n });\n }\n throw new WebinyError(\n \"Result from the data loader must be an array of arrays which contain requested items.\",\n \"DATA_LOADER_RESULTS_ERROR\",\n {\n ...params,\n loader\n }\n );\n }\n acc.push(...res);\n return acc;\n }, []);\n }\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Data loader error.\",\n ex.code || \"DATA_LOADER_ERROR\",\n {\n error: ex,\n ...params,\n loader,\n ids\n }\n );\n }\n throw new WebinyError(\n `Data loader did not return array of items or empty array.`,\n \"INVALID_DATA_LOADER_RESULT\",\n {\n loader,\n ids,\n results\n }\n );\n }\n\n public clearAll(params: Omit<ClearLoaderParams, \"entry\">): void {\n for (const name of loaderNames) {\n const loader = this.getLoader(name, params);\n loader.clearAll();\n }\n }\n /**\n * Helper to clear the cache for certain data loader.\n * If entry is passed then clear target key only.\n */\n private clear(name: Loaders, params: ClearLoaderParams): void {\n const { entry } = params;\n const loader = this.getLoader(name, params);\n if (!entry) {\n loader.clearAll();\n return;\n }\n loader.clear(entry.id);\n const { tenant, locale } = params.model;\n loader.clear(\n createPartitionKey({\n tenant,\n locale,\n id: entry.id\n })\n );\n }\n}\n"],"mappings":";;;;;;;;;AAAA;AACA;AAGA;AACA;AAMA;AACA;AACA;AAEA,MAAMA,oBAAoB,GAAIC,MAAoB,IAAK;EACnD,MAAM;IAAEC,MAAM;IAAEC;EAAM,CAAC,GAAGF,MAAM;EAChC,MAAM;IAAEG,MAAM;IAAEC;EAAO,CAAC,GAAGF,KAAK;EAChC,OAAO,IAAIG,mBAAU,CAAqB,MAAOC,GAAsB,IAAK;IACxE,MAAMC,OAAmC,GAAG,CAAC,CAAC;IAC9C,KAAK,MAAMC,EAAE,IAAIF,GAAG,EAAE;MAClB,MAAMG,cAA8B,GAAG;QACnCR,MAAM;QACNS,YAAY,EAAE,IAAAC,wBAAkB,EAAC;UAC7BR,MAAM;UACNC,MAAM;UACNI;QACJ,CAAC,CAAC;QACFI,OAAO,EAAE;UACLC,UAAU,EAAE;QAChB;MACJ,CAAC;MACD,MAAM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AC7E,MAAwC,EAAQ;IAC5D,KAAK,MAAMwD,IAAI,IAAIX,WAAW,EAAE;MAC5B,MAAMmB,MAAM,GAAG,IAAI,CAACT,SAAS,CAACC,IAAI,EAAExD,MAAM,CAAC;MAC3CgE,MAAM,CAACa,QAAQ,EAAE;IACrB;EACJ;EACA;AACJ;AACA;AACA;EACY1B,KAAK,CAACK,IAAa,EAAExD,MAAyB,EAAQ;IAC1D,MAAM;MAAE8E;IAAM,CAAC,GAAG9E,MAAM;IACxB,MAAMgE,MAAM,GAAG,IAAI,CAACT,SAAS,CAACC,IAAI,EAAExD,MAAM,CAAC;IAC3C,IAAI,CAAC8E,KAAK,EAAE;MACRd,MAAM,CAACa,QAAQ,EAAE;MACjB;IACJ;IACAb,MAAM,CAACb,KAAK,CAAC2B,KAAK,CAACtE,EAAE,CAAC;IACtB,MAAM;MAAEL,MAAM;MAAEC;IAAO,CAAC,GAAGJ,MAAM,CAACE,KAAK;IACvC8D,MAAM,CAACb,KAAK,CACR,IAAAxC,wBAAkB,EAAC;MACfR,MAAM;MACNC,MAAM;MACNI,EAAE,EAAEsE,KAAK,CAACtE;IACd,CAAC,CAAC,CACL;EACL;AACJ;AAAC"}
1
+ {"version":3,"names":["DataLoadersHandler","constructor","params","DataLoaderCache","entity","getAllEntryRevisions","ids","map","id","entryId","parseIdentifier","loadMany","getRevisionById","getPublishedRevisionByEntryId","getLatestRevisionByEntryId","getLoader","name","model","cacheParams","tenant","locale","loader","cache","getDataLoader","factory","getDataLoaderFactory","setDataLoader","results","Array","isArray","reduce","acc","res","message","WebinyError","code","data","JSON","stringify","push","ex","error","clearAll"],"sources":["dataLoaders.ts"],"sourcesContent":["import DataLoader from \"dataloader\";\nimport WebinyError from \"@webiny/error\";\nimport { CmsModel, CmsStorageEntry } from \"@webiny/api-headless-cms/types\";\nimport { CacheKeyParams, DataLoaderCache } from \"~/operations/entry/dataLoader/DataLoaderCache\";\nimport { Entity } from \"dynamodb-toolbox\";\nimport { DataLoaders, getDataLoaderFactory } from \"~/operations/entry/dataLoader\";\nimport { parseIdentifier } from \"@webiny/utils\";\nimport { DataLoadersHandlerInterface, DataLoadersHandlerInterfaceClearAllParams } from \"~/types\";\n\ninterface DataLoaderParams {\n model: Pick<CmsModel, \"tenant\" | \"locale\" | \"modelId\">;\n ids: readonly string[];\n}\n\ninterface GetLoaderParams {\n model: Pick<CmsModel, \"tenant\" | \"locale\" | \"modelId\">;\n}\n\ninterface DataLoadersHandlerParams {\n entity: Entity<any>;\n}\n\nexport interface ClearAllParams {\n model: Pick<CmsModel, \"tenant\" | \"locale\" | \"modelId\">;\n}\n\nexport class DataLoadersHandler implements DataLoadersHandlerInterface {\n private readonly entity: Entity<any>;\n private readonly cache: DataLoaderCache = new DataLoaderCache();\n\n public constructor(params: DataLoadersHandlerParams) {\n this.entity = params.entity;\n }\n\n public async getAllEntryRevisions(params: DataLoaderParams): Promise<CmsStorageEntry[]> {\n const ids = params.ids.map(id => {\n const { id: entryId } = parseIdentifier(id);\n return entryId;\n });\n return await this.loadMany(\"getAllEntryRevisions\", params, ids);\n }\n\n public async getRevisionById(params: DataLoaderParams): Promise<CmsStorageEntry[]> {\n return await this.loadMany(\"getRevisionById\", params, params.ids);\n }\n\n public async getPublishedRevisionByEntryId(\n params: DataLoaderParams\n ): Promise<CmsStorageEntry[]> {\n const ids = params.ids.map(id => {\n const { id: entryId } = parseIdentifier(id);\n return entryId;\n });\n return await this.loadMany(\"getPublishedRevisionByEntryId\", params, ids);\n }\n\n public async getLatestRevisionByEntryId(params: DataLoaderParams): Promise<CmsStorageEntry[]> {\n const ids = params.ids.map(id => {\n const { id: entryId } = parseIdentifier(id);\n return entryId;\n });\n return await this.loadMany(\"getLatestRevisionByEntryId\", params, ids);\n }\n\n /**\n * TODO @ts-refactor\n * Maybe pass on the generics to DataLoader definition?\n */\n private getLoader(name: DataLoaders, params: GetLoaderParams): DataLoader<any, any> {\n const { model } = params;\n const cacheParams: CacheKeyParams = {\n tenant: model.tenant,\n locale: model.locale,\n name\n };\n let loader = this.cache.getDataLoader(cacheParams);\n if (loader) {\n return loader;\n }\n const factory = getDataLoaderFactory(name);\n loader = factory({\n entity: this.entity,\n tenant: model.tenant,\n locale: model.locale\n });\n this.cache.setDataLoader(cacheParams, loader);\n return loader;\n }\n\n private async loadMany(\n loader: DataLoaders,\n params: GetLoaderParams,\n ids: readonly string[]\n ): Promise<CmsStorageEntry[]> {\n let results: any[] = [];\n try {\n results = await this.getLoader(loader, params).loadMany(ids);\n if (Array.isArray(results) === true) {\n return results.reduce((acc, res) => {\n if (Array.isArray(res) === false) {\n if (res && res.message) {\n throw new WebinyError(res.message, res.code, {\n ...res,\n data: JSON.stringify(res.data || {})\n });\n }\n throw new WebinyError(\n \"Result from the data loader must be an array of arrays which contain requested items.\",\n \"DATA_LOADER_RESULTS_ERROR\",\n {\n ...params,\n loader\n }\n );\n }\n acc.push(...res);\n return acc;\n }, []);\n }\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Data loader error.\",\n ex.code || \"DATA_LOADER_ERROR\",\n {\n error: ex,\n ...params,\n loader,\n ids\n }\n );\n }\n throw new WebinyError(\n `Data loader did not return array of items or empty array.`,\n \"INVALID_DATA_LOADER_RESULT\",\n {\n loader,\n ids,\n results\n }\n );\n }\n\n public clearAll(params?: DataLoadersHandlerInterfaceClearAllParams): void {\n this.cache.clearAll(params?.model);\n }\n}\n"],"mappings":";;;;;;;;;AACA;AAEA;AAEA;AACA;AAoBO,MAAMA,kBAAkB,CAAwC;EAI5DC,WAAW,CAACC,MAAgC,EAAE;IAAA;IAAA,6CAFX,IAAIC,gCAAe,EAAE;IAG3D,IAAI,CAACC,MAAM,GAAGF,MAAM,CAACE,MAAM;EAC/B;EAEA,MAAaC,oBAAoB,CAACH,MAAwB,EAA8B;IACpF,MAAMI,GAAG,GAAGJ,MAAM,CAACI,GAAG,CAACC,GAAG,CAACC,EAAE,IAAI;MAC7B,MAAM;QAAEA,EAAE,EAAEC;MAAQ,CAAC,GAAG,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G;QACd,CAAC,EAAE,EAAE,CAAC;MACV;IACJ,CAAC,CAAC,OAAOS,EAAE,EAAE;MACT,MAAM,IAAIN,cAAW,CACjBM,EAAE,CAACP,OAAO,IAAI,oBAAoB,EAClCO,EAAE,CAACL,IAAI,IAAI,mBAAmB;QAE1BM,KAAK,EAAED;MAAE,GACNtC,MAAM;QACTmB,MAAM;QACNf;MAAG,GAEV;IACL;IACA,MAAM,IAAI4B,cAAW,CAChB,2DAA0D,EAC3D,4BAA4B,EAC5B;MACIb,MAAM;MACNf,GAAG;MACHqB;IACJ,CAAC,CACJ;EACL;EAEOe,QAAQ,CAACxC,MAAkD,EAAQ;IACtE,IAAI,CAACoB,KAAK,CAACoB,QAAQ,CAACxC,MAAM,aAANA,MAAM,uBAANA,MAAM,CAAEe,KAAK,CAAC;EACtC;AACJ;AAAC"}
@@ -1,7 +1,7 @@
1
- import { CmsEntryStorageOperations } from "@webiny/api-headless-cms/types";
2
1
  import { Entity } from "dynamodb-toolbox";
3
2
  import { Client } from "@elastic/elasticsearch";
4
3
  import { PluginsContainer } from "@webiny/plugins";
4
+ import { CmsEntryStorageOperations } from "../../types";
5
5
  export interface CreateEntriesStorageOperationsParams {
6
6
  entity: Entity<any>;
7
7
  esEntity: Entity<any>;
@@ -456,9 +456,10 @@ const createEntriesStorageOperations = params => {
456
456
  const {
457
457
  entry
458
458
  } = params;
459
+ const id = entry.id || entry.entryId;
459
460
  const model = getStorageOperationsModel(initialModel);
460
461
  const partitionKey = (0, _keys.createPartitionKey)({
461
- id: entry.id,
462
+ id,
462
463
  locale: model.locale,
463
464
  tenant: model.tenant
464
465
  });
@@ -499,7 +500,7 @@ const createEntriesStorageOperations = params => {
499
500
  } catch (ex) {
500
501
  throw new _error.default(ex.message || "Could not delete entry records from DynamoDB table.", ex.code || "DELETE_ENTRY_ERROR", {
501
502
  error: ex,
502
- entry
503
+ id
503
504
  });
504
505
  }
505
506
  try {
@@ -510,7 +511,7 @@ const createEntriesStorageOperations = params => {
510
511
  } catch (ex) {
511
512
  throw new _error.default(ex.message || "Could not delete entry records from DynamoDB Elasticsearch table.", ex.code || "DELETE_ENTRY_ERROR", {
512
513
  error: ex,
513
- entry
514
+ id
514
515
  });
515
516
  }
516
517
  };
@@ -620,6 +621,87 @@ const createEntriesStorageOperations = params => {
620
621
  });
621
622
  }
622
623
  };
624
+ const deleteMultipleEntries = async (initialModel, params) => {
625
+ const {
626
+ entries
627
+ } = params;
628
+ const model = getStorageOperationsModel(initialModel);
629
+ /**
630
+ * First we need all the revisions of the entries we want to delete.
631
+ */
632
+ const revisions = await dataLoaders.getAllEntryRevisions({
633
+ model,
634
+ ids: entries
635
+ });
636
+ /**
637
+ * Then we need to construct the queries for all the revisions and entries.
638
+ */
639
+ const items = [];
640
+ const esItems = [];
641
+ for (const id of entries) {
642
+ /**
643
+ * Latest item.
644
+ */
645
+ items.push(entity.deleteBatch({
646
+ PK: (0, _keys.createPartitionKey)({
647
+ id,
648
+ locale: model.locale,
649
+ tenant: model.tenant
650
+ }),
651
+ SK: "L"
652
+ }));
653
+ esItems.push(esEntity.deleteBatch({
654
+ PK: (0, _keys.createPartitionKey)({
655
+ id,
656
+ locale: model.locale,
657
+ tenant: model.tenant
658
+ }),
659
+ SK: "L"
660
+ }));
661
+ /**
662
+ * Published item.
663
+ */
664
+ items.push(entity.deleteBatch({
665
+ PK: (0, _keys.createPartitionKey)({
666
+ id,
667
+ locale: model.locale,
668
+ tenant: model.tenant
669
+ }),
670
+ SK: "P"
671
+ }));
672
+ esItems.push(esEntity.deleteBatch({
673
+ PK: (0, _keys.createPartitionKey)({
674
+ id,
675
+ locale: model.locale,
676
+ tenant: model.tenant
677
+ }),
678
+ SK: "P"
679
+ }));
680
+ }
681
+ /**
682
+ * Exact revisions of all the entries
683
+ */
684
+ for (const revision of revisions) {
685
+ items.push(entity.deleteBatch({
686
+ PK: (0, _keys.createPartitionKey)({
687
+ id: revision.id,
688
+ locale: model.locale,
689
+ tenant: model.tenant
690
+ }),
691
+ SK: (0, _keys.createRevisionSortKey)({
692
+ version: revision.version
693
+ })
694
+ }));
695
+ }
696
+ await (0, _batchWrite.batchWriteAll)({
697
+ table: entity.table,
698
+ items
699
+ });
700
+ await (0, _batchWrite.batchWriteAll)({
701
+ table: esEntity.table,
702
+ items: esItems
703
+ });
704
+ };
623
705
  const list = async (initialModel, params) => {
624
706
  var _response, _response$body;
625
707
  const model = getStorageOperationsModel(initialModel);
@@ -1141,6 +1223,7 @@ const createEntriesStorageOperations = params => {
1141
1223
  update,
1142
1224
  delete: deleteEntry,
1143
1225
  deleteRevision,
1226
+ deleteMultipleEntries,
1144
1227
  get,
1145
1228
  publish,
1146
1229
  unpublish,
@@ -1152,7 +1235,8 @@ const createEntriesStorageOperations = params => {
1152
1235
  getByIds,
1153
1236
  getLatestByIds,
1154
1237
  getPublishedByIds,
1155
- getPreviousRevision
1238
+ getPreviousRevision,
1239
+ dataLoaders
1156
1240
  };
1157
1241
  };
1158
1242
  exports.createEntriesStorageOperations = createEntriesStorageOperations;