@webiny/api-headless-cms-ddb 0.0.0-unstable.9e825fd5fb → 0.0.0-unstable.aa00eecd97

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 (38) hide show
  1. package/index.js +8 -5
  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 +57 -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 +57 -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 +11 -26
  31. package/operations/entry/dataLoaders.js +42 -205
  32. package/operations/entry/dataLoaders.js.map +1 -1
  33. package/operations/entry/index.d.ts +1 -1
  34. package/operations/entry/index.js +141 -28
  35. package/operations/entry/index.js.map +1 -1
  36. package/package.json +18 -18
  37. package/types.d.ts +10 -1
  38. package/types.js.map +1 -1
@@ -53,11 +53,24 @@ const convertFromStorageEntry = params => {
53
53
  values
54
54
  });
55
55
  };
56
+ const MAX_LIST_LIMIT = 10000;
56
57
  const createEntriesStorageOperations = params => {
57
58
  const {
58
59
  entity,
59
60
  plugins
60
61
  } = params;
62
+ let storageOperationsCmsModelPlugin;
63
+ const getStorageOperationsCmsModelPlugin = () => {
64
+ if (storageOperationsCmsModelPlugin) {
65
+ return storageOperationsCmsModelPlugin;
66
+ }
67
+ storageOperationsCmsModelPlugin = plugins.oneByType(_apiHeadlessCms.StorageOperationsCmsModelPlugin.type);
68
+ return storageOperationsCmsModelPlugin;
69
+ };
70
+ const getStorageOperationsModel = model => {
71
+ const plugin = getStorageOperationsCmsModelPlugin();
72
+ return plugin.getModel(model);
73
+ };
61
74
  const dataLoaders = new _dataLoaders.DataLoadersHandler({
62
75
  entity
63
76
  });
@@ -82,11 +95,12 @@ const createEntriesStorageOperations = params => {
82
95
  });
83
96
  };
84
97
  };
