@webiny/db-dynamodb 0.0.0-unstable.6f45466a1d → 0.0.0-unstable.7be00a75a9

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 (215) hide show
  1. package/DynamoDbDriver.js +204 -209
  2. package/DynamoDbDriver.js.map +1 -1
  3. package/exports/api/db.d.ts +2 -0
  4. package/exports/api/db.js +2 -0
  5. package/feature/FilterUtil/FilterUtil.d.ts +11 -0
  6. package/feature/FilterUtil/FilterUtil.js +26 -0
  7. package/feature/FilterUtil/FilterUtil.js.map +1 -0
  8. package/feature/FilterUtil/abstractions/FilterUtil.d.ts +17 -0
  9. package/feature/FilterUtil/abstractions/FilterUtil.js +5 -0
  10. package/feature/FilterUtil/abstractions/FilterUtil.js.map +1 -0
  11. package/feature/FilterUtil/createFilters.d.ts +22 -0
  12. package/feature/FilterUtil/createFilters.js +92 -0
  13. package/feature/FilterUtil/createFilters.js.map +1 -0
  14. package/feature/FilterUtil/extractWhereArgs.d.ts +7 -0
  15. package/feature/FilterUtil/extractWhereArgs.js +20 -0
  16. package/feature/FilterUtil/extractWhereArgs.js.map +1 -0
  17. package/feature/FilterUtil/feature.d.ts +4 -0
  18. package/feature/FilterUtil/feature.js +11 -0
  19. package/feature/FilterUtil/feature.js.map +1 -0
  20. package/feature/FilterUtil/index.d.ts +2 -0
  21. package/feature/FilterUtil/index.js +2 -0
  22. package/feature/ValueFilter/ValueFilterRegistry.d.ts +12 -0
  23. package/feature/ValueFilter/ValueFilterRegistry.js +27 -0
  24. package/feature/ValueFilter/ValueFilterRegistry.js.map +1 -0
  25. package/feature/ValueFilter/abstractions/ValueFilter.d.ts +22 -0
  26. package/feature/ValueFilter/abstractions/ValueFilter.js +5 -0
  27. package/feature/ValueFilter/abstractions/ValueFilter.js.map +1 -0
  28. package/feature/ValueFilter/abstractions/ValueFilterRegistry.d.ts +10 -0
  29. package/feature/ValueFilter/abstractions/ValueFilterRegistry.js +5 -0
  30. package/feature/ValueFilter/abstractions/ValueFilterRegistry.js.map +1 -0
  31. package/feature/ValueFilter/feature.d.ts +4 -0
  32. package/feature/ValueFilter/feature.js +33 -0
  33. package/feature/ValueFilter/feature.js.map +1 -0
  34. package/feature/ValueFilter/filters/AndInFilter.d.ts +11 -0
  35. package/feature/ValueFilter/filters/AndInFilter.js +28 -0
  36. package/feature/ValueFilter/filters/AndInFilter.js.map +1 -0
  37. package/feature/ValueFilter/filters/BetweenFilter.d.ts +11 -0
  38. package/feature/ValueFilter/filters/BetweenFilter.js +31 -0
  39. package/feature/ValueFilter/filters/BetweenFilter.js.map +1 -0
  40. package/feature/ValueFilter/filters/ContainsFilter.d.ts +11 -0
  41. package/feature/ValueFilter/filters/ContainsFilter.js +42 -0
  42. package/feature/ValueFilter/filters/ContainsFilter.js.map +1 -0
  43. package/feature/ValueFilter/filters/EqFilter.d.ts +11 -0
  44. package/feature/ValueFilter/filters/EqFilter.js +24 -0
  45. package/feature/ValueFilter/filters/EqFilter.js.map +1 -0
  46. package/feature/ValueFilter/filters/FuzzyFilter.d.ts +11 -0
  47. package/feature/ValueFilter/filters/FuzzyFilter.js +37 -0
  48. package/feature/ValueFilter/filters/FuzzyFilter.js.map +1 -0
  49. package/feature/ValueFilter/filters/GtFilter.d.ts +11 -0
  50. package/feature/ValueFilter/filters/GtFilter.js +22 -0
  51. package/feature/ValueFilter/filters/GtFilter.js.map +1 -0
  52. package/feature/ValueFilter/filters/GteFilter.d.ts +11 -0
  53. package/feature/ValueFilter/filters/GteFilter.js +22 -0
  54. package/feature/ValueFilter/filters/GteFilter.js.map +1 -0
  55. package/feature/ValueFilter/filters/InFilter.d.ts +11 -0
  56. package/feature/ValueFilter/filters/InFilter.js +28 -0
  57. package/feature/ValueFilter/filters/InFilter.js.map +1 -0
  58. package/feature/ValueFilter/filters/LtFilter.d.ts +11 -0
  59. package/feature/ValueFilter/filters/LtFilter.js +22 -0
  60. package/feature/ValueFilter/filters/LtFilter.js.map +1 -0
  61. package/feature/ValueFilter/filters/LteFilter.d.ts +11 -0
  62. package/feature/ValueFilter/filters/LteFilter.js +22 -0
  63. package/feature/ValueFilter/filters/LteFilter.js.map +1 -0
  64. package/feature/ValueFilter/filters/StartsWithFilter.d.ts +11 -0
  65. package/feature/ValueFilter/filters/StartsWithFilter.js +29 -0
  66. package/feature/ValueFilter/filters/StartsWithFilter.js.map +1 -0
  67. package/feature/ValueFilter/index.d.ts +3 -0
  68. package/feature/ValueFilter/index.js +3 -0
  69. package/index.d.ts +1 -0
  70. package/index.js +9 -2
  71. package/index.js.map +1 -1
  72. package/package.json +28 -20
  73. package/plugins/definitions/DateTimeTransformPlugin.js +18 -27
  74. package/plugins/definitions/DateTimeTransformPlugin.js.map +1 -1
  75. package/plugins/definitions/FieldPlugin.js +34 -37
  76. package/plugins/definitions/FieldPlugin.js.map +1 -1
  77. package/plugins/definitions/TimeTransformPlugin.js +25 -37
  78. package/plugins/definitions/TimeTransformPlugin.js.map +1 -1
  79. package/plugins/definitions/ValueTransformPlugin.d.ts +1 -6
  80. package/plugins/definitions/ValueTransformPlugin.js +17 -22
  81. package/plugins/definitions/ValueTransformPlugin.js.map +1 -1
  82. package/plugins/definitions/assignFields.js +14 -18
  83. package/plugins/definitions/assignFields.js.map +1 -1
  84. package/plugins/index.d.ts +0 -5
  85. package/plugins/index.js +0 -12
  86. package/store/entity.js +6 -9
  87. package/store/entity.js.map +1 -1
  88. package/store/keys.js +6 -13
  89. package/store/keys.js.map +1 -1
  90. package/store/types.js +0 -3
  91. package/toolbox.d.ts +53 -1
  92. package/toolbox.js +0 -4
  93. package/types.js +0 -3
  94. package/utils/batch/batchRead.js +37 -56
  95. package/utils/batch/batchRead.js.map +1 -1
  96. package/utils/batch/batchWrite.js +33 -51
  97. package/utils/batch/batchWrite.js.map +1 -1
  98. package/utils/batch/index.js +0 -2
  99. package/utils/batch/types.js +0 -3
  100. package/utils/cleanup.js +35 -25
  101. package/utils/cleanup.js.map +1 -1
  102. package/utils/count.js +8 -13
  103. package/utils/count.js.map +1 -1
  104. package/utils/createEntity.d.ts +1 -1
  105. package/utils/createEntity.js +75 -78
  106. package/utils/createEntity.js.map +1 -1
  107. package/utils/createTable.js +25 -35
  108. package/utils/createTable.js.map +1 -1
  109. package/utils/cursor.js +7 -10
  110. package/utils/cursor.js.map +1 -1
  111. package/utils/delete.js +6 -8
  112. package/utils/delete.js.map +1 -1
  113. package/utils/entity/Entity.d.ts +1 -1
  114. package/utils/entity/Entity.js +85 -89
  115. package/utils/entity/Entity.js.map +1 -1
  116. package/utils/entity/EntityReadBatch.js +23 -31
  117. package/utils/entity/EntityReadBatch.js.map +1 -1
  118. package/utils/entity/EntityReadBatchBuilder.js +12 -15
  119. package/utils/entity/EntityReadBatchBuilder.js.map +1 -1
  120. package/utils/entity/EntityWriteBatch.js +34 -41
  121. package/utils/entity/EntityWriteBatch.js.map +1 -1
  122. package/utils/entity/EntityWriteBatchBuilder.js +15 -16
  123. package/utils/entity/EntityWriteBatchBuilder.js.map +1 -1
  124. package/utils/entity/getEntity.js +8 -9
  125. package/utils/entity/getEntity.js.map +1 -1
  126. package/utils/entity/index.js +0 -2
  127. package/utils/entity/types.js +0 -4
  128. package/utils/get.js +12 -26
  129. package/utils/get.js.map +1 -1
  130. package/utils/index.d.ts +1 -5
  131. package/utils/index.js +1 -7
  132. package/utils/put.js +7 -9
  133. package/utils/put.js.map +1 -1
  134. package/utils/query.js +77 -128
  135. package/utils/query.js.map +1 -1
  136. package/utils/scan.js +47 -73
  137. package/utils/scan.js.map +1 -1
  138. package/utils/sort.js +37 -61
  139. package/utils/sort.js.map +1 -1
  140. package/utils/table/Table.js +22 -21
  141. package/utils/table/Table.js.map +1 -1
  142. package/utils/table/TableReadBatch.js +37 -45
  143. package/utils/table/TableReadBatch.js.map +1 -1
  144. package/utils/table/TableWriteBatch.js +47 -56
  145. package/utils/table/TableWriteBatch.js.map +1 -1
  146. package/utils/table/index.js +0 -2
  147. package/utils/table/types.js +0 -3
  148. package/plugins/definitions/AttributePlugin.d.ts +0 -18
  149. package/plugins/definitions/AttributePlugin.js +0 -38
  150. package/plugins/definitions/AttributePlugin.js.map +0 -1
  151. package/plugins/definitions/FieldPathPlugin.d.ts +0 -22
  152. package/plugins/definitions/FieldPathPlugin.js +0 -26
  153. package/plugins/definitions/FieldPathPlugin.js.map +0 -1
  154. package/plugins/definitions/ValueFilterPlugin.d.ts +0 -22
  155. package/plugins/definitions/ValueFilterPlugin.js +0 -35
  156. package/plugins/definitions/ValueFilterPlugin.js.map +0 -1
  157. package/plugins/filters/andIn.d.ts +0 -3
  158. package/plugins/filters/andIn.js +0 -24
  159. package/plugins/filters/andIn.js.map +0 -1
  160. package/plugins/filters/between.d.ts +0 -3
  161. package/plugins/filters/between.js +0 -25
  162. package/plugins/filters/between.js.map +0 -1
  163. package/plugins/filters/contains.d.ts +0 -3
  164. package/plugins/filters/contains.js +0 -53
  165. package/plugins/filters/contains.js.map +0 -1
  166. package/plugins/filters/eq.d.ts +0 -3
  167. package/plugins/filters/eq.js +0 -26
  168. package/plugins/filters/eq.js.map +0 -1
  169. package/plugins/filters/fuzzy.d.ts +0 -3
  170. package/plugins/filters/fuzzy.js +0 -29
  171. package/plugins/filters/fuzzy.js.map +0 -1
  172. package/plugins/filters/gt.d.ts +0 -3
  173. package/plugins/filters/gt.js +0 -14
  174. package/plugins/filters/gt.js.map +0 -1
  175. package/plugins/filters/gte.d.ts +0 -3
  176. package/plugins/filters/gte.js +0 -14
  177. package/plugins/filters/gte.js.map +0 -1
  178. package/plugins/filters/in.d.ts +0 -3
  179. package/plugins/filters/in.js +0 -24
  180. package/plugins/filters/in.js.map +0 -1
  181. package/plugins/filters/index.d.ts +0 -2
  182. package/plugins/filters/index.js +0 -14
  183. package/plugins/filters/index.js.map +0 -1
  184. package/plugins/filters/lt.d.ts +0 -3
  185. package/plugins/filters/lt.js +0 -14
  186. package/plugins/filters/lt.js.map +0 -1
  187. package/plugins/filters/lte.d.ts +0 -3
  188. package/plugins/filters/lte.js +0 -14
  189. package/plugins/filters/lte.js.map +0 -1
  190. package/plugins/filters/startsWith.d.ts +0 -3
  191. package/plugins/filters/startsWith.js +0 -34
  192. package/plugins/filters/startsWith.js.map +0 -1
  193. package/plugins/index.js.map +0 -1
  194. package/store/types.js.map +0 -1
  195. package/toolbox.js.map +0 -1
  196. package/types.js.map +0 -1
  197. package/utils/attributes.d.ts +0 -10
  198. package/utils/attributes.js +0 -19
  199. package/utils/attributes.js.map +0 -1
  200. package/utils/batch/index.js.map +0 -1
  201. package/utils/batch/types.js.map +0 -1
  202. package/utils/entity/index.js.map +0 -1
  203. package/utils/entity/types.js.map +0 -1
  204. package/utils/filter.d.ts +0 -12
  205. package/utils/filter.js +0 -174
  206. package/utils/filter.js.map +0 -1
  207. package/utils/index.js.map +0 -1
  208. package/utils/listResponse.d.ts +0 -13
  209. package/utils/listResponse.js +0 -25
  210. package/utils/listResponse.js.map +0 -1
  211. package/utils/table/index.js.map +0 -1
  212. package/utils/table/types.js.map +0 -1
  213. package/utils/update.d.ts +0 -12
  214. package/utils/update.js +0 -12
  215. package/utils/update.js.map +0 -1
