import { Client } from "pg";
import { getLogger } from "@cocalc/backend/logger";
const L = getLogger("db:set-pg-params").debug;
interface Opts {
client: Client;
query: string;
params: string[];
pg_params: { [key: string]: string };
cb: (err?, result?) => void;
}
export async function do_query_with_pg_params(opts: Opts): Promise<void> {
const { client, query, params, pg_params, cb } = opts;
try {
await client.query("BEGIN");
for (const [k, v] of Object.entries(pg_params)) {
const q = `SET LOCAL ${k} TO ${v}`;
L(`Setting query param: ${k}=${v}`);
await client.query(q);
}
const res = await client.query(query, params);
await client.query("COMMIT");
cb(undefined, res);
} catch (err) {
L(`ROLLBACK -- err=${err}`);
await client.query("ROLLBACK");
cb(err);
}
}