@webiny/db-dynamodb 0.0.0-unstable.78f581c1d2 → 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.
- package/DynamoDbDriver.d.ts +18 -32
- package/DynamoDbDriver.js +215 -261
- package/DynamoDbDriver.js.map +1 -1
- package/README.md +6 -62
- package/exports/api/db.d.ts +2 -0
- package/exports/api/db.js +2 -0
- package/feature/FilterUtil/FilterUtil.d.ts +11 -0
- package/feature/FilterUtil/FilterUtil.js +26 -0
- package/feature/FilterUtil/FilterUtil.js.map +1 -0
- package/feature/FilterUtil/abstractions/FilterUtil.d.ts +17 -0
- package/feature/FilterUtil/abstractions/FilterUtil.js +5 -0
- package/feature/FilterUtil/abstractions/FilterUtil.js.map +1 -0
- package/feature/FilterUtil/createFilters.d.ts +22 -0
- package/feature/FilterUtil/createFilters.js +92 -0
- package/feature/FilterUtil/createFilters.js.map +1 -0
- package/feature/FilterUtil/extractWhereArgs.d.ts +7 -0
- package/feature/FilterUtil/extractWhereArgs.js +20 -0
- package/feature/FilterUtil/extractWhereArgs.js.map +1 -0
- package/feature/FilterUtil/feature.d.ts +4 -0
- package/feature/FilterUtil/feature.js +11 -0
- package/feature/FilterUtil/feature.js.map +1 -0
- package/feature/FilterUtil/index.d.ts +2 -0
- package/feature/FilterUtil/index.js +2 -0
- package/feature/ValueFilter/ValueFilterRegistry.d.ts +12 -0
- package/feature/ValueFilter/ValueFilterRegistry.js +27 -0
- package/feature/ValueFilter/ValueFilterRegistry.js.map +1 -0
- package/feature/ValueFilter/abstractions/ValueFilter.d.ts +22 -0
- package/feature/ValueFilter/abstractions/ValueFilter.js +5 -0
- package/feature/ValueFilter/abstractions/ValueFilter.js.map +1 -0
- package/feature/ValueFilter/abstractions/ValueFilterRegistry.d.ts +10 -0
- package/feature/ValueFilter/abstractions/ValueFilterRegistry.js +5 -0
- package/feature/ValueFilter/abstractions/ValueFilterRegistry.js.map +1 -0
- package/feature/ValueFilter/feature.d.ts +4 -0
- package/feature/ValueFilter/feature.js +33 -0
- package/feature/ValueFilter/feature.js.map +1 -0
- package/feature/ValueFilter/filters/AndInFilter.d.ts +11 -0
- package/feature/ValueFilter/filters/AndInFilter.js +28 -0
- package/feature/ValueFilter/filters/AndInFilter.js.map +1 -0
- package/feature/ValueFilter/filters/BetweenFilter.d.ts +11 -0
- package/feature/ValueFilter/filters/BetweenFilter.js +31 -0
- package/feature/ValueFilter/filters/BetweenFilter.js.map +1 -0
- package/feature/ValueFilter/filters/ContainsFilter.d.ts +11 -0
- package/feature/ValueFilter/filters/ContainsFilter.js +42 -0
- package/feature/ValueFilter/filters/ContainsFilter.js.map +1 -0
- package/feature/ValueFilter/filters/EqFilter.d.ts +11 -0
- package/feature/ValueFilter/filters/EqFilter.js +24 -0
- package/feature/ValueFilter/filters/EqFilter.js.map +1 -0
- package/feature/ValueFilter/filters/FuzzyFilter.d.ts +11 -0
- package/feature/ValueFilter/filters/FuzzyFilter.js +37 -0
- package/feature/ValueFilter/filters/FuzzyFilter.js.map +1 -0
- package/feature/ValueFilter/filters/GtFilter.d.ts +11 -0
- package/feature/ValueFilter/filters/GtFilter.js +22 -0
- package/feature/ValueFilter/filters/GtFilter.js.map +1 -0
- package/feature/ValueFilter/filters/GteFilter.d.ts +11 -0
- package/feature/ValueFilter/filters/GteFilter.js +22 -0
- package/feature/ValueFilter/filters/GteFilter.js.map +1 -0
- package/feature/ValueFilter/filters/InFilter.d.ts +11 -0
- package/feature/ValueFilter/filters/InFilter.js +28 -0
- package/feature/ValueFilter/filters/InFilter.js.map +1 -0
- package/feature/ValueFilter/filters/LtFilter.d.ts +11 -0
- package/feature/ValueFilter/filters/LtFilter.js +22 -0
- package/feature/ValueFilter/filters/LtFilter.js.map +1 -0
- package/feature/ValueFilter/filters/LteFilter.d.ts +11 -0
- package/feature/ValueFilter/filters/LteFilter.js +22 -0
- package/feature/ValueFilter/filters/LteFilter.js.map +1 -0
- package/feature/ValueFilter/filters/StartsWithFilter.d.ts +11 -0
- package/feature/ValueFilter/filters/StartsWithFilter.js +29 -0
- package/feature/ValueFilter/filters/StartsWithFilter.js.map +1 -0
- package/feature/ValueFilter/index.d.ts +3 -0
- package/feature/ValueFilter/index.js +3 -0
- package/index.d.ts +5 -1
- package/index.js +12 -14
- package/index.js.map +1 -1
- package/package.json +32 -30
- package/plugins/definitions/DateTimeTransformPlugin.d.ts +2 -1
- package/plugins/definitions/DateTimeTransformPlugin.js +21 -50
- package/plugins/definitions/DateTimeTransformPlugin.js.map +1 -1
- package/plugins/definitions/FieldPlugin.d.ts +2 -2
- package/plugins/definitions/FieldPlugin.js +36 -68
- package/plugins/definitions/FieldPlugin.js.map +1 -1
- package/plugins/definitions/TimeTransformPlugin.d.ts +2 -1
- package/plugins/definitions/TimeTransformPlugin.js +29 -56
- package/plugins/definitions/TimeTransformPlugin.js.map +1 -1
- package/plugins/definitions/ValueTransformPlugin.d.ts +2 -7
- package/plugins/definitions/ValueTransformPlugin.js +20 -46
- package/plugins/definitions/ValueTransformPlugin.js.map +1 -1
- package/plugins/definitions/assignFields.js +15 -34
- package/plugins/definitions/assignFields.js.map +1 -1
- package/plugins/index.d.ts +5 -2
- package/plugins/index.js +5 -19
- package/store/entity.d.ts +6 -0
- package/store/entity.js +8 -0
- package/store/entity.js.map +1 -0
- package/store/keys.d.ts +6 -0
- package/store/keys.js +9 -0
- package/store/keys.js.map +1 -0
- package/store/types.d.ts +6 -0
- package/store/types.js +0 -0
- package/toolbox.d.ts +60 -0
- package/toolbox.js +1 -0
- package/types.d.ts +10 -23
- package/types.js +0 -5
- package/utils/batch/batchRead.d.ts +15 -0
- package/utils/batch/batchRead.js +45 -0
- package/utils/batch/batchRead.js.map +1 -0
- package/utils/batch/batchWrite.d.ts +13 -0
- package/utils/batch/batchWrite.js +39 -0
- package/utils/batch/batchWrite.js.map +1 -0
- package/utils/batch/index.d.ts +3 -0
- package/utils/batch/index.js +3 -0
- package/utils/batch/types.d.ts +30 -0
- package/utils/batch/types.js +0 -0
- package/utils/cleanup.d.ts +3 -3
- package/utils/cleanup.js +36 -41
- package/utils/cleanup.js.map +1 -1
- package/utils/count.d.ts +2 -0
- package/utils/count.js +11 -0
- package/utils/count.js.map +1 -0
- package/utils/createEntity.d.ts +36 -0
- package/utils/createEntity.js +82 -0
- package/utils/createEntity.js.map +1 -0
- package/utils/createTable.d.ts +11 -0
- package/utils/createTable.js +29 -0
- package/utils/createTable.js.map +1 -0
- package/utils/cursor.js +8 -23
- package/utils/cursor.js.map +1 -1
- package/utils/delete.d.ts +10 -0
- package/utils/delete.js +9 -0
- package/utils/delete.js.map +1 -0
- package/utils/entity/Entity.d.ts +28 -0
- package/utils/entity/Entity.js +95 -0
- package/utils/entity/Entity.js.map +1 -0
- package/utils/entity/EntityReadBatch.d.ts +18 -0
- package/utils/entity/EntityReadBatch.js +31 -0
- package/utils/entity/EntityReadBatch.js.map +1 -0
- package/utils/entity/EntityReadBatchBuilder.d.ts +10 -0
- package/utils/entity/EntityReadBatchBuilder.js +18 -0
- package/utils/entity/EntityReadBatchBuilder.js.map +1 -0
- package/utils/entity/EntityWriteBatch.d.ts +23 -0
- package/utils/entity/EntityWriteBatch.js +44 -0
- package/utils/entity/EntityWriteBatch.js.map +1 -0
- package/utils/entity/EntityWriteBatchBuilder.d.ts +11 -0
- package/utils/entity/EntityWriteBatchBuilder.js +19 -0
- package/utils/entity/EntityWriteBatchBuilder.js.map +1 -0
- package/utils/entity/getEntity.d.ts +4 -0
- package/utils/entity/getEntity.js +11 -0
- package/utils/entity/getEntity.js.map +1 -0
- package/utils/entity/index.d.ts +7 -0
- package/utils/entity/index.js +7 -0
- package/utils/entity/types.d.ts +75 -0
- package/utils/entity/types.js +0 -0
- package/utils/get.d.ts +8 -6
- package/utils/get.js +15 -28
- package/utils/get.js.map +1 -1
- package/utils/index.d.ts +13 -0
- package/utils/index.js +13 -0
- package/utils/put.d.ts +12 -0
- package/utils/put.js +10 -0
- package/utils/put.js.map +1 -0
- package/utils/query.d.ts +20 -14
- package/utils/query.js +84 -108
- package/utils/query.js.map +1 -1
- package/utils/scan.d.ts +38 -0
- package/utils/scan.js +56 -0
- package/utils/scan.js.map +1 -0
- package/utils/sort.d.ts +1 -1
- package/utils/sort.js +38 -84
- package/utils/sort.js.map +1 -1
- package/utils/table/Table.d.ts +10 -0
- package/utils/table/Table.js +28 -0
- package/utils/table/Table.js.map +1 -0
- package/utils/table/TableReadBatch.d.ts +19 -0
- package/utils/table/TableReadBatch.js +46 -0
- package/utils/table/TableReadBatch.js.map +1 -0
- package/utils/table/TableWriteBatch.d.ts +21 -0
- package/utils/table/TableWriteBatch.js +52 -0
- package/utils/table/TableWriteBatch.js.map +1 -0
- package/utils/table/index.d.ts +4 -0
- package/utils/table/index.js +4 -0
- package/utils/table/types.d.ts +39 -0
- package/utils/table/types.js +0 -0
- package/BatchProcess.d.ts +0 -47
- package/BatchProcess.js +0 -198
- package/BatchProcess.js.map +0 -1
- package/QueryGenerator.d.ts +0 -21
- package/QueryGenerator.js +0 -78
- package/QueryGenerator.js.map +0 -1
- package/operators/comparison/beginsWith.d.ts +0 -3
- package/operators/comparison/beginsWith.js +0 -24
- package/operators/comparison/beginsWith.js.map +0 -1
- package/operators/comparison/between.d.ts +0 -3
- package/operators/comparison/between.js +0 -30
- package/operators/comparison/between.js.map +0 -1
- package/operators/comparison/eq.d.ts +0 -3
- package/operators/comparison/eq.js +0 -34
- package/operators/comparison/eq.js.map +0 -1
- package/operators/comparison/gt.d.ts +0 -3
- package/operators/comparison/gt.js +0 -24
- package/operators/comparison/gt.js.map +0 -1
- package/operators/comparison/gte.d.ts +0 -3
- package/operators/comparison/gte.js +0 -24
- package/operators/comparison/gte.js.map +0 -1
- package/operators/comparison/lt.d.ts +0 -3
- package/operators/comparison/lt.js +0 -24
- package/operators/comparison/lt.js.map +0 -1
- package/operators/comparison/lte.d.ts +0 -3
- package/operators/comparison/lte.js +0 -24
- package/operators/comparison/lte.js.map +0 -1
- package/operators/index.d.ts +0 -12
- package/operators/index.js +0 -39
- package/operators/index.js.map +0 -1
- package/operators/logical/and.d.ts +0 -3
- package/operators/logical/and.js +0 -63
- package/operators/logical/and.js.map +0 -1
- package/operators/logical/or.d.ts +0 -3
- package/operators/logical/or.js +0 -63
- package/operators/logical/or.js.map +0 -1
- package/plugins/definitions/AttributePlugin.d.ts +0 -20
- package/plugins/definitions/AttributePlugin.js +0 -61
- package/plugins/definitions/AttributePlugin.js.map +0 -1
- package/plugins/definitions/FieldPathPlugin.d.ts +0 -22
- package/plugins/definitions/FieldPathPlugin.js +0 -46
- package/plugins/definitions/FieldPathPlugin.js.map +0 -1
- package/plugins/definitions/NumberTransformPlugin.d.ts +0 -4
- package/plugins/definitions/NumberTransformPlugin.js +0 -45
- package/plugins/definitions/NumberTransformPlugin.js.map +0 -1
- package/plugins/definitions/ValueFilterPlugin.d.ts +0 -20
- package/plugins/definitions/ValueFilterPlugin.js +0 -49
- package/plugins/definitions/ValueFilterPlugin.js.map +0 -1
- package/plugins/filters/andIn.d.ts +0 -3
- package/plugins/filters/andIn.js +0 -35
- package/plugins/filters/andIn.js.map +0 -1
- package/plugins/filters/between.d.ts +0 -3
- package/plugins/filters/between.js +0 -36
- package/plugins/filters/between.js.map +0 -1
- package/plugins/filters/contains.d.ts +0 -3
- package/plugins/filters/contains.js +0 -32
- package/plugins/filters/contains.js.map +0 -1
- package/plugins/filters/eq.d.ts +0 -3
- package/plugins/filters/eq.js +0 -33
- package/plugins/filters/eq.js.map +0 -1
- package/plugins/filters/fuzzy.d.ts +0 -3
- package/plugins/filters/fuzzy.js +0 -30
- package/plugins/filters/fuzzy.js.map +0 -1
- package/plugins/filters/gt.d.ts +0 -3
- package/plugins/filters/gt.js +0 -20
- package/plugins/filters/gt.js.map +0 -1
- package/plugins/filters/gte.d.ts +0 -3
- package/plugins/filters/gte.js +0 -20
- package/plugins/filters/gte.js.map +0 -1
- package/plugins/filters/in.d.ts +0 -3
- package/plugins/filters/in.js +0 -35
- package/plugins/filters/in.js.map +0 -1
- package/plugins/filters/index.d.ts +0 -2
- package/plugins/filters/index.js +0 -34
- package/plugins/filters/index.js.map +0 -1
- package/plugins/filters/lt.d.ts +0 -3
- package/plugins/filters/lt.js +0 -20
- package/plugins/filters/lt.js.map +0 -1
- package/plugins/filters/lte.d.ts +0 -3
- package/plugins/filters/lte.js +0 -20
- package/plugins/filters/lte.js.map +0 -1
- package/plugins/filters/startsWith.d.ts +0 -3
- package/plugins/filters/startsWith.js +0 -33
- package/plugins/filters/startsWith.js.map +0 -1
- package/plugins/index.js.map +0 -1
- package/statements/createKeyConditionExpressionArgs.d.ts +0 -15
- package/statements/createKeyConditionExpressionArgs.js +0 -52
- package/statements/createKeyConditionExpressionArgs.js.map +0 -1
- package/statements/processStatement.d.ts +0 -3
- package/statements/processStatement.js +0 -42
- package/statements/processStatement.js.map +0 -1
- package/types.js.map +0 -1
- package/utils/attributes.d.ts +0 -10
- package/utils/attributes.js +0 -31
- package/utils/attributes.js.map +0 -1
- package/utils/batchRead.d.ts +0 -15
- package/utils/batchRead.js +0 -89
- package/utils/batchRead.js.map +0 -1
- package/utils/batchWrite.d.ts +0 -17
- package/utils/batchWrite.js +0 -30
- package/utils/batchWrite.js.map +0 -1
- package/utils/filter.d.ts +0 -12
- package/utils/filter.js +0 -213
- package/utils/filter.js.map +0 -1
- package/utils/listResponse.d.ts +0 -13
- package/utils/listResponse.js +0 -33
- package/utils/listResponse.js.map +0 -1
- package/utils/table.d.ts +0 -7
- package/utils/table.js +0 -27
- package/utils/table.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils/table/Table.js","sources":["../../../src/utils/table/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 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"],"names":["Table","params","BaseTable","createTableWriteBatch","createTableReadBatch","scan"],"mappings":";;;;AAaO,MAAMA;IAOT,YAAmBC,MAAqD,CAAE;QACtE,IAAI,CAAC,KAAK,GAAG,IAAIC,MAAUD;IAC/B;IAEO,eAAiC;QACpC,OAAOE,sBAAsB;YACzB,OAAO,IAAI,CAAC,KAAK;QACrB;IACJ;IAEO,eAAgC;QACnC,OAAOC,qBAAqB;YACxB,OAAO,IAAI,CAAC,KAAK;QACrB;IACJ;IAEA,MAAa,KAAQH,MAAwB,EAAkC;QAC3E,OAAOI,KAAQ;YACX,GAAGJ,MAAM;YACT,OAAO,IAAI,CAAC,KAAK;QACrB;IACJ;AACJ"}
|
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
export interface ITableReadBatchParams {
|
|
6
|
+
table: TableDef;
|
|
7
|
+
}
|
|
8
|
+
export declare class TableReadBatch implements ITableReadBatch {
|
|
9
|
+
private readonly table;
|
|
10
|
+
private readonly _items;
|
|
11
|
+
private readonly builders;
|
|
12
|
+
constructor(params: ITableReadBatchParams);
|
|
13
|
+
get total(): number;
|
|
14
|
+
get items(): IEntityReadBatchBuilderGetResponse[];
|
|
15
|
+
get(entity: Entity, input: ITableReadBatchKey): void;
|
|
16
|
+
execute<T = GenericRecord>(): Promise<T[]>;
|
|
17
|
+
private getBuilder;
|
|
18
|
+
}
|
|
19
|
+
export declare const createTableReadBatch: (params: ITableReadBatchParams) => ITableReadBatch;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { batchReadAll } from "../batch/batchRead.js";
|
|
2
|
+
import { createEntityReadBatchBuilder } from "../entity/EntityReadBatchBuilder.js";
|
|
3
|
+
import { WebinyError } from "@webiny/error";
|
|
4
|
+
class TableReadBatch {
|
|
5
|
+
constructor(params){
|
|
6
|
+
this._items = [];
|
|
7
|
+
this.builders = new Map();
|
|
8
|
+
this.table = params.table;
|
|
9
|
+
}
|
|
10
|
+
get total() {
|
|
11
|
+
return this._items.length;
|
|
12
|
+
}
|
|
13
|
+
get items() {
|
|
14
|
+
return Array.from(this._items);
|
|
15
|
+
}
|
|
16
|
+
get(entity, input) {
|
|
17
|
+
const builder = this.getBuilder(entity);
|
|
18
|
+
const items = Array.isArray(input) ? input : [
|
|
19
|
+
input
|
|
20
|
+
];
|
|
21
|
+
for (const item of items){
|
|
22
|
+
if (this.table.name !== entity.table.name) throw new WebinyError("Cannot read from two different tables at the same time.");
|
|
23
|
+
this._items.push(builder.get(item));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
async execute() {
|
|
27
|
+
if (0 === this._items.length) return [];
|
|
28
|
+
const items = Array.from(this._items);
|
|
29
|
+
this._items.length = 0;
|
|
30
|
+
return await batchReadAll({
|
|
31
|
+
items,
|
|
32
|
+
table: this.table
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
getBuilder(entity) {
|
|
36
|
+
const builder = this.builders.get(entity.name);
|
|
37
|
+
if (builder) return builder;
|
|
38
|
+
const newBuilder = createEntityReadBatchBuilder(entity);
|
|
39
|
+
this.builders.set(entity.name, newBuilder);
|
|
40
|
+
return newBuilder;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const createTableReadBatch = (params)=>new TableReadBatch(params);
|
|
44
|
+
export { TableReadBatch, createTableReadBatch };
|
|
45
|
+
|
|
46
|
+
//# sourceMappingURL=TableReadBatch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils/table/TableReadBatch.js","sources":["../../../src/utils/table/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"],"names":["TableReadBatch","params","Map","Array","entity","input","builder","items","item","WebinyError","batchReadAll","newBuilder","createEntityReadBatchBuilder","createTableReadBatch"],"mappings":";;;AAeO,MAAMA;IAMT,YAAmBC,MAA6B,CAAE;aAHjC,MAAM,GAAyC,EAAE;aACjD,QAAQ,GAAyC,IAAIC;QAGlE,IAAI,CAAC,KAAK,GAAGD,OAAO,KAAK;IAC7B;IAEA,IAAW,QAAgB;QACvB,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM;IAC7B;IAEA,IAAW,QAA8C;QACrD,OAAOE,MAAM,IAAI,CAAC,IAAI,CAAC,MAAM;IACjC;IAEO,IAAIC,MAAc,EAAEC,KAAyB,EAAQ;QACxD,MAAMC,UAAU,IAAI,CAAC,UAAU,CAACF;QAEhC,MAAMG,QAAQJ,MAAM,OAAO,CAACE,SAASA,QAAQ;YAACA;SAAM;QACpD,KAAK,MAAMG,QAAQD,MAAO;YAItB,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,KAAKH,OAAO,KAAK,CAAE,IAAI,EACtC,MAAM,IAAIK,YAAY;YAG1B,IAAI,CAAC,MAAM,CAAC,IAAI,CAACH,QAAQ,GAAG,CAACE;QACjC;IACJ;IAEA,MAAa,UAA2C;QACpD,IAAI,AAAuB,MAAvB,IAAI,CAAC,MAAM,CAAC,MAAM,EAClB,OAAO,EAAE;QAEb,MAAMD,QAAQJ,MAAM,IAAI,CAAC,IAAI,CAAC,MAAM;QACpC,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG;QACrB,OAAO,MAAMO,aAAgB;YACzBH;YACA,OAAO,IAAI,CAAC,KAAK;QACrB;IACJ;IAEQ,WAAWH,MAAc,EAA2B;QACxD,MAAME,UAAU,IAAI,CAAC,QAAQ,CAAC,GAAG,CAACF,OAAO,IAAI;QAC7C,IAAIE,SACA,OAAOA;QAEX,MAAMK,aAAaC,6BAA6BR;QAChD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAACA,OAAO,IAAI,EAAEO;QAC/B,OAAOA;IACX;AACJ;AAEO,MAAME,uBAAuB,CAACZ,SAC1B,IAAID,eAAeC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
export interface ITableWriteBatchParams {
|
|
5
|
+
table: TableDef;
|
|
6
|
+
items?: BatchWriteItem[];
|
|
7
|
+
}
|
|
8
|
+
export declare class TableWriteBatch implements ITableWriteBatch {
|
|
9
|
+
private readonly table;
|
|
10
|
+
private readonly _items;
|
|
11
|
+
private readonly builders;
|
|
12
|
+
get total(): number;
|
|
13
|
+
get items(): BatchWriteItem[];
|
|
14
|
+
constructor(params: ITableWriteBatchParams);
|
|
15
|
+
put(entity: Entity, item: IPutBatchItem): void;
|
|
16
|
+
delete(entity: Entity, item: IDeleteBatchItem): void;
|
|
17
|
+
combine(items: BatchWriteItem[]): ITableWriteBatch;
|
|
18
|
+
execute(): Promise<BatchWriteResult>;
|
|
19
|
+
private getBuilder;
|
|
20
|
+
}
|
|
21
|
+
export declare const createTableWriteBatch: (params: ITableWriteBatchParams) => ITableWriteBatch;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { batchWriteAll } from "../batch/batchWrite.js";
|
|
2
|
+
import { createEntityWriteBatchBuilder } from "../entity/EntityWriteBatchBuilder.js";
|
|
3
|
+
class TableWriteBatch {
|
|
4
|
+
get total() {
|
|
5
|
+
return this._items.length;
|
|
6
|
+
}
|
|
7
|
+
get items() {
|
|
8
|
+
return Array.from(this._items);
|
|
9
|
+
}
|
|
10
|
+
constructor(params){
|
|
11
|
+
this._items = [];
|
|
12
|
+
this.builders = new Map();
|
|
13
|
+
this.table = params.table;
|
|
14
|
+
if (!params.items?.length) return;
|
|
15
|
+
this._items.push(...params.items);
|
|
16
|
+
}
|
|
17
|
+
put(entity, item) {
|
|
18
|
+
const builder = this.getBuilder(entity);
|
|
19
|
+
this._items.push(builder.put(item));
|
|
20
|
+
}
|
|
21
|
+
delete(entity, item) {
|
|
22
|
+
const builder = this.getBuilder(entity);
|
|
23
|
+
this._items.push(builder.delete(item));
|
|
24
|
+
}
|
|
25
|
+
combine(items) {
|
|
26
|
+
return createTableWriteBatch({
|
|
27
|
+
table: this.table,
|
|
28
|
+
items: this._items.concat(items)
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
async execute() {
|
|
32
|
+
if (0 === this._items.length) return [];
|
|
33
|
+
const items = Array.from(this._items);
|
|
34
|
+
this._items.length = 0;
|
|
35
|
+
return await batchWriteAll({
|
|
36
|
+
items,
|
|
37
|
+
table: this.table
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
getBuilder(entity) {
|
|
41
|
+
if (!entity.name) throw new Error("Entity must have a name.");
|
|
42
|
+
const builder = this.builders.get(entity.name);
|
|
43
|
+
if (builder) return builder;
|
|
44
|
+
const newBuilder = createEntityWriteBatchBuilder(entity);
|
|
45
|
+
this.builders.set(entity.name, newBuilder);
|
|
46
|
+
return newBuilder;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const createTableWriteBatch = (params)=>new TableWriteBatch(params);
|
|
50
|
+
export { TableWriteBatch, createTableWriteBatch };
|
|
51
|
+
|
|
52
|
+
//# sourceMappingURL=TableWriteBatch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils/table/TableWriteBatch.js","sources":["../../../src/utils/table/TableWriteBatch.ts"],"sourcesContent":["import type { Entity, TableDef } from \"~/toolbox.js\";\nimport type {\n BatchWriteItem,\n BatchWriteResult,\n IDeleteBatchItem,\n IPutBatchItem\n} from \"~/utils/batch/types.js\";\nimport type { IEntityWriteBatchBuilder } from \"~/utils/entity/types.js\";\nimport { batchWriteAll } from \"~/utils/batch/batchWrite.js\";\nimport { createEntityWriteBatchBuilder } from \"~/utils/entity/EntityWriteBatchBuilder.js\";\nimport type { ITableWriteBatch } from \"./types.js\";\n\nexport interface ITableWriteBatchParams {\n table: TableDef;\n items?: BatchWriteItem[];\n}\n\nexport class TableWriteBatch implements ITableWriteBatch {\n private readonly table: TableDef;\n private readonly _items: BatchWriteItem[] = [];\n private readonly builders: Map<string, IEntityWriteBatchBuilder> = new Map();\n\n public get total(): number {\n return this._items.length;\n }\n\n public get items(): BatchWriteItem[] {\n return Array.from(this._items);\n }\n\n public constructor(params: ITableWriteBatchParams) {\n this.table = params.table;\n if (!params.items?.length) {\n return;\n }\n this._items.push(...params.items);\n }\n\n public put(entity: Entity, item: IPutBatchItem): void {\n const builder = this.getBuilder(entity);\n this._items.push(builder.put(item));\n }\n\n public delete(entity: Entity, item: IDeleteBatchItem): void {\n const builder = this.getBuilder(entity);\n this._items.push(builder.delete(item));\n }\n\n public combine(items: BatchWriteItem[]): ITableWriteBatch {\n return createTableWriteBatch({\n table: this.table,\n items: this._items.concat(items)\n });\n }\n\n public async execute(): Promise<BatchWriteResult> {\n if (this._items.length === 0) {\n return [];\n }\n const items = Array.from(this._items);\n this._items.length = 0;\n return await batchWriteAll({\n items,\n table: this.table\n });\n }\n\n private getBuilder(entity: Entity): IEntityWriteBatchBuilder {\n if (!entity.name) {\n throw new Error(\"Entity must have a name.\");\n }\n const builder = this.builders.get(entity.name);\n if (builder) {\n return builder;\n }\n const newBuilder = createEntityWriteBatchBuilder(entity);\n this.builders.set(entity.name, newBuilder);\n return newBuilder;\n }\n}\n\nexport const createTableWriteBatch = (params: ITableWriteBatchParams): ITableWriteBatch => {\n return new TableWriteBatch(params);\n};\n"],"names":["TableWriteBatch","Array","params","Map","entity","item","builder","items","createTableWriteBatch","batchWriteAll","Error","newBuilder","createEntityWriteBatchBuilder"],"mappings":";;AAiBO,MAAMA;IAKT,IAAW,QAAgB;QACvB,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM;IAC7B;IAEA,IAAW,QAA0B;QACjC,OAAOC,MAAM,IAAI,CAAC,IAAI,CAAC,MAAM;IACjC;IAEA,YAAmBC,MAA8B,CAAE;aAXlC,MAAM,GAAqB,EAAE;aAC7B,QAAQ,GAA0C,IAAIC;QAWnE,IAAI,CAAC,KAAK,GAAGD,OAAO,KAAK;QACzB,IAAI,CAACA,OAAO,KAAK,EAAE,QACf;QAEJ,IAAI,CAAC,MAAM,CAAC,IAAI,IAAIA,OAAO,KAAK;IACpC;IAEO,IAAIE,MAAc,EAAEC,IAAmB,EAAQ;QAClD,MAAMC,UAAU,IAAI,CAAC,UAAU,CAACF;QAChC,IAAI,CAAC,MAAM,CAAC,IAAI,CAACE,QAAQ,GAAG,CAACD;IACjC;IAEO,OAAOD,MAAc,EAAEC,IAAsB,EAAQ;QACxD,MAAMC,UAAU,IAAI,CAAC,UAAU,CAACF;QAChC,IAAI,CAAC,MAAM,CAAC,IAAI,CAACE,QAAQ,MAAM,CAACD;IACpC;IAEO,QAAQE,KAAuB,EAAoB;QACtD,OAAOC,sBAAsB;YACzB,OAAO,IAAI,CAAC,KAAK;YACjB,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAACD;QAC9B;IACJ;IAEA,MAAa,UAAqC;QAC9C,IAAI,AAAuB,MAAvB,IAAI,CAAC,MAAM,CAAC,MAAM,EAClB,OAAO,EAAE;QAEb,MAAMA,QAAQN,MAAM,IAAI,CAAC,IAAI,CAAC,MAAM;QACpC,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG;QACrB,OAAO,MAAMQ,cAAc;YACvBF;YACA,OAAO,IAAI,CAAC,KAAK;QACrB;IACJ;IAEQ,WAAWH,MAAc,EAA4B;QACzD,IAAI,CAACA,OAAO,IAAI,EACZ,MAAM,IAAIM,MAAM;QAEpB,MAAMJ,UAAU,IAAI,CAAC,QAAQ,CAAC,GAAG,CAACF,OAAO,IAAI;QAC7C,IAAIE,SACA,OAAOA;QAEX,MAAMK,aAAaC,8BAA8BR;QACjD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAACA,OAAO,IAAI,EAAEO;QAC/B,OAAOA;IACX;AACJ;AAEO,MAAMH,wBAAwB,CAACN,SAC3B,IAAIF,gBAAgBE"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { BatchWriteItem, BatchWriteResult, IDeleteBatchItem, IPutBatchItem } from "../../utils/batch/types.js";
|
|
2
|
+
import type { BaseScanParams, ScanResponse } from "../scan.js";
|
|
3
|
+
import type { Entity } from "../../toolbox.js";
|
|
4
|
+
import type { GenericRecord } from "@webiny/api/types.js";
|
|
5
|
+
import { Table } from "../../toolbox.js";
|
|
6
|
+
export type ITableScanParams = BaseScanParams;
|
|
7
|
+
export type ITableScanResponse<T> = ScanResponse<T>;
|
|
8
|
+
export interface ITable<Name extends string = string, PK extends string = string, SK extends string = string> {
|
|
9
|
+
table: Table<Name, PK, SK>;
|
|
10
|
+
createWriter(): ITableWriteBatch;
|
|
11
|
+
createReader(): ITableReadBatch;
|
|
12
|
+
scan<T>(params: ITableScanParams): Promise<ITableScanResponse<T>>;
|
|
13
|
+
}
|
|
14
|
+
export interface ITableWriteBatch {
|
|
15
|
+
readonly total: number;
|
|
16
|
+
readonly items: BatchWriteItem[];
|
|
17
|
+
put(entity: Entity, item: IPutBatchItem): void;
|
|
18
|
+
delete(entity: Entity, item: IDeleteBatchItem): void;
|
|
19
|
+
execute(): Promise<BatchWriteResult>;
|
|
20
|
+
combine(items: BatchWriteItem[]): ITableWriteBatch;
|
|
21
|
+
}
|
|
22
|
+
export interface ITableReadBatchKey {
|
|
23
|
+
PK: string;
|
|
24
|
+
SK: string;
|
|
25
|
+
}
|
|
26
|
+
export interface ITableReadBatchBuilderGetResponse {
|
|
27
|
+
Table: Table<string, string, string>;
|
|
28
|
+
Key: ITableReadBatchKey;
|
|
29
|
+
}
|
|
30
|
+
export interface ITableReadBatchKey {
|
|
31
|
+
PK: string;
|
|
32
|
+
SK: string;
|
|
33
|
+
}
|
|
34
|
+
export interface ITableReadBatch {
|
|
35
|
+
readonly total: number;
|
|
36
|
+
readonly items: ITableReadBatchBuilderGetResponse[];
|
|
37
|
+
get(entity: Entity, input: ITableReadBatchKey | ITableReadBatchKey[]): void;
|
|
38
|
+
execute<T = GenericRecord>(): Promise<T[]>;
|
|
39
|
+
}
|
|
File without changes
|
package/BatchProcess.d.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Remove this when no apps are using our internal db drivers anymore
|
|
3
|
-
*/
|
|
4
|
-
import { DocumentClient } from "aws-sdk/clients/dynamodb";
|
|
5
|
-
import { Batch } from "@webiny/db";
|
|
6
|
-
declare type BatchType = "batchWrite" | "batchGet";
|
|
7
|
-
export declare type AddBatchOperationResponse = () => any | null;
|
|
8
|
-
interface RejectBuildCallable {
|
|
9
|
-
({ message }: {
|
|
10
|
-
message: string;
|
|
11
|
-
}): void;
|
|
12
|
-
}
|
|
13
|
-
interface RejectExecutionCallable {
|
|
14
|
-
({ message }: {
|
|
15
|
-
message: string;
|
|
16
|
-
}): void;
|
|
17
|
-
}
|
|
18
|
-
interface AddBatchOperationArgs {
|
|
19
|
-
/**
|
|
20
|
-
* TODO: determine correct type.
|
|
21
|
-
*/
|
|
22
|
-
[key: string]: any;
|
|
23
|
-
}
|
|
24
|
-
declare class BatchProcess {
|
|
25
|
-
documentClient: DocumentClient;
|
|
26
|
-
batch: Batch;
|
|
27
|
-
resolveBuild: () => void;
|
|
28
|
-
rejectBuild: RejectBuildCallable;
|
|
29
|
-
queryBuild: Promise<void>;
|
|
30
|
-
resolveExecution: () => void;
|
|
31
|
-
rejectExecution: RejectExecutionCallable;
|
|
32
|
-
queryExecution: Promise<void>;
|
|
33
|
-
operations: [Record<string, any>, Record<string, any>][];
|
|
34
|
-
batchType: BatchType;
|
|
35
|
-
results: Record<string, any>[];
|
|
36
|
-
response: Record<string, any>;
|
|
37
|
-
constructor(batch: Batch, documentClient: DocumentClient);
|
|
38
|
-
waitStartExecution(): Promise<void>;
|
|
39
|
-
waitExecution(): Promise<void>;
|
|
40
|
-
addBatchOperation(type: BatchType, args: AddBatchOperationArgs, meta?: {}): AddBatchOperationResponse;
|
|
41
|
-
addBatchWrite(args: AddBatchOperationArgs): AddBatchOperationResponse;
|
|
42
|
-
addBatchDelete(args: AddBatchOperationArgs): AddBatchOperationResponse;
|
|
43
|
-
addBatchGet(args: AddBatchOperationArgs): AddBatchOperationResponse;
|
|
44
|
-
allOperationsAdded(): boolean;
|
|
45
|
-
startExecution(): import("aws-sdk/lib/request").Request<DocumentClient.BatchWriteItemOutput, import("aws-sdk/lib/error").AWSError> | import("aws-sdk/lib/request").Request<DocumentClient.BatchGetItemOutput, import("aws-sdk/lib/error").AWSError>;
|
|
46
|
-
}
|
|
47
|
-
export default BatchProcess;
|
package/BatchProcess.js
DELETED
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", {
|
|
6
|
-
value: true
|
|
7
|
-
});
|
|
8
|
-
exports.default = void 0;
|
|
9
|
-
|
|
10
|
-
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
11
|
-
|
|
12
|
-
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Remove this when no apps are using our internal db drivers anymore
|
|
16
|
-
*/
|
|
17
|
-
// @ts-nocheck
|
|
18
|
-
class BatchProcess {
|
|
19
|
-
constructor(batch, documentClient) {
|
|
20
|
-
(0, _defineProperty2.default)(this, "documentClient", void 0);
|
|
21
|
-
(0, _defineProperty2.default)(this, "batch", void 0);
|
|
22
|
-
(0, _defineProperty2.default)(this, "resolveBuild", void 0);
|
|
23
|
-
(0, _defineProperty2.default)(this, "rejectBuild", void 0);
|
|
24
|
-
(0, _defineProperty2.default)(this, "queryBuild", void 0);
|
|
25
|
-
(0, _defineProperty2.default)(this, "resolveExecution", void 0);
|
|
26
|
-
(0, _defineProperty2.default)(this, "rejectExecution", void 0);
|
|
27
|
-
(0, _defineProperty2.default)(this, "queryExecution", void 0);
|
|
28
|
-
(0, _defineProperty2.default)(this, "operations", void 0);
|
|
29
|
-
(0, _defineProperty2.default)(this, "batchType", void 0);
|
|
30
|
-
(0, _defineProperty2.default)(this, "results", void 0);
|
|
31
|
-
(0, _defineProperty2.default)(this, "response", void 0);
|
|
32
|
-
this.documentClient = documentClient;
|
|
33
|
-
this.batch = batch;
|
|
34
|
-
this.resolveBuild = null;
|
|
35
|
-
this.rejectBuild = null;
|
|
36
|
-
this.queryBuild = new Promise((resolve, reject) => {
|
|
37
|
-
this.resolveBuild = resolve;
|
|
38
|
-
this.rejectBuild = reject;
|
|
39
|
-
});
|
|
40
|
-
this.resolveExecution = null;
|
|
41
|
-
this.rejectExecution = null;
|
|
42
|
-
this.queryExecution = new Promise((resolve, reject) => {
|
|
43
|
-
this.resolveExecution = resolve;
|
|
44
|
-
this.rejectExecution = reject;
|
|
45
|
-
});
|
|
46
|
-
this.operations = [];
|
|
47
|
-
this.results = [];
|
|
48
|
-
this.response = [];
|
|
49
|
-
this.batchType;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
waitStartExecution() {
|
|
53
|
-
return this.queryBuild;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
waitExecution() {
|
|
57
|
-
return this.queryExecution;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
addBatchOperation(type, args, meta = {}) {
|
|
61
|
-
if (!this.batchType) {
|
|
62
|
-
this.batchType = type;
|
|
63
|
-
} else if (this.batchType !== type) {
|
|
64
|
-
const initial = this.batchType;
|
|
65
|
-
const index = this.operations.length;
|
|
66
|
-
this.rejectBuild({
|
|
67
|
-
message: `Cannot batch operations - all operations must be of the same type (the initial operation type was "${initial}", and operation type on index "${index}" is "${type}").`
|
|
68
|
-
});
|
|
69
|
-
return null;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
this.operations.push([args, meta]);
|
|
73
|
-
const index = this.operations.length - 1;
|
|
74
|
-
return () => this.results[index];
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
addBatchWrite(args) {
|
|
78
|
-
return this.addBatchOperation("batchWrite", args);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
addBatchDelete(args) {
|
|
82
|
-
return this.addBatchOperation("batchWrite", (0, _objectSpread2.default)({}, args), {
|
|
83
|
-
delete: true
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
addBatchGet(args) {
|
|
88
|
-
return this.addBatchOperation("batchGet", args);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
allOperationsAdded() {
|
|
92
|
-
return this.operations.length === this.batch.operations.length;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
startExecution() {
|
|
96
|
-
this.resolveBuild();
|
|
97
|
-
const documentClientArgs = {
|
|
98
|
-
ReturnConsumedCapacity: "TOTAL",
|
|
99
|
-
RequestItems: {}
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
const reject = e => {
|
|
103
|
-
e.message = `An error occurred while executing "${this.batchType}" batch operation: ${e.message}`;
|
|
104
|
-
return this.rejectExecution(e);
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
let resolve = response => {
|
|
108
|
-
this.response = response;
|
|
109
|
-
this.resolveExecution();
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
switch (this.batchType) {
|
|
113
|
-
case "batchWrite":
|
|
114
|
-
documentClientArgs.RequestItems = {};
|
|
115
|
-
|
|
116
|
-
for (let i = 0; i < this.operations.length; i++) {
|
|
117
|
-
const [args, meta] = this.operations[i];
|
|
118
|
-
|
|
119
|
-
if (!documentClientArgs.RequestItems[args.table]) {
|
|
120
|
-
documentClientArgs.RequestItems[args.table] = [];
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const push = {};
|
|
124
|
-
|
|
125
|
-
if (meta.delete) {
|
|
126
|
-
push.DeleteRequest = {
|
|
127
|
-
Key: args.query
|
|
128
|
-
};
|
|
129
|
-
} else {
|
|
130
|
-
push.PutRequest = {
|
|
131
|
-
Item: args.data
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
documentClientArgs.RequestItems[args.table].push(push);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
break;
|
|
139
|
-
|
|
140
|
-
case "batchGet":
|
|
141
|
-
documentClientArgs.RequestItems = {};
|
|
142
|
-
|
|
143
|
-
for (let i = 0; i < this.operations.length; i++) {
|
|
144
|
-
const [args] = this.operations[i];
|
|
145
|
-
|
|
146
|
-
if (!documentClientArgs.RequestItems[args.table]) {
|
|
147
|
-
documentClientArgs.RequestItems[args.table] = {
|
|
148
|
-
Keys: []
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
documentClientArgs.RequestItems[args.table].Keys.push(args.query);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
resolve = response => {
|
|
156
|
-
this.response = response;
|
|
157
|
-
const results = []; // The results of batchGet aren't ordered so we have to figure out the order of results ourselves.
|
|
158
|
-
|
|
159
|
-
for (let i = 0; i < this.operations.length; i++) {
|
|
160
|
-
const [args] = this.operations[i];
|
|
161
|
-
const responseItems = response.Responses[args.table];
|
|
162
|
-
let foundResult = null;
|
|
163
|
-
|
|
164
|
-
outer: for (let j = 0; j < responseItems.length; j++) {
|
|
165
|
-
const responseItem = responseItems[j];
|
|
166
|
-
|
|
167
|
-
for (const queryKey in args.query) {
|
|
168
|
-
if (typeof responseItem[queryKey] === "undefined" || args.query[queryKey] !== responseItem[queryKey]) {
|
|
169
|
-
continue outer;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
foundResult = responseItem;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
results.push(foundResult);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
this.results = results;
|
|
180
|
-
this.resolveExecution();
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
break;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
return this.documentClient[this.batchType](documentClientArgs, (error, result) => {
|
|
187
|
-
if (error) {
|
|
188
|
-
reject(error);
|
|
189
|
-
} else {
|
|
190
|
-
resolve(result);
|
|
191
|
-
}
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
var _default = BatchProcess;
|
|
198
|
-
exports.default = _default;
|
package/BatchProcess.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":["BatchProcess","constructor","batch","documentClient","resolveBuild","rejectBuild","queryBuild","Promise","resolve","reject","resolveExecution","rejectExecution","queryExecution","operations","results","response","batchType","waitStartExecution","waitExecution","addBatchOperation","type","args","meta","initial","index","length","message","push","addBatchWrite","addBatchDelete","delete","addBatchGet","allOperationsAdded","startExecution","documentClientArgs","ReturnConsumedCapacity","RequestItems","e","i","table","DeleteRequest","Key","query","PutRequest","Item","data","Keys","responseItems","Responses","foundResult","outer","j","responseItem","queryKey","error","result"],"sources":["BatchProcess.ts"],"sourcesContent":["/**\n * Remove this when no apps are using our internal db drivers anymore\n */\n// @ts-nocheck\nimport { DocumentClient } from \"aws-sdk/clients/dynamodb\";\nimport { Batch } from \"@webiny/db\";\n\ntype BatchType = \"batchWrite\" | \"batchGet\";\n\nexport type AddBatchOperationResponse = () => any | null;\n\ninterface RejectBuildCallable {\n ({ message }: { message: string }): void;\n}\n\ninterface RejectExecutionCallable {\n ({ message }: { message: string }): void;\n}\n\ninterface AddBatchOperationArgs {\n /**\n * TODO: determine correct type.\n */\n [key: string]: any;\n}\n\ninterface Response {\n /**\n * TODO: determine correct type.\n */\n [key: string]: any;\n}\n\ninterface DocumentClientArgs {\n ReturnConsumedCapacity: string;\n RequestItems: Record<string, any>;\n}\n\nclass BatchProcess {\n documentClient: DocumentClient;\n batch: Batch;\n resolveBuild: () => void;\n rejectBuild: RejectBuildCallable;\n queryBuild: Promise<void>;\n resolveExecution: () => void;\n rejectExecution: RejectExecutionCallable;\n queryExecution: Promise<void>;\n operations: [Record<string, any>, Record<string, any>][];\n batchType: BatchType;\n results: Record<string, any>[];\n response: Record<string, any>;\n constructor(batch: Batch, documentClient: DocumentClient) {\n this.documentClient = documentClient;\n this.batch = batch;\n\n this.resolveBuild = null;\n this.rejectBuild = null;\n this.queryBuild = new Promise((resolve, reject) => {\n this.resolveBuild = resolve;\n this.rejectBuild = reject;\n });\n\n this.resolveExecution = null;\n this.rejectExecution = null;\n this.queryExecution = new Promise((resolve, reject) => {\n this.resolveExecution = resolve;\n this.rejectExecution = reject;\n });\n\n this.operations = [];\n this.results = [];\n this.response = [];\n\n this.batchType;\n }\n\n waitStartExecution(): Promise<void> {\n return this.queryBuild;\n }\n\n waitExecution(): Promise<void> {\n return this.queryExecution;\n }\n\n addBatchOperation(\n type: BatchType,\n args: AddBatchOperationArgs,\n meta = {}\n ): AddBatchOperationResponse {\n if (!this.batchType) {\n this.batchType = type;\n } else if (this.batchType !== type) {\n const initial = this.batchType;\n const index = this.operations.length;\n this.rejectBuild({\n message: `Cannot batch operations - all operations must be of the same type (the initial operation type was \"${initial}\", and operation type on index \"${index}\" is \"${type}\").`\n });\n return null;\n }\n\n this.operations.push([args, meta]);\n const index = this.operations.length - 1;\n return () => this.results[index];\n }\n\n addBatchWrite(args: AddBatchOperationArgs): AddBatchOperationResponse {\n return this.addBatchOperation(\"batchWrite\", args);\n }\n\n addBatchDelete(args: AddBatchOperationArgs): AddBatchOperationResponse {\n return this.addBatchOperation(\"batchWrite\", { ...args }, { delete: true });\n }\n\n addBatchGet(args: AddBatchOperationArgs): AddBatchOperationResponse {\n return this.addBatchOperation(\"batchGet\", args);\n }\n\n allOperationsAdded(): boolean {\n return this.operations.length === this.batch.operations.length;\n }\n\n startExecution() {\n this.resolveBuild();\n\n const documentClientArgs: DocumentClientArgs = {\n ReturnConsumedCapacity: \"TOTAL\",\n RequestItems: {}\n };\n\n const reject = (e: Error) => {\n e.message = `An error occurred while executing \"${this.batchType}\" batch operation: ${e.message}`;\n return this.rejectExecution(e);\n };\n\n let resolve = (response: Response) => {\n this.response = response;\n this.resolveExecution();\n };\n\n switch (this.batchType) {\n case \"batchWrite\":\n documentClientArgs.RequestItems = {};\n for (let i = 0; i < this.operations.length; i++) {\n const [args, meta] = this.operations[i];\n\n if (!documentClientArgs.RequestItems[args.table]) {\n documentClientArgs.RequestItems[args.table] = [];\n }\n\n const push: {\n DeleteRequest?: Record<string, any>;\n PutRequest?: Record<string, any>;\n } = {};\n\n if (meta.delete) {\n push.DeleteRequest = {\n Key: args.query\n };\n } else {\n push.PutRequest = {\n Item: args.data\n };\n }\n\n documentClientArgs.RequestItems[args.table].push(push);\n }\n break;\n case \"batchGet\":\n documentClientArgs.RequestItems = {};\n for (let i = 0; i < this.operations.length; i++) {\n const [args] = this.operations[i];\n\n if (!documentClientArgs.RequestItems[args.table]) {\n documentClientArgs.RequestItems[args.table] = { Keys: [] };\n }\n\n documentClientArgs.RequestItems[args.table].Keys.push(args.query);\n }\n\n resolve = response => {\n this.response = response;\n const results = [];\n\n // The results of batchGet aren't ordered so we have to figure out the order of results ourselves.\n for (let i = 0; i < this.operations.length; i++) {\n const [args] = this.operations[i];\n const responseItems = response.Responses[args.table];\n\n let foundResult = null;\n outer: for (let j = 0; j < responseItems.length; j++) {\n const responseItem = responseItems[j];\n for (const queryKey in args.query) {\n if (\n typeof responseItem[queryKey] === \"undefined\" ||\n args.query[queryKey] !== responseItem[queryKey]\n ) {\n continue outer;\n }\n }\n foundResult = responseItem;\n }\n\n results.push(foundResult);\n }\n\n this.results = results;\n this.resolveExecution();\n };\n break;\n }\n\n return this.documentClient[this.batchType](\n documentClientArgs,\n (error: Error, result: Record<string, any>) => {\n if (error) {\n reject(error);\n } else {\n resolve(result);\n }\n }\n );\n }\n}\n\nexport default BatchProcess;\n"],"mappings":"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"}
|
package/QueryGenerator.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { Query, QueryKey, QueryKeys, QuerySort } from "./types";
|
|
2
|
-
interface GenerateParams {
|
|
3
|
-
query: Query;
|
|
4
|
-
keys: QueryKeys;
|
|
5
|
-
sort: QuerySort;
|
|
6
|
-
limit: number;
|
|
7
|
-
tableName: string;
|
|
8
|
-
}
|
|
9
|
-
declare class QueryGenerator {
|
|
10
|
-
generate(params: GenerateParams): {
|
|
11
|
-
TableName: string;
|
|
12
|
-
Limit: number;
|
|
13
|
-
KeyConditionExpression: string;
|
|
14
|
-
ExpressionAttributeNames: Record<string, any>;
|
|
15
|
-
ExpressionAttributeValues: Record<string, any>;
|
|
16
|
-
ScanIndexForward: boolean;
|
|
17
|
-
IndexName: string;
|
|
18
|
-
};
|
|
19
|
-
findQueryKey(query?: Query, keys?: QueryKeys): QueryKey | null;
|
|
20
|
-
}
|
|
21
|
-
export default QueryGenerator;
|
package/QueryGenerator.js
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", {
|
|
6
|
-
value: true
|
|
7
|
-
});
|
|
8
|
-
exports.default = void 0;
|
|
9
|
-
|
|
10
|
-
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
11
|
-
|
|
12
|
-
var _createKeyConditionExpressionArgs = _interopRequireDefault(require("./statements/createKeyConditionExpressionArgs"));
|
|
13
|
-
|
|
14
|
-
class QueryGenerator {
|
|
15
|
-
generate(params) {
|
|
16
|
-
const {
|
|
17
|
-
query,
|
|
18
|
-
keys,
|
|
19
|
-
sort,
|
|
20
|
-
limit,
|
|
21
|
-
tableName
|
|
22
|
-
} = params; // 1. Which key can we use in this query operation?
|
|
23
|
-
|
|
24
|
-
const key = this.findQueryKey(query, keys);
|
|
25
|
-
|
|
26
|
-
if (!key) {
|
|
27
|
-
throw new Error("Cannot perform query - key not found.");
|
|
28
|
-
} // 2. Now that we know the key, let's separate the key attributes from the rest.
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const keyAttributesValues = {};
|
|
32
|
-
const nonKeyAttributesValues = {};
|
|
33
|
-
|
|
34
|
-
for (const queryKey in query) {
|
|
35
|
-
if (key.fields.find(item => item.name === queryKey)) {
|
|
36
|
-
keyAttributesValues[queryKey] = query[queryKey];
|
|
37
|
-
} else {
|
|
38
|
-
nonKeyAttributesValues[queryKey] = query[queryKey];
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const keyConditionExpression = (0, _createKeyConditionExpressionArgs.default)({
|
|
43
|
-
query: keyAttributesValues,
|
|
44
|
-
sort,
|
|
45
|
-
key
|
|
46
|
-
});
|
|
47
|
-
return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, keyConditionExpression), {}, {
|
|
48
|
-
TableName: tableName,
|
|
49
|
-
Limit: limit
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
findQueryKey(query = {}, keys = []) {
|
|
54
|
-
for (let i = 0; i < keys.length; i++) {
|
|
55
|
-
const key = keys[i];
|
|
56
|
-
let hasAllFields = true;
|
|
57
|
-
|
|
58
|
-
for (let j = 0; j < key.fields.length; j++) {
|
|
59
|
-
const field = key.fields[j];
|
|
60
|
-
|
|
61
|
-
if (!query[field.name]) {
|
|
62
|
-
hasAllFields = false;
|
|
63
|
-
break;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (hasAllFields) {
|
|
68
|
-
return key;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return null;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
var _default = QueryGenerator;
|
|
78
|
-
exports.default = _default;
|
package/QueryGenerator.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":["QueryGenerator","generate","params","query","keys","sort","limit","tableName","key","findQueryKey","Error","keyAttributesValues","nonKeyAttributesValues","queryKey","fields","find","item","name","keyConditionExpression","createKeyConditionExpressionArgs","TableName","Limit","i","length","hasAllFields","j","field"],"sources":["QueryGenerator.ts"],"sourcesContent":["import createKeyConditionExpressionArgs from \"./statements/createKeyConditionExpressionArgs\";\nimport { Query, QueryKey, QueryKeyField, QueryKeys, QuerySort } from \"~/types\";\n\ninterface GenerateParams {\n query: Query;\n keys: QueryKeys;\n sort: QuerySort;\n limit: number;\n tableName: string;\n}\nclass QueryGenerator {\n generate(params: GenerateParams) {\n const { query, keys, sort, limit, tableName } = params;\n // 1. Which key can we use in this query operation?\n const key = this.findQueryKey(query, keys);\n\n if (!key) {\n throw new Error(\"Cannot perform query - key not found.\");\n }\n\n // 2. Now that we know the key, let's separate the key attributes from the rest.\n const keyAttributesValues: Record<string, string> = {};\n const nonKeyAttributesValues: Record<string, string> = {};\n for (const queryKey in query) {\n if (key.fields.find((item: QueryKeyField) => item.name === queryKey)) {\n keyAttributesValues[queryKey] = query[queryKey];\n } else {\n nonKeyAttributesValues[queryKey] = query[queryKey];\n }\n }\n\n const keyConditionExpression = createKeyConditionExpressionArgs({\n query: keyAttributesValues,\n sort,\n key\n });\n\n return { ...keyConditionExpression, TableName: tableName, Limit: limit };\n }\n\n findQueryKey(query: Query = {}, keys: QueryKeys = []): QueryKey | null {\n for (let i = 0; i < keys.length; i++) {\n const key = keys[i];\n let hasAllFields = true;\n for (let j = 0; j < key.fields.length; j++) {\n const field = key.fields[j];\n if (!query[field.name]) {\n hasAllFields = false;\n break;\n }\n }\n\n if (hasAllFields) {\n return key;\n }\n }\n return null;\n }\n}\n\nexport default QueryGenerator;\n"],"mappings":";;;;;;;;;;;AAAA;;AAUA,MAAMA,cAAN,CAAqB;EACjBC,QAAQ,CAACC,MAAD,EAAyB;IAC7B,MAAM;MAAEC,KAAF;MAASC,IAAT;MAAeC,IAAf;MAAqBC,KAArB;MAA4BC;IAA5B,IAA0CL,MAAhD,CAD6B,CAE7B;;IACA,MAAMM,GAAG,GAAG,KAAKC,YAAL,CAAkBN,KAAlB,EAAyBC,IAAzB,CAAZ;;IAEA,IAAI,CAACI,GAAL,EAAU;MACN,MAAM,IAAIE,KAAJ,CAAU,uCAAV,CAAN;IACH,CAP4B,CAS7B;;;IACA,MAAMC,mBAA2C,GAAG,EAApD;IACA,MAAMC,sBAA8C,GAAG,EAAvD;;IACA,KAAK,MAAMC,QAAX,IAAuBV,KAAvB,EAA8B;MAC1B,IAAIK,GAAG,CAACM,MAAJ,CAAWC,IAAX,CAAiBC,IAAD,IAAyBA,IAAI,CAACC,IAAL,KAAcJ,QAAvD,CAAJ,EAAsE;QAClEF,mBAAmB,CAACE,QAAD,CAAnB,GAAgCV,KAAK,CAACU,QAAD,CAArC;MACH,CAFD,MAEO;QACHD,sBAAsB,CAACC,QAAD,CAAtB,GAAmCV,KAAK,CAACU,QAAD,CAAxC;MACH;IACJ;;IAED,MAAMK,sBAAsB,GAAG,IAAAC,yCAAA,EAAiC;MAC5DhB,KAAK,EAAEQ,mBADqD;MAE5DN,IAF4D;MAG5DG;IAH4D,CAAjC,CAA/B;IAMA,mEAAYU,sBAAZ;MAAoCE,SAAS,EAAEb,SAA/C;MAA0Dc,KAAK,EAAEf;IAAjE;EACH;;EAEDG,YAAY,CAACN,KAAY,GAAG,EAAhB,EAAoBC,IAAe,GAAG,EAAtC,EAA2D;IACnE,KAAK,IAAIkB,CAAC,GAAG,CAAb,EAAgBA,CAAC,GAAGlB,IAAI,CAACmB,MAAzB,EAAiCD,CAAC,EAAlC,EAAsC;MAClC,MAAMd,GAAG,GAAGJ,IAAI,CAACkB,CAAD,CAAhB;MACA,IAAIE,YAAY,GAAG,IAAnB;;MACA,KAAK,IAAIC,CAAC,GAAG,CAAb,EAAgBA,CAAC,GAAGjB,GAAG,CAACM,MAAJ,CAAWS,MAA/B,EAAuCE,CAAC,EAAxC,EAA4C;QACxC,MAAMC,KAAK,GAAGlB,GAAG,CAACM,MAAJ,CAAWW,CAAX,CAAd;;QACA,IAAI,CAACtB,KAAK,CAACuB,KAAK,CAACT,IAAP,CAAV,EAAwB;UACpBO,YAAY,GAAG,KAAf;UACA;QACH;MACJ;;MAED,IAAIA,YAAJ,EAAkB;QACd,OAAOhB,GAAP;MACH;IACJ;;IACD,OAAO,IAAP;EACH;;AA/CgB;;eAkDNR,c"}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
const beginsWith = {
|
|
8
|
-
canProcess: ({
|
|
9
|
-
value
|
|
10
|
-
}) => {
|
|
11
|
-
return value && typeof value["$beginsWith"] !== "undefined";
|
|
12
|
-
},
|
|
13
|
-
process: ({
|
|
14
|
-
key,
|
|
15
|
-
value,
|
|
16
|
-
args
|
|
17
|
-
}) => {
|
|
18
|
-
args.expression += `begins_with (#${key}, :${key})`;
|
|
19
|
-
args.attributeNames[`#${key}`] = key;
|
|
20
|
-
args.attributeValues[`:${key}`] = value["$beginsWith"];
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
var _default = beginsWith;
|
|
24
|
-
exports.default = _default;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":["beginsWith","canProcess","value","process","key","args","expression","attributeNames","attributeValues"],"sources":["beginsWith.ts"],"sourcesContent":["import { Operator } from \"~/types\";\n\nconst beginsWith: Operator = {\n canProcess: ({ value }) => {\n return value && typeof value[\"$beginsWith\"] !== \"undefined\";\n },\n process: ({ key, value, args }) => {\n args.expression += `begins_with (#${key}, :${key})`;\n args.attributeNames[`#${key}`] = key;\n args.attributeValues[`:${key}`] = value[\"$beginsWith\"];\n }\n};\n\nexport default beginsWith;\n"],"mappings":";;;;;;AAEA,MAAMA,UAAoB,GAAG;EACzBC,UAAU,EAAE,CAAC;IAAEC;EAAF,CAAD,KAAe;IACvB,OAAOA,KAAK,IAAI,OAAOA,KAAK,CAAC,aAAD,CAAZ,KAAgC,WAAhD;EACH,CAHwB;EAIzBC,OAAO,EAAE,CAAC;IAAEC,GAAF;IAAOF,KAAP;IAAcG;EAAd,CAAD,KAA0B;IAC/BA,IAAI,CAACC,UAAL,IAAoB,iBAAgBF,GAAI,MAAKA,GAAI,GAAjD;IACAC,IAAI,CAACE,cAAL,CAAqB,IAAGH,GAAI,EAA5B,IAAiCA,GAAjC;IACAC,IAAI,CAACG,eAAL,CAAsB,IAAGJ,GAAI,EAA7B,IAAkCF,KAAK,CAAC,aAAD,CAAvC;EACH;AARwB,CAA7B;eAWeF,U"}
|