-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
executable file
·265 lines (237 loc) · 8.43 KB
/
database.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
var mysql = require('promise-mysql'),
isHex = require('util').isHex;
var isInteger = Number.isInteger;
const PRODUCTION_DB = 'coiot',
TEST_DB = 'coiot';
const SELECT_USER = 'SELECT * FROM `users` WHERE `username` = ? AND `password` = ?';
const SELECT_USER_BY_USERNAME = 'SELECT * FROM `users` WHERE `username` = ?';
const SELECT_IDLE_USERS =
'SELECT u.id, u.username \
FROM `users` u \
LEFT JOIN `worker_ownership` o ON o.user_id = u.id \
WHERE o.worker_id IS NULL';
const SELECT_DEVICE = 'SELECT * FROM `devices` WHERE `device` = ? AND `key` = ?';
const SELECT_DEVICE_BY_NAME = 'SELECT * FROM `devices` WHERE `device` = ?';
const SELECT_DEVICE_BY_USERNAME =
'SELECT o.device_id, d.device, d.key, o.user_id, u.username \
FROM `devices` d \
JOIN `device_ownership` o ON o.device_id = d.id \
JOIN `users` u ON u.id = o.user_id \
WHERE u.username = ? AND d.device = ? AND d.key = ?';
const SELECT_WORKER = 'SELECT * FROM `workers` WHERE `name` = ? AND `key` = ?';
const SELECT_WORKERS = 'SELECT * FROM `workers`';
const SELECT_DEVICES =
'SELECT d.id, d.device, d.key \
FROM `devices` d \
JOIN `device_ownership` o ON o.device_id = d.id \
WHERE o.user_id = ?';
const SELECT_DEVICE_STAT =
'SELECT s.id, s.type, s.value, s.date \
FROM `statistics` s\
JOIN `device_ownership` o ON o.device_id = s.device_id \
WHERE o.user_id = ? AND s.device_id = ?';
const SELECT_DEVICE_STAT_BY_NAME =
'SELECT d.id, d.device, s.type, s.value, s.date \
FROM `statistics` s \
JOIN `devices` d ON d.id = s.device_id \
WHERE d.device = ?';
const SELECT_MIN_CONN_WORKER =
'SELECT w.id, w.name, w.ip \
FROM `workers` w \
LEFT JOIN (SELECT worker_id, count(id) AS cnt FROM worker_ownership GROUP BY worker_id) s ON s.worker_id = w.id \
ORDER BY s.cnt LIMIT 1';
const INSERT_USER =
'INSERT INTO `users` (`username`, `password`) VALUES (?, ?)'
const INSERT_DEVICE =
'INSERT INTO `devices` (`device`, `key`) VALUES (?, ?)'
const INSERT_DEVICE_OWNERSHIP =
'INSERT INTO `device_ownership` (`user_id`, `device_id`) VALUES (?, ?)'
const INSERT_WORKER_OWNERSHIP =
'INSERT INTO `worker_ownership` (`user_id`, `worker_id`) VALUES (?, ?)'
const DELETE_WORKER = 'DELETE FROM `workers` WHERE `id` = ?'
function wrappQuery(query, done) {
if (done === undefined) {
return query;
}
query.then((rows)=>{done(null, rows)}).catch((err)=>done(err));
}
function raiseError(msg, done) {
if (done === undefined) {
throw new Error(msg);
}
return done(new Error(msg));
}
class Database {
constructor() {
this.__host = "localhost";
this.__user = "root";
this.__pass = "";
this.__database = TEST_DB;
this.__connLimit = 32;
this.__connWait = true;
this.__acqLimit = 8000;
this.__queueLimit = 0;
this.__transaction = {
connectionLimit: 1
}
this.__pool = undefined;
this.__multstates = false;
}
configure(config) {
this.__host = config.host || this.__host;
this.__user = config.username || this.__user;
this.__pass = config.password || this.__pass;
this.__multstates = config.multstates || this.__multstates;
if (config.prod === true || config.test === false) {
this.__database = PRODUCTION_DB;
} else {
this.__database = TEST_DB;
}
}
connect() {
if (this.__pool) {
this.disconnect();
}
this.__pool = mysql.createPool({
host : this.__host,
user : this.__user,
password : this.__pass,
database : this.__database,
connectionLimit : this.__connLimit,
waitForConnections : this.__connWait,
acquireTimeout : this.__acqLimit,
queueLimit : this.__queueLimit,
useTransaction : this.__transaction,
useCursor : this.__cursor,
multipleStatements : this.__multstates
})
}
async disconnect() {
if (this.__pool) {
await this.__pool.end();
}
this.__pool = undefined;
}
query(q, args, done) {
if (!this.__pool) {
return raiseError('Missing database connection.', done);
}
let query = this.__pool.query(q, args);
return wrappQuery(query, done);
}
selectUser(usr, psw, done) {
if (!this.__pool) {
return raiseError('Missing database connection.', done);
}
let query = this.__pool.query(SELECT_USER, [usr, psw]);
return wrappQuery(query, done);
}
selectIdleUsers(done) {
if (!this.__pool) {
return raiseError('Missing database connection.', done);
}
let query = this.__pool.query(SELECT_IDLE_USERS);
return wrappQuery(query, done);
}
selectUserByUsername(usr, done) {
if (!this.__pool) {
return raiseError('Missing database connection.', done);
}
let query = this.__pool.query(SELECT_USER_BY_USERNAME, [usr]);
return wrappQuery(query, done);
}
selectDevice(dev, key, done) {
if (!this.__pool) {
return raiseError('Missing database connection.', done);
}
let query = this.__pool.query(SELECT_DEVICE, [dev, key]);
return wrappQuery(query, done);
}
selectDeviceByUsername(usr, dev, key, done) {
if (!this.__pool) {
return raiseError('Missing database connection.', done);
}
let query = this.__pool.query(SELECT_DEVICE_BY_USERNAME, [usr, dev, key]);
return wrappQuery(query, done);
}
selectDeviceByName(dev, done) {
if (!this.__pool) {
return raiseError('Missing database connection.', done);
}
let query = this.__pool.query(SELECT_DEVICE_BY_NAME, [dev]);
return wrappQuery(query, done);
}
selectWorker(name, key, done) {
if (!this.__pool) {
return raiseError('Missing database connection.', done);
}
let query = this.__pool.query(SELECT_WORKER, [name, key]);
return wrappQuery(query, done);
}
selectWorkers(done) {
if (!this.__pool) {
return raiseError('Missing database connection.', done);
}
let query = this.__pool.query(SELECT_WORKERS);
return wrappQuery(query, done);
}
selectDevices(usr_id, done) {
if (!this.__pool) {
return raiseError('Missing database connection.', done);
}
let query = this.__pool.query(SELECT_DEVICES, [usr_id]);
return wrappQuery(query, done);
}
selectDeviceStat(usr_id, dev_id, done) {
if (!this.__pool) {
return raiseError('Missing database connection.', done);
}
let query = this.__pool.query(SELECT_DEVICE_STAT, [usr_id, dev_id]);
return wrappQuery(query, done);
}
selectMinConnWorker(done) {
if (!this.__pool) {
return raiseError('Missing database connection.', done);
}
let query = this.__pool.query(SELECT_MIN_CONN_WORKER);
return wrappQuery(query, done);
}
insertUser(usr, psw, done) {
if (!this.__pool) {
return raiseError('Missing database connection.', done);
}
let query = this.__pool.query(INSERT_USER, [usr, psw]);
return wrappQuery(query, done);
}
insertDevice(dev, key, done) {
if (!this.__pool) {
return raiseError('Missing database connection.', done);
}
let query = this.__pool.query(INSERT_DEVICE, [dev, key]);
return wrappQuery(query, done);
}
insertDeviceOwnership(usr_id, dev_id, done) {
if (!this.__pool) {
return raiseError('Missing database connection.', done);
}
let query = this.__pool.query(INSERT_DEVICE_OWNERSHIP, [usr_id, dev_id]);
return wrappQuery(query, done);
}
insertWorkerOwnership(usr_id, wrk_id, done) {
if (!this.__pool) {
return raiseError('Missing database connection.', done);
}
let query = this.__pool.query(INSERT_WORKER_OWNERSHIP, [usr_id, wrk_id]);
return wrappQuery(query, done);
}
deleteWorker(id, done) {
if (!this.__pool) {
return raiseError('Missing database connection.', done);
}
let query = this.__pool.query(DELETE_WORKER, [id]);
return wrappQuery(query, done);
}
}
module.exports = {
Database: Database
}