@webiny/db-dynamodb 6.0.0-alpha.5 → 6.0.0-rc.0

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 (177) hide show
  1. package/DynamoDbDriver.d.ts +4 -8
  2. package/DynamoDbDriver.js +71 -102
  3. package/DynamoDbDriver.js.map +1 -1
  4. package/README.md +6 -142
  5. package/index.d.ts +4 -3
  6. package/index.js +4 -28
  7. package/index.js.map +1 -1
  8. package/package.json +18 -20
  9. package/plugins/definitions/AttributePlugin.d.ts +1 -1
  10. package/plugins/definitions/AttributePlugin.js +4 -12
  11. package/plugins/definitions/AttributePlugin.js.map +1 -1
  12. package/plugins/definitions/DateTimeTransformPlugin.d.ts +2 -2
  13. package/plugins/definitions/DateTimeTransformPlugin.js +6 -14
  14. package/plugins/definitions/DateTimeTransformPlugin.js.map +1 -1
  15. package/plugins/definitions/FieldPathPlugin.js +6 -14
  16. package/plugins/definitions/FieldPathPlugin.js.map +1 -1
  17. package/plugins/definitions/FieldPlugin.d.ts +1 -1
  18. package/plugins/definitions/FieldPlugin.js +2 -9
  19. package/plugins/definitions/FieldPlugin.js.map +1 -1
  20. package/plugins/definitions/TimeTransformPlugin.d.ts +2 -2
  21. package/plugins/definitions/TimeTransformPlugin.js +7 -15
  22. package/plugins/definitions/TimeTransformPlugin.js.map +1 -1
  23. package/plugins/definitions/ValueFilterPlugin.js +5 -13
  24. package/plugins/definitions/ValueFilterPlugin.js.map +1 -1
  25. package/plugins/definitions/ValueTransformPlugin.js +6 -14
  26. package/plugins/definitions/ValueTransformPlugin.js.map +1 -1
  27. package/plugins/definitions/assignFields.js +4 -12
  28. package/plugins/definitions/assignFields.js.map +1 -1
  29. package/plugins/filters/andIn.d.ts +1 -1
  30. package/plugins/filters/andIn.js +5 -12
  31. package/plugins/filters/andIn.js.map +1 -1
  32. package/plugins/filters/between.d.ts +1 -1
  33. package/plugins/filters/between.js +5 -12
  34. package/plugins/filters/between.js.map +1 -1
  35. package/plugins/filters/contains.d.ts +1 -1
  36. package/plugins/filters/contains.js +3 -9
  37. package/plugins/filters/contains.js.map +1 -1
  38. package/plugins/filters/eq.d.ts +1 -1
  39. package/plugins/filters/eq.js +3 -9
  40. package/plugins/filters/eq.js.map +1 -1
  41. package/plugins/filters/fuzzy.d.ts +1 -1
  42. package/plugins/filters/fuzzy.js +5 -12
  43. package/plugins/filters/fuzzy.js.map +1 -1
  44. package/plugins/filters/gt.d.ts +1 -1
  45. package/plugins/filters/gt.js +3 -9
  46. package/plugins/filters/gt.js.map +1 -1
  47. package/plugins/filters/gte.d.ts +1 -1
  48. package/plugins/filters/gte.js +3 -9
  49. package/plugins/filters/gte.js.map +1 -1
  50. package/plugins/filters/in.d.ts +1 -1
  51. package/plugins/filters/in.js +5 -12
  52. package/plugins/filters/in.js.map +1 -1
  53. package/plugins/filters/index.d.ts +1 -1
  54. package/plugins/filters/index.js +12 -20
  55. package/plugins/filters/index.js.map +1 -1
  56. package/plugins/filters/lt.d.ts +1 -1
  57. package/plugins/filters/lt.js +3 -9
  58. package/plugins/filters/lt.js.map +1 -1
  59. package/plugins/filters/lte.d.ts +1 -1
  60. package/plugins/filters/lte.js +3 -9
  61. package/plugins/filters/lte.js.map +1 -1
  62. package/plugins/filters/startsWith.d.ts +1 -1
  63. package/plugins/filters/startsWith.js +3 -9
  64. package/plugins/filters/startsWith.js.map +1 -1
  65. package/plugins/index.d.ts +9 -1
  66. package/plugins/index.js +11 -11
  67. package/plugins/index.js.map +1 -1
  68. package/store/entity.d.ts +4 -76
  69. package/store/entity.js +5 -35
  70. package/store/entity.js.map +1 -1
  71. package/store/keys.d.ts +1 -1
  72. package/store/keys.js +7 -13
  73. package/store/keys.js.map +1 -1
  74. package/store/types.d.ts +3 -4
  75. package/store/types.js +1 -5
  76. package/store/types.js.map +1 -1
  77. package/toolbox.d.ts +7 -2
  78. package/toolbox.js +2 -17
  79. package/toolbox.js.map +1 -1
  80. package/types.js +1 -5
  81. package/utils/attributes.d.ts +2 -2
  82. package/utils/attributes.js +4 -12
  83. package/utils/attributes.js.map +1 -1
  84. package/utils/batch/batchRead.d.ts +2 -2
  85. package/utils/batch/batchRead.js +5 -13
  86. package/utils/batch/batchRead.js.map +1 -1
  87. package/utils/batch/batchWrite.d.ts +2 -2
  88. package/utils/batch/batchWrite.js +3 -11
  89. package/utils/batch/batchWrite.js.map +1 -1
  90. package/utils/batch/index.d.ts +3 -3
  91. package/utils/batch/index.js +3 -38
  92. package/utils/batch/index.js.map +1 -1
  93. package/utils/batch/types.d.ts +7 -2
  94. package/utils/batch/types.js +1 -5
  95. package/utils/batch/types.js.map +1 -1
  96. package/utils/cleanup.d.ts +1 -1
  97. package/utils/cleanup.js +3 -10
  98. package/utils/cleanup.js.map +1 -1
  99. package/utils/count.d.ts +1 -1
  100. package/utils/count.js +1 -8
  101. package/utils/count.js.map +1 -1
  102. package/utils/createEntity.d.ts +36 -54
  103. package/utils/createEntity.js +75 -51
  104. package/utils/createEntity.js.map +1 -1
  105. package/utils/createTable.d.ts +10 -7
  106. package/utils/createTable.js +25 -15
  107. package/utils/createTable.js.map +1 -1
  108. package/utils/cursor.js +2 -10
  109. package/utils/cursor.js.map +1 -1
  110. package/utils/delete.d.ts +1 -1
  111. package/utils/delete.js +1 -8
  112. package/utils/delete.js.map +1 -1
  113. package/utils/entity/Entity.d.ts +23 -21
  114. package/utils/entity/Entity.js +56 -34
  115. package/utils/entity/Entity.js.map +1 -1
  116. package/utils/entity/EntityReadBatch.d.ts +9 -8
  117. package/utils/entity/EntityReadBatch.js +14 -16
  118. package/utils/entity/EntityReadBatch.js.map +1 -1
  119. package/utils/entity/EntityReadBatchBuilder.d.ts +4 -4
  120. package/utils/entity/EntityReadBatchBuilder.js +7 -15
  121. package/utils/entity/EntityReadBatchBuilder.js.map +1 -1
  122. package/utils/entity/EntityWriteBatch.d.ts +9 -8
  123. package/utils/entity/EntityWriteBatch.js +10 -18
  124. package/utils/entity/EntityWriteBatch.js.map +1 -1
  125. package/utils/entity/EntityWriteBatchBuilder.d.ts +4 -4
  126. package/utils/entity/EntityWriteBatchBuilder.js +4 -12
  127. package/utils/entity/EntityWriteBatchBuilder.js.map +1 -1
  128. package/utils/entity/getEntity.d.ts +2 -2
  129. package/utils/entity/getEntity.js +3 -10
  130. package/utils/entity/getEntity.js.map +1 -1
  131. package/utils/entity/index.d.ts +7 -7
  132. package/utils/entity/index.js +7 -82
  133. package/utils/entity/index.js.map +1 -1
  134. package/utils/entity/types.d.ts +46 -22
  135. package/utils/entity/types.js +2 -5
  136. package/utils/entity/types.js.map +1 -1
  137. package/utils/filter.d.ts +1 -1
  138. package/utils/filter.js +7 -14
  139. package/utils/filter.js.map +1 -1
  140. package/utils/get.d.ts +1 -1
  141. package/utils/get.js +4 -12
  142. package/utils/get.js.map +1 -1
  143. package/utils/index.d.ts +17 -17
  144. package/utils/index.js +17 -192
  145. package/utils/index.js.map +1 -1
  146. package/utils/listResponse.js +4 -10
  147. package/utils/listResponse.js.map +1 -1
  148. package/utils/put.d.ts +2 -2
  149. package/utils/put.js +1 -8
  150. package/utils/put.js.map +1 -1
  151. package/utils/query.d.ts +5 -12
  152. package/utils/query.js +13 -27
  153. package/utils/query.js.map +1 -1
  154. package/utils/scan.d.ts +2 -2
  155. package/utils/scan.js +5 -13
  156. package/utils/scan.js.map +1 -1
  157. package/utils/sort.d.ts +1 -1
  158. package/utils/sort.js +7 -14
  159. package/utils/sort.js.map +1 -1
  160. package/utils/table/Table.d.ts +4 -5
  161. package/utils/table/Table.js +9 -20
  162. package/utils/table/Table.js.map +1 -1
  163. package/utils/table/TableReadBatch.d.ts +4 -4
  164. package/utils/table/TableReadBatch.js +8 -16
  165. package/utils/table/TableReadBatch.js.map +1 -1
  166. package/utils/table/TableWriteBatch.d.ts +3 -3
  167. package/utils/table/TableWriteBatch.js +6 -14
  168. package/utils/table/TableWriteBatch.js.map +1 -1
  169. package/utils/table/index.d.ts +4 -4
  170. package/utils/table/index.js +4 -49
  171. package/utils/table/index.js.map +1 -1
  172. package/utils/table/types.d.ts +8 -8
  173. package/utils/table/types.js +1 -5
  174. package/utils/table/types.js.map +1 -1
  175. package/utils/update.d.ts +1 -1
  176. package/utils/update.js +1 -8
  177. package/utils/update.js.map +1 -1
