-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_table.js
69 lines (63 loc) · 1.42 KB
/
create_table.js
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
const co = require("co");
function check_if_table_exists(dynamodb, table) {
return co(function* t() {
const params = {
TableName: table
};
const data = yield new Promise((res, rej) =>
dynamodb.describeTable(params, (err, data) => {
if (err) {
console.log("Err");
console.log(err);
rej(err);
} else {
console.log("Data");
console.log(data);
res(data);
}
})
);
if (data && data.code === "ResourceNotFoundException") {
yield create_table(dynamodb, table);
}
return true;
});
}
function create_table(dynamodb, table) {
return co(function* t() {
const params = {
AttributeDefinitions: [
{
AttributeName: "repository",
AttributeType: "S"
},
{
AttributeName: "date",
AttributeType: "S"
}
],
KeySchema: [
{
AttributeName: "repository",
KeyType: "HASH"
},
{
AttributeName: "date",
KeyType: "RANGE"
}
],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
},
TableName: table
};
const data = yield new Promise((res, rej) =>
dynamodb.createTable(params, (err, data) => (err ? rej(err) : res(data)))
);
return data;
});
}
module.exports = {
check_if_table_exists
};