platoo/db/index.js

46 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-05-19 17:14:33 +00:00
const { Pool } = require('pg')
const pool = new Pool({
2020-05-19 17:21:09 +00:00
user: process.env.user || 'admin',
host: process.env.host || '127.0.0.1',
database: process.env.database || 'platoo',
password: process.env.password,
port: process.env.port || 5432,
2020-05-19 17:14:33 +00:00
})
module.exports = {
query: (text, params, callback) => {
const start = Date.now()
return pool.query(text, params, (err, res) => {
const duration = Date.now() - start
//console.log('executed query', { text, duration, rows: res.rowCount })
callback(err, res)
})
},
connect: function(fun){
return pool.connect(fun)
},
getClient: (callback) => {
pool.connect((err, client, done) => {
const query = client.query
// monkey patch the query method to keep track of the last query executed
client.query = (...args) => {
client.lastQuery = args
return query.apply(client, args)
}
// set a timeout of 5 seconds, after which we will log this client's last query
const timeout = setTimeout(() => {
console.error('A client has been checked out for more than 5 seconds!')
console.error(`The last executed query on this client was: ${client.lastQuery}`)
}, 5000)
const release = (err) => {
// call the actual 'done' method, returning this client to the pool
done(err)
// clear our timeout
clearTimeout(timeout)
// set the query method back to its old un-monkey-patched version
client.query = query
}
callback(err, client, release)
})
}
}