-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcreateTable.js
More file actions
130 lines (117 loc) · 3.54 KB
/
Copy pathcreateTable.js
File metadata and controls
130 lines (117 loc) · 3.54 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
const AWS = require('aws-sdk');
const indexOf = require('lodash/indexOf');
const chalk = require('chalk');
const { awsConfigUpdate } = require('./awsConfigUpdate');
awsConfigUpdate();
const dynamodb = new AWS.DynamoDB();
/* Delete Tables */
const getDeleteParams = tableName => ({
TableName: tableName,
});
const createGroceryTable = () => {
const groceryParams = {
TableName: 'grocery',
KeySchema: [
{ AttributeName: 'groceryId', KeyType: 'HASH' },
],
AttributeDefinitions: [
{ AttributeName: 'groceryId', AttributeType: 'S' },
{ AttributeName: 'category', AttributeType: 'S' },
],
ProvisionedThroughput: {
ReadCapacityUnits: 2,
WriteCapacityUnits: 2
},
GlobalSecondaryIndexes: [
{
IndexName: 'GroceryCategoryIndex',
KeySchema: [
{ AttributeName: 'category', KeyType: 'HASH' },
{ AttributeName: 'groceryId', KeyType: 'RANGE' },
],
Projection: {
ProjectionType: 'ALL'
},
ProvisionedThroughput: {
ReadCapacityUnits: 2,
WriteCapacityUnits: 2
},
}
],
StreamSpecification: {
StreamEnabled: true,
StreamViewType: 'NEW_IMAGE'
}
};
return dynamodb.createTable(groceryParams).promise();
};
const createTopSellingTable = () => {
const createTableParams = {
TableName: 'top_selling_groceries',
KeySchema: [
{ AttributeName: 'category', KeyType: 'HASH' },
],
AttributeDefinitions: [
{ AttributeName: 'category', AttributeType: 'S' },
],
ProvisionedThroughput: {
ReadCapacityUnits: 2,
WriteCapacityUnits: 2
}
}
return dynamodb.createTable(createTableParams).promise();
}
const createOrderTable = () => {
const orderParams = {
TableName: 'orders',
KeySchema: [
{ AttributeName: 'userId', KeyType: 'HASH' },
{ AttributeName: 'orderId', KeyType: 'RANGE' },
],
AttributeDefinitions: [
{ AttributeName: 'orderId', AttributeType: 'S' },
{ AttributeName: 'userId', AttributeType: 'S' },
],
ProvisionedThroughput: {
ReadCapacityUnits: 2,
WriteCapacityUnits: 2,
},
};
return dynamodb.createTable(orderParams).promise();
};
const createCartTable = () => {
const userParams = {
TableName: 'cart',
KeySchema: [
{ AttributeName: 'userId', KeyType: 'HASH' },
],
AttributeDefinitions: [
{ AttributeName: 'userId', AttributeType: 'S' },
],
ProvisionedThroughput: {
ReadCapacityUnits: 2,
WriteCapacityUnits: 2,
},
};
return dynamodb.createTable(userParams).promise();
};
let tables;
const listTables = dynamodb.listTables({}).promise();
listTables
.then((data, err) => {
let groceryTablePromise,
userTablePromise,
orderTablePromise,
topSellingTablePromise;
groceryTablePromise = (indexOf(data.TableNames, 'grocery') === -1) ? createGroceryTable() : Promise.resolve();
userTablePromise = (indexOf(data.TableNames, 'cart') === -1) ? createCartTable() : Promise.resolve();
orderTablePromise = (indexOf(data.TableNames, 'orders') === -1) ? createOrderTable() : Promise.resolve();
topSellingTablePromise = (indexOf(data.TableNames, 'top_selling_groceries') === -1) ? createTopSellingTable() : Promise.resolve();
return Promise.all([groceryTablePromise, userTablePromise, orderTablePromise, topSellingTablePromise]);
})
.then(() => {
console.log(chalk.green('Created Tables Successfully'));
})
.catch((e) => {
console.log(chalk.red('Could not create tables. Reason: ', e.message));
});