@webiny/db-dynamodb 6.3.0-beta.4 → 6.4.0-beta.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.
- package/DynamoDbDriver.js +204 -209
- package/DynamoDbDriver.js.map +1 -1
- package/exports/api/db.js +0 -2
- package/feature/FilterUtil/FilterUtil.js +18 -20
- package/feature/FilterUtil/FilterUtil.js.map +1 -1
- package/feature/FilterUtil/abstractions/FilterUtil.js +2 -1
- package/feature/FilterUtil/abstractions/FilterUtil.js.map +1 -1
- package/feature/FilterUtil/createFilters.js +82 -120
- package/feature/FilterUtil/createFilters.js.map +1 -1
- package/feature/FilterUtil/extractWhereArgs.js +15 -19
- package/feature/FilterUtil/extractWhereArgs.js.map +1 -1
- package/feature/FilterUtil/feature.js +6 -5
- package/feature/FilterUtil/feature.js.map +1 -1
- package/feature/FilterUtil/index.js +0 -2
- package/feature/ValueFilter/ValueFilterRegistry.js +21 -17
- package/feature/ValueFilter/ValueFilterRegistry.js.map +1 -1
- package/feature/ValueFilter/abstractions/ValueFilter.js +2 -1
- package/feature/ValueFilter/abstractions/ValueFilter.js.map +1 -1
- package/feature/ValueFilter/abstractions/ValueFilterRegistry.js +2 -1
- package/feature/ValueFilter/abstractions/ValueFilterRegistry.js.map +1 -1
- package/feature/ValueFilter/feature.js +17 -16
- package/feature/ValueFilter/feature.js.map +1 -1
- package/feature/ValueFilter/filters/AndInFilter.js +19 -23
- package/feature/ValueFilter/filters/AndInFilter.js.map +1 -1
- package/feature/ValueFilter/filters/BetweenFilter.js +23 -25
- package/feature/ValueFilter/filters/BetweenFilter.js.map +1 -1
- package/feature/ValueFilter/filters/ContainsFilter.js +34 -53
- package/feature/ValueFilter/filters/ContainsFilter.js.map +1 -1
- package/feature/ValueFilter/filters/EqFilter.js +17 -27
- package/feature/ValueFilter/filters/EqFilter.js.map +1 -1
- package/feature/ValueFilter/filters/FuzzyFilter.js +30 -30
- package/feature/ValueFilter/filters/FuzzyFilter.js.map +1 -1
- package/feature/ValueFilter/filters/GtFilter.js +16 -16
- package/feature/ValueFilter/filters/GtFilter.js.map +1 -1
- package/feature/ValueFilter/filters/GteFilter.js +16 -16
- package/feature/ValueFilter/filters/GteFilter.js.map +1 -1
- package/feature/ValueFilter/filters/InFilter.js +19 -23
- package/feature/ValueFilter/filters/InFilter.js.map +1 -1
- package/feature/ValueFilter/filters/LtFilter.js +16 -16
- package/feature/ValueFilter/filters/LtFilter.js.map +1 -1
- package/feature/ValueFilter/filters/LteFilter.js +16 -16
- package/feature/ValueFilter/filters/LteFilter.js.map +1 -1
- package/feature/ValueFilter/filters/StartsWithFilter.js +20 -36
- package/feature/ValueFilter/filters/StartsWithFilter.js.map +1 -1
- package/feature/ValueFilter/index.js +0 -2
- package/index.js +7 -9
- package/index.js.map +1 -1
- package/package.json +13 -13
- package/plugins/definitions/DateTimeTransformPlugin.js +18 -30
- package/plugins/definitions/DateTimeTransformPlugin.js.map +1 -1
- package/plugins/definitions/FieldPlugin.js +34 -37
- package/plugins/definitions/FieldPlugin.js.map +1 -1
- package/plugins/definitions/TimeTransformPlugin.js +25 -37
- package/plugins/definitions/TimeTransformPlugin.js.map +1 -1
- package/plugins/definitions/ValueTransformPlugin.js +18 -17
- package/plugins/definitions/ValueTransformPlugin.js.map +1 -1
- package/plugins/definitions/assignFields.js +14 -18
- package/plugins/definitions/assignFields.js.map +1 -1
- package/plugins/index.js +0 -2
- package/store/entity.js +6 -9
- package/store/entity.js.map +1 -1
- package/store/keys.js +6 -13
- package/store/keys.js.map +1 -1
- package/store/types.js +0 -3
- package/toolbox.js +0 -5
- package/types.js +0 -3
- package/utils/batch/batchRead.js +37 -56
- package/utils/batch/batchRead.js.map +1 -1
- package/utils/batch/batchWrite.js +33 -51
- package/utils/batch/batchWrite.js.map +1 -1
- package/utils/batch/index.js +0 -2
- package/utils/batch/types.js +0 -3
- package/utils/cleanup.js +35 -25
- package/utils/cleanup.js.map +1 -1
- package/utils/count.js +8 -14
- package/utils/count.js.map +1 -1
- package/utils/createEntity.d.ts +1 -1
- package/utils/createEntity.js +75 -78
- package/utils/createEntity.js.map +1 -1
- package/utils/createTable.js +25 -35
- package/utils/createTable.js.map +1 -1
- package/utils/cursor.js +7 -10
- package/utils/cursor.js.map +1 -1
- package/utils/delete.js +6 -8
- package/utils/delete.js.map +1 -1
- package/utils/entity/Entity.js +85 -90
- package/utils/entity/Entity.js.map +1 -1
- package/utils/entity/EntityReadBatch.js +23 -31
- package/utils/entity/EntityReadBatch.js.map +1 -1
- package/utils/entity/EntityReadBatchBuilder.js +12 -15
- package/utils/entity/EntityReadBatchBuilder.js.map +1 -1
- package/utils/entity/EntityWriteBatch.js +34 -41
- package/utils/entity/EntityWriteBatch.js.map +1 -1
- package/utils/entity/EntityWriteBatchBuilder.js +15 -16
- package/utils/entity/EntityWriteBatchBuilder.js.map +1 -1
- package/utils/entity/getEntity.js +8 -9
- package/utils/entity/getEntity.js.map +1 -1
- package/utils/entity/index.js +0 -2
- package/utils/entity/types.js +0 -4
- package/utils/get.js +12 -26
- package/utils/get.js.map +1 -1
- package/utils/index.js +1 -3
- package/utils/put.js +7 -9
- package/utils/put.js.map +1 -1
- package/utils/query.js +77 -129
- package/utils/query.js.map +1 -1
- package/utils/scan.js +47 -73
- package/utils/scan.js.map +1 -1
- package/utils/sort.js +37 -61
- package/utils/sort.js.map +1 -1
- package/utils/table/Table.js +22 -21
- package/utils/table/Table.js.map +1 -1
- package/utils/table/TableReadBatch.js +37 -45
- package/utils/table/TableReadBatch.js.map +1 -1
- package/utils/table/TableWriteBatch.js +47 -56
- package/utils/table/TableWriteBatch.js.map +1 -1
- package/utils/table/index.js +0 -2
- package/utils/table/types.js +0 -3
- package/exports/api/db.js.map +0 -1
- package/feature/FilterUtil/index.js.map +0 -1
- package/feature/ValueFilter/index.js.map +0 -1
- package/plugins/index.js.map +0 -1
- package/store/types.js.map +0 -1
- package/toolbox.js.map +0 -1
- package/types.js.map +0 -1
- package/utils/batch/index.js.map +0 -1
- package/utils/batch/types.js.map +0 -1
- package/utils/entity/index.js.map +0 -1
- package/utils/entity/types.js.map +0 -1
- package/utils/index.js.map +0 -1
- package/utils/table/index.js.map +0 -1
- package/utils/table/types.js.map +0 -1
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
import { getEntity } from "./getEntity.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
2
|
+
class EntityWriteBatchBuilder {
|
|
3
|
+
constructor(entity){
|
|
4
|
+
this.entity = getEntity(entity);
|
|
5
|
+
}
|
|
6
|
+
put(item) {
|
|
7
|
+
return this.entity.putBatch(item, {
|
|
8
|
+
execute: true,
|
|
9
|
+
strictSchemaCheck: false
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
delete(item) {
|
|
13
|
+
return this.entity.deleteBatch(item);
|
|
14
|
+
}
|
|
15
15
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
};
|
|
16
|
+
const createEntityWriteBatchBuilder = (entity)=>new EntityWriteBatchBuilder(entity);
|
|
17
|
+
export { EntityWriteBatchBuilder, createEntityWriteBatchBuilder };
|
|
19
18
|
|
|
20
19
|
//# sourceMappingURL=EntityWriteBatchBuilder.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"utils/entity/EntityWriteBatchBuilder.js","sources":["../../../src/utils/entity/EntityWriteBatchBuilder.ts"],"sourcesContent":["import type { Entity } from \"~/toolbox.js\";\nimport type { BatchWriteItem, IDeleteBatchItem, IPutBatchItem } from \"~/utils/batch/types.js\";\nimport type { IEntityWriteBatchBuilder } from \"./types.js\";\nimport type { EntityOption } from \"./getEntity.js\";\nimport { getEntity } from \"./getEntity.js\";\n\nexport class EntityWriteBatchBuilder implements IEntityWriteBatchBuilder {\n private readonly entity: Entity;\n\n public constructor(entity: EntityOption) {\n this.entity = getEntity(entity);\n }\n\n public put<T extends Record<string, any>>(item: IPutBatchItem<T>): BatchWriteItem {\n return this.entity.putBatch(item, {\n execute: true,\n strictSchemaCheck: false\n });\n }\n\n public delete(item: IDeleteBatchItem): BatchWriteItem {\n return this.entity.deleteBatch(item);\n }\n}\n\nexport const createEntityWriteBatchBuilder = (entity: Entity): IEntityWriteBatchBuilder => {\n return new EntityWriteBatchBuilder(entity);\n};\n"],"names":["EntityWriteBatchBuilder","entity","getEntity","item","createEntityWriteBatchBuilder"],"mappings":";AAMO,MAAMA;IAGT,YAAmBC,MAAoB,CAAE;QACrC,IAAI,CAAC,MAAM,GAAGC,UAAUD;IAC5B;IAEO,IAAmCE,IAAsB,EAAkB;QAC9E,OAAO,IAAI,CAAC,MAAM,CAAC,QAAQ,CAACA,MAAM;YAC9B,SAAS;YACT,mBAAmB;QACvB;IACJ;IAEO,OAAOA,IAAsB,EAAkB;QAClD,OAAO,IAAI,CAAC,MAAM,CAAC,WAAW,CAACA;IACnC;AACJ;AAEO,MAAMC,gCAAgC,CAACH,SACnC,IAAID,wBAAwBC"}
|
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import { Entity
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
return result;
|
|
1
|
+
import { Entity } from "../../toolbox.js";
|
|
2
|
+
const getEntity = (entity)=>{
|
|
3
|
+
const result = entity instanceof Entity ? entity : entity.entity;
|
|
4
|
+
if (result.name) {
|
|
5
|
+
if (!result.table) throw new Error(`No table provided for entity ${result.name}.`);
|
|
6
|
+
} else throw new Error("No name provided for entity.");
|
|
7
|
+
return result;
|
|
10
8
|
};
|
|
9
|
+
export { getEntity };
|
|
11
10
|
|
|
12
11
|
//# sourceMappingURL=getEntity.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"utils/entity/getEntity.js","sources":["../../../src/utils/entity/getEntity.ts"],"sourcesContent":["import { Entity as ToolboxEntity } from \"~/toolbox.js\";\nimport type { Entity } from \"./Entity.js\";\n\nexport type EntityOption = ToolboxEntity | Entity;\n\nexport const getEntity = (entity: EntityOption): ToolboxEntity => {\n const result = entity instanceof ToolboxEntity ? entity : entity.entity;\n if (!result.name) {\n throw new Error(`No name provided for entity.`);\n } else if (!result.table) {\n throw new Error(`No table provided for entity ${result.name}.`);\n }\n return result;\n};\n"],"names":["getEntity","entity","result","ToolboxEntity","Error"],"mappings":";AAKO,MAAMA,YAAY,CAACC;IACtB,MAAMC,SAASD,kBAAkBE,SAAgBF,SAASA,OAAO,MAAM;IACvE,IAAKC,OAAO,IAAI,EAET;QAAA,IAAI,CAACA,OAAO,KAAK,EACpB,MAAM,IAAIE,MAAM,CAAC,6BAA6B,EAAEF,OAAO,IAAI,CAAC,CAAC,CAAC;IAClE,OAHI,MAAM,IAAIE,MAAM;IAIpB,OAAOF;AACX"}
|
package/utils/entity/index.js
CHANGED
package/utils/entity/types.js
CHANGED
package/utils/get.js
CHANGED
|
@@ -1,31 +1,17 @@
|
|
|
1
1
|
import { cleanupItem } from "./cleanup.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
*/
|
|
10
|
-
export const get = async params => {
|
|
11
|
-
const {
|
|
12
|
-
entity,
|
|
13
|
-
keys
|
|
14
|
-
} = params;
|
|
15
|
-
const result = await entity.get(keys, {
|
|
16
|
-
execute: true
|
|
17
|
-
});
|
|
18
|
-
if (!result?.Item) {
|
|
19
|
-
return null;
|
|
20
|
-
}
|
|
21
|
-
return result.Item;
|
|
2
|
+
const get = async (params)=>{
|
|
3
|
+
const { entity, keys } = params;
|
|
4
|
+
const result = await entity.get(keys, {
|
|
5
|
+
execute: true
|
|
6
|
+
});
|
|
7
|
+
if (!result?.Item) return null;
|
|
8
|
+
return result.Item;
|
|
22
9
|
};
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return
|
|
27
|
-
}
|
|
28
|
-
return cleanupItem(params.entity, result);
|
|
10
|
+
const getClean = async (params)=>{
|
|
11
|
+
const result = await get(params);
|
|
12
|
+
if (!result) return null;
|
|
13
|
+
return cleanupItem(params.entity, result);
|
|
29
14
|
};
|
|
15
|
+
export { get, getClean };
|
|
30
16
|
|
|
31
17
|
//# sourceMappingURL=get.js.map
|
package/utils/get.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
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.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
export * from "./cleanup.js";
|
|
2
2
|
export * from "./createEntity.js";
|
|
3
3
|
export * from "./createTable.js";
|
|
4
|
-
export { decodeCursor, encodeCursor } from "./cursor.js";
|
|
5
4
|
export * from "./get.js";
|
|
6
5
|
export * from "./delete.js";
|
|
7
6
|
export * from "./put.js";
|
|
@@ -11,5 +10,4 @@ export * from "./sort.js";
|
|
|
11
10
|
export * from "./batch/index.js";
|
|
12
11
|
export * from "./entity/index.js";
|
|
13
12
|
export * from "./table/index.js";
|
|
14
|
-
|
|
15
|
-
//# sourceMappingURL=index.js.map
|
|
13
|
+
export { decodeCursor, encodeCursor } from "./cursor.js";
|
package/utils/put.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
entity,
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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,"
|
|
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,147 +1,95 @@
|
|
|
1
|
-
import
|
|
1
|
+
import error from "@webiny/error";
|
|
2
2
|
import { cleanupItem, cleanupItems } from "./cleanup.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
// @ts-expect-error
|
|
22
|
-
result = await entity.query(partitionKey, options);
|
|
23
|
-
} else if (typeof previous.next === "function") {
|
|
24
|
-
/**
|
|
25
|
-
* In case we have a previous result and it has a next method, we run it.
|
|
26
|
-
* In case result of the next method is false, it means it has nothing else to read
|
|
27
|
-
* and we return a null to keep the query from repeating.
|
|
28
|
-
*/
|
|
29
|
-
result = await previous.next();
|
|
30
|
-
if (result === false) {
|
|
31
|
-
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 {
|
|
32
7
|
result: null,
|
|
33
8
|
items: []
|
|
34
|
-
|
|
9
|
+
};
|
|
10
|
+
else {
|
|
11
|
+
result = await previous.next();
|
|
12
|
+
if (false === result) return {
|
|
13
|
+
result: null,
|
|
14
|
+
items: []
|
|
15
|
+
};
|
|
35
16
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
*/
|
|
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
|
+
});
|
|
42
22
|
return {
|
|
43
|
-
|
|
44
|
-
|
|
23
|
+
result,
|
|
24
|
+
items: result.Items
|
|
45
25
|
};
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* We expect the result to contain an Items array and if not, something went wrong, very wrong.
|
|
49
|
-
*/
|
|
50
|
-
if (!result || !result.Items || !Array.isArray(result.Items)) {
|
|
51
|
-
throw new WebinyError("Error when querying for content entries - no result.", "QUERY_ERROR", {
|
|
52
|
-
partitionKey,
|
|
53
|
-
options
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
return {
|
|
57
|
-
result,
|
|
58
|
-
items: result.Items
|
|
59
|
-
};
|
|
60
26
|
};
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
limit: 1
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
const item = items.shift();
|
|
75
|
-
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;
|
|
76
37
|
};
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
return
|
|
81
|
-
}
|
|
82
|
-
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);
|
|
83
42
|
};
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
items.push(...results.items);
|
|
96
|
-
if (!results.result) {
|
|
97
|
-
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;
|
|
98
54
|
}
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
return items;
|
|
55
|
+
return items;
|
|
102
56
|
};
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
57
|
+
const queryAllClean = async (params)=>{
|
|
58
|
+
const results = await queryAll(params);
|
|
59
|
+
return cleanupItems(params.entity, results);
|
|
106
60
|
};
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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
|
+
};
|
|
119
73
|
};
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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
|
+
};
|
|
126
80
|
};
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
previous: previousResult
|
|
138
|
-
})) {
|
|
139
|
-
if (!results.result) {
|
|
140
|
-
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;
|
|
141
91
|
}
|
|
142
|
-
await callback(results.items);
|
|
143
|
-
previousResult = results.result;
|
|
144
|
-
}
|
|
145
92
|
};
|
|
93
|
+
export { queryAll, queryAllClean, queryAllWithCallback, queryOne, queryOneClean, queryPerPage, queryPerPageClean };
|
|
146
94
|
|
|
147
95
|
//# sourceMappingURL=query.js.map
|
package/utils/query.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
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;IAKJ,IAAKH,UAGE,IAAI,AAAyB,cAAzB,OAAOA,SAAS,IAAI,EAmB3B,OAAO;QACH,QAAQ;QACR,OAAO,EAAE;IACb;SAtB4C;QAM5CG,SAAS,MAAMH,SAAS,IAAI;QAC5B,IAAIG,AAAW,UAAXA,QACA,OAAO;YACH,QAAQ;YACR,OAAO,EAAE;QACb;IAER;SAdIA,SAAS,MAAMJ,OAAO,KAAK,CAACE,cAAcC;IA4B9C,IAAI,CAACC,UAAU,CAACA,OAAO,KAAK,IAAI,CAACC,MAAM,OAAO,CAACD,OAAO,KAAK,GACvD,MAAM,IAAIE,MACN,wDACA,eACA;QACIJ;QACAC;IACJ;IAGR,OAAO;QACHC;QACA,OAAOA,OAAO,KAAK;IACvB;AACJ;AAIO,MAAMG,WAAW,OAAUR;IAC9B,MAAM,EAAES,KAAK,EAAE,GAAG,MAAMV,MAAS;QAC7B,GAAGC,MAAM;QACT,SAAS;YACL,GAAIA,OAAO,OAAO,IAAI,CAAC,CAAC;YACxB,OAAO;QACX;IACJ;IACA,MAAMU,OAAOD,MAAM,KAAK;IACxB,OAAOC,OAAOA,OAAO;AACzB;AAEO,MAAMC,gBAAgB,OAAUX;IACnC,MAAMK,SAAS,MAAMG,SAAYR;IACjC,IAAI,CAACK,QACD,OAAO;IAEX,OAAOO,YAAYZ,OAAO,MAAM,EAAEK;AACtC;AAIO,MAAMQ,WAAW,OAAUb;IAC9B,MAAMS,QAAa,EAAE;IACrB,IAAIK;IACJ,IAAIC;IACJ,MAAQD,UAAU,MAAMf,MAAM;QAAE,GAAGC,MAAM;QAAE,UAAUe;IAAe,GAAK;QACrEN,MAAM,IAAI,IAAIK,QAAQ,KAAK;QAC3B,IAAI,CAACA,QAAQ,MAAM,EACf;QAEJC,iBAAiBD,QAAQ,MAAM;IACnC;IACA,OAAOL;AACX;AAEO,MAAMO,gBAAgB,OAAUhB;IACnC,MAAMc,UAAU,MAAMD,SAAYb;IAClC,OAAOiB,aAAajB,OAAO,MAAM,EAAEc;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
|
-
|
|
4
|
-
return
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
|
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 =
|
|
73
|
-
if (mustBreak)
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|