@webiny/db-dynamodb 0.0.0-mt-3 → 0.0.0-unstable.2af142b57e

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 (130) hide show
  1. package/BatchProcess.d.ts +27 -11
  2. package/BatchProcess.js +10 -42
  3. package/BatchProcess.js.map +1 -0
  4. package/DynamoDbDriver.d.ts +19 -11
  5. package/DynamoDbDriver.js +11 -44
  6. package/DynamoDbDriver.js.map +1 -0
  7. package/QueryGenerator.d.ts +15 -13
  8. package/QueryGenerator.js +16 -31
  9. package/QueryGenerator.js.map +1 -0
  10. package/index.js +1 -3
  11. package/index.js.map +1 -0
  12. package/operators/comparison/beginsWith.js.map +1 -0
  13. package/operators/comparison/between.js.map +1 -0
  14. package/operators/comparison/eq.js +0 -2
  15. package/operators/comparison/eq.js.map +1 -0
  16. package/operators/comparison/gt.js.map +1 -0
  17. package/operators/comparison/gte.js.map +1 -0
  18. package/operators/comparison/lt.js.map +1 -0
  19. package/operators/comparison/lte.js.map +1 -0
  20. package/operators/index.js +1 -12
  21. package/operators/index.js.map +1 -0
  22. package/operators/logical/and.js +0 -5
  23. package/operators/logical/and.js.map +1 -0
  24. package/operators/logical/or.js +0 -5
  25. package/operators/logical/or.js.map +1 -0
  26. package/package.json +17 -16
  27. package/plugins/definitions/AttributePlugin.d.ts +3 -3
  28. package/plugins/definitions/AttributePlugin.js +1 -14
  29. package/plugins/definitions/AttributePlugin.js.map +1 -0
  30. package/plugins/definitions/DateTimeTransformPlugin.d.ts +2 -2
  31. package/plugins/definitions/DateTimeTransformPlugin.js +6 -20
  32. package/plugins/definitions/DateTimeTransformPlugin.js.map +1 -0
  33. package/plugins/definitions/FieldPathPlugin.d.ts +3 -3
  34. package/plugins/definitions/FieldPathPlugin.js +3 -17
  35. package/plugins/definitions/FieldPathPlugin.js.map +1 -0
  36. package/plugins/definitions/FieldPlugin.d.ts +3 -3
  37. package/plugins/definitions/FieldPlugin.js +1 -17
  38. package/plugins/definitions/FieldPlugin.js.map +1 -0
  39. package/plugins/definitions/NumberTransformPlugin.d.ts +2 -2
  40. package/plugins/definitions/NumberTransformPlugin.js +3 -18
  41. package/plugins/definitions/NumberTransformPlugin.js.map +1 -0
  42. package/plugins/definitions/TimeTransformPlugin.d.ts +2 -2
  43. package/plugins/definitions/TimeTransformPlugin.js +3 -21
  44. package/plugins/definitions/TimeTransformPlugin.js.map +1 -0
  45. package/plugins/definitions/ValueFilterPlugin.d.ts +10 -10
  46. package/plugins/definitions/ValueFilterPlugin.js +1 -13
  47. package/plugins/definitions/ValueFilterPlugin.js.map +1 -0
  48. package/plugins/definitions/ValueTransformPlugin.d.ts +8 -8
  49. package/plugins/definitions/ValueTransformPlugin.js +3 -18
  50. package/plugins/definitions/ValueTransformPlugin.js.map +1 -0
  51. package/plugins/definitions/assignFields.js +1 -10
  52. package/plugins/definitions/assignFields.js.map +1 -0
  53. package/plugins/filters/andIn.d.ts +2 -2
  54. package/plugins/filters/andIn.js +4 -9
  55. package/plugins/filters/andIn.js.map +1 -0
  56. package/plugins/filters/between.d.ts +2 -2
  57. package/plugins/filters/between.js +4 -9
  58. package/plugins/filters/between.js.map +1 -0
  59. package/plugins/filters/contains.d.ts +2 -2
  60. package/plugins/filters/contains.js +3 -6
  61. package/plugins/filters/contains.js.map +1 -0
  62. package/plugins/filters/eq.d.ts +2 -2
  63. package/plugins/filters/eq.js +15 -4
  64. package/plugins/filters/eq.js.map +1 -0
  65. package/plugins/filters/fuzzy.d.ts +2 -2
  66. package/plugins/filters/fuzzy.js +4 -8
  67. package/plugins/filters/fuzzy.js.map +1 -0
  68. package/plugins/filters/gt.d.ts +2 -2
  69. package/plugins/filters/gt.js +3 -4
  70. package/plugins/filters/gt.js.map +1 -0
  71. package/plugins/filters/gte.d.ts +2 -2
  72. package/plugins/filters/gte.js +3 -4
  73. package/plugins/filters/gte.js.map +1 -0
  74. package/plugins/filters/in.d.ts +2 -2
  75. package/plugins/filters/in.js +4 -9
  76. package/plugins/filters/in.js.map +1 -0
  77. package/plugins/filters/index.js +3 -15
  78. package/plugins/filters/index.js.map +1 -0
  79. package/plugins/filters/lt.d.ts +2 -2
  80. package/plugins/filters/lt.js +3 -4
  81. package/plugins/filters/lt.js.map +1 -0
  82. package/plugins/filters/lte.d.ts +2 -2
  83. package/plugins/filters/lte.js +3 -4
  84. package/plugins/filters/lte.js.map +1 -0
  85. package/plugins/filters/startsWith.d.ts +3 -0
  86. package/plugins/filters/startsWith.js +29 -0
  87. package/plugins/filters/startsWith.js.map +1 -0
  88. package/plugins/index.js +1 -5
  89. package/plugins/index.js.map +1 -0
  90. package/statements/createKeyConditionExpressionArgs.d.ts +12 -9
  91. package/statements/createKeyConditionExpressionArgs.js +5 -8
  92. package/statements/createKeyConditionExpressionArgs.js.map +1 -0
  93. package/statements/processStatement.d.ts +3 -4
  94. package/statements/processStatement.js +7 -11
  95. package/statements/processStatement.js.map +1 -0
  96. package/types.d.ts +37 -8
  97. package/types.js.map +1 -0
  98. package/utils/attributes.d.ts +2 -2
  99. package/utils/attributes.js +3 -14
  100. package/utils/attributes.js.map +1 -0
  101. package/utils/batchRead.d.ts +1 -1
  102. package/utils/batchRead.js +43 -34
  103. package/utils/batchRead.js.map +1 -0
  104. package/utils/batchWrite.js +1 -7
  105. package/utils/batchWrite.js.map +1 -0
  106. package/utils/cleanup.d.ts +2 -2
  107. package/utils/cleanup.js +9 -27
  108. package/utils/cleanup.js.map +1 -0
  109. package/utils/cursor.d.ts +2 -2
  110. package/utils/cursor.js +0 -6
  111. package/utils/cursor.js.map +1 -0
  112. package/utils/filter.d.ts +2 -2
  113. package/utils/filter.js +7 -43
  114. package/utils/filter.js.map +1 -0
  115. package/utils/get.d.ts +2 -2
  116. package/utils/get.js +0 -4
  117. package/utils/get.js.map +1 -0
  118. package/utils/listResponse.d.ts +1 -1
  119. package/utils/listResponse.js +7 -8
  120. package/utils/listResponse.js.map +1 -0
  121. package/utils/query.d.ts +2 -3
  122. package/utils/query.js +7 -34
  123. package/utils/query.js.map +1 -0
  124. package/utils/sort.d.ts +2 -2
  125. package/utils/sort.js +4 -23
  126. package/utils/sort.js.map +1 -0
  127. package/utils/table.js +1 -6
  128. package/utils/table.js.map +1 -0
  129. package/utils/documentClient.d.ts +0 -8
  130. package/utils/documentClient.js +0 -33
@@ -11,5 +11,5 @@ interface Params {
11
11
  * This helper function is meant to be used to batch read from one table.
12
12
  * It will fetch all results, as there is a next() method call built in.
13
13
  */
14
- export declare const batchReadAll: <T = any>(params: Params) => Promise<T[]>;
14
+ export declare const batchReadAll: <T = any>(params: Params, maxChunk?: number) => Promise<T[]>;
15
15
  export {};
@@ -1,58 +1,67 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
3
4
  Object.defineProperty(exports, "__esModule", {
4
5
  value: true
5
6
  });
6
7
  exports.batchReadAll = void 0;
7
-
8
+ var _chunk = _interopRequireDefault(require("lodash/chunk"));
9
+ var _error = _interopRequireDefault(require("@webiny/error"));
10
+ const MAX_BATCH_ITEMS = 100;
8
11
  const flatten = responses => {
9
12
  const entries = [];
10
13
  const values = Object.values(responses);
11
-
12
14
  for (const items of values) {
13
15
  entries.push(...items);
14
16
  }
15
-
16
17
  return entries;
17
18
  };
19
+ const batchReadAllChunk = async params => {
20
+ const {
21
+ table,
22
+ items
23
+ } = params;
24
+ const records = [];
25
+ const result = await table.batchGet(items);
26
+ if (!result || !result.Responses) {
27
+ return records;
28
+ }
29
+ records.push(...flatten(result.Responses));
30
+ if (!result.next || typeof result.next !== "function") {
31
+ return records;
32
+ }
33
+ let previous = result;
34
+ while (typeof previous.next === "function") {
35
+ const nextResult = await previous.next();
36
+ if (!nextResult) {
37
+ return records;
38
+ }
39
+ records.push(...flatten(nextResult.Responses));
40
+ previous = nextResult;
41
+ }
42
+ return records;
43
+ };
18
44
  /**
19
45
  * This helper function is meant to be used to batch read from one table.
20
46
  * It will fetch all results, as there is a next() method call built in.
21
47
  */
22
-
23
-
24
- const batchReadAll = async params => {
48
+ const batchReadAll = async (params, maxChunk = MAX_BATCH_ITEMS) => {
25
49
  if (params.items.length === 0) {
26
50
  return [];
51
+ } else if (maxChunk > MAX_BATCH_ITEMS) {
52
+ throw new _error.default(`Cannot set to load more than ${MAX_BATCH_ITEMS} items from the DynamoDB at once.`, "DYNAMODB_MAX_BATCH_GET_LIMIT_ERROR", {
53
+ maxChunk
54
+ });
27
55
  }
28
-
29
- const items = [];
30
- const result = await params.table.batchGet(params.items);
31
-
32
- if (result.Responses) {
33
- items.push(...flatten(result.Responses));
34
- }
35
-
36
- if (typeof result.next === "function") {
37
- let previous = result;
38
- let nextResult;
39
-
40
- while (nextResult = await previous.next()) {
41
- if (!nextResult) {
42
- return items;
43
- }
44
-
45
- items.push(...flatten(nextResult.Responses));
46
-
47
- if (!nextResult || typeof nextResult.next !== "function") {
48
- return items;
49
- }
50
-
51
- previous = nextResult;
52
- }
56
+ const records = [];
57
+ const chunkItemsList = (0, _chunk.default)(params.items, maxChunk);
58
+ for (const chunkItems of chunkItemsList) {
59
+ const results = await batchReadAllChunk({
60
+ table: params.table,
61
+ items: chunkItems
62
+ });
63
+ records.push(...results);
53
64
  }
54
-
55
- return items;
65
+ return records;
56
66
  };
57
-
58
67
  exports.batchReadAll = batchReadAll;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["MAX_BATCH_ITEMS","flatten","responses","entries","values","Object","items","push","batchReadAllChunk","params","table","records","result","batchGet","Responses","next","previous","nextResult","batchReadAll","maxChunk","length","WebinyError","chunkItemsList","lodashChunk","chunkItems","results"],"sources":["batchRead.ts"],"sourcesContent":["import { Table } from \"dynamodb-toolbox\";\nimport lodashChunk from \"lodash/chunk\";\nimport WebinyError from \"@webiny/error\";\n\ninterface Item {\n Table: Table;\n Key: any;\n}\ninterface Params {\n table: Table;\n items: Item[];\n}\n\nconst MAX_BATCH_ITEMS = 100;\n\nconst flatten = (responses: Record<string, any[]>): any[] => {\n const entries = [];\n const values = Object.values(responses);\n for (const items of values) {\n entries.push(...items);\n }\n return entries;\n};\n\ninterface BatchReadAllChunkParams {\n table: Table;\n items: Item[];\n}\nconst batchReadAllChunk = async <T = any>(params: BatchReadAllChunkParams): Promise<T[]> => {\n const { table, items } = params;\n const records: T[] = [];\n\n const result = await table.batchGet(items);\n if (!result || !result.Responses) {\n return records;\n }\n records.push(...flatten(result.Responses));\n if (!result.next || typeof result.next !== \"function\") {\n return records;\n }\n let previous = result;\n while (typeof previous.next === \"function\") {\n const nextResult = await previous.next();\n if (!nextResult) {\n return records;\n }\n records.push(...flatten(nextResult.Responses));\n previous = nextResult;\n }\n return records;\n};\n/**\n * This helper function is meant to be used to batch read from one table.\n * It will fetch all results, as there is a next() method call built in.\n */\nexport const batchReadAll = async <T = any>(\n params: Params,\n maxChunk = MAX_BATCH_ITEMS\n): Promise<T[]> => {\n if (params.items.length === 0) {\n return [];\n } else if (maxChunk > MAX_BATCH_ITEMS) {\n throw new WebinyError(\n `Cannot set to load more than ${MAX_BATCH_ITEMS} items from the DynamoDB at once.`,\n \"DYNAMODB_MAX_BATCH_GET_LIMIT_ERROR\",\n {\n maxChunk\n }\n );\n }\n\n const records: T[] = [];\n\n const chunkItemsList: Item[][] = lodashChunk(params.items, maxChunk);\n\n for (const chunkItems of chunkItemsList) {\n const results = await batchReadAllChunk<T>({\n table: params.table,\n items: chunkItems\n });\n\n records.push(...results);\n }\n\n return records;\n};\n"],"mappings":";;;;;;;AACA;AACA;AAWA,MAAMA,eAAe,GAAG,GAAG;AAE3B,MAAMC,OAAO,GAAIC,SAAgC,IAAY;EACzD,MAAMC,OAAO,GAAG,EAAE;EAClB,MAAMC,MAAM,GAAGC,MAAM,CAACD,MAAM,CAACF,SAAS,CAAC;EACvC,KAAK,MAAMI,KAAK,IAAIF,MAAM,EAAE;IACxBD,OAAO,CAACI,IAAI,CAAC,GAAGD,KAAK,CAAC;EAC1B;EACA,OAAOH,OAAO;AAClB,CAAC;AAMD,MAAMK,iB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M,CAACH,KAAK,EAAEa,QAAQ,CAAC;EAEpE,KAAK,MAAMK,UAAU,IAAIF,cAAc,EAAE;IACrC,MAAMG,OAAO,GAAG,MAAMjB,iBAAiB,CAAI;MACvCE,KAAK,EAAED,MAAM,CAACC,KAAK;MACnBJ,KAAK,EAAEkB;IACX,CAAC,CAAC;IAEFb,OAAO,CAACJ,IAAI,CAAC,GAAGkB,OAAO,CAAC;EAC5B;EAEA,OAAOd,OAAO;AAClB,CAAC;AAAC"}
@@ -1,14 +1,11 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
7
  exports.batchWriteAll = void 0;
9
-
10
8
  var _chunk = _interopRequireDefault(require("lodash/chunk"));
11
-
12
9
  /**
13
10
  * Method is meant for batch writing to a single table.
14
11
  * It expects already prepared items for the write.
@@ -19,12 +16,9 @@ const batchWriteAll = async (params, maxChunk = 25) => {
19
16
  if (params.items.length === 0) {
20
17
  return;
21
18
  }
22
-
23
19
  const chunkedItems = (0, _chunk.default)(params.items, maxChunk);
24
-
25
20
  for (const items of chunkedItems) {
26
21
  await params.table.batchWrite(items);
27
22
  }
28
23
  };
29
-
30
24
  exports.batchWriteAll = batchWriteAll;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["batchWriteAll","params","maxChunk","items","length","chunkedItems","lodashChunk","table","batchWrite"],"sources":["batchWrite.ts"],"sourcesContent":["import { Table } from \"dynamodb-toolbox\";\nimport lodashChunk from \"lodash/chunk\";\nimport { DocumentClient } from \"aws-sdk/clients/dynamodb\";\n\ninterface Item {\n [key: string]: DocumentClient.WriteRequest;\n}\ninterface Params {\n table: Table;\n items: Item[];\n}\n\n/**\n * Method is meant for batch writing to a single table.\n * It expects already prepared items for the write.\n * It can either delete or put items\n * The method does not check items before actually sending them into the underlying library.\n */\nexport const batchWriteAll = async (params: Params, maxChunk = 25): Promise<void> => {\n if (params.items.length === 0) {\n return;\n }\n const chunkedItems: Item[][] = lodashChunk(params.items, maxChunk);\n for (const items of chunkedItems) {\n await params.table.batchWrite(items);\n }\n};\n"],"mappings":";;;;;;;AACA;AAWA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMA,aAAa,GAAG,OAAOC,MAAc,EAAEC,QAAQ,GAAG,EAAE,KAAoB;EACjF,IAAID,MAAM,CAACE,KAAK,CAACC,MAAM,KAAK,CAAC,EAAE;IAC3B;EACJ;EACA,MAAMC,YAAsB,GAAG,IAAAC,cAAW,EAACL,MAAM,CAACE,KAAK,EAAED,QAAQ,CAAC;EAClE,KAAK,MAAMC,KAAK,IAAIE,YAAY,EAAE;IAC9B,MAAMJ,MAAM,CAACM,KAAK,CAACC,UAAU,CAACL,KAAK,CAAC;EACxC;AACJ,CAAC;AAAC"}
@@ -1,3 +1,3 @@
1
1
  import { Entity } from "dynamodb-toolbox";
2
- export declare const cleanupItem: <T>(entity: Entity<any>, item?: T & Record<string, any>, removeAttributes?: string[]) => T;
3
- export declare const cleanupItems: <T>(entity: Entity<any>, items: (T & Record<string, any>)[], removeAttributes?: string[]) => T[];
2
+ export declare function cleanupItem<T>(entity: Entity<any>, item?: (T & Record<string, any>) | null, removeAttributes?: string[]): T | null;
3
+ export declare function cleanupItems<T>(entity: Entity<any>, items: (T & Record<string, any>)[], removeAttributes?: string[]): T[];
package/utils/cleanup.js CHANGED
@@ -1,53 +1,35 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
- exports.cleanupItems = exports.cleanupItem = void 0;
9
-
10
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
-
12
- 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; }
13
-
14
- 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; }
15
-
7
+ exports.cleanupItem = cleanupItem;
8
+ exports.cleanupItems = cleanupItems;
9
+ var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
16
10
  /**
17
11
  * The attributes defined by us or the dynamodb-toolbox library.
18
12
  * Add more attributes if necessary.
19
13
  */