package/utils/get.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["cleanupItem","get","params","entity","keys","result","execute","Item","getClean"],"sources":["get.ts"],"sourcesContent":["import type { Entity } from \"~/toolbox.js\";\nimport { cleanupItem } from \"~/utils/cleanup.js\";\n\nexport interface GetRecordParamsKeys {\n PK: string;\n SK: string;\n}\n\nexport interface GetRecordParams {\n entity: Entity;\n keys: GetRecordParamsKeys;\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 execute: true\n });\n\n if (!result?.Item) {\n return null;\n }\n return result.Item as T;\n};\n\nexport const getClean = async <T>(params: GetRecordParams): Promise<T | null> => {\n const result = await get<T>(params);\n if (!result) {\n return null;\n }\n return cleanupItem<T>(params.entity, result);\n};\n"],"mappings":"AACA,SAASA,WAAW;AAYpB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,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,EAAE;IAClCE,OAAO,EAAE;EACb,CAAC,CAAC;EAEF,IAAI,CAACD,MAAM,EAAEE,IAAI,EAAE;IACf,OAAO,IAAI;EACf;EACA,OAAOF,MAAM,CAACE,IAAI;AACtB,CAAC;AAED,OAAO,MAAMC,QAAQ,GAAG,MAAUN,MAAuB,IAAwB;EAC7E,MAAMG,MAAM,GAAG,MAAMJ,GAAG,CAAIC,MAAM,CAAC;EACnC,IAAI,CAACG,MAAM,EAAE;IACT,OAAO,IAAI;EACf;EACA,OAAOL,WAAW,CAAIE,MAAM,CAACC,MAAM,EAAEE,MAAM,CAAC;AAChD,CAAC","ignoreList":[]}
1
+ {"version":3,"file":"utils/get.js","sources":["../../src/utils/get.ts"],"sourcesContent":["import type { Entity } from \"~/toolbox.js\";\nimport { cleanupItem } from \"~/utils/cleanup.js\";\n\nexport interface GetRecordParamsKeys {\n PK: string;\n SK: string;\n}\n\nexport interface GetRecordParams {\n entity: Entity;\n keys: GetRecordParamsKeys;\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 execute: true\n });\n\n if (!result?.Item) {\n return null;\n }\n return result.Item as T;\n};\n\nexport const getClean = async <T>(params: GetRecordParams): Promise<T | null> => {\n const result = await get<T>(params);\n if (!result) {\n return null;\n }\n return cleanupItem<T>(params.entity, result);\n};\n"],"names":["get","params","entity","keys","result","getClean","cleanupItem"],"mappings":";AAqBO,MAAMA,MAAM,OAAUC;IACzB,MAAM,EAAEC,MAAM,EAAEC,IAAI,EAAE,GAAGF;IAEzB,MAAMG,SAAS,MAAMF,OAAO,GAAG,CAACC,MAAM;QAClC,SAAS;IACb;IAEA,IAAI,CAACC,QAAQ,MACT,OAAO;IAEX,OAAOA,OAAO,IAAI;AACtB;AAEO,MAAMC,WAAW,OAAUJ;IAC9B,MAAMG,SAAS,MAAMJ,IAAOC;IAC5B,IAAI,CAACG,QACD,OAAO;IAEX,OAAOE,YAAeL,OAAO,MAAM,EAAEG;AACzC"}
package/utils/index.d.ts CHANGED
@@ -1,17 +1,13 @@
1
1
  export * from "./cleanup.js";
