48 lines
941 B
TypeScript
48 lines
941 B
TypeScript
import pg from 'pg';
|
|
const { Pool } = pg;
|
|
|
|
import {
|
|
PG_USER,
|
|
PG_PASS,
|
|
PG_HOST,
|
|
PG_PORT,
|
|
PG_DB,
|
|
} from '$env/static/private'
|
|
const connectionString = `postgres://${PG_USER}:${PG_PASS}@${PG_HOST}:${PG_PORT}/${PG_DB}`;
|
|
|
|
const pool = new Pool({ connectionString });
|
|
class Postgres {
|
|
client: pg.PoolClient | null = null;
|
|
public static async new() {
|
|
const pg = new Postgres();
|
|
pg.client = await pool.connect();
|
|
return pg;
|
|
}
|
|
|
|
async query(query: string, params: any[] = []) {
|
|
return (await this.client!.query(query, params));
|
|
}
|
|
|
|
async release() {
|
|
await this.client!.release(true);
|
|
}
|
|
|
|
async begin() {
|
|
await this.client!.query('begin');
|
|
}
|
|
|
|
async commit() {
|
|
await this.client!.query('commit');
|
|
}
|
|
|
|
async rollback() {
|
|
await this.client!.query('rollback');
|
|
}
|
|
}
|
|
|
|
export default async () => { return await Postgres.new(); }
|
|
|
|
|
|
import init from '$lib/server/database/init_db';
|
|
await init();
|