import * as pg from "pg"; import { query } from "./query"; import { returningClause } from "./returning-clause"; import { IOptionalArgs, IQueryArgs, } from "./types"; export interface IConn { pool: pg.Pool; query(args: IQueryArgs): Promise; insert(args: IQueryArgs): Promise; update(args: IQueryArgs): Promise; delete(args: IQueryArgs): Promise; } export const conn = (dbConfig: pg.ClientConfig, opts?: IOptionalArgs): IConn => { const pool = new pg.Pool({ ...dbConfig }); const defaultQueryArgs = { ...opts, pool, dbUser: dbConfig.user, }; return { pool, query: (args: IQueryArgs): Promise => query({ ...defaultQueryArgs, ...args }), insert: (args: IQueryArgs): Promise => returningClause({ ...defaultQueryArgs, ...args }), update: (args: IQueryArgs): Promise => returningClause({ ...defaultQueryArgs, ...args }), delete: (args: IQueryArgs): Promise => returningClause({ ...defaultQueryArgs, ...args }), }; };