-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
85 lines (72 loc) · 1.75 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
const {Client} = require('pg')
const dbClient = new Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
})
dbClient.connect()
const self = module.exports = {
fetchAll : function (callback) {
dbClient.query('SELECT * FROM guilds;', (err, res) => {
if (err) console.error(err)
if (res.rows.length > 0) {
callback(res.rows)
}
})
},
fetchGuild : function (guildId, callback) {
const sql = 'SELECT * FROM guilds where id = $1;'
const values = [guildId]
dbClient.query(sql, values, (err, res) => {
if (err) console.error(err)
if (res.rows.length > 0) {
callback(res.rows)
}
})
},
addGuildPrefix : function (guildId, prefix, callback) {
const sql = 'insert into guilds (id, prefix, queue, game) VALUES ($1, $2, Null, Null);'
const values = [guildId, prefix]
dbClient.query(sql, values, (err) => {
if (err) {
callback(err)
} else {
callback(0)
}
})
},
updateGuildPrefix : function (guildId, prefix, callback) {
const sql = 'update guilds set prefix = $2 where id = $1;'
const values = [guildId, prefix]
dbClient.query(sql, values, (err) => {
if (err) {
callback(err)
} else {
callback(0)
}
})
},
updateGuildGame : function (guildId, game, callback) {
const sql = 'update guilds set game = $2 where id = $1;'
const values = [guildId, game]
dbClient.query(sql, values, (err) => {
if (err) {
callback(err)
} else {
callback(0)
}
})
},
addGuildGame : function (guildId, game, callback) {
const sql = 'insert into guilds (id, prefix, queue, game) VALUES ($1, $2, Null, $3);'
const values = [guildId, '/', game]
dbClient.query(sql, values, (err) => {
if (err) {
callback(err)
} else {
callback(0)
}
})
}
}