2
2
  export * from "./createEntity.js";
3
3
  export * from "./createTable.js";
4
- export * from "./cursor.js";
5
- export * from "./filter.js";
4
+ export { decodeCursor, encodeCursor } from "./cursor.js";
6
5
  export * from "./get.js";
7
6
  export * from "./delete.js";
8
7
  export * from "./put.js";
9
- export * from "./listResponse.js";
10
8
  export * from "./query.js";
11
- export * from "./count.js";
12
9
  export * from "./scan.js";
13
10
  export * from "./sort.js";
14
- export * from "./update.js";
15
11
  export * from "./batch/index.js";
16
12
  export * from "./entity/index.js";
17
13
  export * from "./table/index.js";
package/utils/index.js CHANGED
@@ -1,19 +1,13 @@
1
1
  export * from "./cleanup.js";
2
2
  export * from "./createEntity.js";
3
3
  export * from "./createTable.js";
4
- export * from "./cursor.js";
5
- export * from "./filter.js";
6
4
  export * from "./get.js";
7
5
  export * from "./delete.js";
8
6
  export * from "./put.js";
9
- export * from "./listResponse.js";
10
7
  export * from "./query.js";
11
- export * from "./count.js";
12
8
  export * from "./scan.js";
13
9
  export * from "./sort.js";
