@webiny/api-headless-cms-ddb 0.0.0-mt-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 (51) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +25 -0
  3. package/definitions/entry.d.ts +8 -0
  4. package/definitions/entry.js +97 -0
  5. package/definitions/group.d.ts +8 -0
  6. package/definitions/group.js +74 -0
  7. package/definitions/model.d.ts +8 -0
  8. package/definitions/model.js +96 -0
  9. package/definitions/settings.d.ts +8 -0
  10. package/definitions/settings.js +62 -0
  11. package/definitions/system.d.ts +8 -0
  12. package/definitions/system.js +50 -0
  13. package/definitions/table.d.ts +8 -0
  14. package/definitions/table.js +30 -0
  15. package/dynamoDb/index.d.ts +2 -0
  16. package/dynamoDb/index.js +24 -0
  17. package/dynamoDb/path/plainObject.d.ts +3 -0
  18. package/dynamoDb/path/plainObject.js +33 -0
  19. package/dynamoDb/path/ref.d.ts +3 -0
  20. package/dynamoDb/path/ref.js +27 -0
  21. package/dynamoDb/storage/date.d.ts +3 -0
  22. package/dynamoDb/storage/date.js +65 -0
  23. package/dynamoDb/storage/longText.d.ts +7 -0
  24. package/dynamoDb/storage/longText.js +83 -0
  25. package/dynamoDb/storage/richText.d.ts +8 -0
  26. package/dynamoDb/storage/richText.js +110 -0
  27. package/dynamoDb/transformValue/datetime.d.ts +3 -0
  28. package/dynamoDb/transformValue/datetime.js +47 -0
  29. package/index.d.ts +2 -0
  30. package/index.js +125 -0
  31. package/operations/entry/dataLoaders.d.ts +38 -0
  32. package/operations/entry/dataLoaders.js +303 -0
  33. package/operations/entry/index.d.ts +8 -0
  34. package/operations/entry/index.js +823 -0
  35. package/operations/entry/keys.d.ts +25 -0
  36. package/operations/entry/keys.js +62 -0
  37. package/operations/entry/systemFields.d.ts +2 -0
  38. package/operations/entry/systemFields.js +50 -0
  39. package/operations/entry/utils.d.ts +31 -0
  40. package/operations/entry/utils.js +406 -0
  41. package/operations/group/index.d.ts +8 -0
  42. package/operations/group/index.js +198 -0
  43. package/operations/model/index.d.ts +6 -0
  44. package/operations/model/index.js +161 -0
  45. package/operations/settings/index.d.ts +6 -0
  46. package/operations/settings/index.js +141 -0
  47. package/operations/system/index.d.ts +6 -0
  48. package/operations/system/index.js +105 -0
  49. package/package.json +61 -0
  50. package/types.d.ts +84 -0
  51. package/types.js +16 -0
