@gugananuvem/aws-local-simulator 1.0.9 → 1.0.11
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/README.md +204 -192
- package/bin/aws-local-simulator.js +62 -62
- package/package.json +2 -2
- package/src/config/config-loader.js +112 -112
- package/src/config/default-config.js +65 -65
- package/src/config/env-loader.js +68 -66
- package/src/index.js +130 -130
- package/src/index.mjs +123 -123
- package/src/server.js +221 -219
- package/src/services/apigateway/index.js +66 -66
- package/src/services/apigateway/server.js +434 -434
- package/src/services/apigateway/simulator.js +1251 -1251
- package/src/services/cognito/index.js +65 -65
- package/src/services/cognito/server.js +228 -228
- package/src/services/cognito/simulator.js +847 -847
- package/src/services/dynamodb/index.js +70 -70
- package/src/services/dynamodb/server.js +121 -121
- package/src/services/dynamodb/simulator.js +620 -614
- package/src/services/ecs/index.js +66 -0
- package/src/services/ecs/server.js +234 -0
- package/src/services/ecs/simulator.js +845 -0
- package/src/services/eventbridge/index.js +84 -84
- package/src/services/index.js +18 -18
- package/src/services/lambda/handler-loader.js +172 -172
- package/src/services/lambda/index.js +72 -72
- package/src/services/lambda/route-registry.js +274 -274
- package/src/services/lambda/server.js +152 -152
- package/src/services/lambda/simulator.js +284 -277
- package/src/services/s3/index.js +69 -69
- package/src/services/s3/server.js +238 -238
- package/src/services/s3/simulator.js +740 -740
- package/src/services/sns/index.js +75 -75
- package/src/services/sqs/index.js +95 -95
- package/src/services/sqs/server.js +273 -273
- package/src/services/sqs/simulator.js +659 -659
- package/src/template/aws-config-template.js +87 -87
- package/src/template/aws-config-template.mjs +90 -90
- package/src/template/config-template.json +203 -165
- package/src/utils/aws-config.js +91 -91
- package/src/utils/local-store.js +67 -67
- package/src/utils/logger.js +59 -59
|
@@ -1,614 +1,620 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DynamoDB Simulator Core
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const LocalStore = require("../../utils/local-store");
|
|
6
|
-
const logger = require("../../utils/logger");
|
|
7
|
-
const crypto = require("crypto");
|
|
8
|
-
const path = require("path");
|
|
9
|
-
|
|
10
|
-
class DynamoDBSimulator {
|
|
11
|
-
constructor(config) {
|
|
12
|
-
this.config = config;
|
|
13
|
-
const dataDir = process.env.AWS_LOCAL_SIMULATOR_DATA_DIR || config.dataDir || "./.aws-local-simulator-data";
|
|
14
|
-
|
|
15
|
-
if (!dataDir) {
|
|
16
|
-
throw new Error("AWS_LOCAL_SIMULATOR_DATA_DIR not set");
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
this.dataDir = path.join(dataDir, "dynamodb");
|
|
20
|
-
this.store = new LocalStore(this.dataDir);
|
|
21
|
-
this.tables = new Map();
|
|
22
|
-
}
|
|
23
|
-
async initialize() {
|
|
24
|
-
logger.debug("Inicializando DynamoDB Simulator...");
|
|
25
|
-
this.loadTables();
|
|
26
|
-
logger.debug(`✅ DynamoDB Simulator inicializado com ${this.tables.size} tabelas`);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
loadTables() {
|
|
30
|
-
// Carrega tabelas da configuração
|
|
31
|
-
if (this.config.dynamodb?.tables) {
|
|
32
|
-
for (const tableDef of this.config.dynamodb.tables) {
|
|
33
|
-
this.createTable(tableDef);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// Carrega tabelas existentes do disco
|
|
38
|
-
const savedTables = this.store.read("__tables__");
|
|
39
|
-
if (savedTables) {
|
|
40
|
-
for (const [name, definition] of Object.entries(savedTables)) {
|
|
41
|
-
if (!this.tables.has(name)) {
|
|
42
|
-
this.tables.set(name, definition);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
createTable(params) {
|
|
49
|
-
const { TableName, KeySchema, AttributeDefinitions, ProvisionedThroughput } = params;
|
|
50
|
-
|
|
51
|
-
if (this.tables.has(TableName)) {
|
|
52
|
-
logger.warn(`Tabela ${TableName} já existe`);
|
|
53
|
-
return { TableDescription: { TableName, TableStatus: "ACTIVE" } };
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const hashKey = KeySchema.find((k) => k.KeyType === "HASH").AttributeName;
|
|
57
|
-
const rangeKey = KeySchema.find((k) => k.KeyType === "RANGE")?.AttributeName;
|
|
58
|
-
|
|
59
|
-
const attributeTypes = {};
|
|
60
|
-
AttributeDefinitions.forEach((attr) => {
|
|
61
|
-
attributeTypes[attr.AttributeName] = attr.AttributeType;
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
const table = {
|
|
65
|
-
name: TableName,
|
|
66
|
-
hashKey,
|
|
67
|
-
rangeKey,
|
|
68
|
-
attributeTypes,
|
|
69
|
-
createdAt: new Date().toISOString(),
|
|
70
|
-
itemCount: 0,
|
|
71
|
-
sizeBytes: 0,
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
this.tables.set(TableName, table);
|
|
75
|
-
this.persistTables();
|
|
76
|
-
|
|
77
|
-
// Inicializa arquivo de dados
|
|
78
|
-
this.store.write(TableName, []);
|
|
79
|
-
|
|
80
|
-
logger.debug(`✅ Tabela criada: ${TableName}`);
|
|
81
|
-
|
|
82
|
-
return {
|
|
83
|
-
TableDescription: {
|
|
84
|
-
TableName,
|
|
85
|
-
TableStatus: "ACTIVE",
|
|
86
|
-
CreationDateTime: new Date().toISOString(),
|
|
87
|
-
KeySchema,
|
|
88
|
-
AttributeDefinitions,
|
|
89
|
-
ProvisionedThroughput: ProvisionedThroughput || {
|
|
90
|
-
ReadCapacityUnits: 5,
|
|
91
|
-
WriteCapacityUnits: 5,
|
|
92
|
-
},
|
|
93
|
-
ItemCount: 0,
|
|
94
|
-
TableSizeBytes: 0,
|
|
95
|
-
},
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
async handleRequest(target, params) {
|
|
100
|
-
const action = target.split(".")[1];
|
|
101
|
-
|
|
102
|
-
logger.verboso(`DynamoDB Action: ${action}`, params);
|
|
103
|
-
|
|
104
|
-
switch (action) {
|
|
105
|
-
case "CreateTable":
|
|
106
|
-
return this.createTable(params);
|
|
107
|
-
case "DescribeTable":
|
|
108
|
-
return this.describeTable(params.TableName);
|
|
109
|
-
case "ListTables":
|
|
110
|
-
return this.listTables(params);
|
|
111
|
-
case "DeleteTable":
|
|
112
|
-
return this.deleteTable(params);
|
|
113
|
-
case "PutItem":
|
|
114
|
-
return this.putItem(params);
|
|
115
|
-
case "GetItem":
|
|
116
|
-
return this.getItem(params);
|
|
117
|
-
case "UpdateItem":
|
|
118
|
-
return this.updateItem(params);
|
|
119
|
-
case "DeleteItem":
|
|
120
|
-
return this.deleteItem(params);
|
|
121
|
-
case "BatchWriteItem":
|
|
122
|
-
return this.batchWriteItem(params);
|
|
123
|
-
case "BatchGetItem":
|
|
124
|
-
return this.batchGetItem(params);
|
|
125
|
-
case "Query":
|
|
126
|
-
return this.query(params);
|
|
127
|
-
case "Scan":
|
|
128
|
-
return this.scan(params);
|
|
129
|
-
default:
|
|
130
|
-
throw new Error(`Unsupported action: ${action}`);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
describeTable(tableName) {
|
|
135
|
-
const table = this.tables.get(tableName);
|
|
136
|
-
if (!table) {
|
|
137
|
-
throw new Error(`Table ${tableName} does not exist`);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const items = this.store.read(tableName);
|
|
141
|
-
|
|
142
|
-
return {
|
|
143
|
-
Table: {
|
|
144
|
-
TableName: table.name,
|
|
145
|
-
TableStatus: "ACTIVE",
|
|
146
|
-
CreationDateTime: table.createdAt,
|
|
147
|
-
KeySchema: [{ AttributeName: table.hashKey, KeyType: "HASH" }, ...(table.rangeKey ? [{ AttributeName: table.rangeKey, KeyType: "RANGE" }] : [])],
|
|
148
|
-
AttributeDefinitions: Object.entries(table.attributeTypes).map(([name, type]) => ({
|
|
149
|
-
AttributeName: name,
|
|
150
|
-
AttributeType: type,
|
|
151
|
-
})),
|
|
152
|
-
ItemCount: items.length,
|
|
153
|
-
TableSizeBytes: JSON.stringify(items).length,
|
|
154
|
-
ProvisionedThroughput: {
|
|
155
|
-
ReadCapacityUnits: 5,
|
|
156
|
-
WriteCapacityUnits: 5,
|
|
157
|
-
},
|
|
158
|
-
},
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
listTables(params = {}) {
|
|
163
|
-
const tableNames = Array.from(this.tables.keys());
|
|
164
|
-
const { Limit = 100, ExclusiveStartTableName } = params;
|
|
165
|
-
|
|
166
|
-
let startIndex = 0;
|
|
167
|
-
if (ExclusiveStartTableName) {
|
|
168
|
-
const index = tableNames.indexOf(ExclusiveStartTableName);
|
|
169
|
-
if (index !== -1) startIndex = index + 1;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
const result = tableNames.slice(startIndex, startIndex + Limit);
|
|
173
|
-
|
|
174
|
-
return {
|
|
175
|
-
TableNames: result,
|
|
176
|
-
LastEvaluatedTableName: result.length === Limit ? result[result.length - 1] : undefined,
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
deleteTable(params) {
|
|
181
|
-
const { TableName } = params;
|
|
182
|
-
|
|
183
|
-
if (!this.tables.has(TableName)) {
|
|
184
|
-
throw new Error(`Table ${TableName} does not exist`);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
this.tables.delete(TableName);
|
|
188
|
-
this.store.delete(TableName);
|
|
189
|
-
this.persistTables();
|
|
190
|
-
|
|
191
|
-
return { TableDescription: { TableName, TableStatus: "DELETING" } };
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
putItem(params) {
|
|
195
|
-
const { TableName, Item, ReturnValues = "NONE" } = params;
|
|
196
|
-
const table = this.tables.get(TableName);
|
|
197
|
-
|
|
198
|
-
if (!table) {
|
|
199
|
-
throw new Error(`Table ${TableName} does not exist`);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
// Normaliza o item
|
|
203
|
-
const normalizedItem = this.normalizeItem(Item, table);
|
|
204
|
-
normalizedItem._createdAt = normalizedItem._createdAt || new Date().toISOString();
|
|
205
|
-
normalizedItem._updatedAt = new Date().toISOString();
|
|
206
|
-
|
|
207
|
-
// Carrega dados existentes
|
|
208
|
-
let items = this.store.read(TableName);
|
|
209
|
-
const itemKey = this.getItemKey(normalizedItem, table);
|
|
210
|
-
|
|
211
|
-
// Encontra e substitui ou adiciona
|
|
212
|
-
const existingIndex = items.findIndex((item) => this.getItemKey(item, table) === itemKey);
|
|
213
|
-
|
|
214
|
-
let oldItem = null;
|
|
215
|
-
if (existingIndex !== -1) {
|
|
216
|
-
oldItem = { ...items[existingIndex] };
|
|
217
|
-
items[existingIndex] = normalizedItem;
|
|
218
|
-
} else {
|
|
219
|
-
items.push(normalizedItem);
|
|
220
|
-
table.itemCount++;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
// Salva no store
|
|
224
|
-
this.store.write(TableName, items);
|
|
225
|
-
this.persistTables();
|
|
226
|
-
|
|
227
|
-
logger.verboso(`PutItem: ${TableName}/${itemKey}`);
|
|
228
|
-
|
|
229
|
-
const response = {};
|
|
230
|
-
if (ReturnValues === "ALL_OLD" && oldItem) {
|
|
231
|
-
response.Attributes = this.marshallItem(oldItem, table);
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
return response;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
getItem(params) {
|
|
238
|
-
const { TableName, Key } = params;
|
|
239
|
-
const table = this.tables.get(TableName);
|
|
240
|
-
|
|
241
|
-
if (!table) {
|
|
242
|
-
throw new Error(`Table ${TableName} does not exist`);
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
const items = this.store.read(TableName);
|
|
246
|
-
const itemKey = this.getItemKeyFromKeys(Key, table);
|
|
247
|
-
|
|
248
|
-
const item = items.find((item) => this.getItemKey(item, table) === itemKey);
|
|
249
|
-
|
|
250
|
-
logger.verboso(`GetItem: ${TableName}/${itemKey} - ${item ? "found" : "not found"}`);
|
|
251
|
-
|
|
252
|
-
return item ? { Item: this.marshallItem(item, table) } : {};
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
updateItem(params) {
|
|
256
|
-
const { TableName, Key, UpdateExpression, ExpressionAttributeNames = {}, ExpressionAttributeValues = {}, ReturnValues = "NONE" } = params;
|
|
257
|
-
const table = this.tables.get(TableName);
|
|
258
|
-
|
|
259
|
-
if (!table) {
|
|
260
|
-
throw new Error(`Table ${TableName} does not exist`);
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
// Busca o item atual
|
|
264
|
-
const items = this.store.read(TableName);
|
|
265
|
-
const itemKey = this.getItemKeyFromKeys(Key, table);
|
|
266
|
-
const index = items.findIndex((item) => this.getItemKey(item, table) === itemKey);
|
|
267
|
-
|
|
268
|
-
if (index === -1) {
|
|
269
|
-
throw new Error(`Item not found in ${TableName}`);
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
const currentItem = items[index];
|
|
273
|
-
const updatedItem = { ...currentItem };
|
|
274
|
-
updatedItem._updatedAt = new Date().toISOString();
|
|
275
|
-
|
|
276
|
-
// Processa a UpdateExpression
|
|
277
|
-
if (UpdateExpression) {
|
|
278
|
-
this.processUpdateExpression(updatedItem, UpdateExpression, ExpressionAttributeNames, ExpressionAttributeValues, table);
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
// Salva o item atualizado
|
|
282
|
-
const oldItem = { ...items[index] };
|
|
283
|
-
items[index] = updatedItem;
|
|
284
|
-
this.store.write(TableName, items);
|
|
285
|
-
|
|
286
|
-
logger.verboso(`UpdateItem: ${TableName}/${itemKey}`);
|
|
287
|
-
|
|
288
|
-
const response = {};
|
|
289
|
-
switch (ReturnValues) {
|
|
290
|
-
case "ALL_OLD":
|
|
291
|
-
response.Attributes = this.marshallItem(oldItem, table);
|
|
292
|
-
break;
|
|
293
|
-
case "ALL_NEW":
|
|
294
|
-
response.Attributes = this.marshallItem(updatedItem, table);
|
|
295
|
-
break;
|
|
296
|
-
default:
|
|
297
|
-
break;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
return response;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
deleteItem(params) {
|
|
304
|
-
const { TableName, Key, ReturnValues = "NONE" } = params;
|
|
305
|
-
const table = this.tables.get(TableName);
|
|
306
|
-
|
|
307
|
-
if (!table) {
|
|
308
|
-
throw new Error(`Table ${TableName} does not exist`);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
const items = this.store.read(TableName);
|
|
312
|
-
const itemKey = this.getItemKeyFromKeys(Key, table);
|
|
313
|
-
const index = items.findIndex((item) => this.getItemKey(item, table) === itemKey);
|
|
314
|
-
|
|
315
|
-
if (index === -1) {
|
|
316
|
-
return {};
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
const oldItem = { ...items[index] };
|
|
320
|
-
items.splice(index, 1);
|
|
321
|
-
this.store.write(TableName, items);
|
|
322
|
-
table.itemCount--;
|
|
323
|
-
this.persistTables();
|
|
324
|
-
|
|
325
|
-
logger.verboso(`DeleteItem: ${TableName}/${itemKey}`);
|
|
326
|
-
|
|
327
|
-
const response = {};
|
|
328
|
-
if (ReturnValues === "ALL_OLD") {
|
|
329
|
-
response.Attributes = this.marshallItem(oldItem, table);
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
return response;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
batchWriteItem(params) {
|
|
336
|
-
const { RequestItems } = params;
|
|
337
|
-
const responses = {};
|
|
338
|
-
|
|
339
|
-
for (const [tableName, operations] of Object.entries(RequestItems)) {
|
|
340
|
-
const table = this.tables.get(tableName);
|
|
341
|
-
if (!table) continue;
|
|
342
|
-
|
|
343
|
-
let items = this.store.read(tableName);
|
|
344
|
-
const unprocessedItems = [];
|
|
345
|
-
|
|
346
|
-
for (const op of operations) {
|
|
347
|
-
if (op.PutRequest) {
|
|
348
|
-
const item = this.normalizeItem(op.PutRequest.Item, table);
|
|
349
|
-
const itemKey = this.getItemKey(item, table);
|
|
350
|
-
const index = items.findIndex((i) => this.getItemKey(i, table) === itemKey);
|
|
351
|
-
|
|
352
|
-
if (index !== -1) {
|
|
353
|
-
items[index] = item;
|
|
354
|
-
} else {
|
|
355
|
-
items.push(item);
|
|
356
|
-
table.itemCount++;
|
|
357
|
-
}
|
|
358
|
-
} else if (op.DeleteRequest) {
|
|
359
|
-
const key = op.DeleteRequest.Key;
|
|
360
|
-
const itemKey = this.getItemKeyFromKeys(key, table);
|
|
361
|
-
const index = items.findIndex((i) => this.getItemKey(i, table) === itemKey);
|
|
362
|
-
|
|
363
|
-
if (index !== -1) {
|
|
364
|
-
items.splice(index, 1);
|
|
365
|
-
table.itemCount--;
|
|
366
|
-
} else {
|
|
367
|
-
unprocessedItems.push(op);
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
this.store.write(tableName, items);
|
|
373
|
-
responses[tableName] = { UnprocessedItems: unprocessedItems };
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
this.persistTables();
|
|
377
|
-
|
|
378
|
-
return { UnprocessedItems: responses };
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
batchGetItem(params) {
|
|
382
|
-
const { RequestItems } = params;
|
|
383
|
-
const responses = {};
|
|
384
|
-
|
|
385
|
-
for (const [tableName, request] of Object.entries(RequestItems)) {
|
|
386
|
-
const table = this.tables.get(tableName);
|
|
387
|
-
if (!table) continue;
|
|
388
|
-
|
|
389
|
-
const items = this.store.read(tableName);
|
|
390
|
-
const { Keys } = request;
|
|
391
|
-
const foundItems = [];
|
|
392
|
-
|
|
393
|
-
for (const key of Keys) {
|
|
394
|
-
const itemKey = this.getItemKeyFromKeys(key, table);
|
|
395
|
-
const item = items.find((i) => this.getItemKey(i, table) === itemKey);
|
|
396
|
-
if (item) {
|
|
397
|
-
foundItems.push(this.marshallItem(item, table));
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
responses[tableName] = { Items: foundItems };
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
return { Responses: responses };
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
query(params) {
|
|
408
|
-
const { TableName, KeyConditionExpression, ExpressionAttributeValues, IndexName } = params;
|
|
409
|
-
const table = this.tables.get(TableName);
|
|
410
|
-
|
|
411
|
-
if (!table) {
|
|
412
|
-
throw new Error(`Table ${TableName} does not exist`);
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
let items = this.store.read(TableName);
|
|
416
|
-
|
|
417
|
-
// Filtra pela chave de partição
|
|
418
|
-
const hashKey = table.hashKey;
|
|
419
|
-
const hashValueMatch = KeyConditionExpression.match(new RegExp(`${hashKey}\\s*=\\s*([^\\s]+)`));
|
|
420
|
-
|
|
421
|
-
if (hashValueMatch) {
|
|
422
|
-
const hashValuePlaceholder = hashValueMatch[1];
|
|
423
|
-
const
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
const
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
const
|
|
435
|
-
const
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
case "
|
|
443
|
-
return itemValue
|
|
444
|
-
case "
|
|
445
|
-
return itemValue
|
|
446
|
-
case "
|
|
447
|
-
return itemValue
|
|
448
|
-
case "
|
|
449
|
-
return itemValue
|
|
450
|
-
|
|
451
|
-
return
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
else if (value.
|
|
506
|
-
else if (value.
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
marshalled[key] = {
|
|
524
|
-
} else if (type === "
|
|
525
|
-
marshalled[key] = {
|
|
526
|
-
} else if (
|
|
527
|
-
marshalled[key] = {
|
|
528
|
-
} else if (
|
|
529
|
-
marshalled[key] = {
|
|
530
|
-
} else {
|
|
531
|
-
marshalled[key] = {
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
1
|
+
/**
|
|
2
|
+
* DynamoDB Simulator Core
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const LocalStore = require("../../utils/local-store");
|
|
6
|
+
const logger = require("../../utils/logger");
|
|
7
|
+
const crypto = require("crypto");
|
|
8
|
+
const path = require("path");
|
|
9
|
+
|
|
10
|
+
class DynamoDBSimulator {
|
|
11
|
+
constructor(config) {
|
|
12
|
+
this.config = config;
|
|
13
|
+
const dataDir = process.env.AWS_LOCAL_SIMULATOR_DATA_DIR || config.dataDir || "./.aws-local-simulator-data";
|
|
14
|
+
|
|
15
|
+
if (!dataDir) {
|
|
16
|
+
throw new Error("AWS_LOCAL_SIMULATOR_DATA_DIR not set");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
this.dataDir = path.join(dataDir, "dynamodb");
|
|
20
|
+
this.store = new LocalStore(this.dataDir);
|
|
21
|
+
this.tables = new Map();
|
|
22
|
+
}
|
|
23
|
+
async initialize() {
|
|
24
|
+
logger.debug("Inicializando DynamoDB Simulator...");
|
|
25
|
+
this.loadTables();
|
|
26
|
+
logger.debug(`✅ DynamoDB Simulator inicializado com ${this.tables.size} tabelas`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
loadTables() {
|
|
30
|
+
// Carrega tabelas da configuração
|
|
31
|
+
if (this.config.dynamodb?.tables) {
|
|
32
|
+
for (const tableDef of this.config.dynamodb.tables) {
|
|
33
|
+
this.createTable(tableDef);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Carrega tabelas existentes do disco
|
|
38
|
+
const savedTables = this.store.read("__tables__");
|
|
39
|
+
if (savedTables) {
|
|
40
|
+
for (const [name, definition] of Object.entries(savedTables)) {
|
|
41
|
+
if (!this.tables.has(name)) {
|
|
42
|
+
this.tables.set(name, definition);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
createTable(params) {
|
|
49
|
+
const { TableName, KeySchema, AttributeDefinitions, ProvisionedThroughput } = params;
|
|
50
|
+
|
|
51
|
+
if (this.tables.has(TableName)) {
|
|
52
|
+
logger.warn(`Tabela ${TableName} já existe`);
|
|
53
|
+
return { TableDescription: { TableName, TableStatus: "ACTIVE" } };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const hashKey = KeySchema.find((k) => k.KeyType === "HASH").AttributeName;
|
|
57
|
+
const rangeKey = KeySchema.find((k) => k.KeyType === "RANGE")?.AttributeName;
|
|
58
|
+
|
|
59
|
+
const attributeTypes = {};
|
|
60
|
+
AttributeDefinitions.forEach((attr) => {
|
|
61
|
+
attributeTypes[attr.AttributeName] = attr.AttributeType;
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const table = {
|
|
65
|
+
name: TableName,
|
|
66
|
+
hashKey,
|
|
67
|
+
rangeKey,
|
|
68
|
+
attributeTypes,
|
|
69
|
+
createdAt: new Date().toISOString(),
|
|
70
|
+
itemCount: 0,
|
|
71
|
+
sizeBytes: 0,
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
this.tables.set(TableName, table);
|
|
75
|
+
this.persistTables();
|
|
76
|
+
|
|
77
|
+
// Inicializa arquivo de dados
|
|
78
|
+
this.store.write(TableName, []);
|
|
79
|
+
|
|
80
|
+
logger.debug(`✅ Tabela criada: ${TableName}`);
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
TableDescription: {
|
|
84
|
+
TableName,
|
|
85
|
+
TableStatus: "ACTIVE",
|
|
86
|
+
CreationDateTime: new Date().toISOString(),
|
|
87
|
+
KeySchema,
|
|
88
|
+
AttributeDefinitions,
|
|
89
|
+
ProvisionedThroughput: ProvisionedThroughput || {
|
|
90
|
+
ReadCapacityUnits: 5,
|
|
91
|
+
WriteCapacityUnits: 5,
|
|
92
|
+
},
|
|
93
|
+
ItemCount: 0,
|
|
94
|
+
TableSizeBytes: 0,
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async handleRequest(target, params) {
|
|
100
|
+
const action = target.split(".")[1];
|
|
101
|
+
|
|
102
|
+
logger.verboso(`DynamoDB Action: ${action}`, params);
|
|
103
|
+
|
|
104
|
+
switch (action) {
|
|
105
|
+
case "CreateTable":
|
|
106
|
+
return this.createTable(params);
|
|
107
|
+
case "DescribeTable":
|
|
108
|
+
return this.describeTable(params.TableName);
|
|
109
|
+
case "ListTables":
|
|
110
|
+
return this.listTables(params);
|
|
111
|
+
case "DeleteTable":
|
|
112
|
+
return this.deleteTable(params);
|
|
113
|
+
case "PutItem":
|
|
114
|
+
return this.putItem(params);
|
|
115
|
+
case "GetItem":
|
|
116
|
+
return this.getItem(params);
|
|
117
|
+
case "UpdateItem":
|
|
118
|
+
return this.updateItem(params);
|
|
119
|
+
case "DeleteItem":
|
|
120
|
+
return this.deleteItem(params);
|
|
121
|
+
case "BatchWriteItem":
|
|
122
|
+
return this.batchWriteItem(params);
|
|
123
|
+
case "BatchGetItem":
|
|
124
|
+
return this.batchGetItem(params);
|
|
125
|
+
case "Query":
|
|
126
|
+
return this.query(params);
|
|
127
|
+
case "Scan":
|
|
128
|
+
return this.scan(params);
|
|
129
|
+
default:
|
|
130
|
+
throw new Error(`Unsupported action: ${action}`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
describeTable(tableName) {
|
|
135
|
+
const table = this.tables.get(tableName);
|
|
136
|
+
if (!table) {
|
|
137
|
+
throw new Error(`Table ${tableName} does not exist`);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const items = this.store.read(tableName);
|
|
141
|
+
|
|
142
|
+
return {
|
|
143
|
+
Table: {
|
|
144
|
+
TableName: table.name,
|
|
145
|
+
TableStatus: "ACTIVE",
|
|
146
|
+
CreationDateTime: table.createdAt,
|
|
147
|
+
KeySchema: [{ AttributeName: table.hashKey, KeyType: "HASH" }, ...(table.rangeKey ? [{ AttributeName: table.rangeKey, KeyType: "RANGE" }] : [])],
|
|
148
|
+
AttributeDefinitions: Object.entries(table.attributeTypes).map(([name, type]) => ({
|
|
149
|
+
AttributeName: name,
|
|
150
|
+
AttributeType: type,
|
|
151
|
+
})),
|
|
152
|
+
ItemCount: items.length,
|
|
153
|
+
TableSizeBytes: JSON.stringify(items).length,
|
|
154
|
+
ProvisionedThroughput: {
|
|
155
|
+
ReadCapacityUnits: 5,
|
|
156
|
+
WriteCapacityUnits: 5,
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
listTables(params = {}) {
|
|
163
|
+
const tableNames = Array.from(this.tables.keys());
|
|
164
|
+
const { Limit = 100, ExclusiveStartTableName } = params;
|
|
165
|
+
|
|
166
|
+
let startIndex = 0;
|
|
167
|
+
if (ExclusiveStartTableName) {
|
|
168
|
+
const index = tableNames.indexOf(ExclusiveStartTableName);
|
|
169
|
+
if (index !== -1) startIndex = index + 1;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const result = tableNames.slice(startIndex, startIndex + Limit);
|
|
173
|
+
|
|
174
|
+
return {
|
|
175
|
+
TableNames: result,
|
|
176
|
+
LastEvaluatedTableName: result.length === Limit ? result[result.length - 1] : undefined,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
deleteTable(params) {
|
|
181
|
+
const { TableName } = params;
|
|
182
|
+
|
|
183
|
+
if (!this.tables.has(TableName)) {
|
|
184
|
+
throw new Error(`Table ${TableName} does not exist`);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
this.tables.delete(TableName);
|
|
188
|
+
this.store.delete(TableName);
|
|
189
|
+
this.persistTables();
|
|
190
|
+
|
|
191
|
+
return { TableDescription: { TableName, TableStatus: "DELETING" } };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
putItem(params) {
|
|
195
|
+
const { TableName, Item, ReturnValues = "NONE" } = params;
|
|
196
|
+
const table = this.tables.get(TableName);
|
|
197
|
+
|
|
198
|
+
if (!table) {
|
|
199
|
+
throw new Error(`Table ${TableName} does not exist`);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Normaliza o item
|
|
203
|
+
const normalizedItem = this.normalizeItem(Item, table);
|
|
204
|
+
normalizedItem._createdAt = normalizedItem._createdAt || new Date().toISOString();
|
|
205
|
+
normalizedItem._updatedAt = new Date().toISOString();
|
|
206
|
+
|
|
207
|
+
// Carrega dados existentes
|
|
208
|
+
let items = this.store.read(TableName);
|
|
209
|
+
const itemKey = this.getItemKey(normalizedItem, table);
|
|
210
|
+
|
|
211
|
+
// Encontra e substitui ou adiciona
|
|
212
|
+
const existingIndex = items.findIndex((item) => this.getItemKey(item, table) === itemKey);
|
|
213
|
+
|
|
214
|
+
let oldItem = null;
|
|
215
|
+
if (existingIndex !== -1) {
|
|
216
|
+
oldItem = { ...items[existingIndex] };
|
|
217
|
+
items[existingIndex] = normalizedItem;
|
|
218
|
+
} else {
|
|
219
|
+
items.push(normalizedItem);
|
|
220
|
+
table.itemCount++;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Salva no store
|
|
224
|
+
this.store.write(TableName, items);
|
|
225
|
+
this.persistTables();
|
|
226
|
+
|
|
227
|
+
logger.verboso(`PutItem: ${TableName}/${itemKey}`);
|
|
228
|
+
|
|
229
|
+
const response = {};
|
|
230
|
+
if (ReturnValues === "ALL_OLD" && oldItem) {
|
|
231
|
+
response.Attributes = this.marshallItem(oldItem, table);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return response;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
getItem(params) {
|
|
238
|
+
const { TableName, Key } = params;
|
|
239
|
+
const table = this.tables.get(TableName);
|
|
240
|
+
|
|
241
|
+
if (!table) {
|
|
242
|
+
throw new Error(`Table ${TableName} does not exist`);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const items = this.store.read(TableName);
|
|
246
|
+
const itemKey = this.getItemKeyFromKeys(Key, table);
|
|
247
|
+
|
|
248
|
+
const item = items.find((item) => this.getItemKey(item, table) === itemKey);
|
|
249
|
+
|
|
250
|
+
logger.verboso(`GetItem: ${TableName}/${itemKey} - ${item ? "found" : "not found"}`);
|
|
251
|
+
|
|
252
|
+
return item ? { Item: this.marshallItem(item, table) } : {};
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
updateItem(params) {
|
|
256
|
+
const { TableName, Key, UpdateExpression, ExpressionAttributeNames = {}, ExpressionAttributeValues = {}, ReturnValues = "NONE" } = params;
|
|
257
|
+
const table = this.tables.get(TableName);
|
|
258
|
+
|
|
259
|
+
if (!table) {
|
|
260
|
+
throw new Error(`Table ${TableName} does not exist`);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Busca o item atual
|
|
264
|
+
const items = this.store.read(TableName);
|
|
265
|
+
const itemKey = this.getItemKeyFromKeys(Key, table);
|
|
266
|
+
const index = items.findIndex((item) => this.getItemKey(item, table) === itemKey);
|
|
267
|
+
|
|
268
|
+
if (index === -1) {
|
|
269
|
+
throw new Error(`Item not found in ${TableName}`);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const currentItem = items[index];
|
|
273
|
+
const updatedItem = { ...currentItem };
|
|
274
|
+
updatedItem._updatedAt = new Date().toISOString();
|
|
275
|
+
|
|
276
|
+
// Processa a UpdateExpression
|
|
277
|
+
if (UpdateExpression) {
|
|
278
|
+
this.processUpdateExpression(updatedItem, UpdateExpression, ExpressionAttributeNames, ExpressionAttributeValues, table);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// Salva o item atualizado
|
|
282
|
+
const oldItem = { ...items[index] };
|
|
283
|
+
items[index] = updatedItem;
|
|
284
|
+
this.store.write(TableName, items);
|
|
285
|
+
|
|
286
|
+
logger.verboso(`UpdateItem: ${TableName}/${itemKey}`);
|
|
287
|
+
|
|
288
|
+
const response = {};
|
|
289
|
+
switch (ReturnValues) {
|
|
290
|
+
case "ALL_OLD":
|
|
291
|
+
response.Attributes = this.marshallItem(oldItem, table);
|
|
292
|
+
break;
|
|
293
|
+
case "ALL_NEW":
|
|
294
|
+
response.Attributes = this.marshallItem(updatedItem, table);
|
|
295
|
+
break;
|
|
296
|
+
default:
|
|
297
|
+
break;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
return response;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
deleteItem(params) {
|
|
304
|
+
const { TableName, Key, ReturnValues = "NONE" } = params;
|
|
305
|
+
const table = this.tables.get(TableName);
|
|
306
|
+
|
|
307
|
+
if (!table) {
|
|
308
|
+
throw new Error(`Table ${TableName} does not exist`);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const items = this.store.read(TableName);
|
|
312
|
+
const itemKey = this.getItemKeyFromKeys(Key, table);
|
|
313
|
+
const index = items.findIndex((item) => this.getItemKey(item, table) === itemKey);
|
|
314
|
+
|
|
315
|
+
if (index === -1) {
|
|
316
|
+
return {};
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const oldItem = { ...items[index] };
|
|
320
|
+
items.splice(index, 1);
|
|
321
|
+
this.store.write(TableName, items);
|
|
322
|
+
table.itemCount--;
|
|
323
|
+
this.persistTables();
|
|
324
|
+
|
|
325
|
+
logger.verboso(`DeleteItem: ${TableName}/${itemKey}`);
|
|
326
|
+
|
|
327
|
+
const response = {};
|
|
328
|
+
if (ReturnValues === "ALL_OLD") {
|
|
329
|
+
response.Attributes = this.marshallItem(oldItem, table);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
return response;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
batchWriteItem(params) {
|
|
336
|
+
const { RequestItems } = params;
|
|
337
|
+
const responses = {};
|
|
338
|
+
|
|
339
|
+
for (const [tableName, operations] of Object.entries(RequestItems)) {
|
|
340
|
+
const table = this.tables.get(tableName);
|
|
341
|
+
if (!table) continue;
|
|
342
|
+
|
|
343
|
+
let items = this.store.read(tableName);
|
|
344
|
+
const unprocessedItems = [];
|
|
345
|
+
|
|
346
|
+
for (const op of operations) {
|
|
347
|
+
if (op.PutRequest) {
|
|
348
|
+
const item = this.normalizeItem(op.PutRequest.Item, table);
|
|
349
|
+
const itemKey = this.getItemKey(item, table);
|
|
350
|
+
const index = items.findIndex((i) => this.getItemKey(i, table) === itemKey);
|
|
351
|
+
|
|
352
|
+
if (index !== -1) {
|
|
353
|
+
items[index] = item;
|
|
354
|
+
} else {
|
|
355
|
+
items.push(item);
|
|
356
|
+
table.itemCount++;
|
|
357
|
+
}
|
|
358
|
+
} else if (op.DeleteRequest) {
|
|
359
|
+
const key = op.DeleteRequest.Key;
|
|
360
|
+
const itemKey = this.getItemKeyFromKeys(key, table);
|
|
361
|
+
const index = items.findIndex((i) => this.getItemKey(i, table) === itemKey);
|
|
362
|
+
|
|
363
|
+
if (index !== -1) {
|
|
364
|
+
items.splice(index, 1);
|
|
365
|
+
table.itemCount--;
|
|
366
|
+
} else {
|
|
367
|
+
unprocessedItems.push(op);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
this.store.write(tableName, items);
|
|
373
|
+
responses[tableName] = { UnprocessedItems: unprocessedItems };
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
this.persistTables();
|
|
377
|
+
|
|
378
|
+
return { UnprocessedItems: responses };
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
batchGetItem(params) {
|
|
382
|
+
const { RequestItems } = params;
|
|
383
|
+
const responses = {};
|
|
384
|
+
|
|
385
|
+
for (const [tableName, request] of Object.entries(RequestItems)) {
|
|
386
|
+
const table = this.tables.get(tableName);
|
|
387
|
+
if (!table) continue;
|
|
388
|
+
|
|
389
|
+
const items = this.store.read(tableName);
|
|
390
|
+
const { Keys } = request;
|
|
391
|
+
const foundItems = [];
|
|
392
|
+
|
|
393
|
+
for (const key of Keys) {
|
|
394
|
+
const itemKey = this.getItemKeyFromKeys(key, table);
|
|
395
|
+
const item = items.find((i) => this.getItemKey(i, table) === itemKey);
|
|
396
|
+
if (item) {
|
|
397
|
+
foundItems.push(this.marshallItem(item, table));
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
responses[tableName] = { Items: foundItems };
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
return { Responses: responses };
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
query(params) {
|
|
408
|
+
const { TableName, KeyConditionExpression, ExpressionAttributeValues, IndexName } = params;
|
|
409
|
+
const table = this.tables.get(TableName);
|
|
410
|
+
|
|
411
|
+
if (!table) {
|
|
412
|
+
throw new Error(`Table ${TableName} does not exist`);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
let items = this.store.read(TableName);
|
|
416
|
+
|
|
417
|
+
// Filtra pela chave de partição
|
|
418
|
+
const hashKey = table.hashKey;
|
|
419
|
+
const hashValueMatch = KeyConditionExpression.match(new RegExp(`${hashKey}\\s*=\\s*([^\\s]+)`));
|
|
420
|
+
|
|
421
|
+
if (hashValueMatch) {
|
|
422
|
+
const hashValuePlaceholder = hashValueMatch[1];
|
|
423
|
+
const rawHashValue = ExpressionAttributeValues[hashValuePlaceholder];
|
|
424
|
+
const hashValue = rawHashValue && typeof rawHashValue === 'object' ? Object.values(rawHashValue)[0] : rawHashValue;
|
|
425
|
+
items = items.filter((item) => item[hashKey] === hashValue);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// Filtra pela chave de ordenação se existir
|
|
429
|
+
if (table.rangeKey) {
|
|
430
|
+
const rangeKey = table.rangeKey;
|
|
431
|
+
const rangeConditionMatch = KeyConditionExpression.match(new RegExp(`${rangeKey}\\s*(=|>|<|>=|<=)\\s*([^\\s]+)`));
|
|
432
|
+
|
|
433
|
+
if (rangeConditionMatch) {
|
|
434
|
+
const operator = rangeConditionMatch[1];
|
|
435
|
+
const rangeValuePlaceholder = rangeConditionMatch[2];
|
|
436
|
+
const rawRangeValue = ExpressionAttributeValues[rangeValuePlaceholder];
|
|
437
|
+
const rangeValue = rawRangeValue && typeof rawRangeValue === 'object' ? Object.values(rawRangeValue)[0] : rawRangeValue;
|
|
438
|
+
|
|
439
|
+
items = items.filter((item) => {
|
|
440
|
+
const itemValue = item[rangeKey];
|
|
441
|
+
switch (operator) {
|
|
442
|
+
case "=":
|
|
443
|
+
return itemValue === rangeValue;
|
|
444
|
+
case ">":
|
|
445
|
+
return itemValue > rangeValue;
|
|
446
|
+
case "<":
|
|
447
|
+
return itemValue < rangeValue;
|
|
448
|
+
case ">=":
|
|
449
|
+
return itemValue >= rangeValue;
|
|
450
|
+
case "<=":
|
|
451
|
+
return itemValue <= rangeValue;
|
|
452
|
+
default:
|
|
453
|
+
return true;
|
|
454
|
+
}
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
const marshalledItems = items.map((item) => this.marshallItem(item, table));
|
|
460
|
+
|
|
461
|
+
return {
|
|
462
|
+
Items: marshalledItems,
|
|
463
|
+
Count: marshalledItems.length,
|
|
464
|
+
ScannedCount: items.length,
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
scan(params) {
|
|
469
|
+
const { TableName, FilterExpression, ExpressionAttributeValues, Limit } = params;
|
|
470
|
+
const table = this.tables.get(TableName);
|
|
471
|
+
|
|
472
|
+
if (!table) {
|
|
473
|
+
throw new Error(`Table ${TableName} does not exist`);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
let items = this.store.read(TableName);
|
|
477
|
+
|
|
478
|
+
// Aplica filtro se existir
|
|
479
|
+
if (FilterExpression) {
|
|
480
|
+
items = this.applyFilter(items, FilterExpression, ExpressionAttributeValues, table);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// Aplica limite
|
|
484
|
+
if (Limit && items.length > Limit) {
|
|
485
|
+
items = items.slice(0, Limit);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
const marshalledItems = items.map((item) => this.marshallItem(item, table));
|
|
489
|
+
|
|
490
|
+
return {
|
|
491
|
+
Items: marshalledItems,
|
|
492
|
+
Count: marshalledItems.length,
|
|
493
|
+
ScannedCount: items.length,
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// Métodos auxiliares
|
|
498
|
+
normalizeItem(item, table) {
|
|
499
|
+
const normalized = { ...item };
|
|
500
|
+
|
|
501
|
+
// Remove os tipos do DynamoDB (S, N, etc)
|
|
502
|
+
for (const [key, value] of Object.entries(normalized)) {
|
|
503
|
+
if (value && typeof value === "object") {
|
|
504
|
+
if (value.S !== undefined) normalized[key] = value.S;
|
|
505
|
+
else if (value.N !== undefined) normalized[key] = parseFloat(value.N);
|
|
506
|
+
else if (value.BOOL !== undefined) normalized[key] = value.BOOL;
|
|
507
|
+
else if (value.L !== undefined) normalized[key] = value.L.map((v) => this.normalizeItem(v, table));
|
|
508
|
+
else if (value.M !== undefined) normalized[key] = this.normalizeItem(value.M, table);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
return normalized;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
marshallItem(item, table) {
|
|
516
|
+
const marshalled = {};
|
|
517
|
+
|
|
518
|
+
for (const [key, value] of Object.entries(item)) {
|
|
519
|
+
if (key.startsWith("_")) continue; // Pula campos internos
|
|
520
|
+
|
|
521
|
+
const type = table.attributeTypes[key];
|
|
522
|
+
if (type === "S") {
|
|
523
|
+
marshalled[key] = { S: String(value) };
|
|
524
|
+
} else if (type === "N") {
|
|
525
|
+
marshalled[key] = { N: String(value) };
|
|
526
|
+
} else if (type === "BOOL") {
|
|
527
|
+
marshalled[key] = { BOOL: Boolean(value) };
|
|
528
|
+
} else if (Array.isArray(value)) {
|
|
529
|
+
marshalled[key] = { L: value.map((v) => ({ S: String(v) })) };
|
|
530
|
+
} else if (typeof value === "object") {
|
|
531
|
+
marshalled[key] = { M: this.marshallItem(value, table) };
|
|
532
|
+
} else {
|
|
533
|
+
marshalled[key] = { S: String(value) };
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
return marshalled;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
getItemKey(item, table) {
|
|
541
|
+
const hashValue = item[table.hashKey];
|
|
542
|
+
const rangeValue = table.rangeKey ? item[table.rangeKey] : null;
|
|
543
|
+
return rangeValue ? `${hashValue}|${rangeValue}` : String(hashValue);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
getItemKeyFromKeys(keys, table) {
|
|
547
|
+
const rawHash = keys[table.hashKey];
|
|
548
|
+
const hashValue = rawHash && typeof rawHash === 'object' ? Object.values(rawHash)[0] : rawHash;
|
|
549
|
+
const rawRange = table.rangeKey ? keys[table.rangeKey] : null;
|
|
550
|
+
const rangeValue = rawRange && typeof rawRange === 'object' ? Object.values(rawRange)[0] : rawRange;
|
|
551
|
+
return rangeValue ? `${hashValue}|${rangeValue}` : String(hashValue);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
processUpdateExpression(item, expression, nameMap, valueMap, table) {
|
|
555
|
+
// Implementação simplificada - expandir conforme necessário
|
|
556
|
+
const setMatch = expression.match(/SET\s+([^]+?)(?=\s+(?:REMOVE|ADD|DELETE)|\s*$)/i);
|
|
557
|
+
|
|
558
|
+
if (setMatch) {
|
|
559
|
+
const assignments = setMatch[1].split(",").map((a) => a.trim());
|
|
560
|
+
|
|
561
|
+
for (const assignment of assignments) {
|
|
562
|
+
const [path, valueExpr] = assignment.split("=").map((s) => s.trim());
|
|
563
|
+
const attributeName = path.replace(/#/g, "");
|
|
564
|
+
const rawValue = valueMap[valueExpr];
|
|
565
|
+
const value = rawValue && typeof rawValue === 'object' ? Object.values(rawValue)[0] : rawValue;
|
|
566
|
+
|
|
567
|
+
item[attributeName] = value;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
applyFilter(items, expression, values, table) {
|
|
573
|
+
// Implementação simplificada
|
|
574
|
+
return items.filter((item) => {
|
|
575
|
+
const match = expression.match(/([^\s]+)\s*=\s*([^\s]+)/);
|
|
576
|
+
if (match) {
|
|
577
|
+
const [, attribute, placeholder] = match;
|
|
578
|
+
const rawValue = values[placeholder];
|
|
579
|
+
const expectedValue = rawValue && typeof rawValue === 'object' ? Object.values(rawValue)[0] : rawValue;
|
|
580
|
+
const actualValue = item[attribute];
|
|
581
|
+
return actualValue === expectedValue;
|
|
582
|
+
}
|
|
583
|
+
return true;
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
persistTables() {
|
|
588
|
+
const tablesObj = {};
|
|
589
|
+
for (const [name, table] of this.tables.entries()) {
|
|
590
|
+
tablesObj[name] = table;
|
|
591
|
+
}
|
|
592
|
+
this.store.write("__tables__", tablesObj);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
async reset() {
|
|
596
|
+
for (const [tableName] of this.tables) {
|
|
597
|
+
this.store.write(tableName, []);
|
|
598
|
+
}
|
|
599
|
+
logger.debug("DynamoDB: Todos os dados resetados");
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
getTablesCount() {
|
|
603
|
+
return this.tables.size;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
getTotalItems() {
|
|
607
|
+
let total = 0;
|
|
608
|
+
for (const [tableName] of this.tables) {
|
|
609
|
+
const items = this.store.read(tableName);
|
|
610
|
+
total += items.length;
|
|
611
|
+
}
|
|
612
|
+
return total;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
listTables() {
|
|
616
|
+
return { TableNames: Array.from(this.tables.keys()) };
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
module.exports = DynamoDBSimulator;
|