Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import log = require("winston");
- import { Client, Pool, QueryResult } from "pg";
- import { SQLCommand } from "../post_put_ts/repositories/sql-command";
- export class DatabaseClient {
- private static pool: Pool;
- public static async connect(): Promise<Pool> {
- log.info(`About to connect to:${process.env.DB_HOST}`);
- this.pool = new Pool({
- user: process.env.DB_USER,
- host: process.env.DB_HOST,
- database: process.env.DB_NAME,
- password: process.env.DB_PASS,
- port: +process.env.DB_PORT
- });
- await this.pool.connect();
- log.info(`Connected Successfully to:${process.env.DB_HOST}`);
- return this.pool;
- }
- public static async execute(command: SQLCommand): Promise<QueryResult> {
- try {
- if (!this.pool) {
- await this.connect();
- }
- return await this.pool.query(command.sql, command.params);
- } catch (err) {
- throw (`An error occured executing sql command: ${command.sql}`)
- }
- finally {
- // how do we do a finally block here?
- }
- }
- }
Add Comment
Please, Sign In to add comment