20
14
  const attributesToRemove = ["PK", "SK", "created", "_ct", "modified", "_mt", "entity", "_et", "GSI1_PK", "GSI1_SK", "TYPE"];
21
-
22
- const cleanupItem = (entity, item, removeAttributes = []) => {
15
+ function cleanupItem(entity, item, removeAttributes = []) {
23
16
  if (!item) {
24
17
  return null;
25
18
  }
26
-
27
- const newItem = _objectSpread({}, item);
28
-
19
+ const newItem = (0, _objectSpread2.default)({}, item);
29
20
  const targets = attributesToRemove.concat(removeAttributes);
30
21
  const attributes = entity.schema.attributes;
31
-
32
22
  for (const key in item) {
33
23
  if (item.hasOwnProperty(key) === false) {
34
24
  continue;
35
25
  }
36
-
37
26
  if (attributes[key] && targets.includes(key) === false) {
38
27
  continue;
39
28
  }
40
-
41
29
  delete newItem[key];
42
30
  }
43
-
44
31
  return newItem;
45
- };
46
-
47
- exports.cleanupItem = cleanupItem;
48
-
49
- const cleanupItems = (entity, items, removeAttributes = []) => {
32
+ }
33
+ function cleanupItems(entity, items, removeAttributes = []) {
50
34
  return items.map(item => cleanupItem(entity, item, removeAttributes));
51
- };
52
-
53
- exports.cleanupItems = cleanupItems;
35
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"names":["attributesToRemove","cleanupItem","entity","item","removeAttributes","newItem","targets","concat","attributes","schema","key","hasOwnProperty","includes","cleanupItems","items","map"],"sources":["cleanup.ts"],"sourcesContent":["import { Entity } from \"dynamodb-toolbox\";\n\n/**\n * The attributes defined by us or the dynamodb-toolbox library.\n * Add more attributes if necessary.\n */\nconst attributesToRemove = [\n \"PK\",\n \"SK\",\n \"created\",\n \"_ct\",\n \"modified\",\n \"_mt\",\n \"entity\",\n \"_et\",\n \"GSI1_PK\",\n \"GSI1_SK\",\n \"TYPE\"\n];\n\nexport function cleanupItem<T>(\n entity: Entity<any>,\n item?: (T & Record<string, any>) | null,\n removeAttributes: string[] = []\n): T | null {\n if (!item) {\n return null;\n }\n const newItem = {\n ...item\n };\n const targets = attributesToRemove.concat(removeAttributes);\n const attributes = entity.schema.attributes;\n for (const key in item) {\n if (item.hasOwnProperty(key) === false) {\n continue;\n }\n if (attributes[key] && targets.includes(key) === false) {\n continue;\n }\n delete newItem[key];\n }\n return newItem;\n}\n\nexport function cleanupItems<T>(\n entity: Entity<any>,\n items: (T & Record<string, any>)[],\n removeAttributes: string[] = []\n): T[] {\n return items.map(item => cleanupItem<T>(entity, item, removeAttributes) as T);\n}\n"],"mappings":";;;;;;;;;AAEA;AACA;AACA;AACA;AACA,MAAMA,kBAAkB,GAAG,CACvB,IAAI,EACJ,IAAI,EACJ,SAAS,EACT,KAAK,EACL,UAAU,EACV,KAAK,EACL,QAAQ,EACR,KAAK,EACL,SAAS,EACT,SAAS,EACT,MAAM,CACT;AAEM,SAASC,WAAW,CACvBC,MAAmB,EACnBC,IAAuC,EACvCC,gBAA0B,GAAG,EAAE,EACvB;EACR,IAAI,CAACD,IAAI,EAAE;IACP,OAAO,IAAI;EACf;EACA,MAAME,OAAO,mCACNF,IAAI,CACV;EACD,MAAMG,OAAO,GAAGN,kBAAkB,CAACO,MAAM,CAACH,gBAAgB,CAAC;EAC3D,MAAMI,UAAU,GAAGN,MAAM,CAACO,MAAM,CAACD,UAAU;EAC3C,KAAK,MAAME,GAAG,IAAIP,IAAI,EAAE;IACpB,IAAIA,IAAI,CAACQ,cAAc,CAACD,GAAG,CAAC,KAAK,KAAK,EAAE;MACpC;IACJ;IACA,IAAIF,UAAU,CAACE,GAAG,CAAC,IAAIJ,OAAO,CAACM,QAAQ,CAACF,GAAG,CAAC,KAAK,KAAK,EAAE;MACpD;IACJ;IACA,OAAOL,OAAO,CAACK,GAAG,CAAC;EACvB;EACA,OAAOL,OAAO;AAClB;AAEO,SAASQ,YAAY,CACxBX,MAAmB,EACnBY,KAAkC,EAClCV,gBAA0B,GAAG,EAAE,EAC5B;EACH,OAAOU,KAAK,CAACC,GAAG,CAACZ,IAAI,IAAIF,WAAW,CAAIC,MAAM,EAAEC,IAAI,EAAEC,gBAAgB,CAAM,CAAC;AACjF"}
package/utils/cursor.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export declare const encodeCursor: (cursor?: any) => string;
2
- export declare const decodeCursor: (cursor?: string) => any;
1
+ export declare const encodeCursor: (cursor?: any) => string | null;
2
+ export declare const decodeCursor: (cursor?: string | null) => string | null;
package/utils/cursor.js CHANGED
@@ -4,23 +4,17 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.encodeCursor = exports.decodeCursor = void 0;
7
-
8
7
  const encodeCursor = cursor => {
9
8
  if (!cursor) {
10
9
  return null;
11
10
  }
12
-
13
11
  return Buffer.from(JSON.stringify(cursor)).toString("base64");
14
12
  };