85
- const create = async (model, params) => {
98
+ const create = async (initialModel, params) => {
86
99
  const {
87
100
  entry,
88
101
  storageEntry: initialStorageEntry
89
102
  } = params;
103
+ const model = getStorageOperationsModel(initialModel);
90
104
  const partitionKey = (0, _keys.createPartitionKey)({
91
105
  id: entry.id,
92
106
  locale: model.locale,
@@ -101,7 +115,7 @@ const createEntriesStorageOperations = params => {
101
115
  /**
102
116
  * We need to:
103
117
  * - create new main entry item
104
- * - create new or update latest entry item
118
+ * - create new or update the latest entry item
105
119
  */
106
120
  const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
107
121
  locked,
@@ -148,11 +162,12 @@ const createEntriesStorageOperations = params => {
148
162
  }
149
163
  return initialStorageEntry;
150
164
  };
151
- const createRevisionFrom = async (model, params) => {
165
+ const createRevisionFrom = async (initialModel, params) => {
152
166
  const {
153
167
  entry,
154
168
  storageEntry: initialStorageEntry
155
169
  } = params;
170
+ const model = getStorageOperationsModel(initialModel);
156
171
  const partitionKey = (0, _keys.createPartitionKey)({
157
172
  id: entry.id,
158
173
  locale: model.locale,
@@ -200,11 +215,12 @@ const createEntriesStorageOperations = params => {
200
215
  */
201
216
  return initialStorageEntry;
202
217
  };
203
- const update = async (model, params) => {
218
+ const update = async (initialModel, params) => {
204
219
  const {
205
220
  entry,
206
221
  storageEntry: initialStorageEntry
207
222
  } = params;
223
+ const model = getStorageOperationsModel(initialModel);
208
224
  const partitionKey = (0, _keys.createPartitionKey)({
209
225
  id: entry.id,
210
226
  locale: model.locale,
@@ -242,7 +258,7 @@ const createEntriesStorageOperations = params => {
242
258
  }
243
259
 
244
260
  /**
245
- * We need the latest entry to update it as well if neccessary.
261
+ * We need the latest entry to update it as well if necessary.
246
262
  */
247
263
  const latestStorageEntry = await getLatestRevisionByEntryId(model, entry);
248
264
  if (latestStorageEntry && latestStorageEntry.id === entry.id) {
@@ -272,14 +288,16 @@ const createEntriesStorageOperations = params => {
272
288
  });
273
289
  }
274
290
  };
275
- const deleteEntry = async (model, params) => {
291
+ const deleteEntry = async (initialModel, params) => {
276
292
  const {
277
293
  entry
278
294
  } = params;
295
+ const id = entry.id || entry.entryId;
296
+ const model = getStorageOperationsModel(initialModel);
279
297
  const queryAllParams = {
280
298
  entity,
281
299
  partitionKey: (0, _keys.createPartitionKey)({
282
- id: entry.id,
300
+ id,
283
301
  locale: model.locale,
284
302
  tenant: model.tenant
285
303
  }),
@@ -293,7 +311,7 @@ const createEntriesStorageOperations = params => {
293
311
  } catch (ex) {
294
312
  throw new _error.default(ex.message || "Could not load all records.", ex.code || "LOAD_ALL_RECORDS_ERROR", {
295
313
  error: ex,
296
- entry
314
+ id
297
315
  });
298
316
  }
299
317
  const items = records.map(item => {
@@ -314,16 +332,17 @@ const createEntriesStorageOperations = params => {
314
332
  throw new _error.default(ex.message || "Could not delete the entry.", ex.code || "DELETE_ENTRY_ERROR", {
315
333
  error: ex,
316
334
  partitionKey: queryAllParams.partitionKey,
317
- entry
335
+ id
318
336
  });
319
337
  }
320
338
  };
321
- const deleteRevision = async (model, params) => {
339
+ const deleteRevision = async (initialModel, params) => {
322
340
  const {
323
341
  entry,
324
342
  latestEntry,
325
343
  latestStorageEntry: initialLatestStorageEntry
326
344
  } = params;
345
+ const model = getStorageOperationsModel(initialModel);
327
346
  const partitionKey = (0, _keys.createPartitionKey)({
328
347
  id: entry.id,
329
348
  locale: model.locale,
@@ -373,7 +392,68 @@ const createEntriesStorageOperations = params => {
373
392
  });
374
393
  }
375
394
  };
376
- const getLatestRevisionByEntryId = async (model, params) => {
395
+ const deleteMultipleEntries = async (initialModel, params) => {
396
+ const {
397
+ entries
398
+ } = params;
399
+ const model = getStorageOperationsModel(initialModel);
400
+ /**
401
+ * First we need all the revisions of the entries we want to delete.
402
+ */
403
+ const revisions = await dataLoaders.getAllEntryRevisions({
404
+ model,
405
+ ids: entries
406
+ });
407
+ /**
408
+ * Then we need to construct the queries for all the revisions and entries.
409
+ */
410
+ const items = [];
411
+ for (const id of entries) {
412
+ /**
413
+ * Latest item.
414
+ */
415
+ items.push(entity.deleteBatch({
416
+ PK: (0, _keys.createPartitionKey)({
417
+ id,
418
+ locale: model.locale,
419
+ tenant: model.tenant
420
+ }),
421
+ SK: "L"
422
+ }));
423
+ /**
424
+ * Published item.
425
+ */
426
+ items.push(entity.deleteBatch({
427
+ PK: (0, _keys.createPartitionKey)({
428
+ id,
429
+ locale: model.locale,
430
+ tenant: model.tenant
431
+ }),
432
+ SK: "P"
433
+ }));
434
+ }
435
+ /**
436
+ * Exact revisions of all the entries
437
+ */
438
+ for (const revision of revisions) {
439
+ items.push(entity.deleteBatch({
440
+ PK: (0, _keys.createPartitionKey)({
441
+ id: revision.id,
442
+ locale: model.locale,
443
+ tenant: model.tenant
444
+ }),
445
+ SK: (0, _keys.createRevisionSortKey)({
446
+ version: revision.version
447
+ })
448
+ }));
449
+ }
450
+ await (0, _batchWrite.batchWriteAll)({
451
+ table: entity.table,
452
+ items
453
+ });
454
+ };
455
+ const getLatestRevisionByEntryId = async (initialModel, params) => {
456
+ const model = getStorageOperationsModel(initialModel);
377
457
  const items = await dataLoaders.getLatestRevisionByEntryId({
378
458
  model,
379
459
  ids: [params.id]
@@ -387,7 +467,8 @@ const createEntriesStorageOperations = params => {
387
467
  model
388
468
  });
389
469
  };
390
- const getPublishedRevisionByEntryId = async (model, params) => {
470
+ const getPublishedRevisionByEntryId = async (initialModel, params) => {
471
+ const model = getStorageOperationsModel(initialModel);
391
472
  const items = await dataLoaders.getPublishedRevisionByEntryId({
392
473
  model,
393
474
  ids: [params.id]
@@ -401,7 +482,8 @@ const createEntriesStorageOperations = params => {
401
482
  model
402
483
  });
403
484
  };
404
- const getRevisionById = async (model, params) => {
485
+ const getRevisionById = async (initialModel, params) => {
486
+ const model = getStorageOperationsModel(initialModel);
405
487
  const items = await dataLoaders.getRevisionById({
406
488
  model,
407
489
  ids: [params.id]
@@ -415,7 +497,8 @@ const createEntriesStorageOperations = params => {
415
497
  model
416
498
  });
417
499
  };
418
- const getRevisions = async (model, params) => {
500
+ const getRevisions = async (initialModel, params) => {
501
+ const model = getStorageOperationsModel(initialModel);
419
502
  const items = await dataLoaders.getAllEntryRevisions({
420
503
  model,
421
504
  ids: [params.id]
@@ -427,7 +510,8 @@ const createEntriesStorageOperations = params => {
427
510
  });
428
511
  });
429
512
  };
430
- const getByIds = async (model, params) => {
513
+ const getByIds = async (initialModel, params) => {
514
+ const model = getStorageOperationsModel(initialModel);
431
515
  const items = await dataLoaders.getRevisionById({
432
516
  model,
433
517
  ids: params.ids
@@ -439,7 +523,8 @@ const createEntriesStorageOperations = params => {
439
523
  });
440
524
  });
441
525
  };
442
- const getLatestByIds = async (model, params) => {
526
+ const getLatestByIds = async (initialModel, params) => {
527
+ const model = getStorageOperationsModel(initialModel);
443
528
  const items = await dataLoaders.getLatestRevisionByEntryId({
444
529
  model,
445
530
  ids: params.ids
@@ -451,7 +536,8 @@ const createEntriesStorageOperations = params => {
451
536
  });
452
537
  });
453
538
  };
454
- const getPublishedByIds = async (model, params) => {
539
+ const getPublishedByIds = async (initialModel, params) => {
540
+ const model = getStorageOperationsModel(initialModel);
455
541
  const items = await dataLoaders.getPublishedRevisionByEntryId({
456
542
  model,
457
543
  ids: params.ids
@@ -463,7 +549,8 @@ const createEntriesStorageOperations = params => {
463
549
  });
464
550
  });
465
551
  };
466
- const getPreviousRevision = async (model, params) => {
552
+ const getPreviousRevision = async (initialModel, params) => {
553
+ const model = getStorageOperationsModel(initialModel);
467
554
  const {
468
555
  entryId,
469
556
  version
@@ -509,7 +596,8 @@ const createEntriesStorageOperations = params => {
509
596
  }));
510
597
  }
511
598
  };
512
- const list = async (model, params) => {
599
+ const list = async (initialModel, params) => {
600
+ const model = getStorageOperationsModel(initialModel);
513
601
  const {
514
602
  limit: initialLimit = 10,
515
603
  where: initialWhere,
@@ -518,7 +606,7 @@ const createEntriesStorageOperations = params => {
518
606
  fields,
519
607
  search
520
608
  } = params;
521
- const limit = initialLimit <= 0 || initialLimit >= 10000 ? 10000 : initialLimit;
609
+ const limit = initialLimit <= 0 || initialLimit >= MAX_LIST_LIMIT ? MAX_LIST_LIMIT : initialLimit;
522
610
  const type = initialWhere.published ? "P" : "L";
523
611
  const queryAllParams = {
524
612
  entity,
@@ -616,7 +704,8 @@ const createEntriesStorageOperations = params => {
616
704
  items: (0, _cleanup.cleanupItems)(entity, slicedItems)
617
705
  };
618
706
  };
619
- const get = async (model, params) => {
707
+ const get = async (initialModel, params) => {
708
+ const model = getStorageOperationsModel(initialModel);
620
709
  const {
621
710
  items
622
711
  } = await list(model, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), {}, {
@@ -624,11 +713,12 @@ const createEntriesStorageOperations = params => {
624
713
  }));
625
714
  return items.shift() || null;
626
715
  };
627
- const publish = async (model, params) => {
716
+ const publish = async (initialModel, params) => {
628
717
  const {
629
718
  entry,
630
719
  storageEntry: initialStorageEntry
631
720
  } = params;
721
+ const model = getStorageOperationsModel(initialModel);
632
722
  const partitionKey = (0, _keys.createPartitionKey)({
633
723
  id: entry.id,
634
724
  locale: model.locale,
@@ -636,7 +726,7 @@ const createEntriesStorageOperations = params => {
636
726
  });
637
727
 
638
728
  /**
639
- * We need the latest and published entries to see if something needs to be updated along side the publishing one.
729
+ * We need the latest and published entries to see if something needs to be updated alongside the publishing one.
640
730
  */
641
731
  const initialLatestStorageEntry = await getLatestRevisionByEntryId(model, entry);
642
732
  const initialPublishedStorageEntry = await getPublishedRevisionByEntryId(model, entry);
@@ -648,7 +738,7 @@ const createEntriesStorageOperations = params => {
648
738
  * We need to update:
649
739
  * - current entry revision sort key
650
740
  * - published sort key
651
- * - latest sort key - if entry updated is actually latest
741
+ * - the latest sort key - if entry updated is actually latest
652
742
  * - previous published entry to unpublished status - if any previously published entry
653
743
  */
654
744
  const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)({}, storageEntry), {}, {
@@ -704,11 +794,12 @@ const createEntriesStorageOperations = params => {
704
794
  });
705
795
  }
706
796
  };
707
- const unpublish = async (model, params) => {
797
+ const unpublish = async (initialModel, params) => {
708
798
  const {
709
799
  entry,
710
800
  storageEntry: initialStorageEntry
711
801
  } = params;
802
+ const model = getStorageOperationsModel(initialModel);
712
803
  const partitionKey = (0, _keys.createPartitionKey)({
713
804
  id: entry.id,
714
805
  locale: model.locale,
@@ -722,7 +813,7 @@ const createEntriesStorageOperations = params => {
722
813
  * We need to:
723
814
  * - delete currently published entry
724
815
  * - update current entry revision with new data
725
- * - update latest entry status - if entry being unpublished is latest
816
+ * - update the latest entry status - if entry being unpublished is latest
726
817
  */
727
818
  const items = [entity.deleteBatch({
728
819
  PK: partitionKey,
@@ -736,7 +827,7 @@ const createEntriesStorageOperations = params => {
736
827
  }))];
737
828
 
738
829
  /**
739
- * We need the latest entry to see if something needs to be updated along side the unpublishing one.
830
+ * We need the latest entry to see if something needs to be updated alongside the unpublishing one.
740
831
  */
741
832
  const latestStorageEntry = await getLatestRevisionByEntryId(model, entry);
742
833
  if (latestStorageEntry && entry.id === latestStorageEntry.id) {
@@ -764,12 +855,32 @@ const createEntriesStorageOperations = params => {
764
855
  });
765
856
  }
766
857
  };
858
+ const getUniqueFieldValues = async (model, params) => {
859
+ const {
860
+ where,
861
+ fieldId
862
+ } = params;
863
+ const field = model.fields.find(f => f.fieldId === fieldId);
864
+ if (!field) {
865
+ throw new _error.default(`Could not find field with given "fieldId" value.`, "FIELD_NOT_FOUND", {
866
+ fieldId
867
+ });
868
+ }
869
+ const {
870
+ items
871
+ } = await list(model, {
872
+ where,
873
+ limit: MAX_LIST_LIMIT
874
+ });
875
+ return Array.from(new Set(items.map(item => item.values[field.fieldId])));
876
+ };
767
877
  return {
768
878
  create,
769
879
  createRevisionFrom,
770
880
  update,
771
881
  delete: deleteEntry,
772
882
  deleteRevision,
883
+ deleteMultipleEntries,
773
884
  getPreviousRevision,
774
885
  getPublishedByIds,
775
886
  getLatestByIds,
@@ -781,7 +892,9 @@ const createEntriesStorageOperations = params => {
781
892
  getRevisions,
782
893
  publish,
783
894
  list,
784
- unpublish
895
+ unpublish,
896
+ dataLoaders,
897
+ getUniqueFieldValues
785
898
  };
786
899
  };
787
900
  exports.createEntriesStorageOperations = createEntriesStorageOperations;