@webiny/db-dynamodb 0.0.0-mt-2 → 0.0.0-unstable.1e66d121db

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 (110) hide show
  1. package/BatchProcess.d.ts +26 -10
  2. package/BatchProcess.js +9 -7
  3. package/BatchProcess.js.map +1 -0
  4. package/DynamoDbDriver.d.ts +19 -11
  5. package/DynamoDbDriver.js +10 -8
  6. package/DynamoDbDriver.js.map +1 -0
  7. package/QueryGenerator.d.ts +15 -13
  8. package/QueryGenerator.js +16 -17
  9. package/QueryGenerator.js.map +1 -0
  10. package/index.js +1 -1
  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.map +1 -0
  15. package/operators/comparison/gt.js.map +1 -0
  16. package/operators/comparison/gte.js.map +1 -0
  17. package/operators/comparison/lt.js.map +1 -0
  18. package/operators/comparison/lte.js.map +1 -0
  19. package/operators/index.js +1 -1
  20. package/operators/index.js.map +1 -0
  21. package/operators/logical/and.js.map +1 -0
  22. package/operators/logical/or.js.map +1 -0
  23. package/package.json +17 -16
  24. package/plugins/definitions/AttributePlugin.d.ts +3 -3
  25. package/plugins/definitions/AttributePlugin.js +1 -1
  26. package/plugins/definitions/AttributePlugin.js.map +1 -0
  27. package/plugins/definitions/DateTimeTransformPlugin.d.ts +2 -2
  28. package/plugins/definitions/DateTimeTransformPlugin.js +7 -7
  29. package/plugins/definitions/DateTimeTransformPlugin.js.map +1 -0
  30. package/plugins/definitions/FieldPathPlugin.d.ts +3 -3
  31. package/plugins/definitions/FieldPathPlugin.js +4 -6
  32. package/plugins/definitions/FieldPathPlugin.js.map +1 -0
  33. package/plugins/definitions/FieldPlugin.d.ts +3 -3
  34. package/plugins/definitions/FieldPlugin.js +1 -1
  35. package/plugins/definitions/FieldPlugin.js.map +1 -0
  36. package/plugins/definitions/NumberTransformPlugin.d.ts +2 -2
  37. package/plugins/definitions/NumberTransformPlugin.js +3 -7
  38. package/plugins/definitions/NumberTransformPlugin.js.map +1 -0
  39. package/plugins/definitions/TimeTransformPlugin.d.ts +2 -2
  40. package/plugins/definitions/TimeTransformPlugin.js +3 -7
  41. package/plugins/definitions/TimeTransformPlugin.js.map +1 -0
  42. package/plugins/definitions/ValueFilterPlugin.d.ts +10 -10
  43. package/plugins/definitions/ValueFilterPlugin.js +1 -1
  44. package/plugins/definitions/ValueFilterPlugin.js.map +1 -0
  45. package/plugins/definitions/ValueTransformPlugin.d.ts +8 -8
  46. package/plugins/definitions/ValueTransformPlugin.js +4 -6
  47. package/plugins/definitions/ValueTransformPlugin.js.map +1 -0
  48. package/plugins/definitions/assignFields.js +1 -1
  49. package/plugins/definitions/assignFields.js.map +1 -0
  50. package/plugins/filters/andIn.js +1 -1
  51. package/plugins/filters/andIn.js.map +1 -0
  52. package/plugins/filters/between.js +1 -1
  53. package/plugins/filters/between.js.map +1 -0
  54. package/plugins/filters/contains.js.map +1 -0
  55. package/plugins/filters/eq.js +13 -0
  56. package/plugins/filters/eq.js.map +1 -0
  57. package/plugins/filters/fuzzy.js +1 -1
  58. package/plugins/filters/fuzzy.js.map +1 -0
  59. package/plugins/filters/gt.js.map +1 -0
  60. package/plugins/filters/gte.js.map +1 -0
  61. package/plugins/filters/in.js +1 -1
  62. package/plugins/filters/in.js.map +1 -0
  63. package/plugins/filters/index.js +4 -2
  64. package/plugins/filters/index.js.map +1 -0
  65. package/plugins/filters/lt.js.map +1 -0
  66. package/plugins/filters/lte.js.map +1 -0
  67. package/plugins/filters/startsWith.d.ts +3 -0
  68. package/plugins/filters/startsWith.js +33 -0
  69. package/plugins/filters/startsWith.js.map +1 -0
  70. package/plugins/index.js +1 -1
  71. package/plugins/index.js.map +1 -0
  72. package/statements/createKeyConditionExpressionArgs.d.ts +12 -9
  73. package/statements/createKeyConditionExpressionArgs.js +5 -1
  74. package/statements/createKeyConditionExpressionArgs.js.map +1 -0
  75. package/statements/processStatement.d.ts +3 -4
  76. package/statements/processStatement.js +8 -5
  77. package/statements/processStatement.js.map +1 -0
  78. package/types.d.ts +37 -8
  79. package/types.js.map +1 -0
  80. package/utils/attributes.d.ts +2 -2
  81. package/utils/attributes.js +3 -7
  82. package/utils/attributes.js.map +1 -0
  83. package/utils/batchRead.d.ts +1 -1
  84. package/utils/batchRead.js +56 -25
  85. package/utils/batchRead.js.map +1 -0
  86. package/utils/batchWrite.js +1 -1
  87. package/utils/batchWrite.js.map +1 -0
  88. package/utils/cleanup.d.ts +2 -2
  89. package/utils/cleanup.js +9 -17
  90. package/utils/cleanup.js.map +1 -0
  91. package/utils/cursor.d.ts +2 -2
  92. package/utils/cursor.js.map +1 -0
  93. package/utils/filter.d.ts +2 -2
  94. package/utils/filter.js +7 -6
  95. package/utils/filter.js.map +1 -0
  96. package/utils/get.d.ts +2 -2
  97. package/utils/get.js.map +1 -0
  98. package/utils/listResponse.d.ts +1 -1
  99. package/utils/listResponse.js +9 -6
  100. package/utils/listResponse.js.map +1 -0
  101. package/utils/query.d.ts +2 -3
  102. package/utils/query.js +7 -15
  103. package/utils/query.js.map +1 -0
  104. package/utils/sort.d.ts +2 -2
  105. package/utils/sort.js +4 -6
  106. package/utils/sort.js.map +1 -0
  107. package/utils/table.js +1 -1
  108. package/utils/table.js.map +1 -0
  109. package/utils/documentClient.d.ts +0 -8
  110. package/utils/documentClient.js +0 -33
