@saltcorn/postgres 1.1.4-alpha.1 → 1.1.4-alpha.3

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 +21 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/postgres",
3
- "version": "1.1.4-alpha.1",
3
+ "version": "1.1.4-alpha.3",
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.4-alpha.1",
15
+ "@saltcorn/db-common": "1.1.4-alpha.3",
16
16
  "pg": "^8.13.1",
17
17
  "pg-copy-streams": "^6.0.6",
18
18
  "replacestream": "4.0.3"
package/postgres.js CHANGED
@@ -536,26 +536,42 @@ const listScTables = async () => {
536
536
  /* rules of using this:
537
537
 
538
538
  - no try catch inside unless you rethrow: wouldnt roll back
539
- - no res.json, res.redirect etc inside (client is released on res finish event)
540
539
  - no state.refresh_*() inside: other works wouldnt see updates as they are in transactioon
541
540
  - you can use state.refresh_*(true) for update on own worker only
542
541
 
543
542
  */
544
543
  const withTransaction = async (f, onError) => {
545
- await query("BEGIN;");
544
+ const client = await getClient();
545
+ const reqCon = getRequestContext();
546
+ if (reqCon)
547
+ //if not, probably in a test
548
+ reqCon.client = client;
549
+ sql_log("BEGIN;");
550
+ await client.query("BEGIN;");
546
551
  let aborted = false;
547
552
  const rollback = async () => {
548
553
  aborted = true;
549
- await query("ROLLBACK;");
554
+ sql_log("ROLLBACK;");
555
+ await client.query("ROLLBACK;");
550
556
  };
551
557
  try {
552
558
  const result = await f(rollback);
553
- if (!aborted) await query("COMMIT;");
559
+
560
+ if (!aborted) {
561
+ sql_log("COMMIT;");
562
+ await client.query("COMMIT;");
563
+ }
554
564
  return result;
555
565
  } catch (error) {
556
- if (!aborted) await query("ROLLBACK;");
566
+ if (!aborted) {
567
+ sql_log("ROLLBACK;");
568
+ await client.query("ROLLBACK;");
569
+ }
557
570
  if (onError) return onError(error);
558
571
  else throw error;
572
+ } finally {
573
+ if (reqCon) reqCon.client = null;
574
+ client.release();
559
575
  }
560
576
  };
561
577