14
- export * from "./update.js";
15
10
  export * from "./batch/index.js";
16
11
  export * from "./entity/index.js";
17
12
  export * from "./table/index.js";
18
-
19
- //# sourceMappingURL=index.js.map
13
+ export { decodeCursor, encodeCursor } from "./cursor.js";
package/utils/put.js CHANGED
@@ -1,12 +1,10 @@
1
- export const put = async params => {
2
- const {
3
- entity,
4
- item
5
- } = params;
6
- return await entity.put(item, {
7
- execute: true,
8
- strictSchemaCheck: false
9
- });
1
+ const put = async (params)=>{
2
+ const { entity, item } = params;
3
+ return await entity.put(item, {
4
+ execute: true,
5
+ strictSchemaCheck: false
6
+ });
10
7
  };
8
+ export { put };
11
9
 
12
10
  //# sourceMappingURL=put.js.map
package/utils/put.js.map CHANGED
@@ -1 +1 @@
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":[]}
1
+ {"version":3,"file":"utils/put.js","sources":["../../src/utils/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"],"names":["put","params","entity","item"],"mappings":"AAcO,MAAMA,MAAM,OAAgDC;IAC/D,MAAM,EAAEC,MAAM,EAAEC,IAAI,EAAE,GAAGF;IAEzB,OAAO,MAAMC,OAAO,GAAG,CAACC,MAAM;QAC1B,SAAS;QACT,mBAAmB;IACvB;AACJ"}
package/utils/query.js CHANGED
@@ -1,146 +1,95 @@
1
- import WebinyError from "@webiny/error";
1
+ import error from "@webiny/error";
2
2
  import { cleanupItem, cleanupItems } from "./cleanup.js";
3
- /**
4
- * Will run query only once. Pass the previous to run the query again to fetch new data.
5
- * It returns the result and the items it found.
6
- * Result is required to fetch the items that were not fetched in the previous run.
7
- */
8
- const query = async params => {
9
- const {
10
- entity,
11
- previous,
12
- partitionKey,
13
- options
14
- } = params;
15
- let result;
16
- /**
17
- * In case there is no previous result we must make a new query.
18
- * This is the first query on the given partition key.
19
- */
20
- if (!previous) {
21
- result = await entity.query(partitionKey, options);
22
- } else if (typeof previous.next === "function") {
23
- /**
24
- * In case we have a previous result and it has a next method, we run it.
25
- * In case result of the next method is false, it means it has nothing else to read
26
- * and we return a null to keep the query from repeating.
27
- */
28
- result = await previous.next();
29
- if (result === false) {
30
- return {
3
+ const query = async (params)=>{
4
+ const { entity, previous, partitionKey, options } = params;
5
+ let result;
6
+ if (previous) if ("function" != typeof previous.next) return {
31
7
  result: null,
32
8
  items: []
33
- };
9
+ };
10
+ else {
11
+ result = await previous.next();
12
+ if (false === result) return {
13
+ result: null,
14
+ items: []
15
+ };
34
16
  }
35
- } else {
36
- /**
37
- * This could probably never happen but keep it here just in case to break the query loop.
38
- * Basically, either previous does not exist or it exists and it does not have the next method
39
- * and at that point a result returned will be null and loop should not start again.
40
- */
17
+ else result = await entity.query(partitionKey, options);
18
+ if (!result || !result.Items || !Array.isArray(result.Items)) throw new error("Error when querying for content entries - no result.", "QUERY_ERROR", {
19
+ partitionKey,
20
+ options
21
+ });
41
22
  return {
42
- result: null,
43
- items: []
23
+ result,
24
+ items: result.Items
44
25
  };
45
- }
46
- /**
47
- * We expect the result to contain an Items array and if not, something went wrong, very wrong.
48
- */
49
- if (!result || !result.Items || !Array.isArray(result.Items)) {
50
- throw new WebinyError("Error when querying for content entries - no result.", "QUERY_ERROR", {
51
- partitionKey,
52
- options
53
- });
54
- }
55
- return {
56
- result,
57
- items: result.Items
58
- };
59
26
  };
60
- /**
61
- * Will run the query to fetch the first possible item from the database.
62
- */
63
- export const queryOne = async params => {
64
- const {
65
- items
66
- } = await query({
67
- ...params,
68
- options: {
69
- ...(params.options || {}),
70
- limit: 1
71
- }
72
- });
73
- const item = items.shift();
74
- return item ? item : null;
27
+ const queryOne = async (params)=>{
28
+ const { items } = await query({
29
+ ...params,
30
+ options: {
31
+ ...params.options || {},
32
+ limit: 1
33
+ }
34
+ });
35
+ const item = items.shift();
36
+ return item ? item : null;
75
37
  };
76
- export const queryOneClean = async params => {
77
- const result = await queryOne(params);
78
- if (!result) {
79
- return null;
80
- }
81
- return cleanupItem(params.entity, result);
38
+ const queryOneClean = async (params)=>{
39
+ const result = await queryOne(params);
40
+ if (!result) return null;
41
+ return cleanupItem(params.entity, result);
82
42
  };
83
- /**
84
- * Will run the query to fetch the results no matter how many iterations it needs to go through.
85
- */
86
- export const queryAll = async params => {
87
- const items = [];
88
- let results;
89
- let previousResult = undefined;
90
- while (results = await query({
91
- ...params,
92
- previous: previousResult
93
- })) {
94
- items.push(...results.items);
95
- if (!results.result) {
96
- return items;
43
+ const queryAll = async (params)=>{
44
+ const items = [];
45
+ let results;
46
+ let previousResult;
47
+ while(results = await query({
48
+ ...params,
49
+ previous: previousResult
50
+ })){
51
+ items.push(...results.items);
52
+ if (!results.result) break;
53
+ previousResult = results.result;
97
54
  }
98
- previousResult = results.result;
99
- }
100
- return items;
55
+ return items;
101
56
  };
102
- export const queryAllClean = async params => {
103
- const results = await queryAll(params);
104
- return cleanupItems(params.entity, results);
57
+ const queryAllClean = async (params)=>{
58
+ const results = await queryAll(params);
59
+ return cleanupItems(params.entity, results);
105
60
  };
106
- export const queryPerPage = async params => {
107
- const result = await query({
108
- ...params,
109
- options: {
110
- ...params.options,
111
- limit: params.options?.limit || 50
112
- }
113
- });
114
- return {
115
- items: result.items,
116
- lastEvaluatedKey: result.result?.LastEvaluatedKey
117
- };
61
+ const queryPerPage = async (params)=>{
62
+ const result = await query({
63
+ ...params,
64
+ options: {
65
+ ...params.options,
66
+ limit: params.options?.limit || 50
67
+ }
68
+ });
69
+ return {
70
+ items: result.items,
71
+ lastEvaluatedKey: result.result?.LastEvaluatedKey
72
+ };
118
73
  };
119
- export const queryPerPageClean = async params => {
120
- const result = await queryPerPage(params);
121
- return {
122
- items: cleanupItems(params.entity, result.items),
123
- lastEvaluatedKey: result.lastEvaluatedKey
124
- };
74
+ const queryPerPageClean = async (params)=>{
75
+ const result = await queryPerPage(params);
76
+ return {
77
+ items: cleanupItems(params.entity, result.items),
78
+ lastEvaluatedKey: result.lastEvaluatedKey
79
+ };
125
80
  };
126
-
127
- /**
128
- * Will run the query to fetch the results no matter how many iterations it needs to go through.
129
- * Results of each iteration will be passed to the provided callback
130
- */
131
- export const queryAllWithCallback = async (params, callback) => {
132
- let results;
133
- let previousResult = undefined;
134
- while (results = await query({
135
- ...params,
136
- previous: previousResult
137
- })) {
138
- if (!results.result) {
139
- break;
81
+ const queryAllWithCallback = async (params, callback)=>{
82
+ let results;
83
+ let previousResult;
84
+ while(results = await query({
85
+ ...params,
86
+ previous: previousResult
87
+ })){
88
+ if (!results.result) break;
89
+ await callback(results.items);
90
+ previousResult = results.result;
140
91
  }
141
- await callback(results.items);
142
- previousResult = results.result;
143
- }
144
92
  };
93
+ export { queryAll, queryAllClean, queryAllWithCallback, queryOne, queryOneClean, queryPerPage, queryPerPageClean };
145
94
 
146
95
  //# sourceMappingURL=query.js.map
@@ -1 +1 @@
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,EAAE;MACX,CAAC;IACL;EACJ,CAAC,MAAM;IACH;AACR;AACA;AACA;AACA;IACQ,OAAO;MACHF,MAAM,EAAE,IAAI;MACZE,KAAK,EAAE;IACX,CAAC;EACL;EACA;AACJ;AACA;EACI,IAAI,CAACF,MAAM,IAAI,CAACA,MAAM,CAACG,KAAK,IAAI,CAACC,KAAK,CAACC,OAAO,CAACL,MAAM,CAACG,KAAK,CAAC,EAAE;IAC1D,MAAM,IAAIZ,WAAW,CACjB,sDAAsD,EACtD,aAAa,EACb;MACIO,YAAY;MACZC;IACJ,CACJ,CAAC;EACL;EACA,OAAO;IACHC,MAAM;IACNE,KAAK,EAAEF,MAAM,CAACG;EAClB,CAAC;AACL,CAAC;AACD;AACA;AACA;AACA,OAAO,MAAMG,QAAQ,GAAG,MAAUX,MAAsB,IAAwB;EAC5E,MAAM;IAAEO;EAA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B,IAAqC;EAC7F,MAAMK,MAAM,GAAG,MAAMN,KAAK,CAAI;IAC1B,GAAGC,MAAM;IACTI,OAAO,EAAE;MACL,GAAGJ,MAAM,CAACI,OAAO;MACjBQ,KAAK,EAAEZ,MAAM,CAACI,OAAO,EAAEQ,KAAK,IAAI;IACpC;EACJ,CAAC,CAAC;EAEF,OAAO;IACHL,KAAK,EAAEF,MAAM,CAACE,KAAK;IACnBgB,gBAAgB,EAAElB,MAAM,CAACA,MAAM,EAAEmB;EACrC,CAAC;AACL,CAAC;AAED,OAAO,MAAMC,iBAAiB,GAAG,MAC7BzB,MAAsB,IACW;EACjC,MAAMK,MAAM,GAAG,MAAMiB,YAAY,CAAItB,MAAM,CAAC;EAC5C,OAAO;IACHO,KAAK,EAAET,YAAY,CAAIE,MAAM,CAACC,MAAM,EAAEI,MAAM,CAACE,KAAK,CAAC;IACnDgB,gBAAgB,EAAElB,MAAM,CAACkB;EAC7B,CAAC;AACL,CAAC;;AAED;AACA;AACA;AACA;AACA,OAAO,MAAMG,oBAAoB,GAAG,MAAAA,CAChC1B,MAAsB,EACtB2B,QAAuC,KACvB;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":[]}
1
+ {"version":3,"file":"utils/query.js","sources":["../../src/utils/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 // @ts-expect-error\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"],"names":["query","params","entity","previous","partitionKey","options","result","Array","WebinyError","queryOne","items","item","queryOneClean","cleanupItem","queryAll","results","previousResult","queryAllClean","cleanupItems","queryPerPage","queryPerPageClean","queryAllWithCallback","callback"],"mappings":";;AA6BA,MAAMA,QAAQ,OAAUC;IACpB,MAAM,EAAEC,MAAM,EAAEC,QAAQ,EAAEC,YAAY,EAAEC,OAAO,EAAE,GAAGJ;IACpD,IAAIK;IA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c;AACvC;AAOO,MAAMI,eAAe,OAAUlB;IAClC,MAAMK,SAAS,MAAMN,MAAS;QAC1B,GAAGC,MAAM;QACT,SAAS;YACL,GAAGA,OAAO,OAAO;YACjB,OAAOA,OAAO,OAAO,EAAE,SAAS;QACpC;IACJ;IAEA,OAAO;QACH,OAAOK,OAAO,KAAK;QACnB,kBAAkBA,OAAO,MAAM,EAAE;IACrC;AACJ;AAEO,MAAMc,oBAAoB,OAC7BnB;IAEA,MAAMK,SAAS,MAAMa,aAAgBlB;IACrC,OAAO;QACH,OAAOiB,aAAgBjB,OAAO,MAAM,EAAEK,OAAO,KAAK;QAClD,kBAAkBA,OAAO,gBAAgB;IAC7C;AACJ;AAMO,MAAMe,uBAAuB,OAChCpB,QACAqB;IAEA,IAAIP;IACJ,IAAIC;IACJ,MAAQD,UAAU,MAAMf,MAAM;QAAE,GAAGC,MAAM;QAAE,UAAUe;IAAe,GAAK;QACrE,IAAI,CAACD,QAAQ,MAAM,EACf;QAEJ,MAAMO,SAASP,QAAQ,KAAK;QAC5BC,iBAAiBD,QAAQ,MAAM;IACnC;AACJ"}
package/utils/scan.js CHANGED
@@ -1,82 +1,56 @@
1
1
  import { executeWithRetry } from "@webiny/utils";
2
- const createNext = result => {
3
- if (!result?.LastEvaluatedKey || !result.next) {
4
- return undefined;
5
- }
6
- return async () => {
7
- const response = await result.next();
8
- return convertResult(response);
9
- };
10
- };
11
- const convertResult = result => {
12
- return {
13
- items: result.Items || [],
14
- count: result.Count,
15
- scannedCount: result.ScannedCount,
16
- lastEvaluatedKey: result.LastEvaluatedKey || undefined,
17
- next: createNext(result),
18
- error: result.error,
19
- requestId: result.$response?.requestId || ""
20
- };
2
+ const createNext = (result)=>{
3
+ if (!result?.LastEvaluatedKey || !result.next) return;
4
+ return async ()=>{
5
+ const response = await result.next();
6
+ return convertResult(response);
7
+ };
21
8
  };
22
- export const scan = async params => {
23
- const {
24
- options
25
- } = params;
26
- const table = params.table ? params.table : params.entity.table;
27
- if (!table) {
28
- throw new Error(`Missing table for scan: ${JSON.stringify(options)}`);
29
- }
30
- const result = await table.scan({
31
- ...options,
32
- execute: true
33
- }, params.params);
34
- return convertResult(result);
9
+ const convertResult = (result)=>({
10
+ items: result.Items || [],
11
+ count: result.Count,
12
+ scannedCount: result.ScannedCount,
13
+ lastEvaluatedKey: result.LastEvaluatedKey || void 0,
14
+ next: createNext(result),
15
+ error: result.error,
16
+ requestId: result.$response?.requestId || ""
17
+ });
18
+ const scan = async (params)=>{
19
+ const { options } = params;
20
+ const table = params.table ? params.table : params.entity.table;
21
+ if (!table) throw new Error(`Missing table for scan: ${JSON.stringify(options)}`);
22
+ const result = await table.scan({
23
+ ...options,
24
+ execute: true
25
+ }, params.params);
26
+ return convertResult(result);
35
27
  };
36
- export const scanWithCallback = async (params, callback, options) => {
37
- // For backwards compatibility, we still allow for executing the scan without retries.
38
- const usingRetry = Boolean(options?.retry);
39
- const retryOptions = options?.retry === true ? {} : options?.retry;
40
- const executeScan = () => scan(params);
41
- const getInitialResult = () => {
42
- if (usingRetry) {
43
- return executeWithRetry(executeScan, retryOptions);
44
- }
45
- return executeScan();
46
- };
47
- let result = await getInitialResult();
48
- if (!result.items?.length && !result.lastEvaluatedKey) {
49
- return;
50
- }
51
-
52
- // If the result of the callback was `false`, that means the
53
- // user's intention was to stop further table scanning.
54
- const callbackResult = await callback(result);
55
- const mustBreak = callbackResult === false;
56
- if (mustBreak) {
57
- return;
58
- }
59
- while (result.next) {
60
- const executeNext = () => result.next();
61
- const getNextResult = () => {
62
- if (usingRetry) {
63
- return executeWithRetry(executeNext, retryOptions);
64
- }
65
- return executeNext();
28
+ const scanWithCallback = async (params, callback, options)=>{
29
+ const usingRetry = Boolean(options?.retry);
30
+ const retryOptions = options?.retry === true ? {} : options?.retry;
31
+ const executeScan = ()=>scan(params);
32
+ const getInitialResult = ()=>{
33
+ if (usingRetry) return executeWithRetry(executeScan, retryOptions);
34
+ return executeScan();
66
35
  };
67
- result = await getNextResult();
68
-
69
- // If the result of the callback was `false`, that means the
70
- // user's intention was to stop further table scanning.
36
+ let result = await getInitialResult();
37
+ if (!result.items?.length && !result.lastEvaluatedKey) return;
71
38
  const callbackResult = await callback(result);
72
- const mustBreak = callbackResult === false;
73
- if (mustBreak) {
74
- break;
75
- }
76
- if (!result.next) {
77
- return;
39
+ const mustBreak = false === callbackResult;
40
+ if (mustBreak) return;
41
+ while(result.next){
42
+ const executeNext = ()=>result.next();
43
+ const getNextResult = ()=>{
44
+ if (usingRetry) return executeWithRetry(executeNext, retryOptions);
45
+ return executeNext();
46
+ };
47
+ result = await getNextResult();
48
+ const callbackResult = await callback(result);
49
+ const mustBreak = false === callbackResult;
50
+ if (mustBreak) break;
51
+ if (!result.next) return;
78
52
  }
79
- }
80
53
  };
54
+ export { scan, scanWithCallback };
81
55
 
82
56
  //# sourceMappingURL=scan.js.map
package/utils/scan.js.map CHANGED
@@ -1 +1 @@
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,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,EAAED,SAAS,IAAI;EAC9C,CAAC;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AAQ,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":[]}
1
+ {"version":3,"file":"utils/scan.js","sources":["../../src/utils/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"],"names":["createNext","result","response","convertResult","undefined","scan","params","options","table","Error","JSON","scanWithCallback","callback","usingRetry","Boolean","retryOptions","executeScan","getInitialResult","executeWithRetry","callbackResult","mustBreak","executeNext","getNextResult"],"mappings":";AAgDA,MAAMA,aAAa,CAAIC;IACnB,IAAI,CAACA,QAAQ,oBAAoB,CAACA,OAAO,IAAI,EACzC;IAEJ,OAAO;QACH,MAAMC,WAAW,MAAMD,OAAQ,IAAI;QACnC,OAAOE,cAAcD;IACzB;AACJ;AAEA,MAAMC,gBAAgB,CAAIF,SACf;QACH,OAAOA,OAAO,KAAK,IAAI,EAAE;QACzB,OAAOA,OAAO,KAAK;QACnB,cAAcA,OAAO,YAAY;QACjC,kBAAkBA,OAAO,gBAAgB,IAAIG;QAC7C,MAAMJ,WAAcC;QACpB,OAAOA,OAAO,KAAK;QACnB,WAAWA,OAAO,SAAS,EAAE,aAAa;IAC9C;AAWG,MAAMI,OAAO,OAAUC;IAC1B,MAAM,EAAEC,OAAO,EAAE,GAAGD;IAEpB,MAAME,QAAQF,OAAO,KAAK,GAAGA,OAAO,KAAK,GAAGA,OAAO,MAAM,CAAC,KAAK;IAC/D,IAAI,CAACE,OACD,MAAM,IAAIC,MAAM,CAAC,wBAAwB,EAAEC,KAAK,SAAS,CAACH,UAAU;IAGxE,MAA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