import createConnectionPool, {ConnectionPool, ConnectionPoolConfig, sql} from '@databases/pg';

export {sql};

const portString = process.env["DB_PORT"];
const portNumber = portString ? parseInt(portString) : undefined;

const clientConfig: ConnectionPoolConfig = {
	host: process.env["DB_HOST"],
	user: process.env["DB_USER"],
	database: process.env["DB_NAME"],
	password: process.env["DB_PASSWORD"],
	port: portNumber,
};

// @ts-ignore
const db: ConnectionPool = createConnectionPool({
	connectionString: false,
	...clientConfig
});

process.once('SIGTERM', () => {
	db.dispose().catch((ex) => {
	 	console.error(ex);
	});
});

export {db};