@saltcorn/postgres 1.1.2-beta.9 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/postgres.js +9 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/postgres",
3
- "version": "1.1.2-beta.9",
3
+ "version": "1.1.2",
4
4
  "description": "Postgres structures for Saltcorn, open-source no-code platform",
5
5
  "homepage": "https://saltcorn.com",
6
6
  "scripts": {
@@ -12,7 +12,7 @@
12
12
  "license": "MIT",
13
13
  "main": "index.js",
14
14
  "dependencies": {
15
- "@saltcorn/db-common": "1.1.2-beta.9",
15
+ "@saltcorn/db-common": "1.1.2",
16
16
  "pg": "^8.13.1",
17
17
  "pg-copy-streams": "^6.0.6",
18
18
  "replacestream": "4.0.3"
package/postgres.js CHANGED
@@ -23,6 +23,10 @@ let client = null;
23
23
 
24
24
  let log_sql_enabled = false;
25
25
 
26
+ const quote = (s) => `"${s}"`;
27
+
28
+ const ppPK = (pk) => (pk ? quote(pk) : "id");
29
+
26
30
  /**
27
31
  * Control Logging sql statements to console
28
32
  * @param {boolean} [val = true] - if true then log sql statements to console
@@ -242,11 +246,11 @@ const insert = async (tbl, obj, opts = Object.create(null)) => {
242
246
  ? `insert into "${schema}"."${sqlsanitize(
243
247
  tbl
244
248
  )}"(${fnameList}) values(${valPosList.join()}) ${conflict}returning ${
245
- opts.noid ? "*" : opts.pk_name || "id"
249
+ opts.noid ? "*" : ppPK(opts.pk_name)
246
250
  }`
247
251
  : `insert into "${schema}"."${sqlsanitize(
248
252
  tbl
249
- )}" DEFAULT VALUES returning ${opts.noid ? "*" : opts.pk_name || "id"}`;
253
+ )}" DEFAULT VALUES returning ${opts.noid ? "*" : ppPK(opts.pk_name)}`;
250
254
  sql_log(sql, valList);
251
255
  const { rows } = await (client || opts.client || pool).query(sql, valList);
252
256
  if (opts.noid) return;
@@ -274,7 +278,7 @@ const update = async (tbl, obj, id, opts = Object.create(null)) => {
274
278
  valList.push(id === "undefined" ? obj[opts.pk_name || "id"] : id);
275
279
  const q = `update "${getTenantSchema()}"."${sqlsanitize(
276
280
  tbl
277
- )}" set ${assigns} where ${opts.pk_name || "id"}=$${kvs.length + 1}`;
281
+ )}" set ${assigns} where ${ppPK(opts.pk_name)}=$${kvs.length + 1}`;
278
282
  sql_log(q, valList);
279
283
  await (client || opts.client || pool).query(q, valList);
280
284
  };
@@ -346,10 +350,10 @@ const getClient = async () => await pool.connect();
346
350
  * @param {string} tblname - table name
347
351
  * @returns {Promise<void>} no result
348
352
  */
349
- const reset_sequence = async (tblname) => {
353
+ const reset_sequence = async (tblname, pkname = "id") => {
350
354
  const sql = `SELECT setval(pg_get_serial_sequence('"${getTenantSchema()}"."${sqlsanitize(
351
355
  tblname
352
- )}"', 'id'), coalesce(max(id),0) + 1, false) FROM "${getTenantSchema()}"."${sqlsanitize(
356
+ )}"', '${pkname}'), coalesce(max("${pkname}"),0) + 1, false) FROM "${getTenantSchema()}"."${sqlsanitize(
353
357
  tblname
354
358
  )}";`;
355
359
  await (client || pool).query(sql);
@@ -460,7 +464,6 @@ const drop_index = async (table_name, field_name) => {
460
464
  * @returns {Promise<void>} no results
461
465
  */
462
466
  const copyFrom = async (fileStream, tableName, fieldNames, client) => {
463
- const quote = (s) => `"${s}"`;
464
467
  const sql = `COPY "${getTenantSchema()}"."${sqlsanitize(
465
468
  tableName
466
469
  )}" (${fieldNames.map(quote).join(",")}) FROM STDIN CSV HEADER`;