@webiny/db-dynamodb 0.0.0-ee-vpcs.549378cf03
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/BatchProcess.d.ts +47 -0
- package/BatchProcess.js +198 -0
- package/BatchProcess.js.map +1 -0
- package/DynamoDbDriver.d.ts +35 -0
- package/DynamoDbDriver.js +274 -0
- package/DynamoDbDriver.js.map +1 -0
- package/LICENSE +21 -0
- package/QueryGenerator.d.ts +21 -0
- package/QueryGenerator.js +78 -0
- package/QueryGenerator.js.map +1 -0
- package/README.md +67 -0
- package/index.d.ts +2 -0
- package/index.js +15 -0
- package/index.js.map +1 -0
- package/operators/comparison/beginsWith.d.ts +3 -0
- package/operators/comparison/beginsWith.js +24 -0
- package/operators/comparison/beginsWith.js.map +1 -0
- package/operators/comparison/between.d.ts +3 -0
- package/operators/comparison/between.js +30 -0
- package/operators/comparison/between.js.map +1 -0
- package/operators/comparison/eq.d.ts +3 -0
- package/operators/comparison/eq.js +34 -0
- package/operators/comparison/eq.js.map +1 -0
- package/operators/comparison/gt.d.ts +3 -0
- package/operators/comparison/gt.js +24 -0
- package/operators/comparison/gt.js.map +1 -0
- package/operators/comparison/gte.d.ts +3 -0
- package/operators/comparison/gte.js +24 -0
- package/operators/comparison/gte.js.map +1 -0
- package/operators/comparison/lt.d.ts +3 -0
- package/operators/comparison/lt.js +24 -0
- package/operators/comparison/lt.js.map +1 -0
- package/operators/comparison/lte.d.ts +3 -0
- package/operators/comparison/lte.js +24 -0
- package/operators/comparison/lte.js.map +1 -0
- package/operators/index.d.ts +12 -0
- package/operators/index.js +39 -0
- package/operators/index.js.map +1 -0
- package/operators/logical/and.d.ts +3 -0
- package/operators/logical/and.js +63 -0
- package/operators/logical/and.js.map +1 -0
- package/operators/logical/or.d.ts +3 -0
- package/operators/logical/or.js +63 -0
- package/operators/logical/or.js.map +1 -0
- package/package.json +46 -0
- package/plugins/definitions/AttributePlugin.d.ts +20 -0
- package/plugins/definitions/AttributePlugin.js +61 -0
- package/plugins/definitions/AttributePlugin.js.map +1 -0
- package/plugins/definitions/DateTimeTransformPlugin.d.ts +4 -0
- package/plugins/definitions/DateTimeTransformPlugin.js +54 -0
- package/plugins/definitions/DateTimeTransformPlugin.js.map +1 -0
- package/plugins/definitions/FieldPathPlugin.d.ts +22 -0
- package/plugins/definitions/FieldPathPlugin.js +46 -0
- package/plugins/definitions/FieldPathPlugin.js.map +1 -0
- package/plugins/definitions/FieldPlugin.d.ts +34 -0
- package/plugins/definitions/FieldPlugin.js +73 -0
- package/plugins/definitions/FieldPlugin.js.map +1 -0
- package/plugins/definitions/NumberTransformPlugin.d.ts +4 -0
- package/plugins/definitions/NumberTransformPlugin.js +45 -0
- package/plugins/definitions/NumberTransformPlugin.js.map +1 -0
- package/plugins/definitions/TimeTransformPlugin.d.ts +4 -0
- package/plugins/definitions/TimeTransformPlugin.js +61 -0
- package/plugins/definitions/TimeTransformPlugin.js.map +1 -0
- package/plugins/definitions/ValueFilterPlugin.d.ts +20 -0
- package/plugins/definitions/ValueFilterPlugin.js +49 -0
- package/plugins/definitions/ValueFilterPlugin.js.map +1 -0
- package/plugins/definitions/ValueTransformPlugin.d.ts +22 -0
- package/plugins/definitions/ValueTransformPlugin.js +50 -0
- package/plugins/definitions/ValueTransformPlugin.js.map +1 -0
- package/plugins/definitions/assignFields.d.ts +1 -0
- package/plugins/definitions/assignFields.js +37 -0
- package/plugins/definitions/assignFields.js.map +1 -0
- package/plugins/filters/andIn.d.ts +3 -0
- package/plugins/filters/andIn.js +36 -0
- package/plugins/filters/andIn.js.map +1 -0
- package/plugins/filters/between.d.ts +3 -0
- package/plugins/filters/between.js +37 -0
- package/plugins/filters/between.js.map +1 -0
- package/plugins/filters/contains.d.ts +3 -0
- package/plugins/filters/contains.js +33 -0
- package/plugins/filters/contains.js.map +1 -0
- package/plugins/filters/eq.d.ts +3 -0
- package/plugins/filters/eq.js +34 -0
- package/plugins/filters/eq.js.map +1 -0
- package/plugins/filters/fuzzy.d.ts +3 -0
- package/plugins/filters/fuzzy.js +31 -0
- package/plugins/filters/fuzzy.js.map +1 -0
- package/plugins/filters/gt.d.ts +3 -0
- package/plugins/filters/gt.js +21 -0
- package/plugins/filters/gt.js.map +1 -0
- package/plugins/filters/gte.d.ts +3 -0
- package/plugins/filters/gte.js +21 -0
- package/plugins/filters/gte.js.map +1 -0
- package/plugins/filters/in.d.ts +3 -0
- package/plugins/filters/in.js +36 -0
- package/plugins/filters/in.js.map +1 -0
- package/plugins/filters/index.d.ts +2 -0
- package/plugins/filters/index.js +34 -0
- package/plugins/filters/index.js.map +1 -0
- package/plugins/filters/lt.d.ts +3 -0
- package/plugins/filters/lt.js +21 -0
- package/plugins/filters/lt.js.map +1 -0
- package/plugins/filters/lte.d.ts +3 -0
- package/plugins/filters/lte.js +21 -0
- package/plugins/filters/lte.js.map +1 -0
- package/plugins/filters/startsWith.d.ts +3 -0
- package/plugins/filters/startsWith.js +34 -0
- package/plugins/filters/startsWith.js.map +1 -0
- package/plugins/index.d.ts +2 -0
- package/plugins/index.js +19 -0
- package/plugins/index.js.map +1 -0
- package/statements/createKeyConditionExpressionArgs.d.ts +15 -0
- package/statements/createKeyConditionExpressionArgs.js +52 -0
- package/statements/createKeyConditionExpressionArgs.js.map +1 -0
- package/statements/processStatement.d.ts +3 -0
- package/statements/processStatement.js +42 -0
- package/statements/processStatement.js.map +1 -0
- package/types.d.ts +58 -0
- package/types.js +5 -0
- package/types.js.map +1 -0
- package/utils/attributes.d.ts +10 -0
- package/utils/attributes.js +31 -0
- package/utils/attributes.js.map +1 -0
- package/utils/batchRead.d.ts +15 -0
- package/utils/batchRead.js +89 -0
- package/utils/batchRead.js.map +1 -0
- package/utils/batchWrite.d.ts +17 -0
- package/utils/batchWrite.js +30 -0
- package/utils/batchWrite.js.map +1 -0
- package/utils/cleanup.d.ts +3 -0
- package/utils/cleanup.js +45 -0
- package/utils/cleanup.js.map +1 -0
- package/utils/cursor.d.ts +2 -0
- package/utils/cursor.js +26 -0
- package/utils/cursor.js.map +1 -0
- package/utils/filter.d.ts +12 -0
- package/utils/filter.js +213 -0
- package/utils/filter.js.map +1 -0
- package/utils/get.d.ts +17 -0
- package/utils/get.js +30 -0
- package/utils/get.js.map +1 -0
- package/utils/listResponse.d.ts +13 -0
- package/utils/listResponse.js +33 -0
- package/utils/listResponse.js.map +1 -0
- package/utils/query.d.ts +32 -0
- package/utils/query.js +119 -0
- package/utils/query.js.map +1 -0
- package/utils/sort.d.ts +17 -0
- package/utils/sort.js +91 -0
- package/utils/sort.js.map +1 -0
- package/utils/table.d.ts +7 -0
- package/utils/table.js +27 -0
- package/utils/table.js.map +1 -0
|
@@ -0,0 +1,47 @@
|
|
|
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.BatchGetItemOutput, import("aws-sdk/lib/error").AWSError> | import("aws-sdk/lib/request").Request<DocumentClient.BatchWriteItemOutput, import("aws-sdk/lib/error").AWSError>;
|
|
46
|
+
}
|
|
47
|
+
export default BatchProcess;
|
package/BatchProcess.js
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
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;
|
|
@@ -0,0 +1 @@
|
|
|
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":";;;;;;;;;;;;;AAAA;AACA;AACA;AACA;AAmCA,MAAMA,YAAN,CAAmB;EAafC,WAAW,CAACC,KAAD,EAAeC,cAAf,EAA+C;IAAA;IAAA;IAAA;IAAA;IAAA;IAAA;IAAA;IAAA;IAAA;IAAA;IAAA;IAAA;IACtD,KAAKA,cAAL,GAAsBA,cAAtB;IACA,KAAKD,KAAL,GAAaA,KAAb;IAEA,KAAKE,YAAL,GAAoB,IAApB;IACA,KAAKC,WAAL,GAAmB,IAAnB;IACA,KAAKC,UAAL,GAAkB,IAAIC,OAAJ,CAAY,CAACC,OAAD,EAAUC,MAAV,KAAqB;MAC/C,KAAKL,YAAL,GAAoBI,OAApB;MACA,KAAKH,WAAL,GAAmBI,MAAnB;IACH,CAHiB,CAAlB;IAKA,KAAKC,gBAAL,GAAwB,IAAxB;IACA,KAAKC,eAAL,GAAuB,IAAvB;IACA,KAAKC,cAAL,GAAsB,IAAIL,OAAJ,CAAY,CAACC,OAAD,EAAUC,MAAV,KAAqB;MACnD,KAAKC,gBAAL,GAAwBF,OAAxB;MACA,KAAKG,eAAL,GAAuBF,MAAvB;IACH,CAHqB,CAAtB;IAKA,KAAKI,UAAL,GAAkB,EAAlB;IACA,KAAKC,OAAL,GAAe,EAAf;IACA,KAAKC,QAAL,GAAgB,EAAhB;IAEA,KAAKC,SAAL;EACH;;EAEDC,kBAAkB,GAAkB;IAChC,OAAO,KAAKX,UAAZ;EACH;;EAEDY,aAAa,GAAkB;IAC3B,OAAO,KAAKN,cAAZ;EACH;;EAEDO,iBAAiB,CACbC,IADa,EAEbC,IAFa,EAGbC,IAAI,GAAG,EAHM,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,WAAb;UACH;;UAED,KAAKnC,OAAL,GAAeA,OAAf;UACA,KAAKJ,gBAAL;QACH,CA5BD;;QA6BA;IArER;;IAwEA,OAAO,KAAKP,cAAL,CAAoB,KAAKa,SAAzB,EACHkB,kBADG,EAEH,CAACoB,KAAD,EAAeC,MAAf,KAA+C;MAC3C,IAAID,KAAJ,EAAW;QACP7C,MAAM,CAAC6C,KAAD,CAAN;MACH,CAFD,MAEO;QACH9C,OAAO,CAAC+C,MAAD,CAAP;MACH;IACJ,CARE,CAAP;EAUH;;AAvLc;;eA0LJvD,Y"}
|
|
@@ -0,0 +1,35 @@
|
|
|
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 BatchProcess from "./BatchProcess";
|
|
6
|
+
import { DbDriver, Args, Result, ArgsBatch } from "@webiny/db";
|
|
7
|
+
declare type ConstructorArgs = {
|
|
8
|
+
documentClient?: DocumentClient;
|
|
9
|
+
};
|
|
10
|
+
interface ReadLogsParams {
|
|
11
|
+
table: string;
|
|
12
|
+
}
|
|
13
|
+
interface CreateLogParams {
|
|
14
|
+
id: string;
|
|
15
|
+
operation: string;
|
|
16
|
+
/**
|
|
17
|
+
* TODO: determine the data type.
|
|
18
|
+
*/
|
|
19
|
+
data: any;
|
|
20
|
+
table: string;
|
|
21
|
+
}
|
|
22
|
+
declare class DynamoDbDriver implements DbDriver {
|
|
23
|
+
batchProcesses: Record<string, BatchProcess>;
|
|
24
|
+
documentClient: DocumentClient;
|
|
25
|
+
constructor({ documentClient }?: ConstructorArgs);
|
|
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;
|
|
34
|
+
}
|
|
35
|
+
export default DynamoDbDriver;
|
|
@@ -0,0 +1,274 @@
|
|
|
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
|
+
var _dynamodb = require("aws-sdk/clients/dynamodb");
|
|
15
|
+
|
|
16
|
+
var _BatchProcess = _interopRequireDefault(require("./BatchProcess"));
|
|
17
|
+
|
|
18
|
+
var _QueryGenerator = _interopRequireDefault(require("./QueryGenerator"));
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Remove this when no apps are using our internal db drivers anymore
|
|
22
|
+
*/
|
|
23
|
+
// @ts-nocheck
|
|
24
|
+
const LOG_KEYS = [{
|
|
25
|
+
primary: true,
|
|
26
|
+
unique: true,
|
|
27
|
+
name: "primary",
|
|
28
|
+
fields: [{
|
|
29
|
+
name: "PK"
|
|
30
|
+
}, {
|
|
31
|
+
name: "SK"
|
|
32
|
+
}]
|
|
33
|
+
}];
|
|
34
|
+
|
|
35
|
+
class DynamoDbDriver {
|
|
36
|
+
constructor({
|
|
37
|
+
documentClient
|
|
38
|
+
} = {}) {
|
|
39
|
+
(0, _defineProperty2.default)(this, "batchProcesses", void 0);
|
|
40
|
+
(0, _defineProperty2.default)(this, "documentClient", void 0);
|
|
41
|
+
this.batchProcesses = {};
|
|
42
|
+
this.documentClient = documentClient || new _dynamodb.DocumentClient();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
getClient() {
|
|
46
|
+
return this.documentClient;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async create({
|
|
50
|
+
table,
|
|
51
|
+
data,
|
|
52
|
+
meta,
|
|
53
|
+
__batch: batch
|
|
54
|
+
}) {
|
|
55
|
+
if (!batch) {
|
|
56
|
+
const result = await this.documentClient.put({
|
|
57
|
+
TableName: table,
|
|
58
|
+
Item: data,
|
|
59
|
+
ReturnConsumedCapacity: meta ? "TOTAL" : "NONE"
|
|
60
|
+
}).promise();
|
|
61
|
+
return [true, {
|
|
62
|
+
response: result.$response
|
|
63
|
+
}];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const batchProcess = this.getBatchProcess(batch);
|
|
67
|
+
batchProcess.addBatchWrite({
|
|
68
|
+
table,
|
|
69
|
+
data
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
if (batchProcess.allOperationsAdded()) {
|
|
73
|
+
batchProcess.startExecution();
|
|
74
|
+
} else {
|
|
75
|
+
await batchProcess.waitStartExecution();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
await batchProcess.waitExecution();
|
|
79
|
+
return [true, {
|
|
80
|
+
response: batchProcess.response
|
|
81
|
+
}];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async update({
|
|
85
|
+
query,
|
|
86
|
+
data,
|
|
87
|
+
table,
|
|
88
|
+
meta,
|
|
89
|
+
__batch: batch
|
|
90
|
+
}) {
|
|
91
|
+
if (!batch) {
|
|
92
|
+
const update = {
|
|
93
|
+
UpdateExpression: "SET ",
|
|
94
|
+
ExpressionAttributeNames: {},
|
|
95
|
+
ExpressionAttributeValues: {}
|
|
96
|
+
};
|
|
97
|
+
const updateExpression = [];
|
|
98
|
+
|
|
99
|
+
for (const key in data) {
|
|
100
|
+
updateExpression.push(`#${key} = :${key}`);
|
|
101
|
+
update.ExpressionAttributeNames[`#${key}`] = key;
|
|
102
|
+
update.ExpressionAttributeValues[`:${key}`] = data[key];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
update.UpdateExpression += updateExpression.join(", ");
|
|
106
|
+
const result = await this.documentClient.update((0, _objectSpread2.default)({
|
|
107
|
+
TableName: table,
|
|
108
|
+
Key: query,
|
|
109
|
+
ReturnConsumedCapacity: meta ? "TOTAL" : "NONE"
|
|
110
|
+
}, update)).promise();
|
|
111
|
+
return [true, {
|
|
112
|
+
response: result.$response
|
|
113
|
+
}];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const batchProcess = this.getBatchProcess(batch);
|
|
117
|
+
batchProcess.addBatchWrite({
|
|
118
|
+
table,
|
|
119
|
+
data
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
if (batchProcess.allOperationsAdded()) {
|
|
123
|
+
batchProcess.startExecution();
|
|
124
|
+
} else {
|
|
125
|
+
await batchProcess.waitStartExecution();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
await batchProcess.waitExecution();
|
|
129
|
+
return [true, {
|
|
130
|
+
response: batchProcess.response
|
|
131
|
+
}];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async delete({
|
|
135
|
+
query,
|
|
136
|
+
table,
|
|
137
|
+
meta,
|
|
138
|
+
__batch: batch
|
|
139
|
+
}) {
|
|
140
|
+
if (!batch) {
|
|
141
|
+
const result = await this.documentClient.delete({
|
|
142
|
+
TableName: table,
|
|
143
|
+
Key: query,
|
|
144
|
+
ReturnConsumedCapacity: meta ? "TOTAL" : "NONE"
|
|
145
|
+
}).promise();
|
|
146
|
+
return [true, {
|
|
147
|
+
response: result.$response
|
|
148
|
+
}];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const batchProcess = this.getBatchProcess(batch);
|
|
152
|
+
batchProcess.addBatchDelete({
|
|
153
|
+
table,
|
|
154
|
+
query
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
if (batchProcess.allOperationsAdded()) {
|
|
158
|
+
batchProcess.startExecution();
|
|
159
|
+
} else {
|
|
160
|
+
await batchProcess.waitStartExecution();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
await batchProcess.waitExecution();
|
|
164
|
+
return [true, {
|
|
165
|
+
response: batchProcess.response
|
|
166
|
+
}];
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async read({
|
|
170
|
+
table,
|
|
171
|
+
query,
|
|
172
|
+
sort,
|
|
173
|
+
limit,
|
|
174
|
+
keys,
|
|
175
|
+
meta,
|
|
176
|
+
__batch: batch
|
|
177
|
+
}) {
|
|
178
|
+
if (!batch) {
|
|
179
|
+
const queryGenerator = new _QueryGenerator.default();
|
|
180
|
+
const queryParams = queryGenerator.generate({
|
|
181
|
+
query,
|
|
182
|
+
keys,
|
|
183
|
+
sort,
|
|
184
|
+
limit,
|
|
185
|
+
tableName: table
|
|
186
|
+
});
|
|
187
|
+
const response = await this.documentClient.query((0, _objectSpread2.default)((0, _objectSpread2.default)({}, queryParams), {}, {
|
|
188
|
+
ReturnConsumedCapacity: meta ? "TOTAL" : "NONE"
|
|
189
|
+
})).promise();
|
|
190
|
+
|
|
191
|
+
if (Array.isArray(response.Items)) {
|
|
192
|
+
return [response.Items, {
|
|
193
|
+
response: response.$response
|
|
194
|
+
}];
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return [[], {
|
|
198
|
+
response: response.$response
|
|
199
|
+
}];
|
|
200
|
+
} // DynamoDb doesn't support batch queries, so we can immediately assume the GetRequest operation.
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
const batchProcess = this.getBatchProcess(batch);
|
|
204
|
+
const getResult = batchProcess.addBatchGet({
|
|
205
|
+
table,
|
|
206
|
+
query
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
if (batchProcess.allOperationsAdded()) {
|
|
210
|
+
batchProcess.startExecution();
|
|
211
|
+
} else {
|
|
212
|
+
await batchProcess.waitStartExecution();
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
await batchProcess.waitExecution();
|
|
216
|
+
const result = getResult();
|
|
217
|
+
|
|
218
|
+
if (result) {
|
|
219
|
+
return [[result], {
|
|
220
|
+
response: batchProcess.response
|
|
221
|
+
}];
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return [[], {
|
|
225
|
+
response: batchProcess.response
|
|
226
|
+
}];
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async createLog({
|
|
230
|
+
id,
|
|
231
|
+
operation,
|
|
232
|
+
data,
|
|
233
|
+
table
|
|
234
|
+
}) {
|
|
235
|
+
await this.create({
|
|
236
|
+
table: table,
|
|
237
|
+
keys: LOG_KEYS,
|
|
238
|
+
data: (0, _objectSpread2.default)({
|
|
239
|
+
PK: "log",
|
|
240
|
+
SK: id,
|
|
241
|
+
id,
|
|
242
|
+
operation
|
|
243
|
+
}, data)
|
|
244
|
+
});
|
|
245
|
+
return [true, {}];
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
async readLogs({
|
|
249
|
+
table
|
|
250
|
+
}) {
|
|
251
|
+
return this.read({
|
|
252
|
+
table,
|
|
253
|
+
keys: LOG_KEYS,
|
|
254
|
+
query: {
|
|
255
|
+
PK: "log",
|
|
256
|
+
SK: {
|
|
257
|
+
$gte: " "
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
getBatchProcess(__batch) {
|
|
264
|
+
if (!this.batchProcesses[__batch.instance.id]) {
|
|
265
|
+
this.batchProcesses[__batch.instance.id] = new _BatchProcess.default(__batch.instance, this.documentClient);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return this.batchProcesses[__batch.instance.id];
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
var _default = DynamoDbDriver;
|
|
274
|
+
exports.default = _default;
|
|
@@ -0,0 +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;;AACA;;AACA;;AANA;AACA;AACA;AACA;AAWA,MAAMA,QAAmB,GAAG,CACxB;EACIC,OAAO,EAAE,IADb;EAEIC,MAAM,EAAE,IAFZ;EAGIC,IAAI,EAAE,SAHV;E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,KADJ;QAEAC,EAAE,EAAEH,EAFJ;QAGAA,EAHA;QAIAC;MAJA,GAKG/C,IALH;IAHU,CAAZ,CAAN;IAYA,OAAO,CAAC,IAAD,EAAO,EAAP,CAAP;EACH;;EAEa,MAARkD,QAAQ,CAAI;IAAEnD;EAAF,CAAJ,EAA+B;IACzC,OAAO,KAAKgC,IAAL,CAAa;MAChBhC,KADgB;MAEhBmC,IAAI,EAAE/C,QAFU;MAGhBiC,KAAK,EAAE;QACH4B,EAAE,EAAE,KADD;QAEHC,EAAE,EAAE;UAAEE,IAAI,EAAE;QAAR;MAFD;IAHS,CAAb,CAAP;EAQH;;EAEDtC,eAAe,CAACX,OAAD,EAAmC;IAC9C,IAAI,CAAC,KAAKP,cAAL,CAAoBO,OAAO,CAACkD,QAAR,CAAiBN,EAArC,CAAL,EAA+C;MAC3C,KAAKnD,cAAL,CAAoBO,OAAO,CAACkD,QAAR,CAAiBN,EAArC,IAA2C,IAAIO,qBAAJ,CACvCnD,OAAO,CAACkD,QAD+B,EAEvC,KAAK1D,cAFkC,CAA3C;IAIH;;IAED,OAAO,KAAKC,cAAL,CAAoBO,OAAO,CAACkD,QAAR,CAAiBN,EAArC,CAAP;EACH;;AA3MoC;;eA8M1BtD,c"}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Webiny
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,21 @@
|
|
|
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;
|