From 5900a03c045861732bbf6f7bff1280f3c94b86ce Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Sat, 20 Aug 2022 12:02:56 +0200 Subject: [PATCH] fix(deno-client): pg module now supports prepared statements --- deno-client/mod.ts | 2 +- deno-client/pg.ts | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/deno-client/mod.ts b/deno-client/mod.ts index 457b7d6d6e..f8e0983d2f 100644 --- a/deno-client/mod.ts +++ b/deno-client/mod.ts @@ -7,9 +7,9 @@ export { UserApi, WorkspaceApi } from './windmill-api/index.ts' +export type Sql = string export type Email = string export type Base64 = string -export type Sql = string export type Resource = any /** diff --git a/deno-client/pg.ts b/deno-client/pg.ts index 5f79c056eb..0401e64571 100644 --- a/deno-client/pg.ts +++ b/deno-client/pg.ts @@ -29,3 +29,27 @@ export function pgClient( db.hostname = db.host return new Client(db) } + +/** + * 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 rows corresponding to the returned objetcs + * + * Prepared Statements: + * ```ts + * const { rows } = await pgSql(db)`SELECT ID, NAME FROM CLIENTS WHERE ID = ${id}`; + * ``` + */ +export function pgSql( + db: Resource<"postgresql"> +) { + return async function queryObject( + query: TemplateStringsArray, + ...args: unknown[] + ) { + return await pgClient(db).queryObject(query, args) + } +}