Created
October 10, 2019 18:54
-
-
Save mzalazar/a5f5b8567c811860218b81266bd4eb88 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Pool } from 'pg' | |
import * as dotenv from 'dotenv' | |
import { colors } from 'colors' | |
dotenv.config() | |
export class DatabasePool { | |
constructor() { | |
if (!!DatabasePool.instance) { | |
return DatabasePool.instance; | |
} | |
DatabasePool.instance = this | |
console.log('Instantiating DatabasePool =)'.bgGreen.black) | |
this.pool = new Pool({ | |
user: process.env.DB_USER, | |
host: process.env.DB_HOST, | |
database: process.env.DB_DBNAME, | |
password: process.env.DB_PASS, | |
port: parseInt(process.env.DB_PORT) | |
}) | |
return this | |
} | |
getPool() { | |
return this.pool | |
} | |
} | |
// usage | |
const db = new DatabasePool() // get singleton | |
db.getPool().query('SELECT * FROM users').then(data => { console.log(data.rows) }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment