-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.js
95 lines (82 loc) · 3.03 KB
/
sql.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
const mysql = require("mysql")
class SQLParser {
static localhost = true
static production = true
static isConnected = false
static isConnecting = null
static conn = null
static connInfo = this.production ? {
connectionLimit: 128,
host : '209.222.20.135',
user : 'pizza_project',
password: 'F#rvYnmkcJ&yx837J2x$#XyZr$*#xPoZPcJg6G9X#EvPrb9fGQoJs8!FQo8iC&Rf',
database: 'pizza_project',
charset : 'latin1_swedish_ci'
} : {
connectionLimit: 128,
host : 'localhost',
user : 'pizza_project',
password: '',
database: 'pizza_project',
charset : 'latin1_swedish_ci'
}
static async connect() {
if (this.isConnected) return true
if (this.isConnecting) return await this.isConnecting // promise
let self = this
this.isConnecting = new Promise(async(resolve, reject) => {
self.conn = mysql.createPool(self.connInfo)
self.isConnected = true
self.isConnecting = null
console.debug('SQL connection established')
resolve(self)
}).catch(err => {
console.error(err)
setTimeout(async () => await self.connect(), 10000)
})
}
static close() {
if (!this.isConnected) return
this.conn.close()
this.isConnected = false
}
static async query(query, params, callback, flags) {
if (!this.isConnected) await this.connect()
if (!this.production)
console.debug({
query: query.replace(/\s+/gi, ' ').trim(), // this is strictly for readability purposes,
params,
})
return await new Promise(async(resolve, reject) => {
let conn = await new Promise((resolve, reject) => {
this.conn.getConnection((err, conn) => {
if (err) reject(err)
resolve(conn)
})
}).catch(reject)
let r = await new Promise((resolve, reject) => {
conn.query(query, params, (err, res) => {
conn.release()
if (err) reject(err)
resolve(res)
})
}).catch(err => {
try { conn.release() } catch (e) {}
reject(err)
})
let result = r // null
if (!r || r.length === 0)
result = null // multiple ? [] : null
else if (typeof r.length === 'number' && r.length === 1)
result = query.includes('LIMIT 1') ? r[0] : r // multiple ? r : typeof r.length === 'number' ? r[0] : r
else result = r
if (callback) return callback(result)
resolve(result)
}).catch(err => {
query = err.sql = query.replace(/\s+/gi, ' ').trim()
console.warn(`Error in query ${query} with params ${params ? params.toString() : ''}`)
console.error(err)
})
}
}
module.exports = SQLParser