29 lines
649 B
JavaScript
29 lines
649 B
JavaScript
import pg from 'pg'
|
|
|
|
export const dbpool = new pg.Pool({
|
|
connectionString: process.env.DEXORDER_DB_URL || 'postgres://dexorder:redroxed@localhost:5432/dexorder',
|
|
max: parseInt(process.env.DEXORDER_POOL_SIZE) || 10,
|
|
idleTimeoutMillis: parseInt(process.env.DEXORDER_POOL_TIMEOUT) || 10*60*1000,
|
|
})
|
|
|
|
dbpool.on('connect', (client) => {
|
|
client.query("SET TIME ZONE 'UTC'")
|
|
})
|
|
|
|
|
|
export async function withDb(cb) {
|
|
const db = await dbpool.connect()
|
|
try {
|
|
return await cb(db)
|
|
}
|
|
finally {
|
|
db.release()
|
|
}
|
|
}
|
|
|
|
|
|
export async function sql(query) {
|
|
return await withDb(async (db)=>await db.query(query) )
|
|
}
|
|
|