@cocalc/database
Version:
CoCalc: code for working with our PostgreSQL database
34 lines • 1.33 kB
JavaScript
;
/*
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
* License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.do_query_with_pg_params = void 0;
const logger_1 = require("@cocalc/backend/logger");
const L = (0, logger_1.getLogger)("db:set-pg-params").debug;
// Run the actual query after a setup query in a transaction; this is
// used by __do_query in postgres-base
async function do_query_with_pg_params(opts) {
const { client, query, params, pg_params, cb } = opts;
try {
await client.query("BEGIN");
for (const [k, v] of Object.entries(pg_params)) {
// SET LOCAL: only for this transaction!
// NOTE: interestingly, $1, $2 params do not work … but this isn't user-facing
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);
}
}
exports.do_query_with_pg_params = do_query_with_pg_params;
//# sourceMappingURL=set-pg-params.js.map