@gugananuvem/aws-local-simulator 1.0.16 → 1.0.17

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gugananuvem/aws-local-simulator",
3
- "version": "1.0.16",
3
+ "version": "1.0.17",
4
4
  "description": "Simulador local completo para serviços AWS",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -120,6 +120,6 @@
120
120
  "optional": true
121
121
  }
122
122
  },
123
- "buildDate": "2026-04-20T18:31:32.841Z",
123
+ "buildDate": "2026-04-20T21:57:52.362Z",
124
124
  "published": true
125
125
  }
@@ -29,26 +29,24 @@ class DynamoDBSimulator {
29
29
  }
30
30
 
31
31
  loadTables() {
32
- // Carrega tabelas da configuração
33
- if (this.config.dynamodb?.tables) {
34
- for (const tableDef of this.config.dynamodb.tables) {
35
- this.createTable(tableDef);
36
- }
37
- }
38
-
39
- // Carrega tabelas existentes do disco
32
+ // Carrega tabelas existentes do disco PRIMEIRO para evitar sobrescrever definições persistidas
40
33
  const savedTables = this.store.read("__tables__");
41
34
  if (savedTables) {
42
35
  for (const [name, definition] of Object.entries(savedTables)) {
43
- if (!this.tables.has(name)) {
44
- this.tables.set(name, definition);
45
- }
36
+ this.tables.set(name, definition);
37
+ }
38
+ }
39
+
40
+ // Cria tabelas da configuração apenas se ainda não existirem no disco
41
+ if (this.config.dynamodb?.tables) {
42
+ for (const tableDef of this.config.dynamodb.tables) {
43
+ this.createTable(tableDef);
46
44
  }
47
45
  }
48
46
  }
49
47
 
50
48
  createTable(params) {
51
- const { TableName, KeySchema, AttributeDefinitions, ProvisionedThroughput } = params;
49
+ const { TableName, KeySchema, AttributeDefinitions, ProvisionedThroughput, GlobalSecondaryIndexes } = params;
52
50
 
53
51
  if (this.tables.has(TableName)) {
54
52
  logger.warn(`Tabela ${TableName} já existe`);
@@ -63,11 +61,21 @@ class DynamoDBSimulator {
63
61
  attributeTypes[attr.AttributeName] = attr.AttributeType;
64
62
  });
65
63
 
64
+ const globalSecondaryIndexes = {};
65
+ if (GlobalSecondaryIndexes) {
66
+ for (const gsi of GlobalSecondaryIndexes) {
67
+ const gsiHashKey = gsi.KeySchema.find((k) => k.KeyType === "HASH").AttributeName;
68
+ const gsiRangeKey = gsi.KeySchema.find((k) => k.KeyType === "RANGE")?.AttributeName;
69
+ globalSecondaryIndexes[gsi.IndexName] = { hashKey: gsiHashKey, rangeKey: gsiRangeKey };
70
+ }
71
+ }
72
+
66
73
  const table = {
67
74
  name: TableName,
68
75
  hashKey,
69
76
  rangeKey,
70
77
  attributeTypes,
78
+ globalSecondaryIndexes,
71
79
  createdAt: new Date().toISOString(),
72
80
  itemCount: 0,
73
81
  sizeBytes: 0,
@@ -424,8 +432,24 @@ class DynamoDBSimulator {
424
432
 
425
433
  let items = this.store.read(TableName);
426
434
 
435
+ // Resolve hash key e range key: usa GSI se IndexName estiver presente, caso contrário usa a tabela principal
436
+ let hashKey;
437
+ let rangeKey;
438
+
439
+ if (IndexName != null) {
440
+ const gsiDefs = table.globalSecondaryIndexes || {};
441
+ const gsi = gsiDefs[IndexName];
442
+ if (!gsi) {
443
+ throw new Error(`GSI "${IndexName}" not found on table "${TableName}"`);
444
+ }
445
+ hashKey = gsi.hashKey;
446
+ rangeKey = gsi.rangeKey;
447
+ } else {
448
+ hashKey = table.hashKey;
449
+ rangeKey = table.rangeKey;
450
+ }
451
+
427
452
  // Filtra pela chave de partição
428
- const hashKey = table.hashKey;
429
453
  const hashValueMatch = KeyConditionExpression.match(new RegExp(`${hashKey}\\s*=\\s*([^\\s]+)`));
430
454
 
431
455
  if (hashValueMatch) {
@@ -436,8 +460,7 @@ class DynamoDBSimulator {
436
460
  }
437
461
 
438
462
  // Filtra pela chave de ordenação se existir
439
- if (table.rangeKey) {
440
- const rangeKey = table.rangeKey;
463
+ if (rangeKey) {
441
464
  const rangeConditionMatch = KeyConditionExpression.match(new RegExp(`${rangeKey}\\s*(=|>|<|>=|<=)\\s*([^\\s]+)`));
442
465
 
443
466
  if (rangeConditionMatch) {
@@ -31,7 +31,7 @@ class LocalStore {
31
31
  if (!fs.existsSync(filePath)) return [];
32
32
 
33
33
  try {
34
- const content = fs.readFileSync(filePath, 'utf8');
34
+ const content = fs.readFileSync(filePath, 'utf8').replace(/^\uFEFF/, '');
35
35
  return JSON.parse(content);
36
36
  } catch (error) {
37
37
  console.error(`Erro ao ler ${entity}:`, error);