@@ -0,0 +1,303 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.DataLoadersHandler = void 0;
9
+
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
12
+ var _dataloader = _interopRequireDefault(require("dataloader"));
13
+
14
+ var _error = _interopRequireDefault(require("@webiny/error"));
15
+
16
+ var _query = require("@webiny/db-dynamodb/utils/query");
17
+
18
+ var _keys = require("./keys");
19
+
20
+ var _cleanup = require("@webiny/db-dynamodb/utils/cleanup");
21
+
22
+ var _utils = require("@webiny/utils");
23
+
24
+ var _batchRead = require("@webiny/db-dynamodb/utils/batchRead");
25
+
26
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
27
+
28
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
29
+
30
+ const getAllEntryRevisions = params => {
31
+ const {
32
+ entity,
33
+ model
34
+ } = params;
35
+ const {
36
+ tenant,
37
+ locale
38
+ } = model;
39
+ return new _dataloader.default(async ids => {
40
+ const results = [];
41
+
42
+ for (const id of ids) {
43
+ const queryAllParams = {
44
+ entity,
45
+ partitionKey: (0, _keys.createPartitionKey)({
46
+ tenant,
47
+ locale,
48
+ id
49
+ }),
50
+ options: {
51
+ beginsWith: "REV#"
52
+ }
53
+ };
54
+ const items = await (0, _query.queryAll)(queryAllParams);
55
+ const entries = (0, _cleanup.cleanupItems)(entity, items);
56
+ results.push(entries);
57
+ }
58
+
59
+ return results;
60
+ });
61
+ };
62
+
63
+ const getRevisionById = params => {
64
+ const {
65
+ entity,
66
+ model
67
+ } = params;
68
+ const {
69
+ locale,
70
+ tenant
71
+ } = model;
72
+ return new _dataloader.default(async ids => {
73
+ const queries = ids.reduce((collection, id) => {
74
+ const partitionKey = (0, _keys.createPartitionKey)({
75
+ tenant,
76
+ locale,
77
+ id
78
+ });
79
+ const {
80
+ version
81
+ } = (0, _utils.parseIdentifier)(id);
82
+ const sortKey = (0, _keys.createRevisionSortKey)({
83
+ version
84
+ });
85
+ const keys = `${partitionKey}__${sortKey}`;
86
+
87
+ if (collection[keys]) {
88
+ return collection;
89
+ }
90
+
91
+ collection[keys] = entity.getBatch({
92
+ PK: partitionKey,
93
+ SK: sortKey
94
+ });
95
+ return collection;
96
+ }, {});
97
+ const records = await (0, _batchRead.batchReadAll)({
98
+ table: entity.table,
99
+ items: Object.values(queries)
100
+ });
101
+ const items = (0, _cleanup.cleanupItems)(entity, records);
102
+ return ids.map(id => {
103
+ return items.filter(item => {
104
+ return id === item.id;
105
+ });
106
+ });
107
+ });
108
+ };
109
+
110
+ const getPublishedRevisionByEntryId = params => {
111
+ const {
112
+ entity,
113
+ model
114
+ } = params;
115
+ const {
116
+ locale,
117
+ tenant
118
+ } = model;
119
+ const publishedKey = (0, _keys.createPublishedSortKey)();
120
+ return new _dataloader.default(async ids => {
121
+ const queries = ids.reduce((collection, id) => {
122
+ const partitionKey = (0, _keys.createPartitionKey)({
123
+ tenant,
124
+ locale,
125
+ id
126
+ });
127
+
128
+ if (collection[partitionKey]) {
129
+ return collection;
130
+ }
131
+
132
+ collection[partitionKey] = entity.getBatch({
133
+ PK: partitionKey,
134
+ SK: publishedKey
135
+ });
136
+ return collection;
137
+ }, {});
138
+ const records = await (0, _batchRead.batchReadAll)({
139
+ table: entity.table,
140
+ items: Object.values(queries)
141
+ });
142
+ const items = (0, _cleanup.cleanupItems)(entity, records);
143
+ return ids.map(id => {
144
+ const {
145
+ id: entryId
146
+ } = (0, _utils.parseIdentifier)(id);
147
+ return items.filter(item => {
148
+ return entryId === item.entryId;
149
+ });
150
+ });
151
+ });
152
+ };
153
+
154
+ const getLatestRevisionByEntryId = params => {
155
+ const {
156
+ entity,
157
+ model
158
+ } = params;
159
+ const {
160
+ locale,
161
+ tenant
162
+ } = model;
163
+ const latestKey = (0, _keys.createLatestSortKey)();
164
+ return new _dataloader.default(async ids => {
165
+ const queries = ids.reduce((collection, id) => {
166
+ const partitionKey = (0, _keys.createPartitionKey)({
167
+ tenant,
168
+ locale,
169
+ id
170
+ });
171
+
172
+ if (collection[partitionKey]) {
173
+ return collection;
174
+ }
175
+
176
+ collection[partitionKey] = entity.getBatch({
177
+ PK: partitionKey,
178
+ SK: latestKey
179
+ });
180
+ return collection;
181
+ }, {});
182
+ const records = await (0, _batchRead.batchReadAll)({
183
+ table: entity.table,
184
+ items: Object.values(queries)
185
+ });
186
+ const items = (0, _cleanup.cleanupItems)(entity, records);
187
+ return ids.map(id => {
188
+ const {
189
+ id: entryId
190
+ } = (0, _utils.parseIdentifier)(id);
191
+ return items.filter(item => {
192
+ return entryId === item.entryId;
193
+ });
194
+ });
195
+ });
196
+ };
197
+
198
+ const dataLoaders = {
199
+ getAllEntryRevisions,
200
+ getRevisionById,
201
+ getPublishedRevisionByEntryId,
202
+ getLatestRevisionByEntryId
203
+ };
204
+ const loaderNames = Object.keys(dataLoaders);
205
+
206
+ class DataLoadersHandler {
207
+ constructor(params) {
208
+ (0, _defineProperty2.default)(this, "loaders", new Map());
209
+ (0, _defineProperty2.default)(this, "entity", void 0);
210
+ this.entity = params.entity;
211
+ }
212
+
213
+ async getAllEntryRevisions(params) {
214
+ return await this.loadMany("getAllEntryRevisions", params, params.ids);
215
+ }
216
+
217
+ async getRevisionById(params) {
218
+ return await this.loadMany("getRevisionById", params, params.ids);
219
+ }
220
+
221
+ async getPublishedRevisionByEntryId(params) {
222
+ return await this.loadMany("getPublishedRevisionByEntryId", params, params.ids);
223
+ }
224
+
225
+ async getLatestRevisionByEntryId(params) {
226
+ return await this.loadMany("getLatestRevisionByEntryId", params, params.ids);
227
+ }
228
+
229
+ getLoader(name, params) {
230
+ if (!dataLoaders[name]) {
231
+ throw new _error.default("Unknown data loader.", "UNKNOWN_DATA_LOADER", {
232
+ name
233
+ });
234
+ }
235
+
236
+ const {
237
+ model
238
+ } = params;
239
+ const {
240
+ tenant,
241
+ locale
242
+ } = model;
243
+ const loaderKey = `${name}-${tenant}-${locale}-${model.modelId}`;
244
+
245
+ if (!this.loaders.has(loaderKey)) {
246
+ this.loaders.set(loaderKey, dataLoaders[name](_objectSpread(_objectSpread({}, params), {}, {
247
+ entity: this.entity
248
+ })));
249
+ }
250
+
251
+ return this.loaders.get(loaderKey);
252
+ }
253
+
254
+ async loadMany(loader, params, ids) {
255
+ let results;
256
+
257
+ try {
258
+ results = await this.getLoader(loader, params).loadMany(ids);
259
+
260
+ if (Array.isArray(results) === true) {
261
+ return results.reduce((acc, res) => {
262
+ if (Array.isArray(res) === false) {
263
+ if (res && res.message) {
264
+ throw new _error.default(res.message, res.code, _objectSpread(_objectSpread({}, res), {}, {
265
+ data: JSON.stringify(res.data || {})
266
+ }));
267
+ }
268
+
269
+ throw new _error.default("Result from the data loader must be an array of arrays which contain requested items.", "DATA_LOADER_RESULTS_ERROR", _objectSpread(_objectSpread({}, params), {}, {
270
+ loader
271
+ }));
272
+ }
273
+
274
+ acc.push(...res);
275
+ return acc;
276
+ }, []);
277
+ }
278
+ } catch (ex) {
279
+ throw new _error.default(ex.message || "Data loader error.", ex.code || "DATA_LOADER_ERROR", _objectSpread(_objectSpread({
280
+ error: ex
281
+ }, params), {}, {
282
+ loader,
283
+ ids
284
+ }));
285
+ }
286
+
287
+ throw new _error.default(`Data loader did not return array of items or empty array.`, "INVALID_DATA_LOADER_RESULT", {
288
+ loader,
289
+ ids,
290
+ results
291
+ });
292
+ }
293
+
294
+ clearAll(params) {
295
+ for (const name of loaderNames) {
296
+ const loader = this.getLoader(name, params);
297
+ loader.clearAll();
298
+ }
299
+ }
300
+
301
+ }
302
+
303
+ exports.DataLoadersHandler = DataLoadersHandler;
@@ -0,0 +1,8 @@
1
+ import { CmsEntryStorageOperations } from "@webiny/api-headless-cms/types";
2
+ import { Entity } from "dynamodb-toolbox";
3
+ import { PluginsContainer } from "@webiny/plugins";
4
+ export interface Params {
5
+ entity: Entity<any>;
6
+ plugins: PluginsContainer;
7
+ }
8
+ export declare const createEntriesStorageOperations: (params: Params) => CmsEntryStorageOperations;