forked from aws-samples/aws-dynamodb-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-dynamodb-tables.js
More file actions
184 lines (176 loc) Β· 6.28 KB
/
Copy pathsetup-dynamodb-tables.js
File metadata and controls
184 lines (176 loc) Β· 6.28 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env node
const { DynamoDBClient, CreateTableCommand, ListTablesCommand, DeleteTableCommand } = require('@aws-sdk/client-dynamodb');
const client = new DynamoDBClient({
region: 'us-east-1',
endpoint: 'http://localhost:8000',
credentials: {
accessKeyId: 'test',
secretAccessKey: 'test'
}
});
const tables = [
{
TableName: 'test_users',
KeySchema: [
{ AttributeName: 'PK', KeyType: 'HASH' },
{ AttributeName: 'SK', KeyType: 'RANGE' }
],
AttributeDefinitions: [
{ AttributeName: 'PK', AttributeType: 'S' },
{ AttributeName: 'SK', AttributeType: 'S' },
{ AttributeName: 'GSI1PK', AttributeType: 'S' },
{ AttributeName: 'GSI2PK', AttributeType: 'S' }
],
GlobalSecondaryIndexes: [
{
IndexName: 'GSI1',
KeySchema: [{ AttributeName: 'GSI1PK', KeyType: 'HASH' }],
Projection: { ProjectionType: 'ALL' },
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 }
},
{
IndexName: 'GSI2',
KeySchema: [{ AttributeName: 'GSI2PK', KeyType: 'HASH' }],
Projection: { ProjectionType: 'ALL' },
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 }
}
],
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 }
},
{
TableName: 'test_products',
KeySchema: [
{ AttributeName: 'PK', KeyType: 'HASH' },
{ AttributeName: 'SK', KeyType: 'RANGE' }
],
AttributeDefinitions: [
{ AttributeName: 'PK', AttributeType: 'S' },
{ AttributeName: 'SK', AttributeType: 'S' },
{ AttributeName: 'GSI1PK', AttributeType: 'S' },
{ AttributeName: 'GSI2PK', AttributeType: 'S' }
],
GlobalSecondaryIndexes: [
{
IndexName: 'GSI1',
KeySchema: [{ AttributeName: 'GSI1PK', KeyType: 'HASH' }],
Projection: { ProjectionType: 'ALL' },
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 }
},
{
IndexName: 'GSI2',
KeySchema: [{ AttributeName: 'GSI2PK', KeyType: 'HASH' }],
Projection: { ProjectionType: 'ALL' },
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 }
}
],
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 }
},
{
TableName: 'test_categories',
KeySchema: [
{ AttributeName: 'PK', KeyType: 'HASH' },
{ AttributeName: 'SK', KeyType: 'RANGE' }
],
AttributeDefinitions: [
{ AttributeName: 'PK', AttributeType: 'S' },
{ AttributeName: 'SK', AttributeType: 'S' },
{ AttributeName: 'GSI1PK', AttributeType: 'S' }
],
GlobalSecondaryIndexes: [
{
IndexName: 'GSI1',
KeySchema: [{ AttributeName: 'GSI1PK', KeyType: 'HASH' }],
Projection: { ProjectionType: 'ALL' },
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 }
}
],
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 }
},
{
TableName: 'test_orders',
KeySchema: [
{ AttributeName: 'PK', KeyType: 'HASH' },
{ AttributeName: 'SK', KeyType: 'RANGE' }
],
AttributeDefinitions: [
{ AttributeName: 'PK', AttributeType: 'S' },
{ AttributeName: 'SK', AttributeType: 'S' },
{ AttributeName: 'GSI1PK', AttributeType: 'S' },
{ AttributeName: 'GSI2PK', AttributeType: 'S' }
],
GlobalSecondaryIndexes: [
{
IndexName: 'GSI1',
KeySchema: [{ AttributeName: 'GSI1PK', KeyType: 'HASH' }],
Projection: { ProjectionType: 'ALL' },
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 }
},
{
IndexName: 'GSI2',
KeySchema: [{ AttributeName: 'GSI2PK', KeyType: 'HASH' }],
Projection: { ProjectionType: 'ALL' },
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 }
}
],
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 }
},
{
TableName: 'test_shopping_carts',
KeySchema: [
{ AttributeName: 'PK', KeyType: 'HASH' },
{ AttributeName: 'SK', KeyType: 'RANGE' }
],
AttributeDefinitions: [
{ AttributeName: 'PK', AttributeType: 'S' },
{ AttributeName: 'SK', AttributeType: 'S' },
{ AttributeName: 'GSI1PK', AttributeType: 'S' },
{ AttributeName: 'GSI1SK', AttributeType: 'S' }
],
GlobalSecondaryIndexes: [
{
IndexName: 'GSI1',
KeySchema: [
{ AttributeName: 'GSI1PK', KeyType: 'HASH' },
{ AttributeName: 'GSI1SK', KeyType: 'RANGE' }
],
Projection: { ProjectionType: 'ALL' },
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 }
}
],
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 }
}
];
async function createTables() {
try {
console.log('π Checking DynamoDB Local connection...');
const { TableNames } = await client.send(new ListTablesCommand({}));
console.log('β
Connected to DynamoDB Local');
console.log(`π Existing tables: ${TableNames.length > 0 ? TableNames.join(', ') : 'None'}`);
for (const tableConfig of tables) {
try {
if (TableNames.includes(tableConfig.TableName)) {
console.log(`ποΈ Deleting existing table: ${tableConfig.TableName}`);
await client.send(new DeleteTableCommand({ TableName: tableConfig.TableName }));
// Wait a bit for table deletion
await new Promise(resolve => setTimeout(resolve, 1000));
}
console.log(`π¨ Creating table: ${tableConfig.TableName}`);
await client.send(new CreateTableCommand(tableConfig));
console.log(`β
Created table: ${tableConfig.TableName}`);
} catch (error) {
if (error.name === 'ResourceInUseException') {
console.log(`β
Table ${tableConfig.TableName} already exists`);
} else {
console.error(`β Error creating table ${tableConfig.TableName}:`, error.message);
throw error;
}
}
}
console.log('π All DynamoDB tables are ready for integration tests!');
} catch (error) {
console.error('β Error setting up DynamoDB tables:', error.message);
console.error('π‘ Make sure DynamoDB Local is running: docker run -p 8000:8000 amazon/dynamodb-local');
process.exit(1);
}
}
createTables();