@webiny/db-dynamodb 0.0.0-unstable.9e825fd5fb β 0.0.0-unstable.9f53ea597d
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 +216 -227
- package/DynamoDbDriver.js.map +1 -1
- package/README.md +6 -62
- package/exports/api/db.d.ts +3 -0
- package/exports/api/db.js +3 -0
- package/feature/DynamoDBClient/DynamoDBClient.d.ts +9 -0
- package/feature/DynamoDBClient/DynamoDBClient.js +8 -0
- package/feature/DynamoDBClient/DynamoDBClient.js.map +1 -0
- package/feature/DynamoDBClient/abstractions.d.ts +8 -0
- package/feature/DynamoDBClient/abstractions.js +5 -0
- package/feature/DynamoDBClient/abstractions.js.map +1 -0
- package/feature/DynamoDBClient/index.d.ts +6 -0
- package/feature/DynamoDBClient/index.js +15 -0
- package/feature/DynamoDBClient/index.js.map +1 -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/index.d.ts +3 -0
- package/feature/ValueFilter/index.js +1 -0
- package/index.d.ts +10 -3
- package/index.js +14 -35
- package/index.js.map +1 -1
- package/package.json +32 -30
- package/plugins/definitions/DateTimeTransformPlugin.d.ts +2 -1
- package/plugins/definitions/DateTimeTransformPlugin.js +22 -37
- package/plugins/definitions/DateTimeTransformPlugin.js.map +1 -1
- package/plugins/definitions/FieldPlugin.d.ts +2 -2
- package/plugins/definitions/FieldPlugin.js +37 -53
- package/plugins/definitions/FieldPlugin.js.map +1 -1
- package/plugins/definitions/TimeTransformPlugin.d.ts +2 -1
- package/plugins/definitions/TimeTransformPlugin.js +30 -43
- package/plugins/definitions/TimeTransformPlugin.js.map +1 -1
- package/plugins/definitions/ValueTransformPlugin.d.ts +2 -7
- package/plugins/definitions/ValueTransformPlugin.js +21 -34
- package/plugins/definitions/ValueTransformPlugin.js.map +1 -1
- package/plugins/definitions/assignFields.js +16 -26
- package/plugins/definitions/assignFields.js.map +1 -1
- package/plugins/index.d.ts +5 -2
- package/plugins/index.js +5 -15
- 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 +3 -24
- 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 -31
- 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 +9 -18
- 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 +16 -25
- 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 +15 -14
- package/utils/query.js +83 -107
- 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 +39 -68
- 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 -164
- package/BatchProcess.js.map +0 -1
- package/QueryGenerator.d.ts +0 -21
- package/QueryGenerator.js +0 -64
- 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 -32
- 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 -28
- package/operators/index.js.map +0 -1
- package/operators/logical/and.d.ts +0 -3
- package/operators/logical/and.js +0 -58
- package/operators/logical/and.js.map +0 -1
- package/operators/logical/or.d.ts +0 -3
- package/operators/logical/or.js +0 -58
- package/operators/logical/or.js.map +0 -1
- package/plugins/definitions/AttributePlugin.d.ts +0 -20
- package/plugins/definitions/AttributePlugin.js +0 -48
- package/plugins/definitions/AttributePlugin.js.map +0 -1
- package/plugins/definitions/FieldPathPlugin.d.ts +0 -22
- package/plugins/definitions/FieldPathPlugin.js +0 -34
- package/plugins/definitions/FieldPathPlugin.js.map +0 -1
- package/plugins/definitions/NumberTransformPlugin.d.ts +0 -4
- package/plugins/definitions/NumberTransformPlugin.js +0 -34
- package/plugins/definitions/NumberTransformPlugin.js.map +0 -1
- package/plugins/definitions/ValueFilterPlugin.d.ts +0 -20
- package/plugins/definitions/ValueFilterPlugin.js +0 -37
- package/plugins/definitions/ValueFilterPlugin.js.map +0 -1
- package/plugins/filters/andIn.d.ts +0 -3
- package/plugins/filters/andIn.js +0 -30
- package/plugins/filters/andIn.js.map +0 -1
- package/plugins/filters/between.d.ts +0 -3
- package/plugins/filters/between.js +0 -31
- package/plugins/filters/between.js.map +0 -1
- package/plugins/filters/contains.d.ts +0 -3
- package/plugins/filters/contains.js +0 -29
- package/plugins/filters/contains.js.map +0 -1
- package/plugins/filters/eq.d.ts +0 -3
- package/plugins/filters/eq.js +0 -31
- package/plugins/filters/eq.js.map +0 -1
- package/plugins/filters/fuzzy.d.ts +0 -3
- package/plugins/filters/fuzzy.js +0 -26
- package/plugins/filters/fuzzy.js.map +0 -1
- package/plugins/filters/gt.d.ts +0 -3
- package/plugins/filters/gt.js +0 -19
- package/plugins/filters/gt.js.map +0 -1
- package/plugins/filters/gte.d.ts +0 -3
- package/plugins/filters/gte.js +0 -19
- package/plugins/filters/gte.js.map +0 -1
- package/plugins/filters/in.d.ts +0 -3
- package/plugins/filters/in.js +0 -30
- package/plugins/filters/in.js.map +0 -1
- package/plugins/filters/index.d.ts +0 -2
- package/plugins/filters/index.js +0 -20
- package/plugins/filters/index.js.map +0 -1
- package/plugins/filters/lt.d.ts +0 -3
- package/plugins/filters/lt.js +0 -19
- package/plugins/filters/lt.js.map +0 -1
- package/plugins/filters/lte.d.ts +0 -3
- package/plugins/filters/lte.js +0 -19
- package/plugins/filters/lte.js.map +0 -1
- package/plugins/filters/startsWith.d.ts +0 -3
- package/plugins/filters/startsWith.js +0 -29
- 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 -45
- package/statements/createKeyConditionExpressionArgs.js.map +0 -1
- package/statements/processStatement.d.ts +0 -3
- package/statements/processStatement.js +0 -35
- 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 -24
- package/utils/attributes.js.map +0 -1
- package/utils/batchRead.d.ts +0 -15
- package/utils/batchRead.js +0 -67
- package/utils/batchRead.js.map +0 -1
- package/utils/batchWrite.d.ts +0 -17
- package/utils/batchWrite.js +0 -24
- package/utils/batchWrite.js.map +0 -1
- package/utils/createStandardEntity.d.ts +0 -2
- package/utils/createStandardEntity.js +0 -34
- package/utils/createStandardEntity.js.map +0 -1
- package/utils/filter.d.ts +0 -12
- package/utils/filter.js +0 -176
- package/utils/filter.js.map +0 -1
- package/utils/listResponse.d.ts +0 -13
- package/utils/listResponse.js +0 -29
- package/utils/listResponse.js.map +0 -1
- package/utils/table.d.ts +0 -7
- package/utils/table.js +0 -22
- package/utils/table.js.map +0 -1
package/DynamoDbDriver.d.ts
CHANGED
|
@@ -1,35 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import { DbDriver, Args, Result, ArgsBatch } from "@webiny/db";
|
|
7
|
-
declare type ConstructorArgs = {
|
|
8
|
-
documentClient?: DocumentClient;
|
|
9
|
-
};
|
|
10
|
-
interface ReadLogsParams {
|
|
11
|
-
table: string;
|
|
1
|
+
import type { DynamoDBDocument } from "@webiny/aws-sdk/client-dynamodb/index.js";
|
|
2
|
+
import type { DbDriver, GetValueResult, GetValuesResult, IListValuesParams, ListValuesResult, RemoveValueResult, RemoveValuesResult, StorageKey, StoreValueResult, StoreValuesResult } from "@webiny/db";
|
|
3
|
+
import type { GenericRecord } from "@webiny/api/types.js";
|
|
4
|
+
interface ConstructorArgs {
|
|
5
|
+
documentClient: DynamoDBDocument;
|
|
12
6
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
getClient(): DocumentClient;
|
|
27
|
-
create({ table, data, meta, __batch: batch }: Args): Promise<Result>;
|
|
28
|
-
update({ query, data, table, meta, __batch: batch }: Args): Promise<Result>;
|
|
29
|
-
delete({ query, table, meta, __batch: batch }: Args): Promise<Result>;
|
|
30
|
-
read<T>({ table, query, sort, limit, keys, meta, __batch: batch }: Args): Promise<Result<T[]>>;
|
|
31
|
-
createLog({ id, operation, data, table }: CreateLogParams): Promise<Result>;
|
|
32
|
-
readLogs<T>({ table }: ReadLogsParams): Promise<Result<T[]>>;
|
|
33
|
-
getBatchProcess(__batch: ArgsBatch): BatchProcess;
|
|
7
|
+
declare class DynamoDbDriver implements DbDriver<DynamoDBDocument> {
|
|
8
|
+
readonly documentClient: DynamoDBDocument;
|
|
9
|
+
readonly table: import("./utils").ITable<string, "PK", "SK">;
|
|
10
|
+
readonly entity: import("./store/types").IStoreEntity;
|
|
11
|
+
constructor({ documentClient }: ConstructorArgs);
|
|
12
|
+
getClient(): DynamoDBDocument;
|
|
13
|
+
storeValue<V>(key: string, input: V): Promise<StoreValueResult<V>>;
|
|
14
|
+
storeValues<V extends GenericRecord<StorageKey>>(values: V): Promise<StoreValuesResult<V>>;
|
|
15
|
+
getValue<V>(key: StorageKey): Promise<GetValueResult<V>>;
|
|
16
|
+
getValues<V extends GenericRecord<StorageKey>>(input: (keyof V)[]): Promise<GetValuesResult<V>>;
|
|
17
|
+
listValues<V extends GenericRecord<StorageKey>>(params?: IListValuesParams): Promise<ListValuesResult<V>>;
|
|
18
|
+
removeValue<V>(key: StorageKey): Promise<RemoveValueResult<V>>;
|
|
19
|
+
removeValues<V extends GenericRecord<StorageKey>>(input: (keyof V)[]): Promise<RemoveValuesResult<V>>;
|
|
34
20
|
}
|
|
35
21
|
export default DynamoDbDriver;
|
package/DynamoDbDriver.js
CHANGED
|
@@ -1,239 +1,228 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
Object.defineProperty(exports, "__esModule", {
|
|
5
|
-
value: true
|
|
6
|
-
});
|
|
7
|
-
exports.default = void 0;
|
|
8
|
-
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
9
|
-
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
10
|
-
var _dynamodb = require("aws-sdk/clients/dynamodb");
|
|
11
|
-
var _BatchProcess = _interopRequireDefault(require("./BatchProcess"));
|
|
12
|
-
var _QueryGenerator = _interopRequireDefault(require("./QueryGenerator"));
|
|
13
|
-
/**
|
|
14
|
-
* Remove this when no apps are using our internal db drivers anymore
|
|
15
|
-
*/
|
|
16
|
-
// @ts-nocheck
|
|
17
|
-
|
|
18
|
-
const LOG_KEYS = [{
|
|
19
|
-
primary: true,
|
|
20
|
-
unique: true,
|
|
21
|
-
name: "primary",
|
|
22
|
-
fields: [{
|
|
23
|
-
name: "PK"
|
|
24
|
-
}, {
|
|
25
|
-
name: "SK"
|
|
26
|
-
}]
|
|
27
|
-
}];
|
|
1
|
+
import { createTable } from "./utils/createTable.js";
|
|
2
|
+
import { createEntity } from "./store/entity.js";
|
|
3
|
+
import { createPartitionKey, createSortKey, createType } from "./store/keys.js";
|
|
28
4
|
class DynamoDbDriver {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return this.documentClient;
|
|
39
|
-
}
|
|
40
|
-
async create({
|
|
41
|
-
table,
|
|
42
|
-
data,
|
|
43
|
-
meta,
|
|
44
|
-
__batch: batch
|
|
45
|
-
}) {
|
|
46
|
-
if (!batch) {
|
|
47
|
-
const result = await this.documentClient.put({
|
|
48
|
-
TableName: table,
|
|
49
|
-
Item: data,
|
|
50
|
-
ReturnConsumedCapacity: meta ? "TOTAL" : "NONE"
|
|
51
|
-
}).promise();
|
|
52
|
-
return [true, {
|
|
53
|
-
response: result.$response
|
|
54
|
-
}];
|
|
55
|
-
}
|
|
56
|
-
const batchProcess = this.getBatchProcess(batch);
|
|
57
|
-
batchProcess.addBatchWrite({
|
|
58
|
-
table,
|
|
59
|
-
data
|
|
60
|
-
});
|
|
61
|
-
if (batchProcess.allOperationsAdded()) {
|
|
62
|
-
batchProcess.startExecution();
|
|
63
|
-
} else {
|
|
64
|
-
await batchProcess.waitStartExecution();
|
|
5
|
+
constructor({ documentClient }){
|
|
6
|
+
this.documentClient = documentClient;
|
|
7
|
+
this.table = createTable({
|
|
8
|
+
name: process.env.DB_TABLE,
|
|
9
|
+
documentClient
|
|
10
|
+
});
|
|
11
|
+
this.entity = createEntity({
|
|
12
|
+
table: this.table
|
|
13
|
+
});
|
|
65
14
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
response: batchProcess.response
|
|
69
|
-
}];
|
|
70
|
-
}
|
|
71
|
-
async update({
|
|
72
|
-
query,
|
|
73
|
-
data,
|
|
74
|
-
table,
|
|
75
|
-
meta,
|
|
76
|
-
__batch: batch
|
|
77
|
-
}) {
|
|
78
|
-
if (!batch) {
|
|
79
|
-
const update = {
|
|
80
|
-
UpdateExpression: "SET ",
|
|
81
|
-
ExpressionAttributeNames: {},
|
|
82
|
-
ExpressionAttributeValues: {}
|
|
83
|
-
};
|
|
84
|
-
const updateExpression = [];
|
|
85
|
-
for (const key in data) {
|
|
86
|
-
updateExpression.push(`#${key} = :${key}`);
|
|
87
|
-
update.ExpressionAttributeNames[`#${key}`] = key;
|
|
88
|
-
update.ExpressionAttributeValues[`:${key}`] = data[key];
|
|
89
|
-
}
|
|
90
|
-
update.UpdateExpression += updateExpression.join(", ");
|
|
91
|
-
const result = await this.documentClient.update((0, _objectSpread2.default)({
|
|
92
|
-
TableName: table,
|
|
93
|
-
Key: query,
|
|
94
|
-
ReturnConsumedCapacity: meta ? "TOTAL" : "NONE"
|
|
95
|
-
}, update)).promise();
|
|
96
|
-
return [true, {
|
|
97
|
-
response: result.$response
|
|
98
|
-
}];
|
|
15
|
+
getClient() {
|
|
16
|
+
return this.documentClient;
|
|
99
17
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
18
|
+
async storeValue(key, input) {
|
|
19
|
+
let value;
|
|
20
|
+
try {
|
|
21
|
+
value = JSON.stringify(input);
|
|
22
|
+
} catch (ex) {
|
|
23
|
+
return {
|
|
24
|
+
key,
|
|
25
|
+
error: ex
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
await this.entity.put({
|
|
30
|
+
PK: createPartitionKey(),
|
|
31
|
+
SK: createSortKey({
|
|
32
|
+
key
|
|
33
|
+
}),
|
|
34
|
+
TYPE: createType(),
|
|
35
|
+
data: {
|
|
36
|
+
key,
|
|
37
|
+
value
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
return {
|
|
41
|
+
key,
|
|
42
|
+
data: input
|
|
43
|
+
};
|
|
44
|
+
} catch (ex) {
|
|
45
|
+
return {
|
|
46
|
+
key,
|
|
47
|
+
error: ex
|
|
48
|
+
};
|
|
49
|
+
}
|
|
109
50
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
51
|
+
async storeValues(values) {
|
|
52
|
+
const keys = Object.keys(values);
|
|
53
|
+
const batchWrite = this.entity.createEntityWriter();
|
|
54
|
+
try {
|
|
55
|
+
for (const key of keys){
|
|
56
|
+
const input = values[key];
|
|
57
|
+
let value;
|
|
58
|
+
try {
|
|
59
|
+
value = JSON.stringify(input);
|
|
60
|
+
} catch (ex) {
|
|
61
|
+
throw ex;
|
|
62
|
+
}
|
|
63
|
+
batchWrite.put({
|
|
64
|
+
PK: createPartitionKey(),
|
|
65
|
+
SK: createSortKey({
|
|
66
|
+
key
|
|
67
|
+
}),
|
|
68
|
+
TYPE: createType(),
|
|
69
|
+
data: {
|
|
70
|
+
key,
|
|
71
|
+
value
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
await batchWrite.execute();
|
|
76
|
+
return {
|
|
77
|
+
keys,
|
|
78
|
+
data: values
|
|
79
|
+
};
|
|
80
|
+
} catch (ex) {
|
|
81
|
+
return {
|
|
82
|
+
keys,
|
|
83
|
+
error: ex
|
|
84
|
+
};
|
|
85
|
+
}
|
|
130
86
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
87
|
+
async getValue(key) {
|
|
88
|
+
try {
|
|
89
|
+
const result = await this.entity.get({
|
|
90
|
+
PK: createPartitionKey(),
|
|
91
|
+
SK: createSortKey({
|
|
92
|
+
key
|
|
93
|
+
})
|
|
94
|
+
});
|
|
95
|
+
return {
|
|
96
|
+
key,
|
|
97
|
+
data: result?.data?.value ? JSON.parse(result.data.value) : null
|
|
98
|
+
};
|
|
99
|
+
} catch (ex) {
|
|
100
|
+
return {
|
|
101
|
+
key,
|
|
102
|
+
error: ex
|
|
103
|
+
};
|
|
104
|
+
}
|
|
140
105
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
106
|
+
async getValues(input) {
|
|
107
|
+
const keys = [
|
|
108
|
+
...input
|
|
109
|
+
];
|
|
110
|
+
const batchRead = this.entity.createEntityReader({
|
|
111
|
+
read: keys.map((key)=>({
|
|
112
|
+
PK: createPartitionKey(),
|
|
113
|
+
SK: createSortKey({
|
|
114
|
+
key
|
|
115
|
+
})
|
|
116
|
+
}))
|
|
117
|
+
});
|
|
118
|
+
try {
|
|
119
|
+
const results = await batchRead.execute();
|
|
120
|
+
const data = keys.reduce((collection, initialKey)=>{
|
|
121
|
+
const key = initialKey;
|
|
122
|
+
const result = results.find((item)=>item.PK === createPartitionKey() && item.SK === createSortKey({
|
|
123
|
+
key
|
|
124
|
+
}));
|
|
125
|
+
if (!result?.data?.value) {
|
|
126
|
+
collection[key] = null;
|
|
127
|
+
return collection;
|
|
128
|
+
}
|
|
129
|
+
try {
|
|
130
|
+
collection[key] = JSON.parse(result.data.value);
|
|
131
|
+
} catch {
|
|
132
|
+
collection[key] = null;
|
|
133
|
+
}
|
|
134
|
+
return collection;
|
|
135
|
+
}, {});
|
|
136
|
+
return {
|
|
137
|
+
keys,
|
|
138
|
+
data
|
|
139
|
+
};
|
|
140
|
+
} catch (ex) {
|
|
141
|
+
return {
|
|
142
|
+
keys,
|
|
143
|
+
error: ex
|
|
144
|
+
};
|
|
145
|
+
}
|
|
175
146
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
147
|
+
async listValues(params) {
|
|
148
|
+
try {
|
|
149
|
+
const partitionKey = createPartitionKey();
|
|
150
|
+
const options = {
|
|
151
|
+
...params
|
|
152
|
+
};
|
|
153
|
+
const results = await this.entity.queryAll({
|
|
154
|
+
partitionKey,
|
|
155
|
+
options
|
|
156
|
+
});
|
|
157
|
+
const data = results.reduce((collection, item)=>{
|
|
158
|
+
const key = item.data.key;
|
|
159
|
+
try {
|
|
160
|
+
collection[key] = JSON.parse(item.data.value);
|
|
161
|
+
} catch {
|
|
162
|
+
collection[key] = null;
|
|
163
|
+
}
|
|
164
|
+
return collection;
|
|
165
|
+
}, {});
|
|
166
|
+
return {
|
|
167
|
+
keys: Object.keys(data),
|
|
168
|
+
data
|
|
169
|
+
};
|
|
170
|
+
} catch (ex) {
|
|
171
|
+
return {
|
|
172
|
+
error: ex
|
|
173
|
+
};
|
|
174
|
+
}
|
|
187
175
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
176
|
+
async removeValue(key) {
|
|
177
|
+
const result = await this.getValue(key);
|
|
178
|
+
if (result.error) return {
|
|
179
|
+
key,
|
|
180
|
+
error: result.error
|
|
181
|
+
};
|
|
182
|
+
try {
|
|
183
|
+
await this.entity.delete({
|
|
184
|
+
PK: createPartitionKey(),
|
|
185
|
+
SK: createSortKey({
|
|
186
|
+
key
|
|
187
|
+
})
|
|
188
|
+
});
|
|
189
|
+
return {
|
|
190
|
+
key,
|
|
191
|
+
data: result.data
|
|
192
|
+
};
|
|
193
|
+
} catch (ex) {
|
|
194
|
+
return {
|
|
195
|
+
key,
|
|
196
|
+
error: ex
|
|
197
|
+
};
|
|
198
|
+
}
|
|
194
199
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
async readLogs({
|
|
218
|
-
table
|
|
219
|
-
}) {
|
|
220
|
-
return this.read({
|
|
221
|
-
table,
|
|
222
|
-
keys: LOG_KEYS,
|
|
223
|
-
query: {
|
|
224
|
-
PK: "log",
|
|
225
|
-
SK: {
|
|
226
|
-
$gte: " "
|
|
200
|
+
async removeValues(input) {
|
|
201
|
+
const keys = [
|
|
202
|
+
...input
|
|
203
|
+
];
|
|
204
|
+
const batchDelete = this.entity.createEntityWriter({
|
|
205
|
+
delete: keys.map((key)=>({
|
|
206
|
+
PK: createPartitionKey(),
|
|
207
|
+
SK: createSortKey({
|
|
208
|
+
key
|
|
209
|
+
})
|
|
210
|
+
}))
|
|
211
|
+
});
|
|
212
|
+
try {
|
|
213
|
+
await batchDelete.execute();
|
|
214
|
+
return {
|
|
215
|
+
keys
|
|
216
|
+
};
|
|
217
|
+
} catch (ex) {
|
|
218
|
+
return {
|
|
219
|
+
keys,
|
|
220
|
+
error: ex
|
|
221
|
+
};
|
|
227
222
|
}
|
|
228
|
-
}
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
getBatchProcess(__batch) {
|
|
232
|
-
if (!this.batchProcesses[__batch.instance.id]) {
|
|
233
|
-
this.batchProcesses[__batch.instance.id] = new _BatchProcess.default(__batch.instance, this.documentClient);
|
|
234
223
|
}
|
|
235
|
-
return this.batchProcesses[__batch.instance.id];
|
|
236
|
-
}
|
|
237
224
|
}
|
|
238
|
-
|
|
239
|
-
|
|
225
|
+
const src_DynamoDbDriver = DynamoDbDriver;
|
|
226
|
+
export default src_DynamoDbDriver;
|
|
227
|
+
|
|
228
|
+
//# sourceMappingURL=DynamoDbDriver.js.map
|
package/DynamoDbDriver.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["LOG_KEYS","primary","unique","name","fields","DynamoDbDriver","constructor","documentClient","batchProcesses","DocumentClient","getClient","create","table","data","meta","__batch","batch","result","put","TableName","Item","ReturnConsumedCapacity","promise","response","$response","batchProcess","getBatchProcess","addBatchWrite","allOperationsAdded","startExecution","waitStartExecution","waitExecution","update","query","UpdateExpression","ExpressionAttributeNames","ExpressionAttributeValues","updateExpression","key","push","join","Key","delete","addBatchDelete","read","sort","limit","keys","queryGenerator","QueryGenerator","queryParams","generate","tableName","Array","isArray","Items","getResult","addBatchGet","createLog","id","operation","PK","SK","readLogs","$gte","instance","BatchProcess"],"sources":["DynamoDbDriver.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 BatchProcess from \"./BatchProcess\";\nimport QueryGenerator from \"./QueryGenerator\";\nimport { DbDriver, Args, Result, ArgsBatch } from \"@webiny/db\";\nimport { QueryKeys } from \"~/types\";\n\ntype ConstructorArgs = {\n documentClient?: DocumentClient;\n};\n\nconst LOG_KEYS: QueryKeys = [\n {\n primary: true,\n unique: true,\n name: \"primary\",\n fields: [{ name: \"PK\" }, { name: \"SK\" }]\n }\n];\n\ninterface Update {\n UpdateExpression: string;\n ExpressionAttributeNames: Record<string, any>;\n ExpressionAttributeValues: Record<string, any>;\n}\n\ninterface ReadLogsParams {\n table: string;\n}\n\ninterface CreateLogParams {\n id: string;\n operation: string;\n /**\n * TODO: determine the data type.\n */\n data: any;\n table: string;\n}\n\nclass DynamoDbDriver implements DbDriver {\n batchProcesses: Record<string, BatchProcess>;\n documentClient: DocumentClient;\n constructor({ documentClient }: ConstructorArgs = {}) {\n this.batchProcesses = {};\n this.documentClient = documentClient || new DocumentClient();\n }\n\n getClient() {\n return this.documentClient;\n }\n\n async create({ table, data, meta, __batch: batch }: Args): Promise<Result> {\n if (!batch) {\n const result = await this.documentClient\n .put({\n TableName: table,\n Item: data,\n ReturnConsumedCapacity: meta ? \"TOTAL\" : \"NONE\"\n })\n .promise();\n return [true, { response: result.$response }];\n }\n\n const batchProcess = this.getBatchProcess(batch);\n batchProcess.addBatchWrite({ table, data });\n\n if (batchProcess.allOperationsAdded()) {\n batchProcess.startExecution();\n } else {\n await batchProcess.waitStartExecution();\n }\n\n await batchProcess.waitExecution();\n\n return [true, { response: batchProcess.response }];\n }\n\n async update({ query, data, table, meta, __batch: batch }: Args): Promise<Result> {\n if (!batch) {\n const update: Update = {\n UpdateExpression: \"SET \",\n ExpressionAttributeNames: {},\n ExpressionAttributeValues: {}\n };\n\n const updateExpression = [];\n for (const key in data) {\n updateExpression.push(`#${key} = :${key}`);\n update.ExpressionAttributeNames[`#${key}`] = key;\n update.ExpressionAttributeValues[`:${key}`] = data[key];\n }\n\n update.UpdateExpression += updateExpression.join(\", \");\n\n const result = await this.documentClient\n .update({\n TableName: table,\n Key: query,\n ReturnConsumedCapacity: meta ? \"TOTAL\" : \"NONE\",\n ...update\n })\n .promise();\n\n return [true, { response: result.$response }];\n }\n\n const batchProcess = this.getBatchProcess(batch);\n\n batchProcess.addBatchWrite({\n table,\n data\n });\n\n if (batchProcess.allOperationsAdded()) {\n batchProcess.startExecution();\n } else {\n await batchProcess.waitStartExecution();\n }\n\n await batchProcess.waitExecution();\n\n return [true, { response: batchProcess.response }];\n }\n\n async delete({ query, table, meta, __batch: batch }: Args): Promise<Result> {\n if (!batch) {\n const result = await this.documentClient\n .delete({\n TableName: table,\n Key: query,\n ReturnConsumedCapacity: meta ? \"TOTAL\" : \"NONE\"\n })\n .promise();\n\n return [true, { response: result.$response }];\n }\n\n const batchProcess = this.getBatchProcess(batch);\n batchProcess.addBatchDelete({\n table,\n query\n });\n\n if (batchProcess.allOperationsAdded()) {\n batchProcess.startExecution();\n } else {\n await batchProcess.waitStartExecution();\n }\n\n await batchProcess.waitExecution();\n\n return [true, { response: batchProcess.response }];\n }\n\n async read<T>({\n table,\n query,\n sort,\n limit,\n keys,\n meta,\n __batch: batch\n }: Args): Promise<Result<T[]>> {\n if (!batch) {\n const queryGenerator = new QueryGenerator();\n const queryParams = queryGenerator.generate({\n query,\n keys,\n sort,\n limit,\n tableName: table\n });\n\n const response = await this.documentClient\n .query({ ...queryParams, ReturnConsumedCapacity: meta ? \"TOTAL\" : \"NONE\" })\n .promise();\n\n if (Array.isArray(response.Items)) {\n return [response.Items as T[], { response: response.$response }];\n }\n return [[], { response: response.$response }];\n }\n\n // DynamoDb doesn't support batch queries, so we can immediately assume the GetRequest operation.\n const batchProcess = this.getBatchProcess(batch);\n const getResult = batchProcess.addBatchGet({\n table,\n query\n });\n\n if (batchProcess.allOperationsAdded()) {\n batchProcess.startExecution();\n } else {\n await batchProcess.waitStartExecution();\n }\n\n await batchProcess.waitExecution();\n\n const result = getResult() as T;\n if (result) {\n return [[result], { response: batchProcess.response }];\n }\n\n return [[], { response: batchProcess.response }];\n }\n\n async createLog({ id, operation, data, table }: CreateLogParams): Promise<Result> {\n await this.create({\n table: table,\n keys: LOG_KEYS,\n data: {\n PK: \"log\",\n SK: id,\n id,\n operation,\n ...data\n }\n });\n\n return [true, {}];\n }\n\n async readLogs<T>({ table }: ReadLogsParams) {\n return this.read<T>({\n table,\n keys: LOG_KEYS,\n query: {\n PK: \"log\",\n SK: { $gte: \" \" }\n }\n });\n }\n\n getBatchProcess(__batch: ArgsBatch): BatchProcess {\n if (!this.batchProcesses[__batch.instance.id]) {\n this.batchProcesses[__batch.instance.id] = new BatchProcess(\n __batch.instance,\n this.documentClient\n );\n }\n\n return this.batchProcesses[__batch.instance.id];\n }\n}\n\nexport default DynamoDbDriver;\n"],"mappings":";;;;;;;;;AAIA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MmC,SAAS,CAAC;IAAEC,EAAE;IAAEC,SAAS;IAAE/C,IAAI;IAAED;EAAuB,CAAC,EAAmB;IAC9E,MAAM,IAAI,CAACD,MAAM,CAAC;MACdC,KAAK,EAAEA,KAAK;MACZmC,IAAI,EAAE/C,QAAQ;MACda,IAAI;QACAgD,EAAE,EAAE,KAAK;QACTC,EAAE,EAAEH,EAAE;QACNA,EAAE;QACFC;MAAS,GACN/C,IAAI;IAEf,CAAC,CAAC;IAEF,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;EACrB;EAEA,MAAMkD,QAAQ,CAAI;IAAEnD;EAAsB,CAAC,EAAE;IACzC,OAAO,IAAI,CAACgC,IAAI,CAAI;MAChBhC,KAAK;MACLmC,IAAI,EAAE/C,QAAQ;MACdiC,KAAK,EAAE;QACH4B,EAAE,EAAE,KAAK;QACTC,EAAE,EAAE;UAAEE,IAAI,EAAE;QAAI;MACpB;IACJ,CAAC,CAAC;EACN;EAEAtC,eAAe,CAACX,OAAkB,EAAgB;IAC9C,IAAI,CAAC,IAAI,CAACP,cAAc,CAACO,OAAO,CAACkD,QAAQ,CAACN,EAAE,CAAC,EAAE;MAC3C,IAAI,CAACnD,cAAc,CAACO,OAAO,CAACkD,QAAQ,CAACN,EAAE,CAAC,GAAG,IAAIO,qBAAY,CACvDnD,OAAO,CAACkD,QAAQ,EAChB,IAAI,CAAC1D,cAAc,CACtB;IACL;IAEA,OAAO,IAAI,CAACC,cAAc,CAACO,OAAO,CAACkD,QAAQ,CAACN,EAAE,CAAC;EACnD;AACJ;AAAC,eAEctD,cAAc;AAAA"}
|
|
1
|
+
{"version":3,"file":"DynamoDbDriver.js","sources":["../src/DynamoDbDriver.ts"],"sourcesContent":["import type { DynamoDBDocument } from \"@webiny/aws-sdk/client-dynamodb/index.js\";\nimport type {\n DbDriver,\n GetValueResult,\n GetValuesResult,\n IListValuesParams,\n ListValuesResult,\n RemoveValueResult,\n RemoveValuesResult,\n StorageKey,\n StoreValueResult,\n StoreValuesResult\n} from \"@webiny/db\";\nimport { createTable } from \"~/utils/createTable.js\";\nimport type { GenericRecord } from \"@webiny/api/types.js\";\nimport { createEntity } from \"~/store/entity.js\";\nimport { createPartitionKey, createSortKey, createType } from \"~/store/keys.js\";\n\ninterface ConstructorArgs {\n documentClient: DynamoDBDocument;\n}\n\nclass DynamoDbDriver implements DbDriver<DynamoDBDocument> {\n public readonly documentClient;\n\n public readonly table;\n public readonly entity;\n\n constructor({ documentClient }: ConstructorArgs) {\n this.documentClient = documentClient;\n this.table = createTable({\n name: process.env.DB_TABLE as string,\n documentClient\n });\n this.entity = createEntity({\n table: this.table\n });\n }\n\n public getClient() {\n return this.documentClient;\n }\n\n public async storeValue<V>(key: string, input: V): Promise<StoreValueResult<V>> {\n let value: string | undefined;\n try {\n value = JSON.stringify(input);\n } catch (ex) {\n return {\n key,\n error: ex\n };\n }\n\n try {\n await this.entity.put({\n PK: createPartitionKey(),\n SK: createSortKey({ key }),\n TYPE: createType(),\n data: {\n key,\n value\n }\n });\n\n return {\n key,\n data: input\n };\n } catch (ex) {\n return {\n key,\n error: ex\n };\n }\n }\n public async storeValues<V extends GenericRecord<StorageKey>>(\n values: V\n ): Promise<StoreValuesResult<V>> {\n const keys = Object.keys(values);\n\n const batchWrite = this.entity.createEntityWriter();\n try {\n for (const key of keys) {\n const input = values[key];\n let value: string | undefined;\n try {\n value = JSON.stringify(input);\n } catch (ex) {\n throw ex;\n }\n batchWrite.put({\n PK: createPartitionKey(),\n SK: createSortKey({ key }),\n TYPE: createType(),\n data: {\n key,\n value\n }\n });\n }\n\n await batchWrite.execute();\n return {\n keys,\n data: values\n };\n } catch (ex) {\n return {\n keys,\n error: ex\n };\n }\n }\n public async getValue<V>(key: StorageKey): Promise<GetValueResult<V>> {\n try {\n const result = await this.entity.get({\n PK: createPartitionKey(),\n SK: createSortKey({ key })\n });\n return {\n key,\n data: result?.data?.value ? JSON.parse(result.data.value) : null\n };\n } catch (ex) {\n return {\n key,\n error: ex\n };\n }\n }\n public async getValues<V extends GenericRecord<StorageKey>>(\n input: (keyof V)[]\n ): Promise<GetValuesResult<V>> {\n const keys = [...input] as string[];\n\n const batchRead = this.entity.createEntityReader({\n read: keys.map(key => {\n return {\n PK: createPartitionKey(),\n SK: createSortKey({ key })\n };\n })\n });\n\n try {\n const results = await batchRead.execute();\n const data = keys.reduce((collection, initialKey) => {\n const key = initialKey as keyof V;\n const result = results.find(item => {\n return item.PK === createPartitionKey() && item.SK === createSortKey({ key });\n });\n if (!result?.data?.value) {\n collection[key] = null;\n return collection;\n }\n try {\n collection[key] = JSON.parse(result.data.value);\n } catch {\n collection[key] = null;\n }\n\n return collection;\n }, {} as GenericRecord);\n return {\n keys,\n data\n };\n } catch (ex) {\n return {\n keys,\n error: ex\n };\n }\n }\n public async listValues<V extends GenericRecord<StorageKey>>(\n params?: IListValuesParams\n ): Promise<ListValuesResult<V>> {\n try {\n const partitionKey = createPartitionKey();\n const options = {\n ...params\n };\n const results = await this.entity.queryAll({\n partitionKey,\n options\n });\n\n const data = results.reduce((collection, item) => {\n const key = item.data.key as keyof V;\n try {\n collection[key] = JSON.parse(item.data.value);\n } catch {\n collection[key] = null;\n }\n\n return collection;\n }, {} as GenericRecord);\n\n return {\n keys: Object.keys(data),\n data\n };\n } catch (ex) {\n return {\n error: ex\n };\n }\n }\n\n public async removeValue<V>(key: StorageKey): Promise<RemoveValueResult<V>> {\n const result = await this.getValue<V>(key);\n if (result.error) {\n return {\n key,\n error: result.error\n };\n }\n try {\n await this.entity.delete({\n PK: createPartitionKey(),\n SK: createSortKey({ key })\n });\n return {\n key,\n data: result.data\n };\n } catch (ex) {\n return {\n key,\n error: ex\n };\n }\n }\n\n public async removeValues<V extends GenericRecord<StorageKey>>(\n input: (keyof V)[]\n ): Promise<RemoveValuesResult<V>> {\n const keys = [...input] as string[];\n\n const batchDelete = this.entity.createEntityWriter({\n delete: keys.map(key => {\n return {\n PK: createPartitionKey(),\n SK: createSortKey({ key })\n };\n })\n });\n\n try {\n await batchDelete.execute();\n return {\n keys\n };\n } catch (ex) {\n return {\n keys,\n error: ex\n };\n }\n }\n}\n\nexport default DynamoDbDriver;\n"],"names":["DynamoDbDriver","documentClient","createTable","process","createEntity","key","input","value","JSON","ex","createPartitionKey","createSortKey","createType","values","keys","Object","batchWrite","result","batchRead","results","data","collection","initialKey","item","params","partitionKey","options","batchDelete"],"mappings":";;;AAsBA,MAAMA;IAMF,YAAY,EAAEC,cAAc,EAAmB,CAAE;QAC7C,IAAI,CAAC,cAAc,GAAGA;QACtB,IAAI,CAAC,KAAK,GAAGC,YAAY;YACrB,MAAMC,QAAQ,GAAG,CAAC,QAAQ;YAC1BF;QACJ;QACA,IAAI,CAAC,MAAM,GAAGG,aAAa;YACvB,OAAO,IAAI,CAAC,KAAK;QACrB;IACJ;IAEO,YAAY;QACf,OAAO,IAAI,CAAC,cAAc;IAC9B;IAEA,MAAa,WAAcC,GAAW,EAAEC,KAAQ,EAAgC;QAC5E,IAAIC;QACJ,IAAI;YACAA,QAAQC,KAAK,SAAS,CAACF;QAC3B,EAAE,OAAOG,IAAI;YACT,OAAO;gBACHJ;gBACA,OAAOI;YACX;QACJ;QAEA,IAAI;YACA,MAAM,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC;gBAClB,IAAIC;gBACJ,IAAIC,cAAc;oBAAEN;gBAAI;gBACxB,MAAMO;gBACN,MAAM;oBACFP;oBACAE;gBACJ;YACJ;YAEA,OAAO;gBACHF;gBACA,MAAMC;YACV;QACJ,EAAE,OAAOG,IAAI;YACT,OAAO;gBACHJ;gBACA,OAAOI;YACX;QACJ;IACJ;IACA,MAAa,YACTI,MAAS,EACoB;QAC7B,MAAMC,OAAOC,OAAO,IAAI,CAACF;QAEzB,MAAMG,aAAa,IAAI,CAAC,MAAM,CAAC,kBAAkB;QACjD,IAAI;YACA,KAAK,MAAMX,OAAOS,KAAM;gBACpB,MAAMR,QAAQO,MAAM,CAACR,IAAI;gBACzB,IAAIE;gBACJ,IAA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AO,IAAI;YACrB;QACJ,EAAE,OAAOR,IAAI;YACT,OAAO;gBACHJ;gBACA,OAAOI;YACX;QACJ;IACJ;IAEA,MAAa,aACTH,KAAkB,EACY;QAC9B,MAAMQ,OAAO;eAAIR;SAAM;QAEvB,MAAMqB,cAAc,IAAI,CAAC,MAAM,CAAC,kBAAkB,CAAC;YAC/C,QAAQb,KAAK,GAAG,CAACT,CAAAA,MACN;oBACH,IAAIK;oBACJ,IAAIC,cAAc;wBAAEN;oBAAI;gBAC5B;QAER;QAEA,IAAI;YACA,MAAMsB,YAAY,OAAO;YACzB,OAAO;gBACHb;YACJ;QACJ,EAAE,OAAOL,IAAI;YACT,OAAO;gBACHK;gBACA,OAAOL;YACX;QACJ;IACJ;AACJ;AAEA,2BAAeT"}
|
package/README.md
CHANGED
|
@@ -1,67 +1,11 @@
|
|
|
1
1
|
# @webiny/db-dynamodb
|
|
2
|
-
[](https://www.npmjs.com/package/webiny-data-dynamodb)
|
|
3
|
-
[](https://www.npmjs.com/package/webiny-data-dynamodb)
|
|
4
|
-
[](https://github.com/prettier/prettier)
|
|
5
|
-
[](http://makeapullrequest.com)
|
|
6
2
|
|
|
7
|
-
|
|
3
|
+
> [!NOTE]
|
|
4
|
+
> This package is part of the [Webiny](https://www.webiny.com) monorepo.
|
|
5
|
+
> Itβs **included in every Webiny project by default** and is not meant to be used as a standalone package.
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
[the official docs](https://github.com/doitadrian/data-dynamodb).
|
|
11
|
-
|
|
12
|
-
## Install
|
|
13
|
-
```
|
|
14
|
-
yarn add @webiny/db-dynamodb
|
|
15
|
-
```
|
|
7
|
+
π **Documentation:** [https://www.webiny.com/docs](https://www.webiny.com/docs)
|
|
16
8
|
|
|
9
|
+
---
|
|
17
10
|
|
|
18
|
-
|
|
19
|
-
We have a number [helper](./src/utils) functions that ease the use of either dynamodb-toolbox, filtering, sorting or just creating proper response.
|
|
20
|
-
|
|
21
|
-
#### [batchRead](./src/utils/batchRead.ts)
|
|
22
|
-
Read a batch of records from the DynamoDB table.
|
|
23
|
-
|
|
24
|
-
This function accepts [table](https://github.com/jeremydaly/dynamodb-toolbox/blob/main/src/classes/Table.ts) and items, an array of objects created by [Entity.getBatch()](https://github.com/jeremydaly/dynamodb-toolbox/blob/main/src/classes/Entity.ts#L313).
|
|
25
|
-
|
|
26
|
-
Internally it reads records until there are no more to read and returns a list of read records.
|
|
27
|
-
|
|
28
|
-
#### [batchWrite](./src/utils/batchWrite.ts)
|
|
29
|
-
Write a batch of records to the DynamoDB table.
|
|
30
|
-
|
|
31
|
-
This function accepts [table](https://github.com/jeremydaly/dynamodb-toolbox/blob/main/src/classes/Table.ts) and items, an array of objects created by [Entity.putBatch()](https://github.com/jeremydaly/dynamodb-toolbox/blob/main/src/classes/Entity.ts#L989).
|
|
32
|
-
It also accepts a number which defines a number of items to be written in one request. DO NOT put that number over the official DynamoDB maximum.
|
|
33
|
-
|
|
34
|
-
Internally it loops through the items received (in chunks of `maxChunks` parameter) and does not return anything.
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
#### [cleanupItem and cleanupItems](./src/utils/cleanup.ts)
|
|
38
|
-
Clean up records received from the DynamoDB table.
|
|
39
|
-
|
|
40
|
-
This function accepts [entity](https://github.com/jeremydaly/dynamodb-toolbox/blob/main/src/classes/Entity.ts) and item to be cleaned up, in case of the cleanupItem.
|
|
41
|
-
In case of `cleanupItems` it accepts an array of items to clean up.
|
|
42
|
-
|
|
43
|
-
We use this to remove the properties that dynamodb-toolbox puts on the record automatically.
|
|
44
|
-
|
|
45
|
-
#### [get](./src/utils/get.ts)
|
|
46
|
-
Get a single record from the DynamoDB table with given keys.
|
|
47
|
-
|
|
48
|
-
This function accepts [entity](https://github.com/jeremydaly/dynamodb-toolbox/blob/main/src/classes/Entity.ts) and keys to fetch the record by.
|
|
49
|
-
|
|
50
|
-
It returns either record or null. By default, [entity.get()](https://github.com/jeremydaly/dynamodb-toolbox/blob/main/src/classes/Entity.ts#L281) returns a object with some meta data and `Item` property, which contains the record (or null if no record).
|
|
51
|
-
|
|
52
|
-
#### [queryOne and queryAll](./src/utils/query.ts)
|
|
53
|
-
Query the DynamoDB table for record(s) by given partition key and query options.
|
|
54
|
-
|
|
55
|
-
This function accepts [entity](https://github.com/jeremydaly/dynamodb-toolbox/blob/main/src/classes/Entity.ts) to perform the query on, `partitionKey` to query by and [options](https://github.com/jeremydaly/dynamodb-toolbox/blob/main/src/classes/Table.ts#L65) to define the query parameters with.
|
|
56
|
-
|
|
57
|
-
The `queryAll` method accepts `limit`, a number with which you can load only a certain amount of records. The `queryOne` method does not have that property.
|
|
58
|
-
|
|
59
|
-
#### [filter](./src/utils/filter.ts)
|
|
60
|
-
Filter the DynamoDB records by given where condition.
|
|
61
|
-
|
|
62
|
-
This function accepts items (records) to be filtered, a definition of fields to filter by (not required by default if no field modification is required), where conditions (eg. `{published: true, date_gte: "2021-01-01"}`) and filtering plugins.
|
|
63
|
-
|
|
64
|
-
#### [sort](./src/utils/sort.ts)
|
|
65
|
-
Sort the DynamoDB records by given sort condition.
|
|
66
|
-
|
|
67
|
-
This function accepts items (records) to be sorted, sort options (eg. createdBy_ASC, id_DESC, etc.) and a definitions of fields to sort by (not required by default if no field modification is required).
|
|
11
|
+
_This README file is automatically generated during the publish process._
|