This repository has been archived by the owner on Oct 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.js
211 lines (188 loc) · 3.9 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
const debug = require('debug')
const pg = require('pg').native
const dlogError = debug('bot:error')
const dlogPgQuery = debug('bot:database:query')
const dlogPgValues = debug('bot:database:values')
const {Query} = pg
const {submit} = Query.prototype
Query.prototype.submit = function () {
const {text} = this
const {values} = this
const query = values.reduce((q, v, i) => q.replace(`$${i + 1}`, v), text)
dlogPgValues(values)
dlogPgQuery(query)
submit.apply(this, arguments) // eslint-disable-line prefer-rest-params
}
const pool = new pg.Pool({
database: 'storeofbot'
})
const error = res => {
dlogError(res)
return {
rowCount: 0,
error: res,
rows: []
}
}
const insert = async (db, table = 'bots') => {
let data = {}
const client = await pool.connect()
const listKeys = Object.keys(db).filter(e => e != 'online')
const query = `
INSERT
INTO ${table}(${
listKeys.join(', ')
})
VALUES (${
listKeys.reduce((t, e, i) => {
if (i == 0) {
return '$1'
}
return `${t}, $${i + 1}`
}, '')
})
RETURNING *;
`
data = await client.query(
query,
listKeys.map(e => db[e])
).catch(error)
client.release()
if (data.rowCount != 1) {
return false
}
return data.rows[0]
}
const select = async (where = {}, table = 'bots') => {
let data = {}
const client = await pool.connect()
data = await client.query(
`
SELECT *, EXTRACT(EPOCH FROM ( now() - time ) ) < 86400 AS online
FROM ${table}
${
Object.keys(where).reduce((t, e, i) => {
if (i == 0) {
return `WHERE ${e} = $1`
}
return `${t} AND ${e} = $${i + 1}`
}, '')
};
`,
Object.keys(where).map(e => where[e])
).catch(error)
client.release()
return data.rows
}
const update = async (db, table = 'bots') => {
let data = {}
const client = await pool.connect()
const listKeys = Object.keys(db).filter(e => e != 'online' && e != 'uptime')
const query = `
UPDATE ${table}
SET
${
listKeys.reduce((total, e, index) => {
return `${total},
${e} = $${index + 2}`
}, 'uptime = now()')
}
WHERE id = $1
RETURNING *;
`
data = await client.query(
query,
listKeys.reduce((total, e) => {
total.push(db[e])
return total
}, [db.id])
).catch(error)
client.release()
if (data.rowCount != 1) {
return false
}
return data.rows[0]
}
const del = async (where = {}, table = 'bots') => {
let data = {}
const client = await pool.connect()
data = await client.query(
`
DELETE
FROM ${table}
${
Object.keys(where).reduce((t, e, i) => {
if (i == 0) {
return `WHERE ${e} = $1`
}
return `${t} AND ${e} = $${i + 1}`
}, '')
};
`,
Object.keys(where).map(e => where[e])
).catch(error)
client.release()
return data
}
const selectWithFilter = async (
categories = [],
types = [],
languages = ['English'],
page = 0,
order = 'name',
table = 'bots'
) => {
let data = {}
const client = await pool.connect()
data = await client.query(`
SELECT *
FROM ${table}
WHERE offline IS NOT TRUE AND categories && $1 AND types && $2 AND languages && $4
ORDER BY ${order}
LIMIT 3
OFFSET $3;
`, [categories, types, page * 3, languages]).catch(error)
client.release()
return data.rows
}
const search = async (name, table = 'bots') => {
let data = {}
const client = await pool.connect()
data = await client.query(`
SELECT *, EXTRACT(EPOCH FROM ( now() - time ) ) < 86400 AS online
FROM ${table}
WHERE offline IS NOT TRUE AND name @@ $1;
`, [name]).catch(error)
client.release()
return data.rows
}
const offline = async (where = {}, table = 'bots') => {
let data = {}
const client = await pool.connect()
data = await client.query(
`
UPDATE ${table}
SET offline = 't'
${
Object.keys(where).reduce((t, e, i) => {
if (i == 0) {
return `WHERE ${e} = $1`
}
return `${t} AND ${e} = $${i + 1}`
}, '')
};
`,
Object.keys(where).map(e => where[e])
).catch(error)
client.release()
return data
}
module.exports = {
select,
selectWithFilter,
del,
insert,
update,
search,
offline
}