@webiny/db-dynamodb 0.0.0-mt-2 → 0.0.0-unstable.085ff6572f

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 (141) 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.d.ts +2 -0
  11. package/index.js +25 -4
  12. package/index.js.map +1 -0
  13. package/operators/comparison/beginsWith.js.map +1 -0
  14. package/operators/comparison/between.js.map +1 -0
  15. package/operators/comparison/eq.js +0 -2
  16. package/operators/comparison/eq.js.map +1 -0
  17. package/operators/comparison/gt.js.map +1 -0
  18. package/operators/comparison/gte.js.map +1 -0
  19. package/operators/comparison/lt.js.map +1 -0
  20. package/operators/comparison/lte.js.map +1 -0
  21. package/operators/index.js +1 -12
  22. package/operators/index.js.map +1 -0
  23. package/operators/logical/and.js +0 -5
  24. package/operators/logical/and.js.map +1 -0
  25. package/operators/logical/or.js +0 -5
  26. package/operators/logical/or.js.map +1 -0
  27. package/package.json +20 -19
  28. package/plugins/definitions/AttributePlugin.d.ts +3 -3
  29. package/plugins/definitions/AttributePlugin.js +1 -14
  30. package/plugins/definitions/AttributePlugin.js.map +1 -0
  31. package/plugins/definitions/DateTimeTransformPlugin.d.ts +2 -2
  32. package/plugins/definitions/DateTimeTransformPlugin.js +6 -20
  33. package/plugins/definitions/DateTimeTransformPlugin.js.map +1 -0
  34. package/plugins/definitions/FieldPathPlugin.d.ts +3 -3
  35. package/plugins/definitions/FieldPathPlugin.js +3 -17
  36. package/plugins/definitions/FieldPathPlugin.js.map +1 -0
  37. package/plugins/definitions/FieldPlugin.d.ts +3 -3
  38. package/plugins/definitions/FieldPlugin.js +1 -17
  39. package/plugins/definitions/FieldPlugin.js.map +1 -0
  40. package/plugins/definitions/NumberTransformPlugin.d.ts +2 -2
  41. package/plugins/definitions/NumberTransformPlugin.js +3 -18
  42. package/plugins/definitions/NumberTransformPlugin.js.map +1 -0
  43. package/plugins/definitions/TimeTransformPlugin.d.ts +2 -2
  44. package/plugins/definitions/TimeTransformPlugin.js +3 -21
  45. package/plugins/definitions/TimeTransformPlugin.js.map +1 -0
  46. package/plugins/definitions/ValueFilterPlugin.d.ts +10 -10
  47. package/plugins/definitions/ValueFilterPlugin.js +1 -13
  48. package/plugins/definitions/ValueFilterPlugin.js.map +1 -0
  49. package/plugins/definitions/ValueTransformPlugin.d.ts +8 -8
  50. package/plugins/definitions/ValueTransformPlugin.js +3 -18
  51. package/plugins/definitions/ValueTransformPlugin.js.map +1 -0
  52. package/plugins/definitions/assignFields.js +1 -10
  53. package/plugins/definitions/assignFields.js.map +1 -0
  54. package/plugins/filters/andIn.d.ts +2 -2
  55. package/plugins/filters/andIn.js +4 -9
  56. package/plugins/filters/andIn.js.map +1 -0
  57. package/plugins/filters/between.d.ts +2 -2
  58. package/plugins/filters/between.js +4 -9
  59. package/plugins/filters/between.js.map +1 -0
  60. package/plugins/filters/contains.d.ts +2 -2
  61. package/plugins/filters/contains.js +3 -6
  62. package/plugins/filters/contains.js.map +1 -0
  63. package/plugins/filters/eq.d.ts +2 -2
  64. package/plugins/filters/eq.js +15 -4
  65. package/plugins/filters/eq.js.map +1 -0
  66. package/plugins/filters/fuzzy.d.ts +2 -2
  67. package/plugins/filters/fuzzy.js +9 -9
  68. package/plugins/filters/fuzzy.js.map +1 -0
  69. package/plugins/filters/gt.d.ts +2 -2
  70. package/plugins/filters/gt.js +3 -4
  71. package/plugins/filters/gt.js.map +1 -0
  72. package/plugins/filters/gte.d.ts +2 -2
  73. package/plugins/filters/gte.js +3 -4
  74. package/plugins/filters/gte.js.map +1 -0
  75. package/plugins/filters/in.d.ts +2 -2
  76. package/plugins/filters/in.js +4 -9
  77. package/plugins/filters/in.js.map +1 -0
  78. package/plugins/filters/index.js +3 -15
  79. package/plugins/filters/index.js.map +1 -0
  80. package/plugins/filters/lt.d.ts +2 -2
  81. package/plugins/filters/lt.js +3 -4
  82. package/plugins/filters/lt.js.map +1 -0
  83. package/plugins/filters/lte.d.ts +2 -2
  84. package/plugins/filters/lte.js +3 -4
  85. package/plugins/filters/lte.js.map +1 -0
  86. package/plugins/filters/startsWith.d.ts +3 -0
  87. package/plugins/filters/startsWith.js +29 -0
  88. package/plugins/filters/startsWith.js.map +1 -0
  89. package/plugins/index.js +1 -5
  90. package/plugins/index.js.map +1 -0
  91. package/statements/createKeyConditionExpressionArgs.d.ts +12 -9
  92. package/statements/createKeyConditionExpressionArgs.js +5 -8
  93. package/statements/createKeyConditionExpressionArgs.js.map +1 -0
  94. package/statements/processStatement.d.ts +3 -4
  95. package/statements/processStatement.js +7 -11
  96. package/statements/processStatement.js.map +1 -0
  97. package/types.d.ts +45 -8
  98. package/types.js.map +1 -0
  99. package/utils/attributes.d.ts +2 -2
  100. package/utils/attributes.js +3 -14
  101. package/utils/attributes.js.map +1 -0
  102. package/utils/batchRead.d.ts +4 -5
  103. package/utils/batchRead.js +43 -34
  104. package/utils/batchRead.js.map +1 -0
  105. package/utils/batchWrite.d.ts +4 -5
  106. package/utils/batchWrite.js +1 -7
  107. package/utils/batchWrite.js.map +1 -0
  108. package/utils/cleanup.d.ts +2 -2
  109. package/utils/cleanup.js +9 -27
  110. package/utils/cleanup.js.map +1 -0
  111. package/utils/createEntity.d.ts +14 -0
  112. package/utils/createEntity.js +60 -0
  113. package/utils/createEntity.js.map +1 -0
  114. package/utils/createTable.d.ts +7 -0
  115. package/utils/createTable.js +25 -0
  116. package/utils/createTable.js.map +1 -0
  117. package/utils/cursor.d.ts +2 -2
  118. package/utils/cursor.js +0 -6
  119. package/utils/cursor.js.map +1 -0
  120. package/utils/filter.d.ts +2 -2
  121. package/utils/filter.js +7 -43
  122. package/utils/filter.js.map +1 -0
  123. package/utils/get.d.ts +2 -2
  124. package/utils/get.js +0 -4
  125. package/utils/get.js.map +1 -0
  126. package/utils/index.d.ts +12 -0
  127. package/utils/index.js +137 -0
  128. package/utils/index.js.map +1 -0
  129. package/utils/listResponse.d.ts +1 -1
  130. package/utils/listResponse.js +7 -8
  131. package/utils/listResponse.js.map +1 -0
  132. package/utils/query.d.ts +8 -4
  133. package/utils/query.js +28 -36
  134. package/utils/query.js.map +1 -0
  135. package/utils/sort.d.ts +2 -2
  136. package/utils/sort.js +4 -23
  137. package/utils/sort.js.map +1 -0
  138. package/utils/table.js +1 -6
  139. package/utils/table.js.map +1 -0
  140. package/utils/documentClient.d.ts +0 -8
  141. package/utils/documentClient.js +0 -33
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,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,CAACM,MAAM,CAACrB,YAAY,EAAEqB,MAAM,CAACjB,cAAc,CAAC;QAC1E,MAAMqB,OAAO,GAAGJ,MAAM,CAACR,YAAY,CAACa,OAAO,CAAC;UACxChB,KAAK;UACLV;QACJ,CAAC,CAAC;QAEF,OAAOqB,MAAM,CAACnC,MAAM,GAAG,CAACuC,OAAO,GAAGA,OAAO;MAC7C,CAAC,CAAC;MACF,IAAI/C,MAAM,KAAK,KAAK,EAAE;QAClB,OAAO,KAAK;MAChB;IACJ;IACA,OAAO,IAAI;EACf,CAAC;AACL,CAAC;AAEM,SAASiD,WAAW,CAAU3D,MAAiB,EAAO;EACzD,MAAMqD,MAAM,GAAGF,oBAAoB,CAACnD,MAAM,CAAC;EAC3C;AACJ;AACA;EACI,IAAI,CAACqD,MAAM,EAAE;IACT,OAAOrD,MAAM,CAAC4D,KAAK;EACvB;EACA,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"}
@@ -0,0 +1,12 @@
1
+ export * from "./batchRead";
2
+ export * from "./batchWrite";
3
+ export * from "./cleanup";
4
+ export * from "./createEntity";
5
+ export * from "./cursor";
6
+ export * from "./filter";
7
+ export * from "./get";
8
+ export * from "./listResponse";
9
+ export * from "./query";
10
+ export * from "./sort";
11
+ export * from "./createTable";
12
+ export * from "./table";
package/utils/index.js ADDED
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ var _batchRead = require("./batchRead");
7
+ Object.keys(_batchRead).forEach(function (key) {
8
+ if (key === "default" || key === "__esModule") return;
9
+ if (key in exports && exports[key] === _batchRead[key]) return;
10
+ Object.defineProperty(exports, key, {
11
+ enumerable: true,
12
+ get: function () {
13
+ return _batchRead[key];
14
+ }
15
+ });
16
+ });
17
+ var _batchWrite = require("./batchWrite");
18
+ Object.keys(_batchWrite).forEach(function (key) {
19
+ if (key === "default" || key === "__esModule") return;
20
+ if (key in exports && exports[key] === _batchWrite[key]) return;
21
+ Object.defineProperty(exports, key, {
22
+ enumerable: true,
23
+ get: function () {
24
+ return _batchWrite[key];
25
+ }
26
+ });
27
+ });
28
+ var _cleanup = require("./cleanup");
29
+ Object.keys(_cleanup).forEach(function (key) {
30
+ if (key === "default" || key === "__esModule") return;
31
+ if (key in exports && exports[key] === _cleanup[key]) return;
32
+ Object.defineProperty(exports, key, {
33
+ enumerable: true,
34
+ get: function () {
35
+ return _cleanup[key];
36
+ }
37
+ });
38
+ });
39
+ var _createEntity = require("./createEntity");
40
+ Object.keys(_createEntity).forEach(function (key) {
41
+ if (key === "default" || key === "__esModule") return;
42
+ if (key in exports && exports[key] === _createEntity[key]) return;
43
+ Object.defineProperty(exports, key, {
44
+ enumerable: true,
45
+ get: function () {
46
+ return _createEntity[key];
47
+ }
48
+ });
49
+ });
50
+ var _cursor = require("./cursor");
51
+ Object.keys(_cursor).forEach(function (key) {
52
+ if (key === "default" || key === "__esModule") return;
53
+ if (key in exports && exports[key] === _cursor[key]) return;
54
+ Object.defineProperty(exports, key, {
55
+ enumerable: true,
56
+ get: function () {
57
+ return _cursor[key];
58
+ }
59
+ });
60
+ });
61
+ var _filter = require("./filter");
62
+ Object.keys(_filter).forEach(function (key) {
63
+ if (key === "default" || key === "__esModule") return;
64
+ if (key in exports && exports[key] === _filter[key]) return;
65
+ Object.defineProperty(exports, key, {
66
+ enumerable: true,
67
+ get: function () {
68
+ return _filter[key];
69
+ }
70
+ });
71
+ });
72
+ var _get = require("./get");
73
+ Object.keys(_get).forEach(function (key) {
74
+ if (key === "default" || key === "__esModule") return;
75
+ if (key in exports && exports[key] === _get[key]) return;
76
+ Object.defineProperty(exports, key, {
77
+ enumerable: true,
78
+ get: function () {
79
+ return _get[key];
80
+ }
81
+ });
82
+ });
83
+ var _listResponse = require("./listResponse");
84
+ Object.keys(_listResponse).forEach(function (key) {
85
+ if (key === "default" || key === "__esModule") return;
86
+ if (key in exports && exports[key] === _listResponse[key]) return;
87
+ Object.defineProperty(exports, key, {
88
+ enumerable: true,
89
+ get: function () {
90
+ return _listResponse[key];
91
+ }
92
+ });
93
+ });
94
+ var _query = require("./query");
95
+ Object.keys(_query).forEach(function (key) {
96
+ if (key === "default" || key === "__esModule") return;
97
+ if (key in exports && exports[key] === _query[key]) return;
98
+ Object.defineProperty(exports, key, {
99
+ enumerable: true,
100
+ get: function () {
101
+ return _query[key];
102
+ }
103
+ });
104
+ });
105
+ var _sort = require("./sort");
106
+ Object.keys(_sort).forEach(function (key) {
107
+ if (key === "default" || key === "__esModule") return;
108
+ if (key in exports && exports[key] === _sort[key]) return;
109
+ Object.defineProperty(exports, key, {
110
+ enumerable: true,
111
+ get: function () {
112
+ return _sort[key];
113
+ }
114
+ });
115
+ });
116
+ var _createTable = require("./createTable");
117
+ Object.keys(_createTable).forEach(function (key) {
118
+ if (key === "default" || key === "__esModule") return;
119
+ if (key in exports && exports[key] === _createTable[key]) return;
120
+ Object.defineProperty(exports, key, {
121
+ enumerable: true,
122
+ get: function () {
123
+ return _createTable[key];
124
+ }
125
+ });
126
+ });
127
+ var _table = require("./table");
128
+ Object.keys(_table).forEach(function (key) {
129
+ if (key === "default" || key === "__esModule") return;
130
+ if (key in exports && exports[key] === _table[key]) return;
131
+ Object.defineProperty(exports, key, {
132
+ enumerable: true,
133
+ get: function () {
134
+ return _table[key];
135
+ }
136
+ });
137
+ });
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"./batchRead\";\nexport * from \"./batchWrite\";\nexport * from \"./cleanup\";\nexport * from \"./createEntity\";\nexport * from \"./cursor\";\nexport * from \"./filter\";\nexport * from \"./get\";\nexport * from \"./listResponse\";\nexport * from \"./query\";\nexport * from \"./sort\";\nexport * from \"./createTable\";\nexport * from \"./table\";\n"],"mappings":";;;;;AAAA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA"}
@@ -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,13 @@ 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
- * Will run the query to fetch the results no matter how much iterations it needs to go through.
30
+ * Will run the query to fetch the results no matter how many iterations it needs to go through.
31
31
  */