15
-
16
13
  exports.encodeCursor = encodeCursor;
17
-
18
14
  const decodeCursor = cursor => {
19
15
  if (!cursor) {
20
16
  return null;
21
17
  }
22
-
23
18
  return JSON.parse(Buffer.from(cursor, "base64").toString("ascii"));
24
19
  };
25
-
26
20
  exports.decodeCursor = decodeCursor;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["encodeCursor","cursor","Buffer","from","JSON","stringify","toString","decodeCursor","parse"],"sources":["cursor.ts"],"sourcesContent":["export const encodeCursor = (cursor?: any): string | null => {\n if (!cursor) {\n return null;\n }\n\n return Buffer.from(JSON.stringify(cursor)).toString(\"base64\");\n};\n\nexport const decodeCursor = (cursor?: string | null): string | null => {\n if (!cursor) {\n return null;\n }\n\n return JSON.parse(Buffer.from(cursor, \"base64\").toString(\"ascii\"));\n};\n"],"mappings":";;;;;;AAAO,MAAMA,YAAY,GAAIC,MAAY,IAAoB;EACzD,IAAI,CAACA,MAAM,EAAE;IACT,OAAO,IAAI;EACf;EAEA,OAAOC,MAAM,CAACC,IAAI,CAACC,IAAI,CAACC,SAAS,CAACJ,MAAM,CAAC,CAAC,CAACK,QAAQ,CAAC,QAAQ,CAAC;AACjE,CAAC;AAAC;AAEK,MAAMC,YAAY,GAAIN,MAAsB,IAAoB;EACnE,IAAI,CAACA,MAAM,EAAE;IACT,OAAO,IAAI;EACf;EAEA,OAAOG,IAAI,CAACI,KAAK,CAACN,MAAM,CAACC,IAAI,CAACF,MAAM,EAAE,QAAQ,CAAC,CAACK,QAAQ,CAAC,OAAO,CAAC,CAAC;AACtE,CAAC;AAAC"}
