forked from saleor/apps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-dynamodb.ts
More file actions
executable file
路100 lines (87 loc) 路 2.27 KB
/
Copy pathsetup-dynamodb.ts
File metadata and controls
executable file
路100 lines (87 loc) 路 2.27 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
/* eslint-disable no-console */
import { parseArgs } from "node:util";
import {
CreateTableCommand,
DescribeTableCommand,
DynamoDBClient,
ResourceNotFoundException,
} from "@aws-sdk/client-dynamodb";
import { env } from "@/lib/env";
const npAtobaraiMainTableName = env.DYNAMODB_MAIN_TABLE_NAME;
try {
const {
values: { "endpoint-url": endpointUrl },
} = parseArgs({
args: process.argv.slice(2),
options: {
"endpoint-url": {
type: "string",
short: "e",
default: "http://localhost:8000",
},
},
});
console.log(`Starting DynamoDB setup with endpoint: ${endpointUrl}`);
const dynamoClient = new DynamoDBClient({
endpoint: endpointUrl,
region: "localhost",
credentials: {
accessKeyId: "local",
secretAccessKey: "local",
},
});
const createTableIfNotExists = async (tableName: string) => {
try {
const possibleTable = await dynamoClient.send(
new DescribeTableCommand({
TableName: tableName,
}),
);
if (possibleTable.Table) {
console.log(`Table ${tableName} already exists - creation is skipped`);
return;
}
} catch (error) {
if (error instanceof ResourceNotFoundException) {
console.log(`Table ${tableName} does not exist, proceeding with creation.`);
} else {
throw error;
}
}
const createTableCommand = new CreateTableCommand({
TableName: tableName,
AttributeDefinitions: [
{
AttributeName: "PK",
AttributeType: "S",
},
{
AttributeName: "SK",
AttributeType: "S",
},
],
KeySchema: [
{
AttributeName: "PK",
KeyType: "HASH",
},
{
AttributeName: "SK",
KeyType: "RANGE",
},
],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5,
},
});
await dynamoClient.send(createTableCommand);
console.log(`Table ${tableName} created successfully`);
};
await createTableIfNotExists(npAtobaraiMainTableName);
console.log("DynamoDB setup completed successfully");
process.exit(0);
} catch (error) {
console.error("Error setting up DynamoDB:", error);
process.exit(1);
}