dynamo-bao 0.1.8 → 0.2.0
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/bin/dynamo-bao-create-table.js +2 -2
- package/package.json +5 -6
- package/src/db.js +1 -3
- package/src/dynamodb-client.js +645 -0
- package/src/mixins/batch-loading-mixin.js +1 -1
- package/src/mixins/mutation-mixin.js +1 -1
- package/src/mixins/query-mixin.js +1 -1
- package/src/mixins/unique-constraint-mixin.js +1 -1
- package/src/model-manager.js +2 -3
- package/src/model.js +11 -8
|
@@ -4,7 +4,7 @@ const {
|
|
|
4
4
|
DynamoDBClient,
|
|
5
5
|
CreateTableCommand,
|
|
6
6
|
ListTablesCommand,
|
|
7
|
-
} = require("
|
|
7
|
+
} = require("../src/dynamodb-client");
|
|
8
8
|
const readline = require("readline");
|
|
9
9
|
const fs = require("fs");
|
|
10
10
|
const path = require("path");
|
|
@@ -51,7 +51,7 @@ const prompt = (query) => {
|
|
|
51
51
|
};
|
|
52
52
|
|
|
53
53
|
async function writeConfigFile(tableName) {
|
|
54
|
-
const region =
|
|
54
|
+
const region = client.region || process.env.AWS_REGION || 'us-east-1';
|
|
55
55
|
const configContent = `const path = require("path");
|
|
56
56
|
|
|
57
57
|
const config = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dynamo-bao",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Simple powerful data modeling primitives for DynamoDB in JavaScript.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -30,23 +30,22 @@
|
|
|
30
30
|
"url": "https://github.com/aag1024/dynamo-bao"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"
|
|
34
|
-
"@aws-sdk/lib-dynamodb": "^3.521.0",
|
|
35
|
-
"chokidar": "^4.0.3",
|
|
33
|
+
"aws4fetch": "^1.0.20",
|
|
36
34
|
"dotenv": "^16.4.7",
|
|
37
35
|
"js-yaml": "^4.1.0",
|
|
38
36
|
"ulid": "^2.3.0"
|
|
39
37
|
},
|
|
40
38
|
"devDependencies": {
|
|
39
|
+
"chokidar": "^4.0.3",
|
|
41
40
|
"clean-jsdoc-theme": "^4.3.0",
|
|
42
41
|
"cross-env": "^7.0.3",
|
|
43
42
|
"docdash": "^2.0.2",
|
|
43
|
+
"formidable": ">=2.1.3",
|
|
44
44
|
"gh-pages": "^6.2.0",
|
|
45
45
|
"jest": "^29.7.0",
|
|
46
46
|
"jsdoc": "^4.0.4",
|
|
47
47
|
"prettier": "3.4.2",
|
|
48
|
-
"serverless": "^3.38.0"
|
|
49
|
-
"formidable": ">=2.1.3"
|
|
48
|
+
"serverless": "^3.38.0"
|
|
50
49
|
},
|
|
51
50
|
"bin": {
|
|
52
51
|
"bao-init": "bin/dynamo-bao-create-table.js",
|
package/src/db.js
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
// src/db.js
|
|
2
|
-
const { DynamoDBClient } = require("
|
|
3
|
-
const { DynamoDBDocumentClient } = require("@aws-sdk/lib-dynamodb");
|
|
2
|
+
const { DynamoDBClient, DynamoDBDocumentClient } = require("./dynamodb-client");
|
|
4
3
|
|
|
5
4
|
function createDbClient() {
|
|
6
5
|
const client = new DynamoDBClient({
|
|
7
6
|
region: process.env.AWS_REGION || "us-east-1",
|
|
8
|
-
maxAttempts: 3,
|
|
9
7
|
...(process.env.DYNAMODB_ENDPOINT && {
|
|
10
8
|
endpoint: process.env.DYNAMODB_ENDPOINT,
|
|
11
9
|
}),
|
|
@@ -0,0 +1,645 @@
|
|
|
1
|
+
const { AwsClient } = require('aws4fetch');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const os = require('os');
|
|
5
|
+
|
|
6
|
+
class DynamoDBClient {
|
|
7
|
+
constructor(config = {}) {
|
|
8
|
+
this.region = config.region || this._resolveRegion();
|
|
9
|
+
this.endpoint = config.endpoint || `https://dynamodb.${this.region}.amazonaws.com`;
|
|
10
|
+
this.credentials = config.credentials;
|
|
11
|
+
|
|
12
|
+
// Don't create the aws client until we actually need it
|
|
13
|
+
this.awsClient = null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
_resolveRegion() {
|
|
17
|
+
// 1. Try environment variables
|
|
18
|
+
if (process.env.AWS_REGION) return process.env.AWS_REGION;
|
|
19
|
+
if (process.env.AWS_DEFAULT_REGION) return process.env.AWS_DEFAULT_REGION;
|
|
20
|
+
|
|
21
|
+
// 2. Try AWS config file
|
|
22
|
+
try {
|
|
23
|
+
const profile = process.env.AWS_PROFILE || 'default';
|
|
24
|
+
const configPath = path.join(os.homedir(), '.aws', 'config');
|
|
25
|
+
|
|
26
|
+
if (fs.existsSync(configPath)) {
|
|
27
|
+
const configFile = fs.readFileSync(configPath, 'utf8');
|
|
28
|
+
const region = this._parseRegionFromConfigFile(configFile, profile);
|
|
29
|
+
if (region) return region;
|
|
30
|
+
}
|
|
31
|
+
} catch (error) {
|
|
32
|
+
// Ignore file reading errors
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// 3. Default fallback
|
|
36
|
+
return 'us-east-1';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
_parseRegionFromConfigFile(content, profile) {
|
|
40
|
+
const lines = content.split('\n');
|
|
41
|
+
let inSection = false;
|
|
42
|
+
let sectionName = '';
|
|
43
|
+
|
|
44
|
+
for (const line of lines) {
|
|
45
|
+
const trimmedLine = line.trim();
|
|
46
|
+
|
|
47
|
+
if (!trimmedLine || trimmedLine.startsWith('#') || trimmedLine.startsWith(';')) {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const sectionMatch = trimmedLine.match(/^\[([^\]]+)\]$/);
|
|
52
|
+
if (sectionMatch) {
|
|
53
|
+
sectionName = sectionMatch[1];
|
|
54
|
+
inSection = sectionName === profile || sectionName === `profile ${profile}`;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (inSection) {
|
|
59
|
+
const keyValueMatch = trimmedLine.match(/^([^=]+)=(.*)$/);
|
|
60
|
+
if (keyValueMatch) {
|
|
61
|
+
const key = keyValueMatch[1].trim();
|
|
62
|
+
const value = keyValueMatch[2].trim();
|
|
63
|
+
|
|
64
|
+
if (key === 'region') {
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
_resolveCredentials() {
|
|
75
|
+
// 1. Use explicitly provided credentials
|
|
76
|
+
if (this.credentials && this.credentials.accessKeyId && this.credentials.secretAccessKey) {
|
|
77
|
+
return this.credentials;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 2. Try environment variables
|
|
81
|
+
if (process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY) {
|
|
82
|
+
return {
|
|
83
|
+
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
|
84
|
+
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
|
|
85
|
+
sessionToken: process.env.AWS_SESSION_TOKEN
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// 3. Try AWS credentials file
|
|
90
|
+
try {
|
|
91
|
+
const profile = process.env.AWS_PROFILE || 'default';
|
|
92
|
+
const credentialsPath = path.join(os.homedir(), '.aws', 'credentials');
|
|
93
|
+
|
|
94
|
+
if (fs.existsSync(credentialsPath)) {
|
|
95
|
+
const credentialsFile = fs.readFileSync(credentialsPath, 'utf8');
|
|
96
|
+
const credentials = this._parseCredentialsFile(credentialsFile, profile);
|
|
97
|
+
if (credentials) {
|
|
98
|
+
return credentials;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
} catch (error) {
|
|
102
|
+
// Ignore file reading errors and continue
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// 4. Try AWS config file for credentials
|
|
106
|
+
try {
|
|
107
|
+
const profile = process.env.AWS_PROFILE || 'default';
|
|
108
|
+
const configPath = path.join(os.homedir(), '.aws', 'config');
|
|
109
|
+
|
|
110
|
+
if (fs.existsSync(configPath)) {
|
|
111
|
+
const configFile = fs.readFileSync(configPath, 'utf8');
|
|
112
|
+
const credentials = this._parseCredentialsFile(configFile, profile);
|
|
113
|
+
if (credentials) {
|
|
114
|
+
return credentials;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
} catch (error) {
|
|
118
|
+
// Ignore file reading errors
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
_parseCredentialsFile(content, profile) {
|
|
125
|
+
const lines = content.split('\n');
|
|
126
|
+
let inSection = false;
|
|
127
|
+
let sectionName = '';
|
|
128
|
+
const credentials = {};
|
|
129
|
+
|
|
130
|
+
for (const line of lines) {
|
|
131
|
+
const trimmedLine = line.trim();
|
|
132
|
+
|
|
133
|
+
// Skip empty lines and comments
|
|
134
|
+
if (!trimmedLine || trimmedLine.startsWith('#') || trimmedLine.startsWith(';')) {
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Check for section headers
|
|
139
|
+
const sectionMatch = trimmedLine.match(/^\[([^\]]+)\]$/);
|
|
140
|
+
if (sectionMatch) {
|
|
141
|
+
sectionName = sectionMatch[1];
|
|
142
|
+
inSection = sectionName === profile || sectionName === `profile ${profile}`;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Parse key-value pairs in the target section
|
|
147
|
+
if (inSection) {
|
|
148
|
+
const keyValueMatch = trimmedLine.match(/^([^=]+)=(.*)$/);
|
|
149
|
+
if (keyValueMatch) {
|
|
150
|
+
const key = keyValueMatch[1].trim();
|
|
151
|
+
const value = keyValueMatch[2].trim();
|
|
152
|
+
|
|
153
|
+
if (key === 'aws_access_key_id') {
|
|
154
|
+
credentials.accessKeyId = value;
|
|
155
|
+
} else if (key === 'aws_secret_access_key') {
|
|
156
|
+
credentials.secretAccessKey = value;
|
|
157
|
+
} else if (key === 'aws_session_token') {
|
|
158
|
+
credentials.sessionToken = value;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (credentials.accessKeyId && credentials.secretAccessKey) {
|
|
165
|
+
return credentials;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
_ensureClient() {
|
|
172
|
+
if (!this.awsClient) {
|
|
173
|
+
const credentials = this._resolveCredentials();
|
|
174
|
+
|
|
175
|
+
if (!credentials) {
|
|
176
|
+
throw new Error('AWS credentials not found. Please set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables, configure ~/.aws/credentials, or provide credentials in config.');
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
this.awsClient = new AwsClient({
|
|
180
|
+
accessKeyId: credentials.accessKeyId,
|
|
181
|
+
secretAccessKey: credentials.secretAccessKey,
|
|
182
|
+
sessionToken: credentials.sessionToken,
|
|
183
|
+
region: this.region
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
return this.awsClient;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
_getOperationName(commandName) {
|
|
190
|
+
const operationMap = {
|
|
191
|
+
'GetCommand': 'GetItem',
|
|
192
|
+
'QueryCommand': 'Query',
|
|
193
|
+
'UpdateCommand': 'UpdateItem',
|
|
194
|
+
'DeleteCommand': 'DeleteItem',
|
|
195
|
+
'BatchGetCommand': 'BatchGetItem',
|
|
196
|
+
'TransactWriteCommand': 'TransactWriteItems',
|
|
197
|
+
'CreateTableCommand': 'CreateTable',
|
|
198
|
+
'ListTablesCommand': 'ListTables',
|
|
199
|
+
'DescribeTableCommand': 'DescribeTable'
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
return operationMap[commandName] || commandName.replace('Command', '');
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async send(command) {
|
|
206
|
+
const client = this._ensureClient();
|
|
207
|
+
const commandName = command.constructor.name;
|
|
208
|
+
const operation = this._getOperationName(commandName);
|
|
209
|
+
const headers = {
|
|
210
|
+
'Content-Type': 'application/x-amz-json-1.0',
|
|
211
|
+
'X-Amz-Target': `DynamoDB_20120810.${operation}`
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
const response = await client.fetch(this.endpoint, {
|
|
215
|
+
method: 'POST',
|
|
216
|
+
headers,
|
|
217
|
+
body: JSON.stringify(command.input)
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
if (!response.ok) {
|
|
221
|
+
const errorBody = await response.json();
|
|
222
|
+
const errorType = errorBody.__type || 'DynamoDBError';
|
|
223
|
+
|
|
224
|
+
// Map DynamoDB error types to expected names
|
|
225
|
+
let errorName = errorType;
|
|
226
|
+
if (errorType.includes('#')) {
|
|
227
|
+
errorName = errorType.split('#')[1];
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const error = new Error(errorBody.message || errorBody.Message || errorType);
|
|
231
|
+
error.name = errorName;
|
|
232
|
+
error.statusCode = response.status;
|
|
233
|
+
error.$metadata = { httpStatusCode: response.status };
|
|
234
|
+
|
|
235
|
+
// Add specific properties based on error type
|
|
236
|
+
if (errorName === 'TransactionCanceledException' && errorBody.CancellationReasons) {
|
|
237
|
+
error.CancellationReasons = errorBody.CancellationReasons;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
throw error;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return response.json();
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
class DynamoDBDocumentClient {
|
|
248
|
+
constructor(client, config = {}) {
|
|
249
|
+
this.client = client;
|
|
250
|
+
this.marshallOptions = config.marshallOptions || {};
|
|
251
|
+
this.unmarshallOptions = config.unmarshallOptions || {};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
static from(client, config = {}) {
|
|
255
|
+
return new DynamoDBDocumentClient(client, config);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
async send(command) {
|
|
259
|
+
const marshalledCommand = this.marshallCommand(command);
|
|
260
|
+
const response = await this.client.send(marshalledCommand);
|
|
261
|
+
return this.unmarshallResponse(response, command);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
marshallCommand(command) {
|
|
265
|
+
const commandName = command.constructor.name;
|
|
266
|
+
const input = { ...command.input };
|
|
267
|
+
|
|
268
|
+
switch (commandName) {
|
|
269
|
+
case 'GetCommand':
|
|
270
|
+
const marshalledKey = {};
|
|
271
|
+
Object.keys(input.Key).forEach(key => {
|
|
272
|
+
marshalledKey[key] = marshall(input.Key[key]);
|
|
273
|
+
});
|
|
274
|
+
input.Key = marshalledKey;
|
|
275
|
+
break;
|
|
276
|
+
|
|
277
|
+
case 'QueryCommand':
|
|
278
|
+
if (input.ExpressionAttributeValues) {
|
|
279
|
+
const marshalledValues = {};
|
|
280
|
+
Object.keys(input.ExpressionAttributeValues).forEach(key => {
|
|
281
|
+
marshalledValues[key] = marshall(input.ExpressionAttributeValues[key]);
|
|
282
|
+
});
|
|
283
|
+
input.ExpressionAttributeValues = marshalledValues;
|
|
284
|
+
}
|
|
285
|
+
if (input.ExclusiveStartKey) {
|
|
286
|
+
const marshalledStartKey = {};
|
|
287
|
+
Object.keys(input.ExclusiveStartKey).forEach(key => {
|
|
288
|
+
marshalledStartKey[key] = marshall(input.ExclusiveStartKey[key]);
|
|
289
|
+
});
|
|
290
|
+
input.ExclusiveStartKey = marshalledStartKey;
|
|
291
|
+
}
|
|
292
|
+
break;
|
|
293
|
+
|
|
294
|
+
case 'UpdateCommand':
|
|
295
|
+
const updateKey = {};
|
|
296
|
+
Object.keys(input.Key).forEach(key => {
|
|
297
|
+
updateKey[key] = marshall(input.Key[key]);
|
|
298
|
+
});
|
|
299
|
+
input.Key = updateKey;
|
|
300
|
+
if (input.ExpressionAttributeValues) {
|
|
301
|
+
const marshalledValues = {};
|
|
302
|
+
Object.keys(input.ExpressionAttributeValues).forEach(key => {
|
|
303
|
+
marshalledValues[key] = marshall(input.ExpressionAttributeValues[key]);
|
|
304
|
+
});
|
|
305
|
+
input.ExpressionAttributeValues = marshalledValues;
|
|
306
|
+
}
|
|
307
|
+
break;
|
|
308
|
+
|
|
309
|
+
case 'DeleteCommand':
|
|
310
|
+
const deleteKey = {};
|
|
311
|
+
Object.keys(input.Key).forEach(key => {
|
|
312
|
+
deleteKey[key] = marshall(input.Key[key]);
|
|
313
|
+
});
|
|
314
|
+
input.Key = deleteKey;
|
|
315
|
+
if (input.ExpressionAttributeValues) {
|
|
316
|
+
const marshalledValues = {};
|
|
317
|
+
Object.keys(input.ExpressionAttributeValues).forEach(key => {
|
|
318
|
+
marshalledValues[key] = marshall(input.ExpressionAttributeValues[key]);
|
|
319
|
+
});
|
|
320
|
+
input.ExpressionAttributeValues = marshalledValues;
|
|
321
|
+
}
|
|
322
|
+
break;
|
|
323
|
+
|
|
324
|
+
case 'BatchGetCommand':
|
|
325
|
+
if (input.RequestItems) {
|
|
326
|
+
Object.keys(input.RequestItems).forEach(tableName => {
|
|
327
|
+
const tableRequest = input.RequestItems[tableName];
|
|
328
|
+
if (tableRequest.Keys) {
|
|
329
|
+
tableRequest.Keys = tableRequest.Keys.map(key => {
|
|
330
|
+
const marshalledKey = {};
|
|
331
|
+
Object.keys(key).forEach(keyField => {
|
|
332
|
+
marshalledKey[keyField] = marshall(key[keyField]);
|
|
333
|
+
});
|
|
334
|
+
return marshalledKey;
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
break;
|
|
340
|
+
|
|
341
|
+
case 'TransactWriteCommand':
|
|
342
|
+
if (input.TransactItems) {
|
|
343
|
+
input.TransactItems = input.TransactItems.map(item => {
|
|
344
|
+
const newItem = { ...item };
|
|
345
|
+
if (item.Put) {
|
|
346
|
+
const marshalledItem = {};
|
|
347
|
+
Object.keys(item.Put.Item).forEach(key => {
|
|
348
|
+
marshalledItem[key] = marshall(item.Put.Item[key]);
|
|
349
|
+
});
|
|
350
|
+
newItem.Put = {
|
|
351
|
+
...item.Put,
|
|
352
|
+
Item: marshalledItem
|
|
353
|
+
};
|
|
354
|
+
if (item.Put.ExpressionAttributeValues) {
|
|
355
|
+
const marshalledValues = {};
|
|
356
|
+
Object.keys(item.Put.ExpressionAttributeValues).forEach(key => {
|
|
357
|
+
marshalledValues[key] = marshall(item.Put.ExpressionAttributeValues[key]);
|
|
358
|
+
});
|
|
359
|
+
newItem.Put.ExpressionAttributeValues = marshalledValues;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
if (item.Update) {
|
|
363
|
+
const updateKey = {};
|
|
364
|
+
Object.keys(item.Update.Key).forEach(key => {
|
|
365
|
+
updateKey[key] = marshall(item.Update.Key[key]);
|
|
366
|
+
});
|
|
367
|
+
newItem.Update = {
|
|
368
|
+
...item.Update,
|
|
369
|
+
Key: updateKey
|
|
370
|
+
};
|
|
371
|
+
if (item.Update.ExpressionAttributeValues) {
|
|
372
|
+
const marshalledValues = {};
|
|
373
|
+
Object.keys(item.Update.ExpressionAttributeValues).forEach(key => {
|
|
374
|
+
marshalledValues[key] = marshall(item.Update.ExpressionAttributeValues[key]);
|
|
375
|
+
});
|
|
376
|
+
newItem.Update.ExpressionAttributeValues = marshalledValues;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
if (item.Delete) {
|
|
380
|
+
const deleteKey = {};
|
|
381
|
+
Object.keys(item.Delete.Key).forEach(key => {
|
|
382
|
+
deleteKey[key] = marshall(item.Delete.Key[key]);
|
|
383
|
+
});
|
|
384
|
+
newItem.Delete = {
|
|
385
|
+
...item.Delete,
|
|
386
|
+
Key: deleteKey
|
|
387
|
+
};
|
|
388
|
+
if (item.Delete.ExpressionAttributeValues) {
|
|
389
|
+
const marshalledValues = {};
|
|
390
|
+
Object.keys(item.Delete.ExpressionAttributeValues).forEach(key => {
|
|
391
|
+
marshalledValues[key] = marshall(item.Delete.ExpressionAttributeValues[key]);
|
|
392
|
+
});
|
|
393
|
+
newItem.Delete.ExpressionAttributeValues = marshalledValues;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
return newItem;
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
break;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
return {
|
|
403
|
+
constructor: { name: commandName },
|
|
404
|
+
input
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
unmarshallResponse(response, command) {
|
|
409
|
+
const commandName = command.constructor.name;
|
|
410
|
+
const result = { ...response };
|
|
411
|
+
|
|
412
|
+
switch (commandName) {
|
|
413
|
+
case 'GetCommand':
|
|
414
|
+
if (result.Item) {
|
|
415
|
+
const unmarshalledItem = {};
|
|
416
|
+
Object.keys(result.Item).forEach(key => {
|
|
417
|
+
unmarshalledItem[key] = unmarshall(result.Item[key]);
|
|
418
|
+
});
|
|
419
|
+
result.Item = unmarshalledItem;
|
|
420
|
+
}
|
|
421
|
+
break;
|
|
422
|
+
|
|
423
|
+
case 'QueryCommand':
|
|
424
|
+
if (result.Items) {
|
|
425
|
+
result.Items = result.Items.map(item => {
|
|
426
|
+
const unmarshalledItem = {};
|
|
427
|
+
Object.keys(item).forEach(key => {
|
|
428
|
+
unmarshalledItem[key] = unmarshall(item[key]);
|
|
429
|
+
});
|
|
430
|
+
return unmarshalledItem;
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
if (result.LastEvaluatedKey) {
|
|
434
|
+
const unmarshalledKey = {};
|
|
435
|
+
Object.keys(result.LastEvaluatedKey).forEach(key => {
|
|
436
|
+
unmarshalledKey[key] = unmarshall(result.LastEvaluatedKey[key]);
|
|
437
|
+
});
|
|
438
|
+
result.LastEvaluatedKey = unmarshalledKey;
|
|
439
|
+
}
|
|
440
|
+
break;
|
|
441
|
+
|
|
442
|
+
case 'UpdateCommand':
|
|
443
|
+
if (result.Attributes) {
|
|
444
|
+
const unmarshalledAttributes = {};
|
|
445
|
+
Object.keys(result.Attributes).forEach(key => {
|
|
446
|
+
unmarshalledAttributes[key] = unmarshall(result.Attributes[key]);
|
|
447
|
+
});
|
|
448
|
+
result.Attributes = unmarshalledAttributes;
|
|
449
|
+
}
|
|
450
|
+
break;
|
|
451
|
+
|
|
452
|
+
case 'DeleteCommand':
|
|
453
|
+
if (result.Attributes) {
|
|
454
|
+
const unmarshalledAttributes = {};
|
|
455
|
+
Object.keys(result.Attributes).forEach(key => {
|
|
456
|
+
unmarshalledAttributes[key] = unmarshall(result.Attributes[key]);
|
|
457
|
+
});
|
|
458
|
+
result.Attributes = unmarshalledAttributes;
|
|
459
|
+
}
|
|
460
|
+
break;
|
|
461
|
+
|
|
462
|
+
case 'BatchGetCommand':
|
|
463
|
+
if (result.Responses) {
|
|
464
|
+
Object.keys(result.Responses).forEach(tableName => {
|
|
465
|
+
result.Responses[tableName] = result.Responses[tableName].map(item => {
|
|
466
|
+
const unmarshalledItem = {};
|
|
467
|
+
Object.keys(item).forEach(key => {
|
|
468
|
+
unmarshalledItem[key] = unmarshall(item[key]);
|
|
469
|
+
});
|
|
470
|
+
return unmarshalledItem;
|
|
471
|
+
});
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
if (result.UnprocessedKeys) {
|
|
475
|
+
Object.keys(result.UnprocessedKeys).forEach(tableName => {
|
|
476
|
+
const tableRequest = result.UnprocessedKeys[tableName];
|
|
477
|
+
if (tableRequest.Keys) {
|
|
478
|
+
tableRequest.Keys = tableRequest.Keys.map(key => {
|
|
479
|
+
const unmarshalledKey = {};
|
|
480
|
+
Object.keys(key).forEach(keyField => {
|
|
481
|
+
unmarshalledKey[keyField] = unmarshall(key[keyField]);
|
|
482
|
+
});
|
|
483
|
+
return unmarshalledKey;
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
break;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
return result;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
// Marshall JavaScript values to DynamoDB JSON
|
|
496
|
+
function marshall(obj) {
|
|
497
|
+
if (obj === null) return { NULL: true };
|
|
498
|
+
if (obj === undefined) return { NULL: true };
|
|
499
|
+
|
|
500
|
+
const type = typeof obj;
|
|
501
|
+
|
|
502
|
+
if (type === 'string') {
|
|
503
|
+
if (obj === '') {
|
|
504
|
+
throw new Error('Cannot marshall empty string - DynamoDB does not support empty string values');
|
|
505
|
+
}
|
|
506
|
+
return { S: obj };
|
|
507
|
+
}
|
|
508
|
+
if (type === 'number') return { N: String(obj) };
|
|
509
|
+
if (type === 'boolean') return { BOOL: obj };
|
|
510
|
+
|
|
511
|
+
if (obj instanceof Uint8Array || Buffer.isBuffer(obj)) {
|
|
512
|
+
return { B: Buffer.from(obj).toString('base64') };
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
if (Array.isArray(obj)) {
|
|
516
|
+
if (obj.length === 0) return { L: [] };
|
|
517
|
+
|
|
518
|
+
const firstType = typeof obj[0];
|
|
519
|
+
const isHomogeneous = obj.every(item => typeof item === firstType);
|
|
520
|
+
|
|
521
|
+
if (isHomogeneous && firstType === 'string') {
|
|
522
|
+
return { SS: obj };
|
|
523
|
+
} else if (isHomogeneous && firstType === 'number') {
|
|
524
|
+
return { NS: obj.map(String) };
|
|
525
|
+
} else if (isHomogeneous && (obj[0] instanceof Uint8Array || Buffer.isBuffer(obj[0]))) {
|
|
526
|
+
return { BS: obj.map(b => Buffer.from(b).toString('base64')) };
|
|
527
|
+
} else {
|
|
528
|
+
return { L: obj.map(marshall) };
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
if (type === 'object') {
|
|
533
|
+
const result = {};
|
|
534
|
+
for (const key in obj) {
|
|
535
|
+
if (obj.hasOwnProperty(key) && obj[key] !== undefined) {
|
|
536
|
+
result[key] = marshall(obj[key]);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
return { M: result };
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
throw new Error(`Cannot marshall type: ${type}`);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
// Unmarshall DynamoDB JSON to JavaScript values
|
|
546
|
+
function unmarshall(obj) {
|
|
547
|
+
if (!obj || typeof obj !== 'object') return obj;
|
|
548
|
+
|
|
549
|
+
const keys = Object.keys(obj);
|
|
550
|
+
if (keys.length !== 1) return obj;
|
|
551
|
+
|
|
552
|
+
const [type] = keys;
|
|
553
|
+
const value = obj[type];
|
|
554
|
+
|
|
555
|
+
switch (type) {
|
|
556
|
+
case 'S': return value;
|
|
557
|
+
case 'N': return Number(value);
|
|
558
|
+
case 'BOOL': return value;
|
|
559
|
+
case 'NULL': return null;
|
|
560
|
+
case 'B': return Buffer.from(value, 'base64');
|
|
561
|
+
case 'SS': return value;
|
|
562
|
+
case 'NS': return value.map(Number);
|
|
563
|
+
case 'BS': return value.map(v => Buffer.from(v, 'base64'));
|
|
564
|
+
case 'L': return value.map(unmarshall);
|
|
565
|
+
case 'M': {
|
|
566
|
+
const result = {};
|
|
567
|
+
for (const key in value) {
|
|
568
|
+
result[key] = unmarshall(value[key]);
|
|
569
|
+
}
|
|
570
|
+
return result;
|
|
571
|
+
}
|
|
572
|
+
default: return obj;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
// Command classes that mimic AWS SDK structure
|
|
577
|
+
class GetCommand {
|
|
578
|
+
constructor(input) {
|
|
579
|
+
this.input = input;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
class QueryCommand {
|
|
584
|
+
constructor(input) {
|
|
585
|
+
this.input = input;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
class UpdateCommand {
|
|
590
|
+
constructor(input) {
|
|
591
|
+
this.input = input;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
class DeleteCommand {
|
|
596
|
+
constructor(input) {
|
|
597
|
+
this.input = input;
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
class BatchGetCommand {
|
|
602
|
+
constructor(input) {
|
|
603
|
+
this.input = input;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
class TransactWriteCommand {
|
|
608
|
+
constructor(input) {
|
|
609
|
+
this.input = input;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
class CreateTableCommand {
|
|
614
|
+
constructor(input) {
|
|
615
|
+
this.input = input;
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
class ListTablesCommand {
|
|
620
|
+
constructor(input) {
|
|
621
|
+
this.input = input;
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
class DescribeTableCommand {
|
|
626
|
+
constructor(input) {
|
|
627
|
+
this.input = input;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
module.exports = {
|
|
632
|
+
DynamoDBClient,
|
|
633
|
+
DynamoDBDocumentClient,
|
|
634
|
+
GetCommand,
|
|
635
|
+
QueryCommand,
|
|
636
|
+
UpdateCommand,
|
|
637
|
+
DeleteCommand,
|
|
638
|
+
BatchGetCommand,
|
|
639
|
+
TransactWriteCommand,
|
|
640
|
+
CreateTableCommand,
|
|
641
|
+
ListTablesCommand,
|
|
642
|
+
DescribeTableCommand,
|
|
643
|
+
marshall,
|
|
644
|
+
unmarshall
|
|
645
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const { defaultLogger: logger } = require("../utils/logger");
|
|
2
2
|
const { ObjectNotFound } = require("../object-not-found");
|
|
3
3
|
const { pluginManager } = require("../plugin-manager");
|
|
4
|
-
const { BatchGetCommand, GetCommand } = require("
|
|
4
|
+
const { BatchGetCommand, GetCommand } = require("../dynamodb-client");
|
|
5
5
|
|
|
6
6
|
// Move these constants from model.js to here
|
|
7
7
|
const BATCH_REQUESTS = new Map(); // testId -> { modelName-delay -> batch }
|
|
@@ -2,7 +2,7 @@ const {
|
|
|
2
2
|
TransactWriteCommand,
|
|
3
3
|
UpdateCommand,
|
|
4
4
|
DeleteCommand,
|
|
5
|
-
} = require("
|
|
5
|
+
} = require("../dynamodb-client");
|
|
6
6
|
const { defaultLogger: logger } = require("../utils/logger");
|
|
7
7
|
const { pluginManager } = require("../plugin-manager");
|
|
8
8
|
const { retryOperation } = require("../utils/retry-helper");
|
|
@@ -5,7 +5,7 @@ const { ObjectNotFound } = require("../object-not-found");
|
|
|
5
5
|
const { PrimaryKeyConfig } = require("../model-config");
|
|
6
6
|
const assert = require("assert");
|
|
7
7
|
const { retryOperation } = require("../utils/retry-helper");
|
|
8
|
-
const { QueryCommand } = require("
|
|
8
|
+
const { QueryCommand } = require("../dynamodb-client");
|
|
9
9
|
const { QueryError } = require("../exceptions");
|
|
10
10
|
|
|
11
11
|
const QueryMethods = {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { GetCommand } = require("
|
|
1
|
+
const { GetCommand } = require("../dynamodb-client");
|
|
2
2
|
const { defaultLogger: logger } = require("../utils/logger");
|
|
3
3
|
const { UNIQUE_CONSTRAINT_KEY } = require("../constants");
|
|
4
4
|
const { ConditionalError } = require("../exceptions");
|
package/src/model-manager.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
const { DynamoDBClient } = require("
|
|
2
|
-
const { DynamoDBDocument } = require("@aws-sdk/lib-dynamodb");
|
|
1
|
+
const { DynamoDBClient, DynamoDBDocumentClient } = require("./dynamodb-client");
|
|
3
2
|
const { defaultLogger: logger } = require("./utils/logger");
|
|
4
3
|
const { pluginManager } = require("./plugin-manager");
|
|
5
4
|
const { ConfigurationError } = require("./exceptions");
|
|
@@ -29,7 +28,7 @@ class ModelManager {
|
|
|
29
28
|
const client = new DynamoDBClient({
|
|
30
29
|
region: config.aws.region,
|
|
31
30
|
});
|
|
32
|
-
this._docClient =
|
|
31
|
+
this._docClient = DynamoDBDocumentClient.from(client);
|
|
33
32
|
this._tableName = config.db.tableName;
|
|
34
33
|
this._testId = config.testId || this._testId;
|
|
35
34
|
|
package/src/model.js
CHANGED
|
@@ -585,14 +585,17 @@ class BaoModel {
|
|
|
585
585
|
value,
|
|
586
586
|
);
|
|
587
587
|
|
|
588
|
-
const
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
588
|
+
const { GetCommand } = require("./dynamodb-client");
|
|
589
|
+
const result = await this.documentClient.send(
|
|
590
|
+
new GetCommand({
|
|
591
|
+
TableName: this.table,
|
|
592
|
+
Key: {
|
|
593
|
+
_pk: key,
|
|
594
|
+
_sk: UNIQUE_CONSTRAINT_KEY,
|
|
595
|
+
},
|
|
596
|
+
ReturnConsumedCapacity: "TOTAL",
|
|
597
|
+
})
|
|
598
|
+
);
|
|
596
599
|
|
|
597
600
|
if (!result.Item) {
|
|
598
601
|
return new ObjectNotFound(result.ConsumedCapacity);
|