-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathclient.js
147 lines (137 loc) · 4.24 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const lib = require('@aws-sdk/lib-dynamodb')
const { isFunction } = require('./validations');
const { ElectroError, ErrorCodes } = require('./errors');
const DocumentClientVersions = {
v2: 'v2',
v3: 'v3',
electro: 'electro',
};
const v3Methods = ['send'];
const v2Methods = ['get', 'put', 'update', 'delete', 'batchWrite', 'batchGet', 'scan', 'query', 'createSet', 'transactWrite', 'transactGet'];
const supportedClientVersions = {
[DocumentClientVersions.v2]: v2Methods,
[DocumentClientVersions.v3]: v3Methods,
}
class DocumentClientV3Wrapper {
static init(client) {
return new DocumentClientV3Wrapper(client, lib);
}
constructor(client, lib) {
this.client = client;
this.lib = lib;
}
promiseWrap(fn) {
return {
promise: async () => {
return fn();
}
}
}
get(params) {
return this.promiseWrap(() => {
const command = new this.lib.GetCommand(params);
return this.client.send(command);
});
}
put(params) {
return this.promiseWrap(() => {
const command = new this.lib.PutCommand(params);
return this.client.send(command);
});
}
update(params) {
return this.promiseWrap(() => {
const command = new this.lib.UpdateCommand(params);
return this.client.send(command);
});
}
delete(params) {
return this.promiseWrap(async () => {
const command = new this.lib.DeleteCommand(params);
return this.client.send(command);
});
}
batchWrite(params) {
return this.promiseWrap(async () => {
const command = new this.lib.BatchWriteCommand(params);
return this.client.send(command);
});
}
batchGet(params) {
return this.promiseWrap(async () => {
const command = new this.lib.BatchGetCommand(params);
return this.client.send(command);
});
}
scan(params) {
return this.promiseWrap(async () => {
const command = new this.lib.ScanCommand(params);
return this.client.send(command);
});
}
query(params) {
return this.promiseWrap(async () => {
const command = new this.lib.QueryCommand(params);
return this.client.send(command);
});
}
transactWrite(params) {
return this.promiseWrap(async () => {
const command = new this.lib.TransactWriteCommand(params);
return this.client.send(command);
});
}
transactGet(params) {
return this.promiseWrap(async () => {
const command = new this.lib.TransactGetCommand(params);
return this.client.send(command);
});
}
createSet(value) {
if (Array.isArray(value)) {
return new Set(value);
} else {
return new Set([value]);
}
}
}
function identifyClientVersion(client = {}) {
if (client instanceof DocumentClientV3Wrapper) return DocumentClientVersions.electro;
for (const [version, methods] of Object.entries(supportedClientVersions)) {
const hasMethods = methods.every(method => {
return method in client && isFunction(client[method]);
});
if (hasMethods) {
return version;
}
}
}
function normalizeClient(client) {
if (client === undefined) return client;
const version = identifyClientVersion(client);
switch(version) {
case DocumentClientVersions.v3:
return DocumentClientV3Wrapper.init(client);
case DocumentClientVersions.v2:
case DocumentClientVersions.electro:
return client;
default:
throw new ElectroError(ErrorCodes.InvalidClientProvided, 'Invalid DynamoDB Document Client provided. ElectroDB supports the v2 and v3 DynamoDB Document Clients from the aws-sdk');
}
}
function normalizeConfig(config = {}) {
return {
...config,
client: normalizeClient(config.client)
}
}
module.exports = {
v2Methods,
v3Methods,
normalizeClient,
normalizeConfig,
identifyClientVersion,
DocumentClientVersions,
supportedClientVersions,
DocumentClientV3Wrapper,
};