package/utils/filter.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { PluginsContainer } from "@webiny/plugins";
2
2
  import { FieldPlugin } from "../plugins/definitions/FieldPlugin";
3
- export interface Params<T extends any = any> {
3
+ export interface Params<T = any> {
4
4
  plugins: PluginsContainer;
5
5
  items: T[];
6
6
  where: Record<string, any>;
@@ -9,4 +9,4 @@ export interface Params<T extends any = any> {
9
9
  */
10
10
  fields: FieldPlugin[];
11
11
  }
12
- export declare const filterItems: <T extends unknown = any>(params: Params<T>) => T[];
12
+ export declare function filterItems<T = any>(params: Params<T>): T[];
package/utils/filter.js CHANGED
@@ -1,26 +1,23 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
- exports.filterItems = void 0;
9
-
7
+ exports.filterItems = filterItems;
10
8
  var _dotProp = _interopRequireDefault(require("dot-prop"));
11
-
12
9
  var _error = _interopRequireDefault(require("@webiny/error"));
13
-
14
10
  var _ValueFilterPlugin = require("../plugins/definitions/ValueFilterPlugin");
15
-
16
11
  const getMappedPlugins = params => {
17
12
  return params.plugins.byType(params.type).reduce((plugins, plugin) => {
13
+ /**
14
+ * We expect op to be a string, that is why we cast.
15
+ */
18
16
  const op = plugin[params.property];
19
17
  plugins[op] = plugin;
20
18
  return plugins;
21
19
  }, {});
22
20
  };
23
-
24
21
  const extractWhereArgs = key => {
25
22
  const result = key.split("_");
26
23
  const field = result.shift();
@@ -28,7 +25,6 @@ const extractWhereArgs = key => {
28
25
  /**
29
26
  * When rawOp is not, it means it is equal negated so just return that.
30
27
  */
31
-
32
28
  if (rawOp === "not") {
33
29
  return {
34
30
  field,
@@ -36,7 +32,6 @@ const extractWhereArgs = key => {
36
32
  negate: true
37
33
  };
38
34
  }
39
-
40
35
  const negate = rawOp.match("not_") !== null;
41
36
  const operation = rawOp.replace("not_", "");
42
37
  return {
@@ -45,19 +40,15 @@ const extractWhereArgs = key => {
45
40
  negate
46
41
  };
47
42
  };
48
-
49
43
  const findFilterPlugin = (plugins, operation) => {
50
44
  if (plugins[operation]) {
51
45
  return plugins[operation];
52
46
  }
53
-
54
47
  throw new _error.default(`Missing filter plugin definition.`, "FILTER_PLUGIN_ERROR", {
55
48
  operation
56
49
  });
57
50
  };
58
-
59
51
  const multiSearchFieldOperations = ["contains", "fuzzy"];
60
-
61
52
  const createFilters = params => {
62
53
  const {
63
54
  plugins,
@@ -68,11 +59,9 @@ const createFilters = params => {
68
59
  /**
69
60
  * Skip everything if there are no conditions to be applied.
70
61
  */
71
-
72
62
  if (keys.length === 0) {
73
63
  return [];
74
64
  }
75
-
76
65
  const filterPlugins = getMappedPlugins({
77
66
  plugins,
78
67
  type: _ValueFilterPlugin.ValueFilterPlugin.type,
@@ -80,29 +69,23 @@ const createFilters = params => {
80
69
  });
81
70
  return keys.reduce((filters, key) => {
82
71
  const compareValue = where[key];
83
-
84
72
  if (compareValue === undefined) {
85
73
  return filters;
86
74
  }
87
75
  /**
88
76
  * @see DynamoDbContainsFilter
89
77
  */
90
-
91
-
92
78
  if (multiSearchFieldOperations.includes(key) === true) {
93
79
  const data = compareValue;
94
80
  let transformValue = undefined;
95
81
  const paths = data.fields.map(field => {
96
82
  const fieldPlugin = fields.find(plugin => plugin.getField() === field);
97
-
98
83
  if (fieldPlugin) {
99
84
  transformValue = value => {
100
85
  return fieldPlugin.transformValue(value);
101
86
  };
102
-
103
87
  return fieldPlugin.getPath();
104
88
  }
105
-
106
89
  return field;
107
90
  });
108
91
  filters.push({
@@ -114,7 +97,6 @@ const createFilters = params => {
114
97
  });
115
98
  return filters;
116
99
  }
117
-
118
100
  const {
119
101
  field,
120
102
  operation,
@@ -124,15 +106,12 @@ const createFilters = params => {
124
106
  const fieldPlugin = fields.find(plugin => plugin.getField() === field);
125
107
  let path = field;
126
108
  let transformValue = undefined;
127
-
128
109
  if (fieldPlugin) {
129
110
  transformValue = value => {
130
111
  return fieldPlugin.transformValue(value);
131
112
  };
132
-
133
113
  path = fieldPlugin.getPath();
134
114
  }
135
-
136
115
  filters.push({
137
116
  compareValue,
138
117
  filterPlugin,
@@ -146,35 +125,27 @@ const createFilters = params => {
146
125
  /**
147
126
  * Transforms the value with given transformer callable.
148
127
  */
149
-
150
-
151
128
  const transform = (value, transformValue) => {
152
129
  if (!transformValue) {
153
130
  return value;
154
131
  }
155
-
156
132
  if (Array.isArray(value)) {
157
133
  return value.map(v => transformValue(v));
158
134
  }
159
-
160
135
  return transformValue(value);
161
136
  };
162
137
  /**
163
138
  * Creates a filter callable that we can send to the .filter() method of the array.
164
139
  */
165
-
166
-
167
140
  const createFilterCallable = params => {
168
141
  const filters = createFilters(params);
169
142
  /**
170
143
  * Just return null so there are no filters to be applied.
171
144
  * Later in the code we check for null so we do not loop through the items.
172
145
  */
173
-
174
146
  if (filters.length === 0) {
175
147
  return null;
176
148
  }
177
-
178
149
  return item => {
179
150
  for (const filter of filters) {
180
151
  const result = filter.paths.some(path => {
@@ -186,27 +157,20 @@ const createFilterCallable = params => {
186
157
  });
187
158
  return filter.negate ? !matched : matched;
188
159
  });
189
-
190
160
  if (result === false) {
191
161
  return false;
192
162
  }
193
163
  }
194
-
195
164
  return true;
196
165
  };
197
166
  };
198
-
199
- const filterItems = params => {
167
+ function filterItems(params) {
200
168
  const filter = createFilterCallable(params);
201
169
  /**
202
170
  * No point in going through all the items when there are no filters to be applied.
203
171
  */
204
-
205
172
  if (!filter) {
206
173
  return params.items;
207
174
  }
208
-
209
175
  return params.items.filter(filter);
210
- };
211
-
212
- exports.filterItems = filterItems;
176
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"names":["getMappedPlugins","params","plugins","byType","type","reduce","plugin","op","property","extractWhereArgs","key","result","split","field","shift","rawOp","length","join","operation","negate","match","replace","findFilterPlugin","WebinyError","multiSearchFieldOperations","createFilters","where","fields","keys","Object","filterPlugins","ValueFilterPlugin","filters","compareValue","undefined","includes","data","transformValue","paths","map","fieldPlugin","find","getField","value","getPath","push","filterPlugin","path","transform","Array","isArray","v","createFilterCallable","item","filter","some","dotProp","get","matched","matches","filterItems","items"],"sources":["filter.ts"],"sourcesContent":["import dotProp from \"dot-prop\";\nimport WebinyError from \"@webiny/error\";\nimport { Plugin, PluginsContainer } from \"@webiny/plugins\";\nimport { ValueFilterPlugin } from \"~/plugins/definitions/ValueFilterPlugin\";\nimport { FieldPlugin } from \"~/plugins/definitions/FieldPlugin\";\nimport { DynamoDbContainsFilter } from \"~/types\";\n\ntype TransformValue = (value: any) => any;\n\nexport interface Params<T = any> {\n plugins: PluginsContainer;\n items: T[];\n where: Record<string, any>;\n /**\n * An array of fields that require some special operation.\n */\n fields: FieldPlugin[];\n}\n\ninterface MappedPluginParams<T extends Plugin = Plugin> {\n plugins: PluginsContainer;\n type: string;\n property: keyof T;\n}\n\ninterface Filter {\n compareValue: any;\n filterPlugin: ValueFilterPlugin;\n transformValue?: TransformValue;\n paths: string[];\n negate: boolean;\n}\n\nconst getMappedPlugins = <T extends Plugin>(params: MappedPluginParams<T>): Record<string, T> => {\n return params.plugins.byType<T>(params.type).reduce((plugins, plugin) => {\n /**\n * We expect op to be a string, that is why we cast.\n */\n const op = plugin[params.property] as unknown as string;\n plugins[op] = plugin;\n return plugins;\n }, {} as Record<string, T>);\n};\n\ninterface ExtractWhereArgsResult {\n field: string;\n operation: string;\n negate: boolean;\n}\nconst extractWhereArgs = (key: string): ExtractWhereArgsResult => {\n const result = key.split(\"_\");\n const field = result.shift() as string;\n const rawOp = result.length === 0 ? \"eq\" : result.join(\"_\");\n /**\n * When rawOp is not, it means it is equal negated so just return that.\n */\n if (rawOp === \"not\") {\n return {\n field,\n operation: \"eq\",\n negate: true\n };\n }\n const negate = rawOp.match(\"not_\") !== null;\n const operation = rawOp.replace(\"not_\", \"\");\n return {\n field,\n operation,\n negate\n };\n};\n\nconst findFilterPlugin = (\n plugins: Record<string, ValueFilterPlugin>,\n operation: string\n): ValueFilterPlugin => {\n if (plugins[operation]) {\n return plugins[operation];\n }\n throw new WebinyError(`Missing filter plugin definition.`, \"FILTER_PLUGIN_ERROR\", {\n operation\n });\n};\n\nconst multiSearchFieldOperations = [\"contains\", \"fuzzy\"];\n\nconst createFilters = (params: Omit<Params, \"items\">): Filter[] => {\n const { plugins, where, fields } = params;\n\n const keys = Object.keys(where);\n /**\n * Skip everything if there are no conditions to be applied.\n */\n if (keys.length === 0) {\n return [];\n }\n const filterPlugins = getMappedPlugins<ValueFilterPlugin>({\n plugins,\n type: ValueFilterPlugin.type,\n property: \"operation\"\n });\n\n return keys.reduce((filters, key) => {\n const compareValue = where[key];\n if (compareValue === undefined) {\n return filters;\n }\n /**\n * @see DynamoDbContainsFilter\n */\n if (multiSearchFieldOperations.includes(key) === true) {\n const data: DynamoDbContainsFilter = compareValue;\n let transformValue: TransformValue | undefined = undefined;\n const paths = data.fields.map(field => {\n const fieldPlugin = fields.find(plugin => plugin.getField() === field);\n if (fieldPlugin) {\n transformValue = (value: any) => {\n return fieldPlugin.transformValue(value);\n };\n return fieldPlugin.getPath();\n }\n return field;\n });\n filters.push({\n compareValue: data.value,\n filterPlugin: findFilterPlugin(filterPlugins, key),\n transformValue,\n paths,\n negate: false\n });\n return filters;\n }\n\n const { field, operation, negate } = extractWhereArgs(key);\n\n const filterPlugin = findFilterPlugin(filterPlugins, operation);\n\n const fieldPlugin = fields.find(plugin => plugin.getField() === field);\n let path: string = field;\n let transformValue: TransformValue | undefined = undefined;\n if (fieldPlugin) {\n transformValue = (value: any) => {\n return fieldPlugin.transformValue(value);\n };\n path = fieldPlugin.getPath();\n }\n\n filters.push({\n compareValue,\n filterPlugin,\n transformValue,\n paths: [path],\n negate\n });\n\n return filters;\n }, [] as Filter[]);\n};\n/**\n * Transforms the value with given transformer callable.\n */\nconst transform = (value: any, transformValue?: TransformValue): any => {\n if (!transformValue) {\n return value;\n }\n if (Array.isArray(value)) {\n return value.map(v => transformValue(v));\n }\n return transformValue(value);\n};\n/**\n * Creates a filter callable that we can send to the .filter() method of the array.\n */\nconst createFilterCallable = (params: Omit<Params, \"items\">): ((item: any) => boolean) | null => {\n const filters = createFilters(params);\n /**\n * Just return null so there are no filters to be applied.\n * Later in the code we check for null so we do not loop through the items.\n */\n if (filters.length === 0) {\n return null;\n }\n\n return (item: any) => {\n for (const filter of filters) {\n const result = filter.paths.some(path => {\n const value = transform(dotProp.get(item, path), filter.transformValue);\n const compareValue = transform(filter.compareValue, filter.transformValue);\n const matched = filter.filterPlugin.matches({\n value,\n compareValue\n });\n\n return filter.negate ? !matched : matched;\n });\n if (result === false) {\n return false;\n }\n }\n return true;\n };\n};\n\nexport function filterItems<T = any>(params: Params<T>): T[] {\n const filter = createFilterCallable(params);\n /**\n * No point in going through all the items when there are no filters to be applied.\n */\n if (!filter) {\n return params.items;\n }\n return params.items.filter(filter);\n}\n"],"mappings":";;;;;;;AAAA;AACA;AAEA;AA8BA,MAAMA,gBAAgB,GAAsBC,MAA6B,IAAwB;EAC7F,OAAOA,MAAM,CAACC,OAAO,CAACC,MAAM,CAAIF,MAAM,CAACG,IAAI,CAAC,CAACC,MAAM,CAAC,CAACH,OAAO,EAAEI,MAAM,KAAK;IACrE;AACR;AACA;IACQ,MAAMC,EAAE,GAAGD,MAAM,CAACL,MAAM,CAACO,QAAQ,CAAsB;IACvDN,OAAO,CAACK,EAAE,CAAC,GAAGD,MAAM;IACpB,OAAOJ,OAAO;EAClB,CAAC,EAAE,CAAC,CAAC,CAAsB;AAC/B,CAAC;AAOD,MAAMO,gBAAgB,GAAIC,GAAW,IAA6B;EAC9D,MAAMC,MAAM,GAAGD,GAAG,CAACE,KAAK,CAAC,GAAG,CAAC;EAC7B,MAAMC,KAAK,GAAGF,MAAM,CAACG,KAAK,EAAY;EACtC,MAAMC,KAAK,GAAGJ,MAAM,CAACK,MAAM,KAAK,CAAC,GAAG,IAAI,GAAGL,MAAM,CAACM,IAAI,CAAC,GAAG,CAAC;EAC3D;AACJ;AACA;EACI,IAAIF,KAAK,KAAK,KAAK,EAAE;IACjB,OAAO;MACHF,KAAK;MACLK,SAAS,EAAE,IAAI;MACfC,MAAM,EAAE;IACZ,CAAC;EACL;EACA,MAAMA,MAAM,GAAGJ,KAAK,CAACK,KAAK,CAAC,MAAM,CAAC,KAAK,IAAI;EAC3C,MAAMF,SAAS,GAAGH,KAAK,CAACM,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC;EAC3C,OAAO;IACHR,KAAK;IACLK,SAAS;IACTC;EACJ,CAAC;AACL,CAAC;AAED,MAAMG,gBAAgB,GAAG,CACrBpB,OAA0C,EAC1CgB,SAAiB,KACG;EACpB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A,OAAO5D,MAAM,CAAC4D,KAAK,CAACP,MAAM,CAACA,MAAM,CAAC;AACtC"}
package/utils/get.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Entity } from "dynamodb-toolbox";
2
- export interface Params {
2
+ export interface GetRecordParams {
3
3
  entity: Entity<any>;
4
4
  keys: {
5
5
  PK: string;
@@ -14,4 +14,4 @@ export interface Params {
14
14
  *
15
15
  * @throws
16
16
  */
17
- export declare const get: <T>(params: Params) => Promise<T>;
17
+ export declare const get: <T>(params: GetRecordParams) => Promise<T | null>;
package/utils/get.js CHANGED
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.get = void 0;
7
-
8
7
  /**
9
8
  * Gets a single record from the DynamoDB table.
10
9
  * Returns either record or null.
@@ -19,12 +18,9 @@ const get = async params => {
19
18
  keys
20
19
  } = params;
21
20
  const result = await entity.get(keys);
22
-
23
21
  if (!result || !result.Item) {
24
22
  return null;
25
23
  }
26
-
27
24
  return result.Item;
28
25
  };
29
-
30
26
  exports.get = get;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["get","params","entity","keys","result","Item"],"sources":["get.ts"],"sourcesContent":["import { Entity } from \"dynamodb-toolbox\";\n\nexport interface GetRecordParams {\n entity: Entity<any>;\n keys: {\n PK: string;\n SK: string;\n };\n}\n\n/**\n * Gets a single record from the DynamoDB table.\n * Returns either record or null.\n *\n * Be aware to wrap in try/catch to avoid the error killing your app.\n *\n * @throws\n */\nexport const get = async <T>(params: GetRecordParams): Promise<T | null> => {\n const { entity, keys } = params;\n\n const result = await entity.get(keys);\n\n if (!result || !result.Item) {\n return null;\n }\n return result.Item;\n};\n"],"mappings":";;;;;;AAUA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMA,GAAG,GAAG,MAAUC,MAAuB,IAAwB;EACxE,MAAM;IAAEC,MAAM;IAAEC;EAAK,CAAC,GAAGF,MAAM;EAE/B,MAAMG,MAAM,GAAG,MAAMF,MAAM,CAACF,GAAG,CAACG,IAAI,CAAC;EAErC,IAAI,CAACC,MAAM,IAAI,CAACA,MAAM,CAACC,IAAI,EAAE;IACzB,OAAO,IAAI;EACf;EACA,OAAOD,MAAM,CAACC,IAAI;AACtB,CAAC;AAAC"}
@@ -9,5 +9,5 @@ interface CreateListResponseParams<T> {
9
9
  totalCount: number;
10
10
  limit: number;
11
11
  }
12
- export declare const createListResponse: <T>(params: CreateListResponseParams<T>) => [T[], MetaResponse];
12
+ export declare function createListResponse<T>(params: CreateListResponseParams<T>): [T[], MetaResponse];
13
13
  export {};
@@ -3,18 +3,19 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.createListResponse = void 0;
7
-
6
+ exports.createListResponse = createListResponse;
8
7
  var _cursor = require("./cursor");
9
-
10
- const createListResponse = params => {
8
+ function createListResponse(params) {
11
9
  const {
12
10
  items: initialItems,
13
11
  after,
14
12
  totalCount,
15
13
  limit
16
14
  } = params;
17
- const start = (0, _cursor.decodeCursor)(after) || 0;
15
+ let start = Number((0, _cursor.decodeCursor)(after));
16
+ if (isNaN(start) === true) {
17
+ start = 0;
18
+ }
18
19
  const hasMoreItems = totalCount > start + limit;
19
20
  const end = limit > totalCount + start + limit ? undefined : start + limit;
20
21
  const items = end ? initialItems.slice(start, end) : initialItems;
@@ -25,6 +26,4 @@ const createListResponse = params => {
25
26
  cursor
26
27
  };
27
28
  return [items, meta];
28
- };
29
-
30
- exports.createListResponse = createListResponse;
29
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"names":["createListResponse","params","items","initialItems","after","totalCount","limit","start","Number","decodeCursor","isNaN","hasMoreItems","end","undefined","slice","cursor","length","encodeCursor","meta"],"sources":["listResponse.ts"],"sourcesContent":["import { decodeCursor, encodeCursor } from \"~/utils/cursor\";\n\ninterface MetaResponse {\n cursor: string | null;\n totalCount: number;\n hasMoreItems: boolean;\n}\n\ninterface CreateListResponseParams<T> {\n items: T[];\n after?: string | null;\n totalCount: number;\n limit: number;\n}\n\nexport function createListResponse<T>(params: CreateListResponseParams<T>): [T[], MetaResponse] {\n const { items: initialItems, after, totalCount, limit } = params;\n let start = Number(decodeCursor(after));\n if (isNaN(start) === true) {\n start = 0;\n }\n const hasMoreItems = totalCount > start + limit;\n const end = limit > totalCount + start + limit ? undefined : start + limit;\n const items = end ? initialItems.slice(start, end) : initialItems;\n\n const cursor = items.length > 0 ? encodeCursor(start + limit) : null;\n\n const meta = {\n hasMoreItems,\n totalCount,\n cursor\n };\n\n return [items, meta];\n}\n"],"mappings":";;;;;;AAAA;AAeO,SAASA,kBAAkB,CAAIC,MAAmC,EAAuB;EAC5F,MAAM;IAAEC,KAAK,EAAEC,YAAY;IAAEC,KAAK;IAAEC,UAAU;IAAEC;EAAM,CAAC,GAAGL,MAAM;EAChE,IAAIM,KAAK,GAAGC,MAAM,CAAC,IAAAC,oBAAY,EAACL,KAAK,CAAC,CAAC;EACvC,IAAIM,KAAK,CAACH,KAAK,CAAC,KAAK,IAAI,EAAE;IACvBA,KAAK,GAAG,CAAC;EACb;EACA,MAAMI,YAAY,GAAGN,UAAU,GAAGE,KAAK,GAAGD,KAAK;EAC/C,MAAMM,GAAG,GAAGN,KAAK,GAAGD,UAAU,GAAGE,KAAK,GAAGD,KAAK,GAAGO,SAAS,GAAGN,KAAK,GAAGD,KAAK;EAC1E,MAAMJ,KAAK,GAAGU,GAAG,GAAGT,YAAY,CAACW,KAAK,CAACP,KAAK,EAAEK,GAAG,CAAC,GAAGT,YAAY;EAEjE,MAAMY,MAAM,GAAGb,KAAK,CAACc,MAAM,GAAG,CAAC,GAAG,IAAAC,oBAAY,EAACV,KAAK,GAAGD,KAAK,CAAC,GAAG,IAAI;EAEpE,MAAMY,IAAI,GAAG;IACTP,YAAY;IACZN,UAAU;IACVU;EACJ,CAAC;EAED,OAAO,CAACb,KAAK,EAAEgB,IAAI,CAAC;AACxB"}
package/utils/query.d.ts CHANGED
@@ -15,7 +15,7 @@ export interface QueryResult<T> {
15
15
  result: any | null;
16
16
  items: T[];
17
17
  }
18
- declare type DbItem<T> = T & {
18
+ export declare type DbItem<T> = T & {
19
19
  PK: string;
20
20
  SK: string;
21
21
  TYPE: string;
@@ -25,9 +25,8 @@ declare type DbItem<T> = T & {
25
25
  /**
26
26
  * Will run the query to fetch the first possible item from the database.
27
27
  */
28
- export declare const queryOne: <T>(params: QueryOneParams) => Promise<DbItem<T>>;
28
+ export declare const queryOne: <T>(params: QueryOneParams) => Promise<DbItem<T> | null>;
29
29
  /**
30
30
  * Will run the query to fetch the results no matter how much iterations it needs to go through.
31
31
  */
32
32
  export declare const queryAll: <T>(params: QueryAllParams) => Promise<DbItem<T>[]>;
33
- export {};