fix(deno-client): wrap the deno-postgres client and not the query statement

This commit is contained in:
Ruben Fiszel
2022-08-19 13:26:35 +02:00
parent 8c04558c4e
commit 68aaf3267c
+25 -8
View File
@@ -1,14 +1,31 @@
import { Client } from "https://deno.land/x/postgres@v0.16.1/mod.ts"
import { type Resource, type Sql } from "./mod.ts"
import { type Resource } from "./mod.ts"
export async function pgQuery(
db: Resource<"postgresql">,
query: Sql
/**
* deno-postgres client API is very flexible:
* https://deno.land/x/postgres@v0.16.1/mod.ts?s=QueryClient
*
* @param db the postgresql resource to generate the client for
*
* @returns the client for the resource
*
* Usage:
* Static query:
* ```ts
* const {rows} = await pgClient(db).queryObject(
* "SELECT ID, NAME FROM CLIENTS"
* );
* ```
*
* Prepared Statements:
* ```ts
* const { rows } = await pgClient(db).queryObject`SELECT ID, NAME FROM CLIENTS WHERE ID = ${id}`;
* ```
*/
export function pgClient(
db: Resource<"postgresql">
) {
db.database = db.dbname
db.hostname = db.host
const returned_query = await new Client(db).queryObject(query)
return returned_query.rows
return new Client(db)
}