38 lines
1.2 KiB
JavaScript
38 lines
1.2 KiB
JavaScript
require('dotenv').config();
|
|
|
|
|
|
module.exports = class PostgresSingleton {
|
|
static instance;
|
|
|
|
constructor() {
|
|
if (PostgresSingleton.instance)
|
|
return this.instance;
|
|
PostgresSingleton.instance = this;
|
|
}
|
|
|
|
static getInstance() {
|
|
if (!PostgresSingleton.instance)
|
|
PostgresSingleton.instance = new PostgresSingleton();
|
|
return PostgresSingleton.instance;
|
|
}
|
|
|
|
setup() {
|
|
if (this.postgres === null || typeof this.postgres == 'undefined')
|
|
this.postgres = require('postgres')({
|
|
host: process.env.POSTGRES_HOST, post: process.env.POSTGRES_PORT, database: process.env.POSTGRES_DATABASE,
|
|
username: process.env.POSTGRES_USERNAME, password: process.env.POSTGRES_PASSWORD
|
|
});
|
|
}
|
|
async start() {
|
|
if (this.postgres === null || typeof this.postgres == 'undefined')
|
|
this.setup();
|
|
}
|
|
async stop() {}
|
|
|
|
async query(query, parameters) {
|
|
const evaluated = Object.entries(parameters)
|
|
.reduce((a, [k, v]) => a.concat(`const ${k}=${JSON.stringify(v)}`), '');
|
|
|
|
return await eval(`(async () => { ${evaluated} return await this.postgres\`${query}\`; })();`);
|
|
}
|
|
} |