electrodb 2.9.3 → 2.10.1
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/.prettierignore +112 -0
- package/.prettierrc +11 -0
- package/README.md +55 -47
- package/index.d.ts +5298 -2347
- package/index.js +24 -12
- package/package.json +20 -7
- package/src/clauses.js +1500 -1051
- package/src/client.js +255 -235
- package/src/entity.js +4518 -3730
- package/src/errors.js +39 -28
- package/src/events.js +26 -16
- package/src/filterOperations.js +124 -0
- package/src/filters.js +99 -101
- package/src/operations.js +347 -537
- package/src/schema.js +1825 -1473
- package/src/service.js +1081 -852
- package/src/set.js +22 -26
- package/src/transaction.js +179 -153
- package/src/types.js +263 -258
- package/src/update.js +94 -89
- package/src/updateOperations.js +197 -0
- package/src/util.js +45 -32
- package/src/validations.js +337 -325
- package/src/where.js +146 -124
- package/tsconfig.json +11 -11
package/src/client.js
CHANGED
|
@@ -1,280 +1,300 @@
|
|
|
1
|
-
const lib = require(
|
|
2
|
-
const util = require(
|
|
3
|
-
const { isFunction } = require(
|
|
4
|
-
const { ElectroError, ErrorCodes } = require(
|
|
1
|
+
const lib = require("@aws-sdk/lib-dynamodb");
|
|
2
|
+
const util = require("@aws-sdk/lib-dynamodb/dist-cjs/commands/utils");
|
|
3
|
+
const { isFunction } = require("./validations");
|
|
4
|
+
const { ElectroError, ErrorCodes } = require("./errors");
|
|
5
5
|
const DocumentClientVersions = {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
v2: "v2",
|
|
7
|
+
v3: "v3",
|
|
8
|
+
electro: "electro",
|
|
9
9
|
};
|
|
10
10
|
const unmarshallOutput = util.unmarshallOutput || ((val) => val);
|
|
11
11
|
|
|
12
|
-
const v3Methods = [
|
|
13
|
-
const v2Methods = [
|
|
12
|
+
const v3Methods = ["send"];
|
|
13
|
+
const v2Methods = [
|
|
14
|
+
"get",
|
|
15
|
+
"put",
|
|
16
|
+
"update",
|
|
17
|
+
"delete",
|
|
18
|
+
"batchWrite",
|
|
19
|
+
"batchGet",
|
|
20
|
+
"scan",
|
|
21
|
+
"query",
|
|
22
|
+
"createSet",
|
|
23
|
+
"transactWrite",
|
|
24
|
+
"transactGet",
|
|
25
|
+
];
|
|
14
26
|
const supportedClientVersions = {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
27
|
+
[DocumentClientVersions.v2]: v2Methods,
|
|
28
|
+
[DocumentClientVersions.v3]: v3Methods,
|
|
29
|
+
};
|
|
18
30
|
|
|
19
31
|
class DocumentClientV2Wrapper {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
32
|
+
static init(client) {
|
|
33
|
+
return new DocumentClientV2Wrapper(client, lib);
|
|
34
|
+
}
|
|
23
35
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
36
|
+
constructor(client, lib) {
|
|
37
|
+
this.client = client;
|
|
38
|
+
this.lib = lib;
|
|
39
|
+
this.__v = "v2";
|
|
40
|
+
}
|
|
29
41
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
42
|
+
get(params) {
|
|
43
|
+
return this.client.get(params);
|
|
44
|
+
}
|
|
33
45
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
46
|
+
put(params) {
|
|
47
|
+
return this.client.put(params);
|
|
48
|
+
}
|
|
37
49
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
50
|
+
update(params) {
|
|
51
|
+
return this.client.update(params);
|
|
52
|
+
}
|
|
41
53
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
54
|
+
delete(params) {
|
|
55
|
+
return this.client.delete(params);
|
|
56
|
+
}
|
|
45
57
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
58
|
+
batchWrite(params) {
|
|
59
|
+
return this.client.batchWrite(params);
|
|
60
|
+
}
|
|
49
61
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
62
|
+
batchGet(params) {
|
|
63
|
+
return this.client.batchGet(params);
|
|
64
|
+
}
|
|
53
65
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
66
|
+
scan(params) {
|
|
67
|
+
return this.client.scan(params);
|
|
68
|
+
}
|
|
57
69
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
70
|
+
query(params) {
|
|
71
|
+
return this.client.query(params);
|
|
72
|
+
}
|
|
61
73
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
74
|
+
_transact(transactionRequest) {
|
|
75
|
+
let cancellationReasons;
|
|
76
|
+
transactionRequest.on("extractError", (response) => {
|
|
77
|
+
try {
|
|
78
|
+
cancellationReasons = JSON.parse(
|
|
79
|
+
response.httpResponse.body.toString(),
|
|
80
|
+
).CancellationReasons;
|
|
81
|
+
} catch (err) {}
|
|
82
|
+
});
|
|
69
83
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
})
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
throw err;
|
|
87
|
-
}
|
|
88
|
-
});
|
|
84
|
+
return {
|
|
85
|
+
async promise() {
|
|
86
|
+
return transactionRequest.promise().catch((err) => {
|
|
87
|
+
if (err) {
|
|
88
|
+
if (Array.isArray(cancellationReasons)) {
|
|
89
|
+
return {
|
|
90
|
+
canceled: cancellationReasons.map((reason) => {
|
|
91
|
+
if (reason.Item) {
|
|
92
|
+
return unmarshallOutput(reason, [{ key: "Item" }]);
|
|
93
|
+
}
|
|
94
|
+
return reason;
|
|
95
|
+
}),
|
|
96
|
+
};
|
|
89
97
|
}
|
|
90
|
-
|
|
91
|
-
|
|
98
|
+
throw err;
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
92
104
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
105
|
+
transactWrite(params) {
|
|
106
|
+
const transactionRequest = this.client.transactWrite(params);
|
|
107
|
+
return this._transact(transactionRequest);
|
|
108
|
+
}
|
|
97
109
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
110
|
+
transactGet(params) {
|
|
111
|
+
const transactionRequest = this.client.transactGet(params);
|
|
112
|
+
return this._transact(transactionRequest);
|
|
113
|
+
}
|
|
102
114
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
115
|
+
createSet(value, ...rest) {
|
|
116
|
+
if (Array.isArray(value)) {
|
|
117
|
+
return this.client.createSet(value, ...rest);
|
|
118
|
+
} else {
|
|
119
|
+
return this.client.createSet([value], ...rest);
|
|
109
120
|
}
|
|
121
|
+
}
|
|
110
122
|
}
|
|
111
123
|
|
|
112
124
|
class DocumentClientV3Wrapper {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
125
|
+
static init(client) {
|
|
126
|
+
return new DocumentClientV3Wrapper(client, lib);
|
|
127
|
+
}
|
|
116
128
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
129
|
+
constructor(client, lib) {
|
|
130
|
+
this.client = client;
|
|
131
|
+
this.lib = lib;
|
|
132
|
+
this.__v = "v3";
|
|
133
|
+
}
|
|
122
134
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
135
|
+
promiseWrap(fn) {
|
|
136
|
+
return {
|
|
137
|
+
promise: async () => {
|
|
138
|
+
return fn();
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
}
|
|
130
142
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
143
|
+
get(params) {
|
|
144
|
+
return this.promiseWrap(() => {
|
|
145
|
+
const command = new this.lib.GetCommand(params);
|
|
146
|
+
return this.client.send(command);
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
put(params) {
|
|
150
|
+
return this.promiseWrap(() => {
|
|
151
|
+
const command = new this.lib.PutCommand(params);
|
|
152
|
+
return this.client.send(command);
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
update(params) {
|
|
156
|
+
return this.promiseWrap(() => {
|
|
157
|
+
const command = new this.lib.UpdateCommand(params);
|
|
158
|
+
return this.client.send(command);
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
delete(params) {
|
|
162
|
+
return this.promiseWrap(async () => {
|
|
163
|
+
const command = new this.lib.DeleteCommand(params);
|
|
164
|
+
return this.client.send(command);
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
batchWrite(params) {
|
|
168
|
+
return this.promiseWrap(async () => {
|
|
169
|
+
const command = new this.lib.BatchWriteCommand(params);
|
|
170
|
+
return this.client.send(command);
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
batchGet(params) {
|
|
174
|
+
return this.promiseWrap(async () => {
|
|
175
|
+
const command = new this.lib.BatchGetCommand(params);
|
|
176
|
+
return this.client.send(command);
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
scan(params) {
|
|
180
|
+
return this.promiseWrap(async () => {
|
|
181
|
+
const command = new this.lib.ScanCommand(params);
|
|
182
|
+
return this.client.send(command);
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
query(params) {
|
|
186
|
+
return this.promiseWrap(async () => {
|
|
187
|
+
const command = new this.lib.QueryCommand(params);
|
|
188
|
+
return this.client.send(command);
|
|
189
|
+
});
|
|
190
|
+
}
|
|
179
191
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
192
|
+
transactWrite(params) {
|
|
193
|
+
return this.promiseWrap(async () => {
|
|
194
|
+
const command = new this.lib.TransactWriteCommand(params);
|
|
195
|
+
return this.client
|
|
196
|
+
.send(command)
|
|
197
|
+
.then((result) => {
|
|
198
|
+
return result;
|
|
199
|
+
})
|
|
200
|
+
.catch((err) => {
|
|
201
|
+
if (err.CancellationReasons) {
|
|
202
|
+
return {
|
|
203
|
+
canceled: err.CancellationReasons.map((reason) => {
|
|
204
|
+
if (reason.Item) {
|
|
205
|
+
return unmarshallOutput(reason, [{ key: "Item" }]);
|
|
206
|
+
}
|
|
207
|
+
return reason;
|
|
208
|
+
}),
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
throw err;
|
|
200
212
|
});
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
transactGet(params) {
|
|
216
|
+
return this.promiseWrap(async () => {
|
|
217
|
+
const command = new this.lib.TransactGetCommand(params);
|
|
218
|
+
return this.client
|
|
219
|
+
.send(command)
|
|
220
|
+
.then((result) => {
|
|
221
|
+
return result;
|
|
222
|
+
})
|
|
223
|
+
.catch((err) => {
|
|
224
|
+
if (err.CancellationReasons) {
|
|
225
|
+
return {
|
|
226
|
+
canceled: err.CancellationReasons.map((reason) => {
|
|
227
|
+
if (reason.Item) {
|
|
228
|
+
return unmarshallOutput(reason, [{ key: "Item" }]);
|
|
229
|
+
}
|
|
230
|
+
return reason;
|
|
231
|
+
}),
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
throw err;
|
|
222
235
|
});
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
}
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
createSet(value) {
|
|
239
|
+
if (Array.isArray(value)) {
|
|
240
|
+
return new Set(value);
|
|
241
|
+
} else {
|
|
242
|
+
return new Set([value]);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
231
245
|
}
|
|
232
246
|
|
|
233
247
|
function identifyClientVersion(client = {}) {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
248
|
+
if (
|
|
249
|
+
client instanceof DocumentClientV3Wrapper ||
|
|
250
|
+
client instanceof DocumentClientV2Wrapper
|
|
251
|
+
) {
|
|
252
|
+
return DocumentClientVersions.electro;
|
|
253
|
+
}
|
|
254
|
+
for (const [version, methods] of Object.entries(supportedClientVersions)) {
|
|
255
|
+
const hasMethods = methods.every((method) => {
|
|
256
|
+
return method in client && isFunction(client[method]);
|
|
257
|
+
});
|
|
258
|
+
if (hasMethods) {
|
|
259
|
+
return version;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
245
262
|
}
|
|
246
263
|
|
|
247
264
|
function normalizeClient(client) {
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
265
|
+
if (client === undefined) return client;
|
|
266
|
+
const version = identifyClientVersion(client);
|
|
267
|
+
switch (version) {
|
|
268
|
+
case DocumentClientVersions.v3:
|
|
269
|
+
return DocumentClientV3Wrapper.init(client);
|
|
270
|
+
case DocumentClientVersions.v2:
|
|
271
|
+
return DocumentClientV2Wrapper.init(client);
|
|
272
|
+
case DocumentClientVersions.electro:
|
|
273
|
+
return client;
|
|
274
|
+
default:
|
|
275
|
+
throw new ElectroError(
|
|
276
|
+
ErrorCodes.InvalidClientProvided,
|
|
277
|
+
"Invalid DynamoDB Document Client provided. ElectroDB supports the v2 and v3 DynamoDB Document Clients from the aws-sdk",
|
|
278
|
+
);
|
|
279
|
+
}
|
|
260
280
|
}
|
|
261
281
|
|
|
262
282
|
function normalizeConfig(config = {}) {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
283
|
+
return {
|
|
284
|
+
...config,
|
|
285
|
+
client: normalizeClient(config.client),
|
|
286
|
+
};
|
|
267
287
|
}
|
|
268
288
|
|
|
269
289
|
module.exports = {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
290
|
+
util,
|
|
291
|
+
v2Methods,
|
|
292
|
+
v3Methods,
|
|
293
|
+
normalizeClient,
|
|
294
|
+
normalizeConfig,
|
|
295
|
+
identifyClientVersion,
|
|
296
|
+
DocumentClientVersions,
|
|
297
|
+
supportedClientVersions,
|
|
298
|
+
DocumentClientV3Wrapper,
|
|
299
|
+
DocumentClientV2Wrapper,
|
|
280
300
|
};
|