@@ -1,12 +1,15 @@
1
- declare const _default: ({ query, sort, key }: {
2
- query: any;
3
- sort: any;
4
- key: any;
5
- }) => {
1
+ import { Query, QueryKey, QuerySort } from "../types";
2
+ interface Output {
6
3
  KeyConditionExpression: string;
7
- ExpressionAttributeNames: {};
8
- ExpressionAttributeValues: {};
4
+ ExpressionAttributeNames: Record<string, any>;
5
+ ExpressionAttributeValues: Record<string, any>;
9
6
  ScanIndexForward: boolean;
10
- IndexName: any;
11
- };
7
+ IndexName: string;
8
+ }
9
+ interface Params {
10
+ query: Query;
11
+ sort: QuerySort;
12
+ key: QueryKey;
13
+ }
14
+ declare const _default: ({ query, sort, key }: Params) => Output;
12
15
  export default _default;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
4
 
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
@@ -9,6 +9,10 @@ exports.default = void 0;
9
9
 
10
10
  var _processStatement = _interopRequireDefault(require("./processStatement"));
11
11
 
12
+ /**
13
+ * Remove this when no apps are using our internal db drivers anymore
14
+ */
15
+ // @ts-nocheck
12
16
  var _default = ({
13
17
  query,
14
18
  sort,
@@ -0,0 +1 @@
1
+ {"version":3,"names":["query","sort","key","args","expression","attributeNames","attributeValues","processStatement","$and","output","KeyConditionExpression","ExpressionAttributeNames","ExpressionAttributeValues","ScanIndexForward","IndexName","sortKey","fields","name","primary"],"sources":["createKeyConditionExpressionArgs.ts"],"sourcesContent":["/**\n * Remove this when no apps are using our internal db drivers anymore\n */\n// @ts-nocheck\nimport processStatement from \"./processStatement\";\nimport { ProcessStatementArgsParam, Query, QueryKey, QuerySort } from \"~/types\";\n\ninterface Output {\n KeyConditionExpression: string;\n ExpressionAttributeNames: Record<string, any>;\n ExpressionAttributeValues: Record<string, any>;\n ScanIndexForward: boolean;\n IndexName: string;\n}\ninterface Params {\n query: Query;\n sort: QuerySort;\n key: QueryKey;\n}\nexport default ({ query, sort, key }: Params): Output => {\n const args: ProcessStatementArgsParam = {\n expression: \"\",\n attributeNames: {},\n attributeValues: {}\n };\n\n processStatement({ args, query: { $and: query } });\n\n const output: Output = {\n KeyConditionExpression: args.expression,\n ExpressionAttributeNames: args.attributeNames,\n ExpressionAttributeValues: args.attributeValues,\n ScanIndexForward: true,\n IndexName: null\n };\n\n const sortKey = key.fields && key.fields[1];\n if (sort && sort[sortKey.name] === -1) {\n output.ScanIndexForward = false;\n }\n\n if (!key.primary) {\n output.IndexName = key.name;\n }\n\n return output;\n};\n"],"mappings":";;;;;;;;;AAIA;;AAJA;AACA;AACA;AACA;eAgBe,CAAC;EAAEA,KAAF;EAASC,IAAT;EAAeC;AAAf,CAAD,KAA0C;EACrD,MAAMC,IAA+B,GAAG;IACpCC,UAAU,EAAE,EADwB;IAEpCC,cAAc,EAAE,EAFoB;IAGpCC,eAAe,EAAE;EAHmB,CAAxC;EAMA,IAAAC,yBAAA,EAAiB;IAAEJ,IAAF;IAAQH,KAAK,EAAE;MAAEQ,IAAI,EAAER;IAAR;EAAf,CAAjB;EAEA,MAAMS,MAAc,GAAG;IACnBC,sBAAsB,EAAEP,IAAI,CAACC,UADV;IAEnBO,wBAAwB,EAAER,IAAI,CAACE,cAFZ;IAGnBO,yBAAyB,EAAET,IAAI,CAACG,eAHb;IAInBO,gBAAgB,EAAE,IAJC;IAKnBC,SAAS,EAAE;EALQ,CAAvB;EAQA,MAAMC,OAAO,GAAGb,GAAG,CAACc,MAAJ,IAAcd,GAAG,CAACc,MAAJ,CAAW,CAAX,CAA9B;;EACA,IAAIf,IAAI,IAAIA,IAAI,CAACc,OAAO,CAACE,IAAT,CAAJ,KAAuB,CAAC,CAApC,EAAuC;IACnCR,MAAM,CAACI,gBAAP,GAA0B,KAA1B;EACH;;EAED,IAAI,CAACX,GAAG,CAACgB,OAAT,EAAkB;IACdT,MAAM,CAACK,SAAP,GAAmBZ,GAAG,CAACe,IAAvB;EACH;;EAED,OAAOR,MAAP;AACH,C"}
@@ -1,4 +1,3 @@
1
- export default function processStatement({ args, query }: {
2
- args: any;
3
- query: any;
4
- }): void;
1
+ import { ProcessStatementCallable } from "../types";
2
+ declare const processStatement: ProcessStatementCallable;
3
+ export default processStatement;
@@ -1,18 +1,18 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
4
 
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
- exports.default = processStatement;
8
+ exports.default = void 0;
9
9
 
10
10
  var _operators = _interopRequireDefault(require("./../operators"));
11
11
 
12
- function processStatement({
12
+ const processStatement = ({
13
13
  args,
14
14
  query
15
- }) {
15
+ }) => {
16
16
  outerLoop: for (const [key, value] of Object.entries(query)) {
17
17
  const operators = Object.values(_operators.default);
18
18
 
@@ -36,4 +36,7 @@ function processStatement({
36
36
 
37
37
  throw new Error(`Invalid operator {${key} : ${value}}.`);
38
38
  }
39
- }
39
+ };
40
+
41
+ var _default = processStatement;
42
+ exports.default = _default;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["processStatement","args","query","outerLoop","key","value","Object","entries","operators","values","allOperators","i","length","operator","canProcess","process","Error"],"sources":["processStatement.ts"],"sourcesContent":["import allOperators from \"./../operators\";\nimport { ProcessStatementCallable } from \"~/types\";\n\nconst processStatement: ProcessStatementCallable = ({ args, query }) => {\n outerLoop: for (const [key, value] of Object.entries(query)) {\n const operators = Object.values(allOperators);\n for (let i = 0; i < operators.length; i++) {\n const operator = operators[i];\n if (operator.canProcess({ key, value, args })) {\n operator.process({ key, value, args, processStatement });\n continue outerLoop;\n }\n }\n throw new Error(`Invalid operator {${key} : ${value}}.`);\n }\n};\nexport default processStatement;\n"],"mappings":";;;;;;;;;AAAA;;AAGA,MAAMA,gBAA0C,GAAG,CAAC;EAAEC,IAAF;EAAQC;AAAR,CAAD,KAAqB;EACpEC,SAAS,EAAE,KAAK,MAAM,CAACC,GAAD,EAAMC,KAAN,CAAX,IAA2BC,MAAM,CAACC,OAAP,CAAeL,KAAf,CAA3B,EAAkD;IACzD,MAAMM,SAAS,GAAGF,MAAM,CAACG,MAAP,CAAcC,kBAAd,CAAlB;;IACA,KAAK,IAAIC,CAAC,GAAG,CAAb,EAAgBA,CAAC,GAAGH,SAAS,CAACI,MAA9B,EAAsCD,CAAC,EAAvC,EAA2C;MACvC,MAAME,QAAQ,GAAGL,SAAS,CAACG,CAAD,CAA1B;;MACA,IAAIE,QAAQ,CAACC,UAAT,CAAoB;QAAEV,GAAF;QAAOC,KAAP;QAAcJ;MAAd,CAApB,CAAJ,EAA+C;QAC3CY,QAAQ,CAACE,OAAT,CAAiB;UAAEX,GAAF;UAAOC,KAAP;UAAcJ,IAAd;UAAoBD;QAApB,CAAjB;QACA,SAASG,SAAT;MACH;IACJ;;IACD,MAAM,IAAIa,KAAJ,CAAW,qBAAoBZ,GAAI,MAAKC,KAAM,IAA9C,CAAN;EACH;AACJ,CAZD;;eAaeL,gB"}
package/types.d.ts CHANGED
@@ -1,23 +1,23 @@
1
- export declare type OperatorArgs = {
1
+ export interface OperatorArgs {
2
2
  expression: string;
3
3
  attributeNames: Record<string, any>;
4
4
  attributeValues: Record<string, any>;
5
- };
6
- declare type CanProcessArgs = {
5
+ }
6
+ interface CanProcessArgs {
7
7
  key: string;
8
8
  value: any;
9
9
  args: OperatorArgs;
10
- };
11
- declare type ProcessArgs = {
10
+ }
11
+ interface ProcessArgs {
12
12
  key: string;
13
13
  value: any;
14
14
  args: OperatorArgs;
15
15
  processStatement: any;
16
- };
17
- export declare type Operator = {
16
+ }
17
+ export interface Operator {
18
18
  canProcess: ({ key }: CanProcessArgs) => boolean;
19
19
  process: ({ key, value, args }: ProcessArgs) => void;
20
- };
20
+ }
21
21
  /**
22
22
  * We use this definition to search for a value in any given field that was passed.
23
23
  * It works as an "OR" condition.
@@ -26,4 +26,33 @@ export interface DynamoDbContainsFilter {
26
26
  fields: string[];
27
27
  value: string;
28
28
  }
29
+ export interface ProcessStatementArgsParam {
30
+ expression: string;
31
+ attributeNames: Record<string, any>;
32
+ attributeValues: Record<string, any>;
33
+ }
34
+ export interface ProcessStatementQueryParam {
35
+ [key: string]: any;
36
+ }
37
+ export interface ProcessStatementParams {
38
+ args: ProcessStatementArgsParam;
39
+ query: ProcessStatementQueryParam;
40
+ }
41
+ export interface ProcessStatementCallable {
42
+ (params: ProcessStatementParams): void;
43
+ }
44
+ export interface Query {
45
+ [key: string]: string;
46
+ }
47
+ export interface QueryKeyField {
48
+ name: string;
49
+ }
50
+ export interface QueryKey {
51
+ fields: QueryKeyField[];
52
+ primary?: boolean;
53
+ unique?: boolean;
54
+ name: string;
55
+ }
56
+ export declare type QueryKeys = QueryKey[];
57
+ export declare type QuerySort = Record<string, -1 | 1>;
29
58
  export {};
package/types.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["export interface OperatorArgs {\n expression: string;\n attributeNames: Record<string, any>;\n attributeValues: Record<string, any>;\n}\n\ninterface CanProcessArgs {\n key: string;\n value: any;\n args: OperatorArgs;\n}\n\ninterface ProcessArgs {\n key: string;\n value: any;\n args: OperatorArgs;\n processStatement: any;\n}\n\nexport interface Operator {\n canProcess: ({ key }: CanProcessArgs) => boolean;\n process: ({ key, value, args }: ProcessArgs) => void;\n}\n\n/**\n * We use this definition to search for a value in any given field that was passed.\n * It works as an \"OR\" condition.\n */\nexport interface DynamoDbContainsFilter {\n fields: string[];\n value: string;\n}\n\nexport interface ProcessStatementArgsParam {\n expression: string;\n attributeNames: Record<string, any>;\n attributeValues: Record<string, any>;\n}\nexport interface ProcessStatementQueryParam {\n [key: string]: any;\n}\nexport interface ProcessStatementParams {\n args: ProcessStatementArgsParam;\n query: ProcessStatementQueryParam;\n}\n\nexport interface ProcessStatementCallable {\n (params: ProcessStatementParams): void;\n}\n\nexport interface Query {\n [key: string]: string;\n}\nexport interface QueryKeyField {\n name: string;\n}\nexport interface QueryKey {\n fields: QueryKeyField[];\n primary?: boolean;\n unique?: boolean;\n name: string;\n}\nexport type QueryKeys = QueryKey[];\n\nexport type QuerySort = Record<string, -1 | 1>;\n"],"mappings":""}
@@ -1,4 +1,4 @@
1
- import { ContextInterface } from "@webiny/handler/types";
1
+ import { Context } from "@webiny/api/types";
2
2
  import { DefinitionParams } from "../plugins/definitions/AttributePlugin";
3
3
  import { PluginsContainer } from "@webiny/plugins";
4
4
  /**
@@ -6,5 +6,5 @@ import { PluginsContainer } from "@webiny/plugins";
6
6
  *
7
7
  * @deprecated
8
8
  */
9
- export declare const getExtraAttributes: (context: ContextInterface, entity: string) => Record<string, DefinitionParams>;
9
+ export declare const getExtraAttributes: (context: Context, entity: string) => Record<string, DefinitionParams>;
10
10
  export declare const getExtraAttributesFromPlugins: (plugins: PluginsContainer, entity: string) => Record<string, DefinitionParams>;
@@ -1,20 +1,16 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
4
 
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
8
  exports.getExtraAttributesFromPlugins = exports.getExtraAttributes = void 0;
9
9
 
10
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
10
+ var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
11
11
 
12
12
  var _AttributePlugin = require("../plugins/definitions/AttributePlugin");
13
13
 
14
- 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; }
15
-
16
- 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; }
17
-
18
14
  /**
19
15
  * Will be removed in favor of directly assigning attributes to a certain entity when creating the storage operations.
20
16
  *
@@ -28,7 +24,7 @@ exports.getExtraAttributes = getExtraAttributes;
28
24
 
29
25
  const getExtraAttributesFromPlugins = (plugins, entity) => {
30
26
  return plugins.byType(_AttributePlugin.AttributePlugin.type).filter(plugin => plugin.entity === entity).reduce((attributes, plugin) => {
31
- return _objectSpread(_objectSpread({}, attributes), plugin.getDefinition());
27
+ return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, attributes), plugin.getDefinition());
32
28
  }, {});
33
29
  };
34
30
 
@@ -0,0 +1 @@
1
+ {"version":3,"names":["getExtraAttributes","context","entity","getExtraAttributesFromPlugins","plugins","byType","AttributePlugin","type","filter","plugin","reduce","attributes","getDefinition"],"sources":["attributes.ts"],"sourcesContent":["import { Context } from \"@webiny/api/types\";\nimport { AttributePlugin, DefinitionParams } from \"~/plugins/definitions/AttributePlugin\";\nimport { PluginsContainer } from \"@webiny/plugins\";\n\n/**\n * Will be removed in favor of directly assigning attributes to a certain entity when creating the storage operations.\n *\n * @deprecated\n */\nexport const getExtraAttributes = (\n context: Context,\n entity: string\n): Record<string, DefinitionParams> => {\n return getExtraAttributesFromPlugins(context.plugins, entity);\n};\n\nexport const getExtraAttributesFromPlugins = (\n plugins: PluginsContainer,\n entity: string\n): Record<string, DefinitionParams> => {\n return plugins\n .byType<AttributePlugin>(AttributePlugin.type)\n .filter(plugin => plugin.entity === entity)\n .reduce((attributes, plugin) => {\n return {\n ...attributes,\n ...plugin.getDefinition()\n };\n }, {});\n};\n"],"mappings":";;;;;;;;;;;AACA;;AAGA;AACA;AACA;AACA;AACA;AACO,MAAMA,kBAAkB,GAAG,CAC9BC,OAD8B,EAE9BC,MAF8B,KAGK;EACnC,OAAOC,6BAA6B,CAACF,OAAO,CAACG,OAAT,EAAkBF,MAAlB,CAApC;AACH,CALM;;;;AAOA,MAAMC,6BAA6B,GAAG,CACzCC,OADyC,EAEzCF,MAFyC,KAGN;EACnC,OAAOE,OAAO,CACTC,MADE,CACsBC,gCAAA,CAAgBC,IADtC,EAEFC,MAFE,CAEKC,MAAM,IAAIA,MAAM,CAACP,MAAP,KAAkBA,MAFjC,EAGFQ,MAHE,CAGK,CAACC,UAAD,EAAaF,MAAb,KAAwB;IAC5B,mEACOE,UADP,GAEOF,MAAM,CAACG,aAAP,EAFP;EAIH,CARE,EAQA,EARA,CAAP;AASH,CAbM"}
@@ -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,10 +1,18 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+
3
5
  Object.defineProperty(exports, "__esModule", {
4
6
  value: true
5
7
  });
6
8
  exports.batchReadAll = void 0;
7
9
 
10
+ var _chunk = _interopRequireDefault(require("lodash/chunk"));
11
+
12
+ var _error = _interopRequireDefault(require("@webiny/error"));
13
+
14
+ const MAX_BATCH_ITEMS = 100;
15
+
8
16
  const flatten = responses => {
9
17
  const entries = [];
10
18
  const values = Object.values(responses);
@@ -15,44 +23,67 @@ const flatten = responses => {
15
23
 
16
24
  return entries;
17
25
  };
18
- /**
19
- * This helper function is meant to be used to batch read from one table.
20
- * It will fetch all results, as there is a next() method call built in.
21
- */
22
26
 
27
+ const batchReadAllChunk = async params => {
28
+ const {
29
+ table,
30
+ items
31
+ } = params;
32
+ const records = [];
33
+ const result = await table.batchGet(items);
23
34
 
24
- const batchReadAll = async params => {
25
- if (params.items.length === 0) {
26
- return [];
35
+ if (!result || !result.Responses) {
36
+ return records;
27
37
  }
28
38
 
29
- const items = [];
30
- const result = await params.table.batchGet(params.items);
39
+ records.push(...flatten(result.Responses));
31
40
 
32
- if (result.Responses) {
33
- items.push(...flatten(result.Responses));
41
+ if (!result.next || typeof result.next !== "function") {
42
+ return records;
34
43
  }
35
44
 
36
- if (typeof result.next === "function") {
37
- let previous = result;
38
- let nextResult;
45
+ let previous = result;
39
46
 
40
- while (nextResult = await previous.next()) {
41
- if (!nextResult) {
42
- return items;
43
- }
47
+ while (typeof previous.next === "function") {
48
+ const nextResult = await previous.next();
44
49
 
45
- items.push(...flatten(nextResult.Responses));
50
+ if (!nextResult) {
51
+ return records;
52
+ }
46
53
 
47
- if (!nextResult || typeof nextResult.next !== "function") {
48
- return items;
49
- }
54
+ records.push(...flatten(nextResult.Responses));
55
+ previous = nextResult;
56
+ }
50
57
 
51
- previous = nextResult;
52
- }
58
+ return records;
59
+ };
60
+ /**
61
+ * This helper function is meant to be used to batch read from one table.
62
+ * It will fetch all results, as there is a next() method call built in.
63
+ */
64
+
65
+
66
+ const batchReadAll = async (params, maxChunk = MAX_BATCH_ITEMS) => {
67
+ if (params.items.length === 0) {
68
+ return [];
69
+ } else if (maxChunk > MAX_BATCH_ITEMS) {
70
+ 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", {
71
+ maxChunk
72
+ });
73
+ }
74
+
75
+ const records = [];
76
+ const chunkItemsList = (0, _chunk.default)(params.items, maxChunk);
77
+
78
+ for (const chunkItems of chunkItemsList) {
79
+ const results = await batchReadAllChunk({
80
+ table: params.table,
81
+ items: chunkItems
82
+ });
83
+ records.push(...results);
53
84
  }
54
85
 
55
- return items;
86
+ return records;
56
87
  };
57
88
 
58
89
  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,GAAxB;;AAEA,MAAMC,OAAO,GAAIC,SAAD,IAA6C;EACzD,MAAMC,OAAO,GAAG,EAAhB;EACA,MAAMC,MAAM,GAAGC,MAAM,CAACD,MAAP,CAAcF,SAAd,CAAf;;EACA,KAAK,MAAMI,KAA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,oCAFE,EAGF;MACImB;IADJ,CAHE,CAAN;EAOH;;EAED,MAAMR,OAAY,GAAG,EAArB;EAEA,MAAMW,cAAwB,GAAG,IAAAC,cAAA,EAAYd,MAAM,CAACH,KAAnB,EAA0Ba,QAA1B,CAAjC;;EAEA,KAAK,MAAMK,UAAX,IAAyBF,cAAzB,EAAyC;IACrC,MAAMG,OAAO,GAAG,MAAMjB,iBAAiB,CAAI;MACvCE,KAAK,EAAED,MAAM,CAACC,KADyB;MAEvCJ,KAAK,EAAEkB;IAFgC,CAAJ,CAAvC;IAKAb,OAAO,CAACJ,IAAR,CAAa,GAAGkB,OAAhB;EACH;;EAED,OAAOd,OAAP;AACH,CA9BM"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
4
 
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
@@ -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,MAAP,EAAuBC,QAAQ,GAAG,EAAlC,KAAwD;EACjF,IAAID,MAAM,CAACE,KAAP,CAAaC,MAAb,KAAwB,CAA5B,EAA+B;IAC3B;EACH;;EACD,MAAMC,YAAsB,GAAG,IAAAC,cAAA,EAAYL,MAAM,CAACE,KAAnB,EAA0BD,QAA1B,CAA/B;;EACA,KAAK,MAAMC,KAAX,IAAoBE,YAApB,EAAkC;IAC9B,MAAMJ,MAAM,CAACM,KAAP,CAAaC,UAAb,CAAwBL,KAAxB,CAAN;EACH;AACJ,CARM"}
@@ -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,17 +1,14 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
4
 
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
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; }
8
+ exports.cleanupItem = cleanupItem;
9
+ exports.cleanupItems = cleanupItems;
13
10
 
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; }
11
+ var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
15
12
 
16
13
  /**
17
14
  * The attributes defined by us or the dynamodb-toolbox library.
@@ -19,13 +16,12 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
19
16
  */
20
17
  const attributesToRemove = ["PK", "SK", "created", "_ct", "modified", "_mt", "entity", "_et", "GSI1_PK", "GSI1_SK", "TYPE"];
21
18
 
22
- const cleanupItem = (entity, item, removeAttributes = []) => {
19
+ function cleanupItem(entity, item, removeAttributes = []) {
23
20
  if (!item) {
24
21
  return null;
25
22
  }
26
23
 
27
- const newItem = _objectSpread({}, item);
28
-
24
+ const newItem = (0, _objectSpread2.default)({}, item);
29
25
  const targets = attributesToRemove.concat(removeAttributes);
30
26
  const attributes = entity.schema.attributes;
31
27
 
@@ -42,12 +38,8 @@ const cleanupItem = (entity, item, removeAttributes = []) => {
42
38
  }
43
39
 
44
40
  return newItem;
45
- };
41
+ }
46
42
 
47
- exports.cleanupItem = cleanupItem;
48
-
49
- const cleanupItems = (entity, items, removeAttributes = []) => {
43
+ function cleanupItems(entity, items, removeAttributes = []) {
50
44
  return items.map(item => cleanupItem(entity, item, removeAttributes));
51
- };
52
-
53
- exports.cleanupItems = cleanupItems;
45
+ }
@@ -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,IADuB,EAEvB,IAFuB,EAGvB,SAHuB,EAIvB,KAJuB,EAKvB,UALuB,EAMvB,KANuB,EAOvB,QAPuB,EAQvB,KARuB,EASvB,SATuB,EAUvB,SAVuB,EAWvB,MAXuB,CAA3B;;AAcO,SAASC,WAAT,CACHC,MADG,EAEHC,IAFG,EAGHC,gBAA0B,GAAG,EAH1B,EAIK;EACR,IAAI,CAACD,IAAL,EAAW;IACP,OAAO,IAAP;EACH;;EACD,MAAME,OAAO,mCACNF,IADM,CAAb;EAGA,MAAMG,OAAO,GAAGN,kBAAkB,CAACO,MAAnB,CAA0BH,gBAA1B,CAAhB;EACA,MAAMI,UAAU,GAAGN,MAAM,CAACO,MAAP,CAAcD,UAAjC;;EACA,KAAK,MAAME,GAAX,IAAkBP,IAAlB,EAAwB;IACpB,IAAIA,IAAI,CAACQ,cAAL,CAAoBD,GAApB,MAA6B,KAAjC,EAAwC;MACpC;IACH;;IACD,IAAIF,UAAU,CAACE,GAAD,CAAV,IAAmBJ,OAAO,CAACM,QAAR,CAAiBF,GAAjB,MAA0B,KAAjD,EAAwD;MACpD;IACH;;IACD,OAAOL,OAAO,CAACK,GAAD,CAAd;EACH;;EACD,OAAOL,OAAP;AACH;;AAEM,SAASQ,YAAT,CACHX,MADG,EAEHY,KAFG,EAGHV,gBAA0B,GAAG,EAH1B,EAIA;EACH,OAAOU,KAAK,CAACC,GAAN,CAAUZ,IAAI,IAAIF,WAAW,CAAIC,MAAJ,EAAYC,IAAZ,EAAkBC,gBAAlB,CAA7B,CAAP;AACH"}
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;
@@ -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,MAAD,IAAiC;EACzD,IAAI,CAACA,MAAL,EAAa;IACT,OAAO,IAAP;EACH;;EAED,OAAOC,MAAM,CAACC,IAAP,CAAYC,IAAI,CAACC,SAAL,CAAeJ,MAAf,CAAZ,EAAoCK,QAApC,CAA6C,QAA7C,CAAP;AACH,CANM;;;;AAQA,MAAMC,YAAY,GAAIN,MAAD,IAA2C;EACnE,IAAI,CAACA,MAAL,EAAa;IACT,OAAO,IAAP;EACH;;EAED,OAAOG,IAAI,CAACI,KAAL,CAAWN,MAAM,CAACC,IAAP,CAAYF,MAAZ,EAAoB,QAApB,EAA8BK,QAA9B,CAAuC,OAAvC,CAAX,CAAP;AACH,CANM"}
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,11 +1,11 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
4
 
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
- exports.filterItems = void 0;
8
+ exports.filterItems = filterItems;
9
9
 
10
10
  var _dotProp = _interopRequireDefault(require("dot-prop"));
11
11
 
@@ -15,6 +15,9 @@ var _ValueFilterPlugin = require("../plugins/definitions/ValueFilterPlugin");
15
15
 
16
16
  const getMappedPlugins = params => {
17
17
  return params.plugins.byType(params.type).reduce((plugins, plugin) => {
18
+ /**
19
+ * We expect op to be a string, that is why we cast.
20
+ */
18
21
  const op = plugin[params.property];
19
22
  plugins[op] = plugin;
20
23
  return plugins;
@@ -196,7 +199,7 @@ const createFilterCallable = params => {
196
199
  };
197
200
  };
198
201
 
199
- const filterItems = params => {
202
+ function filterItems(params) {
200
203
  const filter = createFilterCallable(params);
201
204
  /**
202
205
  * No point in going through all the items when there are no filters to be applied.
@@ -207,6 +210,4 @@ const filterItems = params => {
207
210
  }
208
211
 
209
212
  return params.items.filter(filter);
210
- };
211
-
212
- exports.filterItems = filterItems;
213
+ }
@@ -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,MAAnB,IAAwE;EAC7F,OAAOA,MAAM,CAACC,OAAP,CAAeC,MAAf,CAAyBF,MAAM,CAACG,IAAhC,EAAsCC,MAAtC,CAA6C,CAACH,OAAD,EAAUI,MAAV,KAAqB;IACrE;AACR;AACA;IACQ,MAAMC,EAAE,GAAGD,MAAM,CAACL,MAAM,CAACO,QAAR,CAAjB;IACAN,OAAO,CAACK,EAAD,CAAP,GAAcD,MAAd;IACA,OAAOJ,OAAP;EACH,CAPM,EAOJ,EAPI,CAAP;AAQH,CATD;;AAgBA,MAAMO,gBAAgB,G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B,IAAb,CAAkBR,IAAI,IAAI;QACrC,MAAMJ,KAAK,GAAGK,SAAS,CAACQ,gBAAA,CAAQC,GAAR,CAAYJ,IAAZ,EAAkBN,IAAlB,CAAD,EAA0BO,MAAM,CAACjB,cAAjC,CAAvB;QACA,MAAMJ,YAAY,GAAGe,SAAS,CAACM,MAAM,CAACrB,YAAR,EAAsBqB,MAAM,CAACjB,cAA7B,CAA9B;QACA,MAAMqB,OAAO,GAAGJ,MAAM,CAACR,YAAP,CAAoBa,OAApB,CAA4B;UACxChB,KADwC;UAExCV;QAFwC,CAA5B,CAAhB;QAKA,OAAOqB,MAAM,CAACnC,MAAP,GAAgB,CAACuC,OAAjB,GAA2BA,OAAlC;MACH,CATc,CAAf;;MAUA,IAAI/C,MAAM,KAAK,KAAf,EAAsB;QAClB,OAAO,KAAP;MACH;IACJ;;IACD,OAAO,IAAP;EACH,CAjBD;AAkBH,CA5BD;;AA8BO,SAASiD,WAAT,CAA8B3D,MAA9B,EAAsD;EACzD,MAAMqD,MAAM,GAAGF,oBAAoB,CAACnD,MAAD,CAAnC;EACA;AACJ;AACA;;EACI,IAAI,CAACqD,MAAL,EAAa;IACT,OAAOrD,MAAM,CAAC4D,KAAd;EACH;;EACD,OAAO5D,MAAM,CAAC4D,KAAP,CAAaP,MAAb,CAAoBA,MAApB,CAAP;AACH"}
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>;
@@ -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,MAAV,IAAyD;EACxE,MAAM;IAAEC,MAAF;IAAUC;EAAV,IAAmBF,MAAzB;EAEA,MAAMG,MAAM,GAAG,MAAMF,MAAM,CAACF,GAAP,CAAWG,IAAX,CAArB;;EAEA,IAAI,CAACC,MAAD,IAAW,CAACA,MAAM,CAACC,IAAvB,EAA6B;IACzB,OAAO,IAAP;EACH;;EACD,OAAOD,MAAM,CAACC,IAAd;AACH,CATM"}
@@ -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,23 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.createListResponse = void 0;
6
+ exports.createListResponse = createListResponse;
7
7
 
8
8
  var _cursor = require("./cursor");
9
9
 
10
- const createListResponse = params => {
10
+ function createListResponse(params) {
11
11
  const {
12
12
  items: initialItems,
13
13
  after,
14
14
  totalCount,
15
15
  limit
16
16
  } = params;
17
- const start = (0, _cursor.decodeCursor)(after) || 0;
17
+ let start = Number((0, _cursor.decodeCursor)(after));
18
+
19
+ if (isNaN(start) === true) {
20
+ start = 0;
21
+ }
22
+
18
23
  const hasMoreItems = totalCount > start + limit;
19
24
  const end = limit > totalCount + start + limit ? undefined : start + limit;
20
25
  const items = end ? initialItems.slice(start, end) : initialItems;
@@ -25,6 +30,4 @@ const createListResponse = params => {
25
30
  cursor
26
31
  };
27
32
  return [items, meta];
28
- };
29
-
30
- exports.createListResponse = createListResponse;
33
+ }
@@ -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,kBAAT,CAA+BC,MAA/B,EAAyF;EAC5F,MAAM;IAAEC,KAAK,EAAEC,YAAT;IAAuBC,KAAvB;IAA8BC,UAA9B;IAA0CC;EAA1C,IAAoDL,MAA1D;EACA,IAAIM,KAAK,GAAGC,MAAM,CAAC,IAAAC,oBAAA,EAAaL,KAAb,CAAD,CAAlB;;EACA,IAAIM,KAAK,CAACH,KAAD,CAAL,KAAiB,IAArB,EAA2B;IACvBA,KAAK,GAAG,CAAR;EACH;;EACD,MAAMI,YAAY,GAAGN,UAAU,GAAGE,KAAK,GAAGD,KAA1C;EACA,MAAMM,GAAG,GAAGN,KAAK,GAAGD,UAAU,GAAGE,KAAb,GAAqBD,KAA7B,GAAqCO,SAArC,GAAiDN,KAAK,GAAGD,KAArE;EACA,MAAMJ,KAAK,GAAGU,GAAG,GAAGT,YAAY,CAACW,KAAb,CAAmBP,KAAnB,EAA0BK,GAA1B,CAAH,GAAoCT,YAArD;EAEA,MAAMY,MAAM,GAAGb,KAAK,CAACc,MAAN,GAAe,CAAf,GAAmB,IAAAC,oBAAA,EAAaV,KAAK,GAAGD,KAArB,CAAnB,GAAiD,IAAhE;EAEA,MAAMY,IAAI,GAAG;IACTP,YADS;IAETN,UAFS;IAGTU;EAHS,CAAb;EAMA,OAAO,CAACb,KAAD,EAAQgB,IAAR,CAAP;AACH"}
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 {};