-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsetup.js
More file actions
102 lines (94 loc) · 3.27 KB
/
setup.js
File metadata and controls
102 lines (94 loc) · 3.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
101
102
const config = require('./src/config');
const AWS = require('aws-sdk');
AWS.config.update(
{
region: config.db.DynamoDb.awsConfig.region,
accessKeyId: config.db.DynamoDb.awsConfig.accessKeyId,
secretAccessKey: config.db.DynamoDb.awsConfig.secretAccessKey,
}
);
const dynamodb = new AWS.DynamoDB();
main();
async function main() {
console.time('Retrieving list of tables: ');
let tableList;
try {
tableList = await listTables();
} catch (error) {
console.error(`Error at retrieving list of tables: ${JSON.stringify(error, null, 4)}`);
}
console.timeEnd('Retrieving list of tables: ');
console.log(`Table list: ${tableList.join(', ')}`);
if (tableList.indexOf(config.custom.DynamoDb.tableName) < 0) {
console.time('Setting up score table: ');
try {
const tableSetupResult = await createScoreTable();
console.log(`Table setup result: ${JSON.stringify(tableSetupResult, null, 4)}`);
} catch (error) {
console.error(`Error at setting up score table: ${JSON.stringify(error, null, 4)}`);
}
console.timeEnd('Setting up score table: ');
} else {
console.log(`Score table (${
config.custom.dbTables.scores.tableName
}) already set up. :)`);
}
return;
}
function listTables() {
return new Promise(async (resolve, reject) => {
try {
const parameters = {};
const query = dynamodb.listTables(
parameters,
(error, results) => {
if (error) {
return reject(error);
}
resolve(results.TableNames);
}
);
} catch (e) {
reject(e);
}
});
}
function createScoreTable() {
return new Promise(async (resolve, reject) => {
try {
const parameters = {
AttributeDefinitions: [
{
AttributeName: config.custom.DynamoDb.keyAttributeName,
AttributeType: config.custom.DynamoDb.keyAttributeType,
},
],
KeySchema: [
{
AttributeName: config.custom.DynamoDb.keyAttributeName,
KeyType: 'HASH',
},
],
TableName: config.custom.DynamoDb.tableName,
BillingMode: 'PROVISIONED', // Default b/c it qualifies for the DynamoDB free tier, otherwise 'PAY_PER_REQUEST'
ProvisionedThroughput: { // Only if 'BillingMode' is 'PROVISIONED'
ReadCapacityUnits: '1',
WriteCapacityUnits: '1',
},
};
const query = dynamodb.createTable(
parameters,
(error, results) => {
console.log(`Error: ${JSON.stringify(error, null, 4)}`);
console.log(`Results: ${JSON.stringify(results, null, 4)}`);
if (error) {
return reject(error);
}
resolve(results);
}
);
} catch (e) {
reject(e);
}
});
}