Guest User

Untitled

a guest
Feb 9th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. import log = require("winston");
  2. import { Client, Pool, QueryResult } from "pg";
  3. import { SQLCommand } from "../post_put_ts/repositories/sql-command";
  4.  
  5. export class DatabaseClient {
  6.    private static pool: Pool;
  7.  
  8.    public static async connect(): Promise<Pool> {
  9.       log.info(`About to connect to:${process.env.DB_HOST}`);
  10.  
  11.       this.pool = new Pool({
  12.          user: process.env.DB_USER,
  13.          host: process.env.DB_HOST,
  14.          database: process.env.DB_NAME,
  15.          password: process.env.DB_PASS,
  16.          port: +process.env.DB_PORT
  17.       });
  18.  
  19.       await this.pool.connect();
  20.  
  21.       log.info(`Connected Successfully to:${process.env.DB_HOST}`);
  22.  
  23.       return this.pool;
  24.    }
  25.  
  26.    public static async execute(command: SQLCommand): Promise<QueryResult> {
  27.       try {
  28.          if (!this.pool) {
  29.             await this.connect();
  30.          }
  31.  
  32.          return await this.pool.query(command.sql, command.params);
  33.       } catch (err) {
  34.          throw (`An error occured executing sql command: ${command.sql}`)
  35.       }
  36.       finally {
  37.          // how do we do a finally block here?
  38.       }
  39.  
  40.    }
  41. }
Add Comment
Please, Sign In to add comment