first commit
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* @file
|
||||
* Configures the database connection
|
||||
*/
|
||||
|
||||
const { Pool, Client } = require('pg');
|
||||
const url = require('url');
|
||||
const logger = require('./logger');
|
||||
//const connectionString = 'postgresql://wrenchboard:wrenchboard@10.10.10.23:5432/wrenchboard';
|
||||
const connectionString = process.env.POSTGRE_URL.replace(/'/g, '');
|
||||
const params = url.parse(connectionString);
|
||||
const auth = params.auth.split(':');
|
||||
const config = {
|
||||
user: auth[0],
|
||||
password: auth[1],
|
||||
host: params.hostname,
|
||||
port: params.port,
|
||||
database: params.pathname.split('/')[1],
|
||||
ssl: false
|
||||
};
|
||||
|
||||
const postgres = new Pool(config);
|
||||
|
||||
postgres.on('connect', client => {
|
||||
logger.info('Connected to Database');
|
||||
});
|
||||
|
||||
postgres.on('acquire', client => {
|
||||
logger.info('Client is checked out from the DB connection pool');
|
||||
});
|
||||
|
||||
postgres.on('remove', client => {
|
||||
logger.info('Client is closed & removed from the DB connection pool');
|
||||
});
|
||||
|
||||
postgres.on('error', (err, client) => {
|
||||
logger.error(err);
|
||||
});
|
||||
|
||||
// Connect to PostgreSQL
|
||||
postgres.connect((err, client, release) => {
|
||||
logger.info(connectionString);
|
||||
if (err) {
|
||||
logger.error('Error acquiring client', err.stack);
|
||||
return null;
|
||||
}
|
||||
client.query('SELECT NOW()', (err, result) => {
|
||||
release();
|
||||
if (err) {
|
||||
logger.error('Error executing query', err.stack);
|
||||
return nul;
|
||||
}
|
||||
logger.info(result.rows);
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = postgres;
|
||||
Reference in New Issue
Block a user