32
32
  export declare const queryAll: <T>(params: QueryAllParams) => Promise<DbItem<T>[]>;
33
- export {};
33
+ /**
34
+ * Will run the query to fetch the results no matter how many iterations it needs to go through.
35
+ * Results of each iteration will be passed to the provided callback
36
+ */
37
+ export declare const queryAllWithCallback: <T>(params: QueryAllParams, callback: (items: DbItem<T>[]) => Promise<void>) => Promise<void>;
package/utils/query.js CHANGED
@@ -1,20 +1,12 @@
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.queryOne = exports.queryAll = void 0;
9
-
10
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
-
7
+ exports.queryOne = exports.queryAllWithCallback = exports.queryAll = void 0;
8
+ var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
12
9
  var _error = _interopRequireDefault(require("@webiny/error"));
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
10
  /**
19
11
  * Will run query only once. Pass the previous to run the query again to fetch new data.
20
12
  * It returns the result and the items it found.
@@ -32,7 +24,6 @@ const query = async params => {
32
24
  * In case there is no previous result we must make a new query.
33
25
  * This is the first query on the given partition key.
34
26
  */
35
-
36
27
  if (!previous) {
37
28
  result = await entity.query(partitionKey, options);
38
29
  } else if (typeof previous.next === "function") {
@@ -42,7 +33,6 @@ const query = async params => {
42
33
  * and we return a null to keep the query from repeating.
43
34
  */
44
35
  result = await previous.next();
45
-
46
36
  if (result === false) {
47
37
  return {
48
38
  result: null,
@@ -63,15 +53,12 @@ const query = async params => {
63
53
  /**
64
54
  * We expect the result to contain an Items array and if not, something went wrong, very wrong.
65
55
  */
66
-
67
-
68
56
  if (!result || !result.Items || !Array.isArray(result.Items)) {
69
57
  throw new _error.default("Error when querying for content entries - no result.", "QUERY_ERROR", {
70
58
  partitionKey,
71
59
  options
72
60
  });
73
61
  }
74
-
75
62
  return {
76
63
  result,
77
64
  items: result.Items
@@ -80,48 +67,53 @@ const query = async params => {
80
67
  /**
81
68
  * Will run the query to fetch the first possible item from the database.
82
69
  */
83
-
84
-
85
70
  const queryOne = async params => {
86
71
  const {
87
72
  items
88
- } = await query(_objectSpread(_objectSpread({}, params), {}, {
89
- options: _objectSpread(_objectSpread({}, params.options || {}), {}, {
73
+ } = await query((0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), {}, {
74
+ options: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, params.options || {}), {}, {
90
75
  limit: 1
91
76
  })
92
77
  }));
93
-
94
- if (items.length === 0) {
95
- return null;
96
- }
97
-
98
- return items.shift();
78
+ const item = items.shift();
79
+ return item ? item : null;
99
80
  };
100
81
  /**
101
- * Will run the query to fetch the results no matter how much iterations it needs to go through.
82
+ * Will run the query to fetch the results no matter how many iterations it needs to go through.
102
83
  */
103
-
104
-
105
84
  exports.queryOne = queryOne;
106
-
107
85
  const queryAll = async params => {
108
86
  const items = [];
109
87
  let results;
110
88
  let previousResult = undefined;
111
-
112
- while (results = await query(_objectSpread(_objectSpread({}, params), {}, {
89
+ while (results = await query((0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), {}, {
113
90
  previous: previousResult
114
91
  }))) {
115
92
  items.push(...results.items);
116
-
117
93
  if (!results.result) {
118
94
  return items;
119
95
  }
120
-
121
96
  previousResult = results.result;
122
97
  }
123
-
124
98
  return items;
125
99
  };
126
100
 
127
- exports.queryAll = queryAll;
101
+ /**
102
+ * Will run the query to fetch the results no matter how many iterations it needs to go through.
103
+ * Results of each iteration will be passed to the provided callback
104
+ */
105
+ exports.queryAll = queryAll;
106
+ const queryAllWithCallback = async (params, callback) => {
107
+ let results;
108
+ let previousResult = undefined;
109
+ while (results = await query((0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), {}, {
110
+ previous: previousResult
111
+ }))) {
112
+ if (!results.result) {
113
+ break;
114
+ }
115
+ await callback(results.items);
116
+ previousResult = results.result;
117
+ }
118
+ };
119
+ exports.queryAllWithCallback = queryAllWithCallback;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["query","params","entity","previous","partitionKey","options","result","next","items","Items","Array","isArray","WebinyError","queryOne","limit","item","shift","queryAll","results","previousResult","undefined","push","queryAllWithCallback","callback"],"sources":["query.ts"],"sourcesContent":["import { queryOptions as DynamoDBToolboxQueryOptions } from \"dynamodb-toolbox/dist/classes/Table\";\nimport WebinyError from \"@webiny/error\";\nimport { Entity } from \"dynamodb-toolbox\";\n\nexport interface QueryAllParams {\n entity: Entity<any>;\n partitionKey: string;\n options?: DynamoDBToolboxQueryOptions;\n}\n\nexport interface QueryOneParams extends QueryAllParams {\n options?: Omit<DynamoDBToolboxQueryOptions, \"limit\">;\n}\n\nexport interface QueryParams extends QueryAllParams {\n previous?: any;\n}\n\nexport interface QueryResult<T> {\n result: any | null;\n items: T[];\n}\n\nexport type DbItem<T> = T & {\n PK: string;\n SK: string;\n TYPE: string;\n GSI1_PK?: string;\n GSI1_SK?: string;\n};\n\n/**\n * Will run query only once. Pass the previous to run the query again to fetch new data.\n * It returns the result and the items it found.\n * Result is required to fetch the items that were not fetched in the previous run.\n */\nconst query = async <T>(params: QueryParams): Promise<QueryResult<T>> => {\n const { entity, previous, partitionKey, options } = params;\n let result;\n /**\n * In case there is no previous result we must make a new query.\n * This is the first query on the given partition key.\n */\n if (!previous) {\n result = await entity.query(partitionKey, options);\n } else if (typeof previous.next === \"function\") {\n /**\n * In case we have a previous result and it has a next method, we run it.\n * In case result of the next method is false, it means it has nothing else to read\n * and we return a null to keep the query from repeating.\n */\n result = await previous.next();\n if (result === false) {\n return {\n result: null,\n items: []\n };\n }\n } else {\n /**\n * This could probably never happen but keep it here just in case to break the query loop.\n * Basically, either previous does not exist or it exists and it does not have the next method\n * and at that point a result returned will be null and loop should not start again.\n */\n return {\n result: null,\n items: []\n };\n }\n /**\n * We expect the result to contain an Items array and if not, something went wrong, very wrong.\n */\n if (!result || !result.Items || !Array.isArray(result.Items)) {\n throw new WebinyError(\n \"Error when querying for content entries - no result.\",\n \"QUERY_ERROR\",\n {\n partitionKey,\n options\n }\n );\n }\n return {\n result,\n items: result.Items\n };\n};\n/**\n * Will run the query to fetch the first possible item from the database.\n */\nexport const queryOne = async <T>(params: QueryOneParams): Promise<DbItem<T> | null> => {\n const { items } = await query<DbItem<T>>({\n ...params,\n options: {\n ...(params.options || {}),\n limit: 1\n }\n });\n const item = items.shift();\n return item ? item : null;\n};\n/**\n * Will run the query to fetch the results no matter how many iterations it needs to go through.\n */\nexport const queryAll = async <T>(params: QueryAllParams): Promise<DbItem<T>[]> => {\n const items: DbItem<T>[] = [];\n let results: QueryResult<DbItem<T>>;\n let previousResult: any = undefined;\n while ((results = await query({ ...params, previous: previousResult }))) {\n items.push(...results.items);\n if (!results.result) {\n return items;\n }\n previousResult = results.result;\n }\n return items;\n};\n\n/**\n * Will run the query to fetch the results no matter how many iterations it needs to go through.\n * Results of each iteration will be passed to the provided callback\n */\nexport const queryAllWithCallback = async <T>(\n params: QueryAllParams,\n callback: (items: DbItem<T>[]) => Promise<void>\n): Promise<void> => {\n let results: QueryResult<DbItem<T>>;\n let previousResult: any = undefined;\n while ((results = await query({ ...params, previous: previousResult }))) {\n if (!results.result) {\n break;\n }\n await callback(results.items);\n previousResult = results.result;\n }\n};\n"],"mappings":";;;;;;;;AACA;AA8BA;AACA;AACA;AACA;AACA;AACA,MAAMA,KAAK,GAAG,MAAUC,MAAmB,IAA8B;EACrE,MAAM;IAAEC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A;AAFA;AAGO,MAAME,QAAQ,GAAG,MAAUhB,MAAsB,IAA2B;EAC/E,MAAMO,KAAkB,GAAG,EAAE;EAC7B,IAAIU,OAA+B;EACnC,IAAIC,cAAmB,GAAGC,SAAS;EACnC,OAAQF,OAAO,GAAG,MAAMlB,KAAK,6DAAMC,MAAM;IAAEE,QAAQ,EAAEgB;EAAc,GAAG,EAAG;IACrEX,KAAK,CAACa,IAAI,CAAC,GAAGH,OAAO,CAACV,KAAK,CAAC;IAC5B,IAAI,CAACU,OAAO,CAACZ,MAAM,EAAE;MACjB,OAAOE,KAAK;IAChB;IACAW,cAAc,GAAGD,OAAO,CAACZ,MAAM;EACnC;EACA,OAAOE,KAAK;AAChB,CAAC;;AAED;AACA;AACA;AACA;AAHA;AAIO,MAAMc,oBAAoB,GAAG,OAChCrB,MAAsB,EACtBsB,QAA+C,KAC/B;EAChB,IAAIL,OAA+B;EACnC,IAAIC,cAAmB,GAAGC,SAAS;EACnC,OAAQF,OAAO,GAAG,MAAMlB,KAAK,6DAAMC,MAAM;IAAEE,QAAQ,EAAEgB;EAAc,GAAG,EAAG;IACrE,IAAI,CAACD,OAAO,CAACZ,MAAM,EAAE;MACjB;IACJ;IACA,MAAMiB,QAAQ,CAACL,OAAO,CAACV,KAAK,CAAC;IAC7BW,cAAc,GAAGD,OAAO,CAACZ,MAAM;EACnC;AACJ,CAAC;AAAC"}
package/utils/sort.d.ts CHANGED
@@ -7,11 +7,11 @@ interface Params<T> {
7
7
  /**
8
8
  * Sort options. For example: ["id_ASC"]
9
9
  */
10
- sort: string[];
10
+ sort?: string[];
11
11
  /**
12
12
  * Fields we can sort by.
13
13
  */
14
14
  fields?: FieldPlugin[];
15
15
  }
16
- export declare const sortItems: <T extends unknown = any>(params: Params<T>) => T[];
16
+ export declare function sortItems<T = any>(params: Params<T>): T[];
17
17
  export {};