forked from MattCollins84/simple-notification-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbSetup.js
139 lines (97 loc) · 2.95 KB
/
dbSetup.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
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
"use strict"
const connection = require('./lib/config.js').connection
const async = require('async');
const r = require('rethinkdb');
const prompt = require('prompt');
prompt.start();
let actions = {};
// make sure we are talking to the correct DB
actions.confirmConnection = (callback) => {
console.log("*********************************")
console.log(JSON.stringify(connection, null, 2))
console.log("*********************************")
prompt.get({
properties: {
confirm: {
description: "This is your current DB connection string, are you happy with this? (yes/no)"
}
}
}, (err, result) => {
if (result.confirm !== "yes") {
console.log("Please modify the /lib/config.js to reflect your required DB configuration")
return callback("Invalid DB configuration", false);
}
return callback(null, true)
});
}
// create the users table
actions.createUsersTable = (callback) => {
r.connect(connection, (err, conn) => {
r.tableCreate("users")
.run(conn, (err, cursor) => {
console.log("- Creating users table... " + (err?err.msg:"SUCCESS"))
return callback(null, (err?false:true))
})
})
}
// create the messages table
actions.createMessagesTable = (callback) => {
r.connect(connection, (err, conn) => {
r.tableCreate("messages")
.run(conn, (err, cursor) => {
console.log("- Creating messages table... " + (err?err.msg:"SUCCESS"))
return callback(null, (err?false:true))
})
})
}
// create the queryHash index on users table
actions.createQueryHashIndex = (callback) => {
r.connect(connection, (err, conn) => {
r.table("users")
.indexCreate("queryHash")
.run(conn, (err, cursor) => {
console.log("- Creating queryHash index... " + (err?err.msg:"SUCCESS"))
return callback(null, (err?false:true))
})
})
}
// create the userHashes index on users table
actions.createUserHashesIndex = (callback) => {
r.connect(connection, (err, conn) => {
r.table("users")
.indexCreate("userHashes", { multi: true })
.run(conn, (err, cursor) => {
console.log("- Creating userHashes index... " + (err?err.msg:"SUCCESS"))
return callback(null, (err?false:true))
})
})
}
// create the recipientHash index on messages table
actions.createRecipientHashIndex = (callback) => {
r.connect(connection, (err, conn) => {
r.table("messages")
.indexCreate("recipientHash")
.run(conn, (err, cursor) => {
console.log("- Creating recipientHash index... " + (err?err.msg:"SUCCESS"))
return callback(null, (err?false:true))
})
})
}
// create the time index on messages table
actions.createTimeIndex = (callback) => {
r.connect(connection, (err, conn) => {
r.table("messages")
.indexCreate("time")
.run(conn, (err, cursor) => {
console.log("- Creating time index... " + (err?err.msg:"SUCCESS"))
return callback(null, (err?false:true))
})
})
}
async.series(actions, (err, results) => {
if (err) {
console.log("There were errors during the setup process")
console.log(err);
}
process.exit(0);
})