-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
112 lines (100 loc) · 2.79 KB
/
db.js
File metadata and controls
112 lines (100 loc) · 2.79 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
const bluebird = require('bluebird');
const pg = require('pg-promise')({
promiseLib: bluebird
});
const constants = require('./constants');
const ircServers = require('./ircservers')
const conString = process.env.DB;
function createUserTable() {
const createUsersTableQuery =
`CREATE TABLE IF NOT EXISTS users (
id UUID,
username varchar(${constants.MAX_USERNAME_LENGTH}),
passwordHash varchar(${constants.PASSWORD_HASH_LENGTH}),
email varchar(${constants.MAX_EMAIL_LENGTH}),
created int,
lastLoginAttempt int,
lastLogin int,
permissions int,
PRIMARY KEY(id, username)
)`;
pg(conString)
.query(createUsersTableQuery)
.catch((err) => {
throw err;
})
}
function createNetworkTable() {
const createNetworkTableQuery =
`CREATE TABLE IF NOT EXISTS networks (
id serial primary key,
networkName varchar(80) unique,
encoding varchar(30)
);
CREATE TABLE IF NOT EXISTS networkServers (
id serial primary key,
networkId serial references networks(id),
serverAddress varchar(80)
);`
return pg(conString)
.query(createNetworkTableQuery)
.then(() => {
console.log('Created networks and networkservers tables [OK]')
})
.catch((err) => {
throw err;
})
}
function LoadNetworkData() {
var tempId = 0;
var promises = ircServers.IRCSERVERS.map((network) => {
const InsertNetworkQuery = `INSERT INTO networks (networkname, encoding) VALUES ('${network.Name}', '${network.Encoding}') RETURNING id;`;
return pg(conString)
.query(InsertNetworkQuery)
.then((data) => {
tempId = data;
network.servers.map((server) => {
const InsertNetworkServerQuery =
`INSERT INTO networkservers (networkid, serveraddress) VALUES (${tempId[0].id}, '${server}');`;
pg(conString)
.query(InsertNetworkServerQuery)
.catch((err) => {
console.log(err);
throw err;
})
})
})
.catch((err) => {
throw err
})
})
return Promise.all(promises);
}
createUserTable();
createNetworkTable().then( () => LoadNetworkData());
module.exports = {
createUser(username, password, email) {
return pg(conString)
.query(`INSERT INTO users (username, password, email) VALUES (${username}, ${password}, ${email})`);
},
createLimitedUser(username) {
return pg(conString)
.query(`INSERT INTO users (username) VALUES (${username})`);
},
deleteUser(username) {
return pg(conString)
.query('')
},
getUser() {
return pg(conString)
.query('SELECT')
},
updateUser(username, password, email) {
return pg(conString)
.query('')
},
getNetwork() {
return pg(conString)
.query('SELECT * FROM networks')
}
}