@accounter/server
Version:
24 lines (20 loc) • 684 B
text/typescript
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Injectable, Scope } from 'graphql-modules';
import postgres, { QueryResultBase, QueryResultRow } from 'pg';
import 'reflect-metadata';
type TypedQueryResult<Entity> = QueryResultBase & { rows: Entity[] };
({
scope: Scope.Singleton,
})
export class DBProvider {
constructor(private pool: postgres.Pool) {}
public async query<Entity extends QueryResultRow>(
queryStatement: string,
values?: any[] | undefined,
): Promise<TypedQueryResult<Entity>> {
if (!this.pool) {
throw new Error('DB connection not initialized');
}
return this.pool.query(queryStatement, values);
}
}