package/utils/put.js CHANGED
@@ -1,10 +1,4 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.put = void 0;
7
- const put = async params => {
1
+ export const put = async params => {
8
2
  const {
9
3
  entity,
10
4
  item
@@ -14,6 +8,5 @@ const put = async params => {
14
8
  strictSchemaCheck: false
15
9
  });
16
10
  };
17
- exports.put = put;
18
11
 
19
12
  //# sourceMappingURL=put.js.map
package/utils/put.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["put","params","entity","item","execute","strictSchemaCheck","exports"],"sources":["put.ts"],"sourcesContent":["import type { Entity } from \"~/toolbox\";\nimport type { GenericRecord } from \"@webiny/api/types\";\n\nexport type IPutParamsItem<T extends GenericRecord = GenericRecord> = {\n PK: string;\n SK: string;\n [key: string]: any;\n} & T;\n\nexport interface IPutParams<T extends GenericRecord = GenericRecord> {\n entity: Entity;\n item: IPutParamsItem<T>;\n}\n\nexport const put = async <T extends GenericRecord = GenericRecord>(params: IPutParams<T>) => {\n const { entity, item } = params;\n\n return await entity.put(item, {\n execute: true,\n strictSchemaCheck: false\n });\n};\n"],"mappings":";;;;;;AAcO,MAAMA,GAAG,GAAG,MAAgDC,MAAqB,IAAK;EACzF,MAAM;IAAEC,MAAM;IAAEC;EAAK,CAAC,GAAGF,MAAM;EAE/B,OAAO,MAAMC,MAAM,CAACF,GAAG,CAACG,IAAI,EAAE;IAC1BC,OAAO,EAAE,IAAI;IACbC,iBAAiB,EAAE;EACvB,CAAC,CAAC;AACN,CAAC;AAACC,OAAA,CAAAN,GAAA,GAAAA,GAAA","ignoreList":[]}
1
+ {"version":3,"names":["put","params","entity","item","execute","strictSchemaCheck"],"sources":["put.ts"],"sourcesContent":["import type { Entity } from \"~/toolbox.js\";\nimport type { GenericRecord } from \"@webiny/api/types.js\";\n\nexport type IPutParamsItem<T extends GenericRecord = GenericRecord> = {\n PK: string;\n SK: string;\n [key: string]: any;\n} & T;\n\nexport interface IPutParams<T extends GenericRecord = GenericRecord> {\n entity: Entity;\n item: IPutParamsItem<T>;\n}\n\nexport const put = async <T extends GenericRecord = GenericRecord>(params: IPutParams<T>) => {\n const { entity, item } = params;\n\n return await entity.put(item, {\n execute: true,\n strictSchemaCheck: false\n });\n};\n"],"mappings":"AAcA,OAAO,MAAMA,GAAG,GAAG,MAAgDC,MAAqB,IAAK;EACzF,MAAM;IAAEC,MAAM;IAAEC;EAAK,CAAC,GAAGF,MAAM;EAE/B,OAAO,MAAMC,MAAM,CAACF,GAAG,CAACG,IAAI,EAAE;IAC1BC,OAAO,EAAE,IAAI;IACbC,iBAAiB,EAAE;EACvB,CAAC,CAAC;AACN,CAAC","ignoreList":[]}
package/utils/query.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { Entity, EntityQueryOptions } from "../toolbox";
2
- import type { GenericRecord } from "@webiny/api/types";
1
+ import type { Entity, EntityQueryOptions } from "../toolbox.js";
2
+ import type { GenericRecord } from "@webiny/api/types.js";
3
3
  export interface QueryAllParams {
4
4
  entity: Entity<any>;
5
5
  partitionKey: string;
@@ -15,22 +15,15 @@ export interface QueryResult<T> {
15
15
  result: any | null;
16
16
  items: T[];
17
17
  }
18
- export type DbItem<T> = T & {
19
- PK: string;
20
- SK: string;
21
- TYPE: string;
22
- GSI1_PK?: string;
23
- GSI1_SK?: string;
24
- };
25
18
  /**
26
19
  * Will run the query to fetch the first possible item from the database.
27
20
  */
28
- export declare const queryOne: <T>(params: QueryOneParams) => Promise<DbItem<T> | null>;
21
+ export declare const queryOne: <T>(params: QueryOneParams) => Promise<T | null>;
29
22
  export declare const queryOneClean: <T>(params: QueryOneParams) => Promise<T | null>;
30
23
  /**
31
24
  * Will run the query to fetch the results no matter how many iterations it needs to go through.
32
25
  */
33
- export declare const queryAll: <T>(params: QueryAllParams) => Promise<DbItem<T>[]>;
26
+ export declare const queryAll: <T>(params: QueryAllParams) => Promise<T[]>;
34
27
  export declare const queryAllClean: <T>(params: QueryAllParams) => Promise<T[]>;
35
28
  export interface IQueryPageResponse<T> {
36
29
  items: T[];
@@ -42,4 +35,4 @@ export declare const queryPerPageClean: <T>(params: QueryAllParams) => Promise<I
42
35
  * Will run the query to fetch the results no matter how many iterations it needs to go through.
43
36
  * Results of each iteration will be passed to the provided callback
44
37
  */
45
- export declare const queryAllWithCallback: <T>(params: QueryAllParams, callback: (items: DbItem<T>[]) => Promise<void>) => Promise<void>;
38
+ export declare const queryAllWithCallback: <T>(params: QueryAllParams, callback: (items: T[]) => Promise<void>) => Promise<void>;
package/utils/query.js CHANGED
@@ -1,12 +1,5 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports.queryPerPageClean = exports.queryPerPage = exports.queryOneClean = exports.queryOne = exports.queryAllWithCallback = exports.queryAllClean = exports.queryAll = void 0;
8
- var _error = _interopRequireDefault(require("@webiny/error"));
9
- var _cleanup = require("./cleanup");
1
+ import WebinyError from "@webiny/error";
2
+ import { cleanupItem, cleanupItems } from "./cleanup.js";
10
3
  /**
11
4
  * Will run query only once. Pass the previous to run the query again to fetch new data.
12
5
  * It returns the result and the items it found.
@@ -54,7 +47,7 @@ const query = async params => {
54
47
  * We expect the result to contain an Items array and if not, something went wrong, very wrong.
55
48
  */
56
49
  if (!result || !result.Items || !Array.isArray(result.Items)) {
57
- throw new _error.default("Error when querying for content entries - no result.", "QUERY_ERROR", {
50
+ throw new WebinyError("Error when querying for content entries - no result.", "QUERY_ERROR", {
58
51
  partitionKey,
59
52
  options
60
53
  });
@@ -67,7 +60,7 @@ const query = async params => {
67
60
  /**
68
61
  * Will run the query to fetch the first possible item from the database.
69
62
  */
70
- const queryOne = async params => {
63
+ export const queryOne = async params => {
71
64
  const {
72
65
  items
73
66
  } = await query({
@@ -80,19 +73,17 @@ const queryOne = async params => {
80
73
  const item = items.shift();
81
74
  return item ? item : null;
82
75
  };
83
- exports.queryOne = queryOne;
84
- const queryOneClean = async params => {
76
+ export const queryOneClean = async params => {
85
77
  const result = await queryOne(params);
86
78
  if (!result) {
87
79
  return null;
88
80
  }
89
- return (0, _cleanup.cleanupItem)(params.entity, result);
81
+ return cleanupItem(params.entity, result);
90
82
  };
91
83
  /**
92
84
  * Will run the query to fetch the results no matter how many iterations it needs to go through.
93
85
  */
94
- exports.queryOneClean = queryOneClean;
95
- const queryAll = async params => {
86
+ export const queryAll = async params => {
96
87
  const items = [];
97
88
  let results;
98
89
  let previousResult = undefined;
@@ -108,13 +99,11 @@ const queryAll = async params => {
108
99
  }
109
100
  return items;
110
101
  };
111
- exports.queryAll = queryAll;
112
- const queryAllClean = async params => {
102
+ export const queryAllClean = async params => {
113
103
  const results = await queryAll(params);
114
- return (0, _cleanup.cleanupItems)(params.entity, results);
104
+ return cleanupItems(params.entity, results);
115
105
  };
116
- exports.queryAllClean = queryAllClean;
117
- const queryPerPage = async params => {
106
+ export const queryPerPage = async params => {
118
107
  const result = await query({
119
108
  ...params,
120
109
  options: {
@@ -127,11 +116,10 @@ const queryPerPage = async params => {
127
116
  lastEvaluatedKey: result.result?.LastEvaluatedKey
128
117
  };
129
118
  };
130
- exports.queryPerPage = queryPerPage;
131
- const queryPerPageClean = async params => {
119
+ export const queryPerPageClean = async params => {
132
120
  const result = await queryPerPage(params);
133
121
  return {
134
- items: (0, _cleanup.cleanupItems)(params.entity, result.items),
122
+ items: cleanupItems(params.entity, result.items),
135
123
  lastEvaluatedKey: result.lastEvaluatedKey
136
124
  };
137
125
  };
@@ -140,8 +128,7 @@ const queryPerPageClean = async params => {
140
128
  * Will run the query to fetch the results no matter how many iterations it needs to go through.
141
129
  * Results of each iteration will be passed to the provided callback
142
130
  */
143
- exports.queryPerPageClean = queryPerPageClean;
144
- const queryAllWithCallback = async (params, callback) => {
131
+ export const queryAllWithCallback = async (params, callback) => {
145
132
  let results;
146
133
  let previousResult = undefined;
147
134
  while (results = await query({
@@ -155,6 +142,5 @@ const queryAllWithCallback = async (params, callback) => {
155
142
  previousResult = results.result;
156
143
  }
157
144
  };
158
- exports.queryAllWithCallback = queryAllWithCallback;
159
145
 
160
146
  //# sourceMappingURL=query.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_error","_interopRequireDefault","require","_cleanup","query","params","entity","previous","partitionKey","options","result","next","items","Items","Array","isArray","WebinyError","queryOne","limit","item","shift","exports","queryOneClean","cleanupItem","queryAll","results","previousResult","undefined","push","queryAllClean","cleanupItems","queryPerPage","lastEvaluatedKey","LastEvaluatedKey","queryPerPageClean","queryAllWithCallback","callback"],"sources":["query.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport type { Entity, EntityQueryOptions } from \"~/toolbox\";\nimport { cleanupItem, cleanupItems } from \"~/utils/cleanup\";\nimport type { GenericRecord } from \"@webiny/api/types\";\n\nexport interface QueryAllParams {\n entity: Entity<any>;\n partitionKey: string;\n options?: EntityQueryOptions;\n}\n\nexport interface QueryOneParams extends QueryAllParams {\n options?: Omit<EntityQueryOptions, \"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\nexport const queryOneClean = async <T>(params: QueryOneParams): Promise<T | null> => {\n const result = await queryOne<T>(params);\n if (!result) {\n return null;\n }\n return cleanupItem(params.entity, result);\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\nexport const queryAllClean = async <T>(params: QueryAllParams): Promise<T[]> => {\n const results = await queryAll<T>(params);\n return cleanupItems(params.entity, results);\n};\n\nexport interface IQueryPageResponse<T> {\n items: T[];\n lastEvaluatedKey: GenericRecord;\n}\n\nexport const queryPerPage = async <T>(params: QueryAllParams): Promise<IQueryPageResponse<T>> => {\n const result = await query<T>({\n ...params,\n options: {\n ...params.options,\n limit: params.options?.limit || 50\n }\n });\n\n return {\n items: result.items,\n lastEvaluatedKey: result.result?.LastEvaluatedKey\n };\n};\n\nexport const queryPerPageClean = async <T>(\n params: QueryAllParams\n): Promise<IQueryPageResponse<T>> => {\n const result = await queryPerPage<T>(params);\n return {\n items: cleanupItems<T>(params.entity, result.items),\n lastEvaluatedKey: result.lastEvaluatedKey\n };\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":";;;;;;;AAAA,IAAAA,MAAA,GAAAC,sBAAA,CAAAC,OAAA;AAEA,IAAAC,QAAA,GAAAD,OAAA;AA8BA;AACA;AACA;AACA;AACA;AACA,MAAME,KAAK,GAAG,MAAUC,MAAmB,IAA8B;EACrE,MAAM;IAAEC,MAAM;IAAEC,QAAQ;IAAEC,YAAY;IAAEC;EAAQ,CAAC,GAAGJ,MAAM;EAC1D,IAAIK,MAAM;EACV;AACJ;AACA;AACA;EACI,IAAI,CAACH,QAAQ,EAAE;IACXG,MAAM,GAAG,MAAMJ,MAAM,CAACF,KAAK,CAACI,YAAY,EAAEC,OAAO,CAAC;EACtD,CAAC,MAAM,IAAI,OAAOF,QAAQ,CAACI,IAAI,KAAK,UAAU,EAAE;IAC5C;AACR;AACA;AACA;AACA;IACQD,MAAM,GAAG,MAAMH,QAAQ,CAACI,IAAI,CAAC,CAAC;IAC9B,IAAID,MAAM,KAAK,KAAK,EAAE;MAClB,OAAO;QACHA,MAAM,EAAE,IAAI;QACZE,KAAK,EAAE;MACX,CAAC;IACL;EACJ,CAAC,MAAM;IACH;AACR;AACA;AACA;AACA;IACQ,OAAO;M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AC7B,CAAC;AACL,CAAC;;AAED;AACA;AACA;AACA;AAHAX,OAAA,CAAAa,iBAAA,GAAAA,iBAAA;AAIO,MAAMC,oBAAoB,GAAG,MAAAA,CAChC9B,MAAsB,EACtB+B,QAA+C,KAC/B;EAChB,IAAIX,OAA+B;EACnC,IAAIC,cAAmB,GAAGC,SAAS;EACnC,OAAQF,OAAO,GAAG,MAAMrB,KAAK,CAAC;IAAE,GAAGC,MAAM;IAAEE,QAAQ,EAAEmB;EAAe,CAAC,CAAC,EAAG;IACrE,IAAI,CAACD,OAAO,CAACf,MAAM,EAAE;MACjB;IACJ;IACA,MAAM0B,QAAQ,CAACX,OAAO,CAACb,KAAK,CAAC;IAC7Bc,cAAc,GAAGD,OAAO,CAACf,MAAM;EACnC;AACJ,CAAC;AAACW,OAAA,CAAAc,oBAAA,GAAAA,oBAAA","ignoreList":[]}
1
+ {"version":3,"names":["WebinyError","cleanupItem","cleanupItems","query","params","entity","previous","partitionKey","options","result","next","items","Items","Array","isArray","queryOne","limit","item","shift","queryOneClean","queryAll","results","previousResult","undefined","push","queryAllClean","queryPerPage","lastEvaluatedKey","LastEvaluatedKey","queryPerPageClean","queryAllWithCallback","callback"],"sources":["query.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport type { Entity, EntityQueryOptions } from \"~/toolbox.js\";\nimport { cleanupItem, cleanupItems } from \"~/utils/cleanup.js\";\nimport type { GenericRecord } from \"@webiny/api/types.js\";\n\nexport interface QueryAllParams {\n entity: Entity<any>;\n partitionKey: string;\n options?: EntityQueryOptions;\n}\n\nexport interface QueryOneParams extends QueryAllParams {\n options?: Omit<EntityQueryOptions, \"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\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<T | null> => {\n const { items } = await query<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\nexport const queryOneClean = async <T>(params: QueryOneParams): Promise<T | null> => {\n const result = await queryOne<T>(params);\n if (!result) {\n return null;\n }\n return cleanupItem(params.entity, result);\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<T[]> => {\n const items: T[] = [];\n let results: QueryResult<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\nexport const queryAllClean = async <T>(params: QueryAllParams): Promise<T[]> => {\n const results = await queryAll<T>(params);\n return cleanupItems(params.entity, results);\n};\n\nexport interface IQueryPageResponse<T> {\n items: T[];\n lastEvaluatedKey: GenericRecord;\n}\n\nexport const queryPerPage = async <T>(params: QueryAllParams): Promise<IQueryPageResponse<T>> => {\n const result = await query<T>({\n ...params,\n options: {\n ...params.options,\n limit: params.options?.limit || 50\n }\n });\n\n return {\n items: result.items,\n lastEvaluatedKey: result.result?.LastEvaluatedKey\n };\n};\n\nexport const queryPerPageClean = async <T>(\n params: QueryAllParams\n): Promise<IQueryPageResponse<T>> => {\n const result = await queryPerPage<T>(params);\n return {\n items: cleanupItems<T>(params.entity, result.items),\n lastEvaluatedKey: result.lastEvaluatedKey\n };\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: T[]) => Promise<void>\n): Promise<void> => {\n let results: QueryResult<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":"AAAA,OAAOA,WAAW,MAAM,eAAe;AAEvC,SAASC,WAAW,EAAEC,YAAY;AAsBlC;AACA;AACA;AACA;AACA;AACA,MAAMC,KAAK,GAAG,MAAUC,MAAmB,IAA8B;EACrE,MAAM;IAAEC,MAAM;IAAEC,QAAQ;IAAEC,YAAY;IAAEC;EAAQ,CAAC,GAAGJ,MAAM;EAC1D,IAAIK,MAAM;EACV;AACJ;AACA;AACA;EACI,IAAI,CAACH,QAAQ,EAAE;IACXG,MAAM,GAAG,MAAMJ,MAAM,CAACF,KAAK,CAACI,YAAY,EAAEC,OAAO,CAAC;EACtD,CAAC,MAAM,IAAI,OAAOF,QAAQ,CAACI,IAAI,KAAK,UAAU,EAAE;IAC5C;AACR;AACA;AACA;AACA;IACQD,MAAM,GAAG,MAAMH,QAAQ,CAACI,IAAI,CAAC,CAAC;IAC9B,IAAID,MAAM,KAAK,KAAK,EAAE;MAClB,OAAO;QACHA,MAAM,EAAE,IAAI;QACZE,KAAK,E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B;EAChB,IAAIV,OAAuB;EAC3B,IAAIC,cAAmB,GAAGC,SAAS;EACnC,OAAQF,OAAO,GAAG,MAAMlB,KAAK,CAAC;IAAE,GAAGC,MAAM;IAAEE,QAAQ,EAAEgB;EAAe,CAAC,CAAC,EAAG;IACrE,IAAI,CAACD,OAAO,CAACZ,MAAM,EAAE;MACjB;IACJ;IACA,MAAMsB,QAAQ,CAACV,OAAO,CAACV,KAAK,CAAC;IAC7BW,cAAc,GAAGD,OAAO,CAACZ,MAAM;EACnC;AACJ,CAAC","ignoreList":[]}
package/utils/scan.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { ScanInput, ScanOutput } from "@webiny/aws-sdk/client-dynamodb";
2
- import type { Entity, ScanOptions, TableDef } from "../toolbox";
1
+ import type { ScanInput, ScanOutput } from "@webiny/aws-sdk/client-dynamodb/index.js";
2
+ import type { Entity, ScanOptions, TableDef } from "../toolbox.js";
3
3
  import type { ExecuteWithRetryOptions } from "@webiny/utils";
4
4
  export type { ScanOptions, ScanInput, ScanOutput };
5
5
  export interface BaseScanParams {
package/utils/scan.js CHANGED
@@ -1,10 +1,4 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.scanWithCallback = exports.scan = void 0;
7
- var _utils = require("@webiny/utils");
1
+ import { executeWithRetry } from "@webiny/utils";
8
2
  const createNext = result => {
9
3
  if (!result?.LastEvaluatedKey || !result.next) {
10
4
  return undefined;
@@ -25,7 +19,7 @@ const convertResult = result => {
25
19
  requestId: result.$response?.requestId || ""
26
20
  };
27
21
  };
28
- const scan = async params => {
22
+ export const scan = async params => {
29
23
  const {
30
24
  options
31
25
  } = params;
@@ -39,15 +33,14 @@ const scan = async params => {
39
33
  }, params.params);
40
34
  return convertResult(result);
41
35
  };
42
- exports.scan = scan;
43
- const scanWithCallback = async (params, callback, options) => {
36
+ export const scanWithCallback = async (params, callback, options) => {
44
37
  // For backwards compatibility, we still allow for executing the scan without retries.
45
38
  const usingRetry = Boolean(options?.retry);
46
39
  const retryOptions = options?.retry === true ? {} : options?.retry;
47
40
  const executeScan = () => scan(params);
48
41
  const getInitialResult = () => {
49
42
  if (usingRetry) {
50
- return (0, _utils.executeWithRetry)(executeScan, retryOptions);
43
+ return executeWithRetry(executeScan, retryOptions);
51
44
  }
52
45
  return executeScan();
53
46
  };
@@ -67,7 +60,7 @@ const scanWithCallback = async (params, callback, options) => {
67
60
  const executeNext = () => result.next();
68
61
  const getNextResult = () => {
69
62
  if (usingRetry) {
70
- return (0, _utils.executeWithRetry)(executeNext, retryOptions);
63
+ return executeWithRetry(executeNext, retryOptions);
71
64
  }
72
65
  return executeNext();
73
66
  };
@@ -85,6 +78,5 @@ const scanWithCallback = async (params, callback, options) => {
85
78
  }
86
79
  }
87
80
  };
88
- exports.scanWithCallback = scanWithCallback;
89
81
 
90
82
  //# sourceMappingURL=scan.js.map
package/utils/scan.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["_utils","require","createNext","result","LastEvaluatedKey","next","undefined","response","convertResult","items","Items","count","Count","scannedCount","ScannedCount","lastEvaluatedKey","error","requestId","$response","scan","params","options","table","entity","Error","JSON","stringify","execute","exports","scanWithCallback","callback","usingRetry","Boolean","retry","retryOptions","executeScan","getInitialResult","executeWithRetry","length","callbackResult","mustBreak","executeNext","getNextResult"],"sources":["scan.ts"],"sourcesContent":["import type { ScanInput, ScanOutput } from \"@webiny/aws-sdk/client-dynamodb\";\nimport type { Entity, ScanOptions, TableDef } from \"~/toolbox\";\nimport type { ExecuteWithRetryOptions } from \"@webiny/utils\";\nimport { executeWithRetry } from \"@webiny/utils\";\n\nexport type { ScanOptions, ScanInput, ScanOutput };\n\nexport interface BaseScanParams {\n options?: ScanOptions;\n params?: Partial<ScanInput>;\n}\n\nexport interface ScanWithTable extends BaseScanParams {\n table: TableDef;\n entity?: never;\n}\n\nexport interface ScanWithEntity extends BaseScanParams {\n entity: Entity;\n table?: never;\n}\n\nexport type ScanParams = ScanWithTable | ScanWithEntity;\n\nexport interface ScanResponse<T = any> {\n items: T[];\n count?: number;\n scannedCount?: number;\n lastEvaluatedKey?: ScanOutput[\"LastEvaluatedKey\"];\n next?: () => Promise<ScanResponse<T>>;\n requestId: string;\n error: any;\n}\n\ninterface DdbScanResult<T> {\n Items?: T[];\n Count?: number;\n ScannedCount?: number;\n LastEvaluatedKey?: ScanOutput[\"LastEvaluatedKey\"];\n next?: () => Promise<DdbScanResult<T>>;\n error?: any;\n $response?: {\n requestId: string;\n };\n}\n\ntype NextCb<T> = () => Promise<ScanResponse<T>>;\n\nconst createNext = <T>(result: DdbScanResult<T>): NextCb<T> | undefined => {\n if (!result?.LastEvaluatedKey || !result.next) {\n return undefined;\n }\n return async () => {\n const response = await result!.next!();\n return convertResult(response);\n };\n};\n\nconst convertResult = <T>(result: DdbScanResult<T>): ScanResponse<T> => {\n return {\n items: result.Items || [],\n count: result.Count,\n scannedCount: result.ScannedCount,\n lastEvaluatedKey: result.LastEvaluatedKey || undefined,\n next: createNext<T>(result),\n error: result.error,\n requestId: result.$response?.requestId || \"\"\n };\n};\n\nexport type ScanDbItem<T> = T & {\n PK: string;\n SK: string;\n GSI1_PK: string;\n GSI1_SK: string;\n TYPE: string;\n};\n\nexport const scan = async <T>(params: ScanParams): Promise<ScanResponse<T>> => {\n const { options } = params;\n\n const table = params.table ? params.table : params.entity.table;\n if (!table) {\n throw new Error(`Missing table for scan: ${JSON.stringify(options)}`);\n }\n\n const result = await table.scan(\n {\n ...options,\n execute: true\n },\n params.params\n );\n\n return convertResult(result) as ScanResponse<T>;\n};\n\ninterface ScanWithCallbackOptions {\n retry?: true | ExecuteWithRetryOptions;\n}\n\nexport const scanWithCallback = async <T>(\n params: ScanParams,\n callback: (result: ScanResponse<ScanDbItem<T>>) => Promise<void | boolean>,\n options?: ScanWithCallbackOptions\n): Promise<void> => {\n // For backwards compatibility, we still allow for executing the scan without retries.\n const usingRetry = Boolean(options?.retry);\n const retryOptions = options?.retry === true ? {} : options?.retry;\n\n const executeScan = () => scan<ScanDbItem<T>>(params);\n const getInitialResult = () => {\n if (usingRetry) {\n return executeWithRetry(executeScan, retryOptions);\n }\n return executeScan();\n };\n\n let result = await getInitialResult();\n\n if (!result.items?.length && !result.lastEvaluatedKey) {\n return;\n }\n\n // If the result of the callback was `false`, that means the\n // user's intention was to stop further table scanning.\n const callbackResult = await callback(result);\n const mustBreak = callbackResult === false;\n if (mustBreak) {\n return;\n }\n\n while (result.next) {\n const executeNext = () => result.next!();\n const getNextResult = () => {\n if (usingRetry) {\n return executeWithRetry(executeNext, retryOptions);\n }\n return executeNext();\n };\n\n result = await getNextResult();\n\n // If the result of the callback was `false`, that means the\n // user's intention was to stop further table scanning.\n const callbackResult = await callback(result);\n const mustBreak = callbackResult === false;\n if (mustBreak) {\n break;\n }\n\n if (!result.next) {\n return;\n }\n }\n};\n"],"mappings":";;;;;;AAGA,IAAAA,MAAA,GAAAC,OAAA;AA6CA,MAAMC,UAAU,GAAOC,MAAwB,IAA4B;EACvE,IAAI,CAACA,MAAM,EAAEC,gBAAgB,IAAI,CAACD,MAAM,CAACE,IAAI,EAAE;IAC3C,OAAOC,SAAS;EACpB;EACA,OAAO,YAAY;IACf,MAAMC,QAAQ,GAAG,MAAMJ,MAAM,CAAEE,IAAI,CAAE,CAAC;IACtC,OAAOG,aAAa,CAACD,QAAQ,CAAC;EAClC,CAAC;AACL,CAAC;AAED,MAAMC,aAAa,GAAOL,MAAwB,IAAsB;EACpE,OAAO;IACHM,KAAK,EAAEN,MAAM,CAACO,KAAK,IAAI,EAAE;IACzBC,KAAK,EAAER,MAAM,CAACS,KAAK;IACnBC,YAAY,EAAEV,MAAM,CAACW,YAAY;IACjCC,gBAAgB,EAAEZ,MAAM,CAACC,gBAAgB,IAAIE,SAAS;IACtDD,IAAI,EAAEH,UAAU,CAAIC,MAAM,CAAC;IAC3Ba,KAAK,EAAEb,MAAM,CAACa,KAAK;IACnBC,SAAS,EAAEd,MAAM,CAACe,SAAS,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EAAE;IACnD;EACJ;;EAEA;EACA;EACA,MAAMwB,cAAc,GAAG,MAAMT,QAAQ,CAAC3B,MAAM,CAAC;EAC7C,MAAMqC,SAAS,GAAGD,cAAc,KAAK,KAAK;EAC1C,IAAIC,SAAS,EAAE;IACX;EACJ;EAEA,OAAOrC,MAAM,CAACE,IAAI,EAAE;IAChB,MAAMoC,WAAW,GAAGA,CAAA,KAAMtC,MAAM,CAACE,IAAI,CAAE,CAAC;IACxC,MAAMqC,aAAa,GAAGA,CAAA,KAAM;MACxB,IAAIX,UAAU,EAAE;QACZ,OAAO,IAAAM,uBAAgB,EAACI,WAAW,EAAEP,YAAY,CAAC;MACtD;MACA,OAAOO,WAAW,CAAC,CAAC;IACxB,CAAC;IAEDtC,MAAM,GAAG,MAAMuC,aAAa,CAAC,CAAC;;IAE9B;IACA;IACA,MAAMH,cAAc,GAAG,MAAMT,QAAQ,CAAC3B,MAAM,CAAC;IAC7C,MAAMqC,SAAS,GAAGD,cAAc,KAAK,KAAK;IAC1C,IAAIC,SAAS,EAAE;MACX;IACJ;IAEA,IAAI,CAACrC,MAAM,CAACE,IAAI,EAAE;MACd;IACJ;EACJ;AACJ,CAAC;AAACuB,OAAA,CAAAC,gBAAA,GAAAA,gBAAA","ignoreList":[]}
1
+ {"version":3,"names":["executeWithRetry","createNext","result","LastEvaluatedKey","next","undefined","response","convertResult","items","Items","count","Count","scannedCount","ScannedCount","lastEvaluatedKey","error","requestId","$response","scan","params","options","table","entity","Error","JSON","stringify","execute","scanWithCallback","callback","usingRetry","Boolean","retry","retryOptions","executeScan","getInitialResult","length","callbackResult","mustBreak","executeNext","getNextResult"],"sources":["scan.ts"],"sourcesContent":["import type { ScanInput, ScanOutput } from \"@webiny/aws-sdk/client-dynamodb/index.js\";\nimport type { Entity, ScanOptions, TableDef } from \"~/toolbox.js\";\nimport type { ExecuteWithRetryOptions } from \"@webiny/utils\";\nimport { executeWithRetry } from \"@webiny/utils\";\n\nexport type { ScanOptions, ScanInput, ScanOutput };\n\nexport interface BaseScanParams {\n options?: ScanOptions;\n params?: Partial<ScanInput>;\n}\n\nexport interface ScanWithTable extends BaseScanParams {\n table: TableDef;\n entity?: never;\n}\n\nexport interface ScanWithEntity extends BaseScanParams {\n entity: Entity;\n table?: never;\n}\n\nexport type ScanParams = ScanWithTable | ScanWithEntity;\n\nexport interface ScanResponse<T = any> {\n items: T[];\n count?: number;\n scannedCount?: number;\n lastEvaluatedKey?: ScanOutput[\"LastEvaluatedKey\"];\n next?: () => Promise<ScanResponse<T>>;\n requestId: string;\n error: any;\n}\n\ninterface DdbScanResult<T> {\n Items?: T[];\n Count?: number;\n ScannedCount?: number;\n LastEvaluatedKey?: ScanOutput[\"LastEvaluatedKey\"];\n next?: () => Promise<DdbScanResult<T>>;\n error?: any;\n $response?: {\n requestId: string;\n };\n}\n\ntype NextCb<T> = () => Promise<ScanResponse<T>>;\n\nconst createNext = <T>(result: DdbScanResult<T>): NextCb<T> | undefined => {\n if (!result?.LastEvaluatedKey || !result.next) {\n return undefined;\n }\n return async () => {\n const response = await result!.next!();\n return convertResult(response);\n };\n};\n\nconst convertResult = <T>(result: DdbScanResult<T>): ScanResponse<T> => {\n return {\n items: result.Items || [],\n count: result.Count,\n scannedCount: result.ScannedCount,\n lastEvaluatedKey: result.LastEvaluatedKey || undefined,\n next: createNext<T>(result),\n error: result.error,\n requestId: result.$response?.requestId || \"\"\n };\n};\n\nexport type ScanDbItem<T> = T & {\n PK: string;\n SK: string;\n GSI1_PK: string;\n GSI1_SK: string;\n TYPE: string;\n};\n\nexport const scan = async <T>(params: ScanParams): Promise<ScanResponse<T>> => {\n const { options } = params;\n\n const table = params.table ? params.table : params.entity.table;\n if (!table) {\n throw new Error(`Missing table for scan: ${JSON.stringify(options)}`);\n }\n\n const result = await table.scan(\n {\n ...options,\n execute: true\n },\n params.params\n );\n\n return convertResult(result) as ScanResponse<T>;\n};\n\ninterface ScanWithCallbackOptions {\n retry?: true | ExecuteWithRetryOptions;\n}\n\nexport const scanWithCallback = async <T>(\n params: ScanParams,\n callback: (result: ScanResponse<ScanDbItem<T>>) => Promise<void | boolean>,\n options?: ScanWithCallbackOptions\n): Promise<void> => {\n // For backwards compatibility, we still allow for executing the scan without retries.\n const usingRetry = Boolean(options?.retry);\n const retryOptions = options?.retry === true ? {} : options?.retry;\n\n const executeScan = () => scan<ScanDbItem<T>>(params);\n const getInitialResult = () => {\n if (usingRetry) {\n return executeWithRetry(executeScan, retryOptions);\n }\n return executeScan();\n };\n\n let result = await getInitialResult();\n\n if (!result.items?.length && !result.lastEvaluatedKey) {\n return;\n }\n\n // If the result of the callback was `false`, that means the\n // user's intention was to stop further table scanning.\n const callbackResult = await callback(result);\n const mustBreak = callbackResult === false;\n if (mustBreak) {\n return;\n }\n\n while (result.next) {\n const executeNext = () => result.next!();\n const getNextResult = () => {\n if (usingRetry) {\n return executeWithRetry(executeNext, retryOptions);\n }\n return executeNext();\n };\n\n result = await getNextResult();\n\n // If the result of the callback was `false`, that means the\n // user's intention was to stop further table scanning.\n const callbackResult = await callback(result);\n const mustBreak = callbackResult === false;\n if (mustBreak) {\n break;\n }\n\n if (!result.next) {\n return;\n }\n }\n};\n"],"mappings":"AAGA,SAASA,gBAAgB,QAAQ,eAAe;AA6ChD,MAAMC,UAAU,GAAOC,MAAwB,IAA4B;EACvE,IAAI,CAACA,MAAM,EAAEC,gBAAgB,IAAI,CAACD,MAAM,CAACE,IAAI,EAAE;IAC3C,OAAOC,SAAS;EACpB;EACA,OAAO,YAAY;IACf,MAAMC,QAAQ,GAAG,MAAMJ,MAAM,CAAEE,IAAI,CAAE,CAAC;IACtC,OAAOG,aAAa,CAACD,QAAQ,CAAC;EAClC,CAAC;AACL,CAAC;AAED,MAAMC,aAAa,GAAOL,MAAwB,IAAsB;EACpE,OAAO;IACHM,KAAK,EAAEN,MAAM,CAACO,KAAK,IAAI,EAAE;IACzBC,KAAK,EAAER,MAAM,CAACS,KAAK;IACnBC,YAAY,EAAEV,MAAM,CAACW,YA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C;EACxB,CAAC;EAED,IAAI/B,MAAM,GAAG,MAAMgC,gBAAgB,CAAC,CAAC;EAErC,IAAI,CAAChC,MAAM,CAACM,KAAK,EAAE2B,MAAM,IAAI,CAACjC,MAAM,CAACY,gBAAgB,EAAE;IACnD;EACJ;;EAEA;EACA;EACA,MAAMsB,cAAc,GAAG,MAAMR,QAAQ,CAAC1B,MAAM,CAAC;EAC7C,MAAMmC,SAAS,GAAGD,cAAc,KAAK,KAAK;EAC1C,IAAIC,SAAS,EAAE;IACX;EACJ;EAEA,OAAOnC,MAAM,CAACE,IAAI,EAAE;IAChB,MAAMkC,WAAW,GAAGA,CAAA,KAAMpC,MAAM,CAACE,IAAI,CAAE,CAAC;IACxC,MAAMmC,aAAa,GAAGA,CAAA,KAAM;MACxB,IAAIV,UAAU,EAAE;QACZ,OAAO7B,gBAAgB,CAACsC,WAAW,EAAEN,YAAY,CAAC;MACtD;MACA,OAAOM,WAAW,CAAC,CAAC;IACxB,CAAC;IAEDpC,MAAM,GAAG,MAAMqC,aAAa,CAAC,CAAC;;IAE9B;IACA;IACA,MAAMH,cAAc,GAAG,MAAMR,QAAQ,CAAC1B,MAAM,CAAC;IAC7C,MAAMmC,SAAS,GAAGD,cAAc,KAAK,KAAK;IAC1C,IAAIC,SAAS,EAAE;MACX;IACJ;IAEA,IAAI,CAACnC,MAAM,CAACE,IAAI,EAAE;MACd;IACJ;EACJ;AACJ,CAAC","ignoreList":[]}
package/utils/sort.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { FieldPlugin } from "../plugins/definitions/FieldPlugin";
1
+ import type { FieldPlugin } from "../plugins/definitions/FieldPlugin.js";
2
2
  interface Params<T> {
3
3
  /**
4
4
  * The items we are sorting.
package/utils/sort.js CHANGED
@@ -1,22 +1,15 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports.sortItems = sortItems;
8
- var _orderBy = _interopRequireDefault(require("lodash/orderBy"));
9
- var _error = _interopRequireDefault(require("@webiny/error"));
1
+ import lodashOrderBy from "lodash/orderBy.js";
2
+ import WebinyError from "@webiny/error";
10
3
  const extractSort = (sortBy, fields) => {
11
4
  const result = sortBy.split("_");
12
5
  if (result.length !== 2) {
13
- throw new _error.default("Problem in determining the sorting for the entry items.", "SORT_ERROR", {
6
+ throw new WebinyError("Problem in determining the sorting for the entry items.", "SORT_ERROR", {
14
7
  sortBy
15
8
  });
16
9
  }
17
10
  const [field, order] = result;
18
11
  if (!field) {
19
- throw new _error.default("Sorting field does not exist.", "SORTING_FIELD_ERROR", {
12
+ throw new WebinyError("Sorting field does not exist.", "SORTING_FIELD_ERROR", {
20
13
  field,
21
14
  order,
22
15
  fields
@@ -25,7 +18,7 @@ const extractSort = (sortBy, fields) => {
25
18
  const fieldPlugin = fields.find(f => f.getField() === field);
26
19
  const isSortable = fieldPlugin ? fieldPlugin.isSortable() : true;
27
20
  if (isSortable === false) {
28
- throw new _error.default(`Cannot sort by given field: "${field}".`, "UNSUPPORTED_SORT_ERROR", {
21
+ throw new WebinyError(`Cannot sort by given field: "${field}".`, "UNSUPPORTED_SORT_ERROR", {
29
22
  fields,
30
23
  field
31
24
  });
@@ -35,7 +28,7 @@ const extractSort = (sortBy, fields) => {
35
28
  reverse: order.toUpperCase() === "DESC"
36
29
  };
37
30
  };
38
- function sortItems(params) {
31
+ export function sortItems(params) {
39
32
  const {
40
33
  items,
41
34
  sort: initialSort = [],
@@ -70,7 +63,7 @@ function sortItems(params) {
70
63
  if (info.sorters.length === 0) {
71
64
  return items;
72
65
  }
73
- return (0, _orderBy.default)(items, info.sorters, info.orders);
66
+ return lodashOrderBy(items, info.sorters, info.orders);
74
67
  }
75
68
 
76
69
  //# sourceMappingURL=sort.js.map
package/utils/sort.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["_orderBy","_interopRequireDefault","require","_error","extractSort","sortBy","fields","result","split","length","WebinyError","field","order","fieldPlugin","find","f","getField","isSortable","reverse","toUpperCase","sortItems","params","items","sort","initialSort","Array","isArray","info","sorters","orders","path","getPath","push","lodashOrderBy"],"sources":["sort.ts"],"sourcesContent":["import lodashOrderBy from \"lodash/orderBy\";\nimport WebinyError from \"@webiny/error\";\nimport type { FieldPlugin } from \"~/plugins/definitions/FieldPlugin\";\n\ninterface Info {\n sorters: string[];\n orders: (boolean | \"asc\" | \"desc\")[];\n}\n\ninterface Response {\n reverse: boolean;\n field: string;\n}\n\nconst extractSort = (sortBy: string, fields: FieldPlugin[]): Response => {\n const result = sortBy.split(\"_\");\n if (result.length !== 2) {\n throw new WebinyError(\n \"Problem in determining the sorting for the entry items.\",\n \"SORT_ERROR\",\n {\n sortBy\n }\n );\n }\n const [field, order] = result;\n\n if (!field) {\n throw new WebinyError(\"Sorting field does not exist.\", \"SORTING_FIELD_ERROR\", {\n field,\n order,\n fields\n });\n }\n const fieldPlugin = fields.find(f => f.getField() === field);\n const isSortable = fieldPlugin ? fieldPlugin.isSortable() : true;\n if (isSortable === false) {\n throw new WebinyError(`Cannot sort by given field: \"${field}\".`, \"UNSUPPORTED_SORT_ERROR\", {\n fields,\n field\n });\n }\n\n return {\n field,\n reverse: order.toUpperCase() === \"DESC\"\n };\n};\n\ninterface Params<T> {\n /**\n * The items we are sorting.\n */\n items: T[];\n /**\n * Sort options. For example: [\"id_ASC\"]\n */\n sort?: string[];\n /**\n * Fields we can sort by.\n */\n fields?: FieldPlugin[];\n}\n\nexport function sortItems<T = any>(params: Params<T>): T[] {\n const { items, sort: initialSort = [], fields = [] } = params;\n /**\n * Skip sorting if nothing was passed to sort by or nothing to sort.\n */\n if (items.length <= 1 || Array.isArray(initialSort) === false || initialSort.length === 0) {\n return items;\n }\n\n const info: Info = {\n sorters: [],\n orders: []\n };\n\n for (const sort of initialSort) {\n /**\n * Possibly empty array item was passed.\n */\n if (!sort) {\n continue;\n }\n const { field, reverse } = extractSort(sort, fields);\n const fieldPlugin = fields.find(f => f.getField() === field);\n const path = fieldPlugin ? fieldPlugin.getPath() : field;\n\n info.sorters.push(path);\n info.orders.push(reverse === true ? \"desc\" : \"asc\");\n }\n\n if (info.sorters.length === 0) {\n return items;\n }\n\n return lodashOrderBy(items, info.sorters, info.orders);\n}\n"],"mappings":";;;;;;;AAAA,IAAAA,QAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,MAAA,GAAAF,sBAAA,CAAAC,OAAA;AAaA,MAAME,WAAW,GAAGA,CAACC,MAAc,EAAEC,MAAqB,KAAe;EACrE,MAAMC,MAAM,GAAGF,MAAM,CAACG,KAAK,CAAC,GAAG,CAAC;EAChC,IAAID,MAAM,CAACE,MAAM,KAAK,CAAC,EAAE;IACrB,MAAM,IAAIC,cAAW,CACjB,yDAAyD,EACzD,YAAY,EACZ;MACIL;IACJ,CACJ,CAAC;EACL;EACA,MAAM,CAACM,KAAK,EAAEC,KAAK,CAAC,GAAGL,MAAM;EAE7B,IAAI,CAACI,KAAK,EAAE;IACR,MAAM,IAAID,cAAW,CAAC,+BAA+B,EAAE,qBAAqB,EAAE;MAC1EC,KAAK;MACLC,KAAK;MACLN;IACJ,CAAC,CAAC;EACN;EACA,MAAMO,WAAW,GAAGP,MAAM,CAACQ,IAAI,CAACC,CAAC,IAAIA,CAAC,CAACC,QAAQ,CAAC,CAAC,KAAKL,KAAK,CAAC;EAC5D,MAAMM,UAAU,GAAGJ,WAAW,GAAGA,WAAW,CAACI,UAAU,CAAC,CAAC,GAAG,IAAI;EAChE,IAAIA,UAAU,KAAK,KAAK,EAAE;IACtB,MAAM,IAAIP,cAAW,CAAC,gCAAgCC,KAAK,IAAI,EAAE,wBAAwB,EAAE;MACvFL,MAAM;MACNK;IACJ,CAAC,CAAC;EACN;EAEA,OAAO;IACHA,KAAK;IACLO,OAAO,EAAEN,KAAK,CAACO,WAAW,CAAC,CAAC,KAAK;EACrC,CAAC;AACL,CAAC;AAiBM,SAASC,SAASA,CAAUC,MAAiB,EAAO;EACvD,MAAM;IAAEC,KAAK;IAAEC,IAAI,EAAEC,WAAW,GAAG,EAAE;IAAElB,MAAM,GAAG;EAAG,CAAC,GAAGe,MAAM;EAC7D;AACJ;AACA;EACI,IAAIC,KAAK,CAACb,MAAM,IAAI,CAAC,IAAIgB,KAAK,CAACC,OAAO,CAACF,WAAW,CAAC,KAAK,KAAK,IAAIA,WAAW,CAACf,MAAM,KAAK,CAAC,EAAE;IACvF,OAAOa,KAAK;EAChB;EAEA,MAAMK,IAAU,GAAG;IACfC,OAAO,EAAE,EAAE;IACXC,MAAM,EAAE;EACZ,CAAC;EAED,KAAK,MAAMN,IAAI,IAAIC,WAAW,EAAE;IAC5B;AACR;AACA;IACQ,IAAI,CAACD,IAAI,EAAE;MACP;IACJ;IACA,MAAM;MAAEZ,KAAK;MAAEO;IAAQ,CAAC,GAAGd,WAAW,CAACmB,IAAI,EAAEjB,MAAM,CAAC;IACpD,MAAMO,WAAW,GAAGP,MAAM,CAACQ,IAAI,CAACC,CAAC,IAAIA,CAAC,CAACC,QAAQ,CAAC,CAAC,KAAKL,KAAK,CAAC;IAC5D,MAAMmB,IAAI,GAAGjB,WAAW,GAAGA,WAAW,CAACkB,OAAO,CAAC,CAAC,GAAGpB,KAAK;IAExDgB,IAAI,CAACC,OAAO,CAACI,IAAI,CAACF,IAAI,CAAC;IACvBH,IAAI,CAACE,MAAM,CAACG,IAAI,CAACd,OAAO,KAAK,IAAI,GAAG,MAAM,GAAG,KAAK,CAAC;EACvD;EAEA,IAAIS,IAAI,CAACC,OAAO,CAACnB,MAAM,KAAK,CAAC,EAAE;IAC3B,OAAOa,KAAK;EAChB;EAEA,OAAO,IAAAW,gBAAa,EAACX,KAAK,EAAEK,IAAI,CAACC,OAAO,EAAED,IAAI,CAACE,MAAM,CAAC;AAC1D","ignoreList":[]}
1
+ {"version":3,"names":["lodashOrderBy","WebinyError","extractSort","sortBy","fields","result","split","length","field","order","fieldPlugin","find","f","getField","isSortable","reverse","toUpperCase","sortItems","params","items","sort","initialSort","Array","isArray","info","sorters","orders","path","getPath","push"],"sources":["sort.ts"],"sourcesContent":["import lodashOrderBy from \"lodash/orderBy.js\";\nimport WebinyError from \"@webiny/error\";\nimport type { FieldPlugin } from \"~/plugins/definitions/FieldPlugin.js\";\n\ninterface Info {\n sorters: string[];\n orders: (boolean | \"asc\" | \"desc\")[];\n}\n\ninterface Response {\n reverse: boolean;\n field: string;\n}\n\nconst extractSort = (sortBy: string, fields: FieldPlugin[]): Response => {\n const result = sortBy.split(\"_\");\n if (result.length !== 2) {\n throw new WebinyError(\n \"Problem in determining the sorting for the entry items.\",\n \"SORT_ERROR\",\n {\n sortBy\n }\n );\n }\n const [field, order] = result;\n\n if (!field) {\n throw new WebinyError(\"Sorting field does not exist.\", \"SORTING_FIELD_ERROR\", {\n field,\n order,\n fields\n });\n }\n const fieldPlugin = fields.find(f => f.getField() === field);\n const isSortable = fieldPlugin ? fieldPlugin.isSortable() : true;\n if (isSortable === false) {\n throw new WebinyError(`Cannot sort by given field: \"${field}\".`, \"UNSUPPORTED_SORT_ERROR\", {\n fields,\n field\n });\n }\n\n return {\n field,\n reverse: order.toUpperCase() === \"DESC\"\n };\n};\n\ninterface Params<T> {\n /**\n * The items we are sorting.\n */\n items: T[];\n /**\n * Sort options. For example: [\"id_ASC\"]\n */\n sort?: string[];\n /**\n * Fields we can sort by.\n */\n fields?: FieldPlugin[];\n}\n\nexport function sortItems<T = any>(params: Params<T>): T[] {\n const { items, sort: initialSort = [], fields = [] } = params;\n /**\n * Skip sorting if nothing was passed to sort by or nothing to sort.\n */\n if (items.length <= 1 || Array.isArray(initialSort) === false || initialSort.length === 0) {\n return items;\n }\n\n const info: Info = {\n sorters: [],\n orders: []\n };\n\n for (const sort of initialSort) {\n /**\n * Possibly empty array item was passed.\n */\n if (!sort) {\n continue;\n }\n const { field, reverse } = extractSort(sort, fields);\n const fieldPlugin = fields.find(f => f.getField() === field);\n const path = fieldPlugin ? fieldPlugin.getPath() : field;\n\n info.sorters.push(path);\n info.orders.push(reverse === true ? \"desc\" : \"asc\");\n }\n\n if (info.sorters.length === 0) {\n return items;\n }\n\n return lodashOrderBy(items, info.sorters, info.orders);\n}\n"],"mappings":"AAAA,OAAOA,aAAa,MAAM,mBAAmB;AAC7C,OAAOC,WAAW,MAAM,eAAe;AAavC,MAAMC,WAAW,GAAGA,CAACC,MAAc,EAAEC,MAAqB,KAAe;EACrE,MAAMC,MAAM,GAAGF,MAAM,CAACG,KAAK,CAAC,GAAG,CAAC;EAChC,IAAID,MAAM,CAACE,MAAM,KAAK,CAAC,EAAE;IACrB,MAAM,IAAIN,WAAW,CACjB,yDAAyD,EACzD,YAAY,EACZ;MACIE;IACJ,CACJ,CAAC;EACL;EACA,MAAM,CAACK,KAAK,EAAEC,KAAK,CAAC,GAAGJ,MAAM;EAE7B,IAAI,CAACG,KAAK,EAAE;IACR,MAAM,IAAIP,WAAW,CAAC,+BAA+B,EAAE,qBAAqB,EAAE;MAC1EO,KAAK;MACLC,KAAK;MACLL;IACJ,CAAC,CAAC;EACN;EACA,MAAMM,WAAW,GAAGN,MAAM,CAACO,IAAI,CAACC,CAAC,IAAIA,CAAC,CAACC,QAAQ,CAAC,CAAC,KAAKL,KAAK,CAAC;EAC5D,MAAMM,UAAU,GAAGJ,WAAW,GAAGA,WAAW,CAACI,UAAU,CAAC,CAAC,GAAG,IAAI;EAChE,IAAIA,UAAU,KAAK,KAAK,EAAE;IACtB,MAAM,IAAIb,WAAW,CAAC,gCAAgCO,KAAK,IAAI,EAAE,wBAAwB,EAAE;MACvFJ,MAAM;MACNI;IACJ,CAAC,CAAC;EACN;EAEA,OAAO;IACHA,KAAK;IACLO,OAAO,EAAEN,KAAK,CAACO,WAAW,CAAC,CAAC,KAAK;EACrC,CAAC;AACL,CAAC;AAiBD,OAAO,SAASC,SAASA,CAAUC,MAAiB,EAAO;EACvD,MAAM;IAAEC,KAAK;IAAEC,IAAI,EAAEC,WAAW,GAAG,EAAE;IAAEjB,MAAM,GAAG;EAAG,CAAC,GAAGc,MAAM;EAC7D;AACJ;AACA;EACI,IAAIC,KAAK,CAACZ,MAAM,IAAI,CAAC,IAAIe,KAAK,CAACC,OAAO,CAACF,WAAW,CAAC,KAAK,KAAK,IAAIA,WAAW,CAACd,MAAM,KAAK,CAAC,EAAE;IACvF,OAAOY,KAAK;EAChB;EAEA,MAAMK,IAAU,GAAG;IACfC,OAAO,EAAE,EAAE;IACXC,MAAM,EAAE;EACZ,CAAC;EAED,KAAK,MAAMN,IAAI,IAAIC,WAAW,EAAE;IAC5B;AACR;AACA;IACQ,IAAI,CAACD,IAAI,EAAE;MACP;IACJ;IACA,MAAM;MAAEZ,KAAK;MAAEO;IAAQ,CAAC,GAAGb,WAAW,CAACkB,IAAI,EAAEhB,MAAM,CAAC;IACpD,MAAMM,WAAW,GAAGN,MAAM,CAACO,IAAI,CAACC,CAAC,IAAIA,CAAC,CAACC,QAAQ,CAAC,CAAC,KAAKL,KAAK,CAAC;IAC5D,MAAMmB,IAAI,GAAGjB,WAAW,GAAGA,WAAW,CAACkB,OAAO,CAAC,CAAC,GAAGpB,KAAK;IAExDgB,IAAI,CAACC,OAAO,CAACI,IAAI,CAACF,IAAI,CAAC;IACvBH,IAAI,CAACE,MAAM,CAACG,IAAI,CAACd,OAAO,KAAK,IAAI,GAAG,MAAM,GAAG,KAAK,CAAC;EACvD;EAEA,IAAIS,IAAI,CAACC,OAAO,CAAClB,MAAM,KAAK,CAAC,EAAE;IAC3B,OAAOY,KAAK;EAChB;EAEA,OAAOnB,aAAa,CAACmB,KAAK,EAAEK,IAAI,CAACC,OAAO,EAAED,IAAI,CAACE,MAAM,CAAC;AAC1D","ignoreList":[]}
@@ -1,11 +1,10 @@
1
- import type { TableConstructor } from "../../toolbox";
2
- import { Table as BaseTable } from "../../toolbox";
3
- import type { ITable, ITableReadBatch, ITableScanParams, ITableScanResponse, ITableWriteBatch } from "./types";
4
- export declare class TableDefinition<Name extends string = string, PartitionKey extends string = string, SortKey extends string = string> implements ITable {
1
+ import type { TableConstructor } from "../../toolbox.js";
2
+ import { Table as BaseTable } from "../../toolbox.js";
3
+ import type { ITable, ITableReadBatch, ITableScanParams, ITableScanResponse, ITableWriteBatch } from "./types.js";
4
+ export declare class Table<Name extends string = string, PartitionKey extends string = string, SortKey extends string = string> implements ITable<Name, PartitionKey, SortKey> {
5
5
  readonly table: BaseTable<Name, PartitionKey, SortKey>;
6
6
  constructor(params: TableConstructor<Name, PartitionKey, SortKey>);
7
7
  createWriter(): ITableWriteBatch;
8
8
  createReader(): ITableReadBatch;
9
9
  scan<T>(params: ITableScanParams): Promise<ITableScanResponse<T>>;
10
10
  }
11
- export declare const defineTable: <Name extends string = string, PartitionKey extends string = string, SortKey extends string = string>(params: TableConstructor<Name, PartitionKey, SortKey>) => TableDefinition<Name, PartitionKey, SortKey>;
@@ -1,38 +1,27 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.defineTable = exports.TableDefinition = void 0;
7
- var _toolbox = require("../../toolbox");
8
- var _TableWriteBatch = require("./TableWriteBatch");
9
- var _TableReadBatch = require("./TableReadBatch");
10
- var _scan = require("../scan");
11
- class TableDefinition {
1
+ import { Table as BaseTable } from "../../toolbox.js";
2
+ import { createTableWriteBatch } from "./TableWriteBatch.js";
3
+ import { createTableReadBatch } from "./TableReadBatch.js";
4
+ import { scan } from "../scan.js";
5
+ export class Table {
12
6
  constructor(params) {
13
- this.table = new _toolbox.Table(params);
7
+ this.table = new BaseTable(params);
14
8
  }
15
9
  createWriter() {
16
- return (0, _TableWriteBatch.createTableWriteBatch)({
10
+ return createTableWriteBatch({
17
11
  table: this.table
18
12
  });
19
13
  }
20
14
  createReader() {
21
- return (0, _TableReadBatch.createTableReadBatch)({
15
+ return createTableReadBatch({
22
16
  table: this.table
23
17
  });
24
18
  }
25
19
  async scan(params) {
26
- return (0, _scan.scan)({
20
+ return scan({
27
21
  ...params,
28
22
  table: this.table
29
23
  });
30
24
  }
31
25
  }
32
- exports.TableDefinition = TableDefinition;
33
- const defineTable = params => {
34
- return new TableDefinition(params);
35
- };
36
- exports.defineTable = defineTable;
37
26
 
38
27
  //# sourceMappingURL=Table.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_toolbox","require","_TableWriteBatch","_TableReadBatch","_scan","TableDefinition","constructor","params","table","BaseTable","createWriter","createTableWriteBatch","createReader","createTableReadBatch","scan","exports","defineTable"],"sources":["Table.ts"],"sourcesContent":["import type { TableConstructor } from \"~/toolbox\";\nimport { Table as BaseTable } from \"~/toolbox\";\nimport type {\n ITable,\n ITableReadBatch,\n ITableScanParams,\n ITableScanResponse,\n ITableWriteBatch\n} from \"./types\";\nimport { createTableWriteBatch } from \"./TableWriteBatch\";\nimport { createTableReadBatch } from \"./TableReadBatch\";\nimport { scan } from \"../scan\";\n\nexport class TableDefinition<\n Name extends string = string,\n PartitionKey extends string = string,\n SortKey extends string = string\n> implements ITable\n{\n public readonly table: BaseTable<Name, PartitionKey, SortKey>;\n\n public constructor(params: TableConstructor<Name, PartitionKey, SortKey>) {\n this.table = new BaseTable(params);\n }\n\n public createWriter(): ITableWriteBatch {\n return createTableWriteBatch({\n table: this.table\n });\n }\n\n public createReader(): ITableReadBatch {\n return createTableReadBatch({\n table: this.table\n });\n }\n\n public async scan<T>(params: ITableScanParams): Promise<ITableScanResponse<T>> {\n return scan<T>({\n ...params,\n table: this.table\n });\n }\n}\n\nexport const defineTable = <\n Name extends string = string,\n PartitionKey extends string = string,\n SortKey extends string = string\n>(\n params: TableConstructor<Name, PartitionKey, SortKey>\n) => {\n return new TableDefinition<Name, PartitionKey, SortKey>(params);\n};\n"],"mappings":";;;;;;AACA,IAAAA,QAAA,GAAAC,OAAA;AAQA,IAAAC,gBAAA,GAAAD,OAAA;AACA,IAAAE,eAAA,GAAAF,OAAA;AACA,IAAAG,KAAA,GAAAH,OAAA;AAEO,MAAMI,eAAe,CAK5B;EAGWC,WAAWA,CAACC,MAAqD,EAAE;IACtE,IAAI,CAACC,KAAK,GAAG,IAAIC,cAAS,CAACF,MAAM,CAAC;EACtC;EAEOG,YAAYA,CAAA,EAAqB;IACpC,OAAO,IAAAC,sCAAqB,EAAC;MACzBH,KAAK,EAAE,IAAI,CAACA;IAChB,CAAC,CAAC;EACN;EAEOI,YAAYA,CAAA,EAAoB;IACnC,OAAO,IAAAC,oCAAoB,EAAC;MACxBL,KAAK,EAAE,IAAI,CAACA;IAChB,CAAC,CAAC;EACN;EAEA,MAAaM,IAAIA,CAAIP,MAAwB,EAAkC;IAC3E,OAAO,IAAAO,UAAI,EAAI;MACX,GAAGP,MAAM;MACTC,KAAK,EAAE,IAAI,CAACA;IAChB,CAAC,CAAC;EACN;AACJ;AAACO,OAAA,CAAAV,eAAA,GAAAA,eAAA;AAEM,MAAMW,WAAW,GAKpBT,MAAqD,IACpD;EACD,OAAO,IAAIF,eAAe,CAA8BE,MAAM,CAAC;AACnE,CAAC;AAACQ,OAAA,CAAAC,WAAA,GAAAA,WAAA","ignoreList":[]}
1
+ {"version":3,"names":["Table","BaseTable","createTableWriteBatch","createTableReadBatch","scan","constructor","params","table","createWriter","createReader"],"sources":["Table.ts"],"sourcesContent":["import type { TableConstructor } from \"~/toolbox.js\";\nimport { Table as BaseTable } from \"~/toolbox.js\";\nimport type {\n ITable,\n ITableReadBatch,\n ITableScanParams,\n ITableScanResponse,\n ITableWriteBatch\n} from \"./types.js\";\nimport { createTableWriteBatch } from \"./TableWriteBatch.js\";\nimport { createTableReadBatch } from \"./TableReadBatch.js\";\nimport { scan } from \"../scan.js\";\n\nexport class Table<\n Name extends string = string,\n PartitionKey extends string = string,\n SortKey extends string = string\n> implements ITable<Name, PartitionKey, SortKey>\n{\n public readonly table: BaseTable<Name, PartitionKey, SortKey>;\n\n public constructor(params: TableConstructor<Name, PartitionKey, SortKey>) {\n this.table = new BaseTable(params);\n }\n\n public createWriter(): ITableWriteBatch {\n return createTableWriteBatch({\n table: this.table\n });\n }\n\n public createReader(): ITableReadBatch {\n return createTableReadBatch({\n table: this.table\n });\n }\n\n public async scan<T>(params: ITableScanParams): Promise<ITableScanResponse<T>> {\n return scan<T>({\n ...params,\n table: this.table\n });\n }\n}\n"],"mappings":"AACA,SAASA,KAAK,IAAIC,SAAS;AAQ3B,SAASC,qBAAqB;AAC9B,SAASC,oBAAoB;AAC7B,SAASC,IAAI;AAEb,OAAO,MAAMJ,KAAK,CAKlB;EAGWK,WAAWA,CAACC,MAAqD,EAAE;IACtE,IAAI,CAACC,KAAK,GAAG,IAAIN,SAAS,CAACK,MAAM,CAAC;EACtC;EAEOE,YAAYA,CAAA,EAAqB;IACpC,OAAON,qBAAqB,CAAC;MACzBK,KAAK,EAAE,IAAI,CAACA;IAChB,CAAC,CAAC;EACN;EAEOE,YAAYA,CAAA,EAAoB;IACnC,OAAON,oBAAoB,CAAC;MACxBI,KAAK,EAAE,IAAI,CAACA;IAChB,CAAC,CAAC;EACN;EAEA,MAAaH,IAAIA,CAAIE,MAAwB,EAAkC;IAC3E,OAAOF,IAAI,CAAI;MACX,GAAGE,MAAM;MACTC,KAAK,EAAE,IAAI,CAACA;IAChB,CAAC,CAAC;EACN;AACJ","ignoreList":[]}
@@ -1,7 +1,7 @@
1
- import type { Entity, TableDef } from "../../toolbox";
2
- import type { IEntityReadBatchBuilderGetResponse } from "../entity/types";
3
- import type { GenericRecord } from "@webiny/api/types";
4
- import type { ITableReadBatch, ITableReadBatchKey } from "./types";
1
+ import type { Entity, TableDef } from "../../toolbox.js";
2
+ import type { IEntityReadBatchBuilderGetResponse } from "../../utils/entity/types.js";
3
+ import type { GenericRecord } from "@webiny/api/types.js";
4
+ import type { ITableReadBatch, ITableReadBatchKey } from "./types.js";
5
5
  export interface ITableReadBatchParams {
6
6
  table: TableDef;
7
7
  }
@@ -1,13 +1,7 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.createTableReadBatch = exports.TableReadBatch = void 0;
7
- var _batchRead = require("../batch/batchRead");
8
- var _EntityReadBatchBuilder = require("../entity/EntityReadBatchBuilder");
9
- var _error = require("@webiny/error");
10
- class TableReadBatch {
1
+ import { batchReadAll } from "../batch/batchRead.js";
2
+ import { createEntityReadBatchBuilder } from "../entity/EntityReadBatchBuilder.js";
3
+ import { WebinyError } from "@webiny/error";
4
+ export class TableReadBatch {
11
5
  _items = [];
12
6
  builders = new Map();
13
7
  constructor(params) {
@@ -27,7 +21,7 @@ class TableReadBatch {
27
21
  * We cannot read from two tables at the same time, so check for that.
28
22
  */
29
23
  if (this.table.name !== entity.table.name) {
30
- throw new _error.WebinyError(`Cannot read from two different tables at the same time.`);
24
+ throw new WebinyError(`Cannot read from two different tables at the same time.`);
31
25
  }
32
26
  this._items.push(builder.get(item));
33
27
  }
@@ -38,7 +32,7 @@ class TableReadBatch {
38
32
  }
39
33
  const items = Array.from(this._items);
40
34
  this._items.length = 0;
41
- return await (0, _batchRead.batchReadAll)({
35
+ return await batchReadAll({
42
36
  items,
43
37
  table: this.table
44
38
  });
@@ -48,15 +42,13 @@ class TableReadBatch {
48
42
  if (builder) {
49
43
  return builder;
50
44
  }
51
- const newBuilder = (0, _EntityReadBatchBuilder.createEntityReadBatchBuilder)(entity);
45
+ const newBuilder = createEntityReadBatchBuilder(entity);
52
46
  this.builders.set(entity.name, newBuilder);
53
47
  return newBuilder;
54
48
  }
55
49
  }
56
- exports.TableReadBatch = TableReadBatch;
57
- const createTableReadBatch = params => {
50
+ export const createTableReadBatch = params => {
58
51
  return new TableReadBatch(params);
59
52
  };
60
- exports.createTableReadBatch = createTableReadBatch;
61
53
 
62
54
  //# sourceMappingURL=TableReadBatch.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_batchRead","require","_EntityReadBatchBuilder","_error","TableReadBatch","_items","builders","Map","constructor","params","table","total","length","items","Array","from","get","entity","input","builder","getBuilder","isArray","item","name","WebinyError","push","execute","batchReadAll","newBuilder","createEntityReadBatchBuilder","set","exports","createTableReadBatch"],"sources":["TableReadBatch.ts"],"sourcesContent":["import type { Entity, TableDef } from \"~/toolbox\";\nimport type {\n IEntityReadBatchBuilder,\n IEntityReadBatchBuilderGetResponse\n} from \"~/utils/entity/types\";\nimport { batchReadAll } from \"~/utils/batch/batchRead\";\nimport { createEntityReadBatchBuilder } from \"~/utils/entity/EntityReadBatchBuilder\";\nimport type { GenericRecord } from \"@webiny/api/types\";\nimport { WebinyError } from \"@webiny/error\";\nimport type { ITableReadBatch, ITableReadBatchKey } from \"./types\";\n\nexport interface ITableReadBatchParams {\n table: TableDef;\n}\n\nexport class TableReadBatch implements ITableReadBatch {\n private readonly table: TableDef;\n\n private readonly _items: IEntityReadBatchBuilderGetResponse[] = [];\n private readonly builders: Map<string, IEntityReadBatchBuilder> = new Map();\n\n public constructor(params: ITableReadBatchParams) {\n this.table = params.table;\n }\n\n public get total(): number {\n return this._items.length;\n }\n\n public get items(): IEntityReadBatchBuilderGetResponse[] {\n return Array.from(this._items);\n }\n\n public get(entity: Entity, input: ITableReadBatchKey): void {\n const builder = this.getBuilder(entity);\n\n const items = Array.isArray(input) ? input : [input];\n for (const item of items) {\n /**\n * We cannot read from two tables at the same time, so check for that.\n */\n if (this.table.name !== entity.table!.name) {\n throw new WebinyError(`Cannot read from two different tables at the same time.`);\n }\n\n this._items.push(builder.get(item));\n }\n }\n\n public async execute<T = GenericRecord>(): Promise<T[]> {\n if (this._items.length === 0) {\n return [];\n }\n const items = Array.from(this._items);\n this._items.length = 0;\n return await batchReadAll<T>({\n items,\n table: this.table\n });\n }\n\n private getBuilder(entity: Entity): IEntityReadBatchBuilder {\n const builder = this.builders.get(entity.name);\n if (builder) {\n return builder;\n }\n const newBuilder = createEntityReadBatchBuilder(entity);\n this.builders.set(entity.name, newBuilder);\n return newBuilder;\n }\n}\n\nexport const createTableReadBatch = (params: ITableReadBatchParams): ITableReadBatch => {\n return new TableReadBatch(params);\n};\n"],"mappings":";;;;;;AAKA,IAAAA,UAAA,GAAAC,OAAA;AACA,IAAAC,uBAAA,GAAA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EAAE,IAAI,CAACA;IAChB,CAAC,CAAC;EACN;EAEQU,UAAUA,CAACH,MAAc,EAA2B;IACxD,MAAME,OAAO,GAAG,IAAI,CAACb,QAAQ,CAACU,GAAG,CAACC,MAAM,CAACM,IAAI,CAAC;IAC9C,IAAIJ,OAAO,EAAE;MACT,OAAOA,OAAO;IAClB;IACA,MAAMS,UAAU,GAAG,IAAAC,oDAA4B,EAACZ,MAAM,CAAC;IACvD,IAAI,CAACX,QAAQ,CAACwB,GAAG,CAACb,MAAM,CAACM,IAAI,EAAEK,UAAU,CAAC;IAC1C,OAAOA,UAAU;EACrB;AACJ;AAACG,OAAA,CAAA3B,cAAA,GAAAA,cAAA;AAEM,MAAM4B,oBAAoB,GAAIvB,MAA6B,IAAsB;EACpF,OAAO,IAAIL,cAAc,CAACK,MAAM,CAAC;AACrC,CAAC;AAACsB,OAAA,CAAAC,oBAAA,GAAAA,oBAAA","ignoreList":[]}
1
+ {"version":3,"names":["batchReadAll","createEntityReadBatchBuilder","WebinyError","TableReadBatch","_items","builders","Map","constructor","params","table","total","length","items","Array","from","get","entity","input","builder","getBuilder","isArray","item","name","push","execute","newBuilder","set","createTableReadBatch"],"sources":["TableReadBatch.ts"],"sourcesContent":["import type { Entity, TableDef } from \"~/toolbox.js\";\nimport type {\n IEntityReadBatchBuilder,\n IEntityReadBatchBuilderGetResponse\n} from \"~/utils/entity/types.js\";\nimport { batchReadAll } from \"~/utils/batch/batchRead.js\";\nimport { createEntityReadBatchBuilder } from \"~/utils/entity/EntityReadBatchBuilder.js\";\nimport type { GenericRecord } from \"@webiny/api/types.js\";\nimport { WebinyError } from \"@webiny/error\";\nimport type { ITableReadBatch, ITableReadBatchKey } from \"./types.js\";\n\nexport interface ITableReadBatchParams {\n table: TableDef;\n}\n\nexport class TableReadBatch implements ITableReadBatch {\n private readonly table: TableDef;\n\n private readonly _items: IEntityReadBatchBuilderGetResponse[] = [];\n private readonly builders: Map<string, IEntityReadBatchBuilder> = new Map();\n\n public constructor(params: ITableReadBatchParams) {\n this.table = params.table;\n }\n\n public get total(): number {\n return this._items.length;\n }\n\n public get items(): IEntityReadBatchBuilderGetResponse[] {\n return Array.from(this._items);\n }\n\n public get(entity: Entity, input: ITableReadBatchKey): void {\n const builder = this.getBuilder(entity);\n\n const items = Array.isArray(input) ? input : [input];\n for (const item of items) {\n /**\n * We cannot read from two tables at the same time, so check for that.\n */\n if (this.table.name !== entity.table!.name) {\n throw new WebinyError(`Cannot read from two different tables at the same time.`);\n }\n\n this._items.push(builder.get(item));\n }\n }\n\n public async execute<T = GenericRecord>(): Promise<T[]> {\n if (this._items.length === 0) {\n return [];\n }\n const items = Array.from(this._items);\n this._items.length = 0;\n return await batchReadAll<T>({\n items,\n table: this.table\n });\n }\n\n private getBuilder(entity: Entity): IEntityReadBatchBuilder {\n const builder = this.builders.get(entity.name);\n if (builder) {\n return builder;\n }\n const newBuilder = createEntityReadBatchBuilder(entity);\n this.builders.set(entity.name, newBuilder);\n return newBuilder;\n }\n}\n\nexport const createTableReadBatch = (params: ITableReadBatchParams): ITableReadBatch => {\n return new TableReadBatch(params);\n};\n"],"mappings":"AAKA,SAASA,YAAY;AACrB,SAASC,4BAA4B;AAErC,SAASC,WAAW,QAAQ,eAAe;AAO3C,OAAO,MAAMC,cAAc,CAA4B;EAGlCC,MAAM,GAAyC,EAAE;EACjDC,QAAQ,GAAyC,IAAIC,GAAG,CAAC,CAAC;EAEpEC,WAAWA,CAACC,MAA6B,EAAE;IAC9C,IAAI,CAACC,KAAK,GAAGD,MAAM,CAACC,KAAK;EAC7B;EAEA,IAAWC,KAAKA,CAAA,EAAW;IACvB,OAAO,IAAI,CAACN,MAAM,CAACO,MAAM;EAC7B;EAEA,IAAWC,KAAKA,CAAA,EAAyC;IACrD,OAAOC,KAAK,CAACC,IAAI,CAAC,IAAI,CAACV,MAAM,CAAC;EAClC;EAEOW,GAAGA,CAACC,MAAc,EAAEC,KAAyB,EAAQ;IACxD,MAAMC,OAAO,GAAG,IAAI,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1,6 @@
1
- import type { Entity, TableDef } from "../../toolbox";
2
- import type { BatchWriteItem, BatchWriteResult, IDeleteBatchItem, IPutBatchItem } from "../batch/types";
3
- import type { ITableWriteBatch } from "./types";
1
+ import type { Entity, TableDef } from "../../toolbox.js";
2
+ import type { BatchWriteItem, BatchWriteResult, IDeleteBatchItem, IPutBatchItem } from "../../utils/batch/types.js";
3
+ import type { ITableWriteBatch } from "./types.js";
4
4
  export interface ITableWriteBatchParams {
5
5
  table: TableDef;
6
6
  items?: BatchWriteItem[];