34 lines
788 B
JavaScript
34 lines
788 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 function dbAddr(addr) {
|
|
// format an 0x-style address into postgres bytes
|
|
return '\\' + addr.slice(1)
|
|
}
|
|
|
|
export async function sql(query, ...params) {
|
|
return await withDb(async (db)=>await db.query(query, params) )
|
|
}
|
|
|