Skip to content

Commit f434f11

Browse files
committed
+ delete()
1 parent b7035d0 commit f434f11

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

index.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ module.exports.connect = (config = {}) => {
7272
return result
7373
}
7474

75-
pool.qRow = async (sql, params = [], cache = false, ttl = undefined) => {
75+
pool.qRow = pool.selectRow = async (sql, params = [], cache = false, ttl = undefined) => {
7676
const rows = await pool.q(sql, params, cache, ttl)
7777
return Array.isArray(rows) && rows.length ? rows[0] : false
7878
}
@@ -86,32 +86,48 @@ module.exports.connect = (config = {}) => {
8686
}
8787
}
8888

89-
pool.insert = async (table, row) => {
89+
pool.insert = pool.i = async (table, row) => {
9090
qid++
9191
const log = debug.extend(qid)
9292
log('INSERT INTO', table/*, row, rows/*, fields */)
93+
log(getTable(row))
9394
const [rows, fields] = await pool.query('INSERT INTO ?? SET ?', [table, row])
9495
.catch(error => {
9596
console.error('[MYSQL] insert', table, row, error)
9697
throw error
9798
})
98-
log(getTable(row))
9999
log(getTable(rows))
100100
return rows || false
101101
}
102102

103-
pool.update = async (table, row, where) => {
103+
pool.update = async (table, row, where = false) => {
104104
qid++
105105
const log = debug.extend(qid)
106106
log('UPDATE', table/*, [row, where], rows/*, fields */)
107-
const _where = Object.keys(where).map(key => key + '=' + pool.escape(where[key])).join(' AND ')
108-
const [rows, fields] = await pool.query(`UPDATE ?? SET ? WHERE ${_where}`, [table, row])
107+
log(getTable(row))
108+
log(getTable(where))
109+
const _where = where ? 'WHERE ' + Object.keys(where).map(key => key + '=' + pool.escape(where[key])).join(' AND ') : ''
110+
const [rows, fields] = await pool.query(`UPDATE ?? SET ? ${_where}`, [table, row])
109111
.catch(error => {
110112
console.error('[MYSQL] update', table, [row, where], error)
111113
throw error
112114
})
113-
log(getTable(row))
115+
116+
log(getTable(rows))
117+
return rows || false
118+
}
119+
120+
pool.delete = pool.del = async (table, where = false) => {
121+
qid++
122+
const log = debug.extend(qid)
123+
log('DELETE', table/*, [row, where], rows/*, fields */)
114124
log(getTable(where))
125+
const _where = where ? 'WHERE ' + Object.keys(where).map(key => key + '=' + pool.escape(where[key])).join(' AND ') : ''
126+
const [rows, fields] = await pool.query(`DELETE FROM ?? ${_where}`, [table])
127+
.catch(error => {
128+
console.error('[MYSQL] delete', table, where, error)
129+
throw error
130+
})
115131
log(getTable(rows))
116132
return rows || false
117133
}
@@ -188,9 +204,11 @@ module.exports.connect = (config = {}) => {
188204
return pool
189205
}
190206

191-
process.on('unhandledRejection', (reason, p) => {
192-
console.error('Unhandled Rejection at:', p, 'reason:', reason)
207+
/*
208+
process.on('unhandledRejection', (reason) => { // , promise
209+
console.error('Unhandled rejection:', reason)
193210
})
194211
process.on('uncaughtException', (error) => {
195-
console.error(`Caught exception: ${error}\n` + `Exception origin: ${error.stack}`)
212+
console.error('Uncaught exception:', error)
196213
})
214+
*/

0 commit comments

Comments
 (0)