electrodb 2.10.0 → 2.10.2

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/src/client.js CHANGED
@@ -1,280 +1,300 @@
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');
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
- v2: 'v2',
7
- v3: 'v3',
8
- electro: 'electro',
6
+ v2: "v2",
7
+ v3: "v3",
8
+ electro: "electro",
9
9
  };
10
10
  const unmarshallOutput = util.unmarshallOutput || ((val) => val);
11
11
 
12
- const v3Methods = ['send'];
13
- const v2Methods = ['get', 'put', 'update', 'delete', 'batchWrite', 'batchGet', 'scan', 'query', 'createSet', 'transactWrite', 'transactGet'];
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
- [DocumentClientVersions.v2]: v2Methods,
16
- [DocumentClientVersions.v3]: v3Methods,
17
- }
27
+ [DocumentClientVersions.v2]: v2Methods,
28
+ [DocumentClientVersions.v3]: v3Methods,
29
+ };
18
30
 
19
31
  class DocumentClientV2Wrapper {
20
- static init(client) {
21
- return new DocumentClientV2Wrapper(client, lib);
22
- }
32
+ static init(client) {
33
+ return new DocumentClientV2Wrapper(client, lib);
34
+ }
23
35
 
24
- constructor(client, lib) {
25
- this.client = client;
26
- this.lib = lib;
27
- this.__v = 'v2';
28
- }
36
+ constructor(client, lib) {
37
+ this.client = client;
38
+ this.lib = lib;
39
+ this.__v = "v2";
40
+ }
29
41
 
30
- get(params) {
31
- return this.client.get(params);
32
- }
42
+ get(params) {
43
+ return this.client.get(params);
44
+ }
33
45
 
34
- put(params) {
35
- return this.client.put(params);
36
- }
46
+ put(params) {
47
+ return this.client.put(params);
48
+ }
37
49
 
38
- update(params) {
39
- return this.client.update(params);
40
- }
50
+ update(params) {
51
+ return this.client.update(params);
52
+ }
41
53
 
42
- delete(params) {
43
- return this.client.delete(params);
44
- }
54
+ delete(params) {
55
+ return this.client.delete(params);
56
+ }
45
57
 
46
- batchWrite(params) {
47
- return this.client.batchWrite(params);
48
- }
58
+ batchWrite(params) {
59
+ return this.client.batchWrite(params);
60
+ }
49
61
 
50
- batchGet(params) {
51
- return this.client.batchGet(params);
52
- }
62
+ batchGet(params) {
63
+ return this.client.batchGet(params);
64
+ }
53
65
 
54
- scan(params) {
55
- return this.client.scan(params);
56
- }
66
+ scan(params) {
67
+ return this.client.scan(params);
68
+ }
57
69
 
58
- query(params) {
59
- return this.client.query(params);
60
- }
70
+ query(params) {
71
+ return this.client.query(params);
72
+ }
61
73
 
62
- _transact(transactionRequest) {
63
- let cancellationReasons;
64
- transactionRequest.on('extractError', (response) => {
65
- try {
66
- cancellationReasons = JSON.parse(response.httpResponse.body.toString()).CancellationReasons;
67
- } catch (err) {}
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
- return {
71
- async promise() {
72
- return transactionRequest.promise()
73
- .catch((err) => {
74
- if (err) {
75
- if (Array.isArray(cancellationReasons)) {
76
- return {
77
- canceled: cancellationReasons
78
- .map(reason => {
79
- if (reason.Item) {
80
- return unmarshallOutput(reason, [{ key: "Item" }]);
81
- }
82
- return reason;
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
- transactWrite(params) {
94
- const transactionRequest = this.client.transactWrite(params);
95
- return this._transact(transactionRequest);
96
- }
105
+ transactWrite(params) {
106
+ const transactionRequest = this.client.transactWrite(params);
107
+ return this._transact(transactionRequest);
108
+ }
97
109
 
98
- transactGet(params) {
99
- const transactionRequest = this.client.transactGet(params);
100
- return this._transact(transactionRequest);
101
- }
110
+ transactGet(params) {
111
+ const transactionRequest = this.client.transactGet(params);
112
+ return this._transact(transactionRequest);
113
+ }
102
114
 
103
- createSet(value, ...rest) {
104
- if (Array.isArray(value)) {
105
- return this.client.createSet(value, ...rest);
106
- } else {
107
- return this.client.createSet([value], ...rest);
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
- static init(client) {
114
- return new DocumentClientV3Wrapper(client, lib);
115
- }
125
+ static init(client) {
126
+ return new DocumentClientV3Wrapper(client, lib);
127
+ }
116
128
 
117
- constructor(client, lib) {
118
- this.client = client;
119
- this.lib = lib;
120
- this.__v = 'v3';
121
- }
129
+ constructor(client, lib) {
130
+ this.client = client;
131
+ this.lib = lib;
132
+ this.__v = "v3";
133
+ }
122
134
 
123
- promiseWrap(fn) {
124
- return {
125
- promise: async () => {
126
- return fn();
127
- }
128
- }
129
- }
135
+ promiseWrap(fn) {
136
+ return {
137
+ promise: async () => {
138
+ return fn();
139
+ },
140
+ };
141
+ }
130
142
 
131
- get(params) {
132
- return this.promiseWrap(() => {
133
- const command = new this.lib.GetCommand(params);
134
- return this.client.send(command);
135
- });
136
- }
137
- put(params) {
138
- return this.promiseWrap(() => {
139
- const command = new this.lib.PutCommand(params);
140
- return this.client.send(command);
141
- });
142
- }
143
- update(params) {
144
- return this.promiseWrap(() => {
145
- const command = new this.lib.UpdateCommand(params);
146
- return this.client.send(command);
147
- });
148
- }
149
- delete(params) {
150
- return this.promiseWrap(async () => {
151
- const command = new this.lib.DeleteCommand(params);
152
- return this.client.send(command);
153
- });
154
- }
155
- batchWrite(params) {
156
- return this.promiseWrap(async () => {
157
- const command = new this.lib.BatchWriteCommand(params);
158
- return this.client.send(command);
159
- });
160
- }
161
- batchGet(params) {
162
- return this.promiseWrap(async () => {
163
- const command = new this.lib.BatchGetCommand(params);
164
- return this.client.send(command);
165
- });
166
- }
167
- scan(params) {
168
- return this.promiseWrap(async () => {
169
- const command = new this.lib.ScanCommand(params);
170
- return this.client.send(command);
171
- });
172
- }
173
- query(params) {
174
- return this.promiseWrap(async () => {
175
- const command = new this.lib.QueryCommand(params);
176
- return this.client.send(command);
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
- transactWrite(params) {
181
- return this.promiseWrap(async () => {
182
- const command = new this.lib.TransactWriteCommand(params);
183
- return this.client.send(command)
184
- .then((result) => {
185
- return result;
186
- })
187
- .catch(err => {
188
- if (err.CancellationReasons) {
189
- return {
190
- canceled: err.CancellationReasons.map(reason => {
191
- if (reason.Item) {
192
- return unmarshallOutput(reason, [{ key: "Item" }]);
193
- }
194
- return reason;
195
- })
196
- }
197
- }
198
- throw err;
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
- transactGet(params) {
203
- return this.promiseWrap(async () => {
204
- const command = new this.lib.TransactGetCommand(params);
205
- return this.client.send(command)
206
- .then((result) => {
207
- return result;
208
- })
209
- .catch(err => {
210
- if (err.CancellationReasons) {
211
- return {
212
- canceled: err.CancellationReasons.map(reason => {
213
- if (reason.Item) {
214
- return unmarshallOutput(reason, [{ key: "Item" }]);
215
- }
216
- return reason;
217
- })
218
- }
219
- }
220
- throw err;
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
- createSet(value) {
225
- if (Array.isArray(value)) {
226
- return new Set(value);
227
- } else {
228
- return new Set([value]);
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
- if (client instanceof DocumentClientV3Wrapper || client instanceof DocumentClientV2Wrapper) {
235
- return DocumentClientVersions.electro;
236
- }
237
- for (const [version, methods] of Object.entries(supportedClientVersions)) {
238
- const hasMethods = methods.every(method => {
239
- return method in client && isFunction(client[method]);
240
- });
241
- if (hasMethods) {
242
- return version;
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
- if (client === undefined) return client;
249
- const version = identifyClientVersion(client);
250
- switch(version) {
251
- case DocumentClientVersions.v3:
252
- return DocumentClientV3Wrapper.init(client);
253
- case DocumentClientVersions.v2:
254
- return DocumentClientV2Wrapper.init(client);
255
- case DocumentClientVersions.electro:
256
- return client;
257
- default:
258
- throw new ElectroError(ErrorCodes.InvalidClientProvided, 'Invalid DynamoDB Document Client provided. ElectroDB supports the v2 and v3 DynamoDB Document Clients from the aws-sdk');
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
- return {
264
- ...config,
265
- client: normalizeClient(config.client)
266
- }
283
+ return {
284
+ ...config,
285
+ client: normalizeClient(config.client),
286
+ };
267
287
  }
268
288
 
269
289
  module.exports = {
270
- util,
271
- v2Methods,
272
- v3Methods,
273
- normalizeClient,
274
- normalizeConfig,
275
- identifyClientVersion,
276
- DocumentClientVersions,
277
- supportedClientVersions,
278
- DocumentClientV3Wrapper,
279
- DocumentClientV2Wrapper,
290
+ util,
291
+ v2Methods,
292
+ v3Methods,
293
+ normalizeClient,
294
+ normalizeConfig,
295
+ identifyClientVersion,
296
+ DocumentClientVersions,
297
+ supportedClientVersions,
298
+ DocumentClientV3Wrapper,
299
+ DocumentClientV2Wrapper,
280
300
  };