-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdatabase.js
62 lines (55 loc) · 1.71 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
const config = require('./config.json');
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database(config.sqliteFile || ':memory:');
module.exports = {
dbInit: async () => {
const tableQueries = [
`
CREATE TABLE IF NOT EXISTS readySystems (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
guildId VARCHAR(128) NOT NULL,
channelId VARCHAR(128) NOT NULL,
userId VARCHAR(128) NOT NULL,
UNIQUE (guildId, channelId)
)
`,
`
CREATE TABLE IF NOT EXISTS readyChecks (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
readySystemId INTEGER NOT NULL,
userId VARCHAR(128) NOT NULL,
isReady INTEGER NOT NULL DEFAULT 0,
UNIQUE (readySystemId, userId)
)
`
];
for (let query of tableQueries) {
await module.exports.dbExecute(query);
}
},
// Execute a query on the database
dbExecute: (sql, params=[]) => new Promise((resolve, reject) => {
db.run(sql, params, (err) => {
// Reject on errors, provide the error
if (err) { return reject(err); }
// Execution is complete
resolve();
});
}),
dbQueryOne: (sql, params=[]) => new Promise((resolve, reject) => {
db.get(sql, params, (err, row) => {
// Reject on errors, provide the error
if (err) { return reject(err); }
// Send back the row object
resolve(row || null);
});
}),
dbQueryAll: (sql, params=[]) => new Promise((resolve, reject) => {
db.all(sql, params, (err, rows) => {
// Reject on errors, provide the error
if (err) { return reject(err); }
// Send back the rows array
resolve((rows.length === 0) ? null : rows);
});
}),
};