@saltcorn/sql 0.4.2 → 0.4.4

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 (3) hide show
  1. package/action.js +12 -4
  2. package/index.js +16 -11
  3. package/package.json +1 -1
package/action.js CHANGED
@@ -20,6 +20,13 @@ module.exports = {
20
20
  "Comma separated list of row variables to use as SQL query parameters. User variables can be used as <code>user.id</code> etc",
21
21
  type: "String",
22
22
  },
23
+ {
24
+ name: "read_only",
25
+ label: "Read only",
26
+ sublabel: "Run the SQL in a read-only transactions",
27
+ type: "Bool",
28
+ },
29
+
23
30
  ...(mode === "workflow"
24
31
  ? [
25
32
  {
@@ -35,7 +42,7 @@ module.exports = {
35
42
  ],
36
43
  run: async ({
37
44
  row,
38
- configuration: { sql, row_parameters, results_variable },
45
+ configuration: { sql, row_parameters, read_only, results_variable },
39
46
  user,
40
47
  mode,
41
48
  }) => {
@@ -59,9 +66,10 @@ module.exports = {
59
66
  await client.query(
60
67
  `SET LOCAL search_path TO "${db.getTenantSchema()}";`
61
68
  );
62
- await client.query(
63
- `SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;`
64
- );
69
+ if (read_only)
70
+ await client.query(
71
+ `SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;`
72
+ );
65
73
  }
66
74
  const qres = await client.query(sql, phValues);
67
75
 
package/index.js CHANGED
@@ -96,18 +96,23 @@ const run = async (
96
96
  } else if (typeof state[sp] === "undefined") phValues.push(null);
97
97
  else phValues.push(state[sp]);
98
98
  });
99
-
100
- const client = is_sqlite ? db : await db.getClient();
101
- await client.query(`BEGIN;`);
102
- if (!is_sqlite) {
103
- await client.query(`SET LOCAL search_path TO "${db.getTenantSchema()}";`);
104
- await client.query(`SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;`);
99
+ let qres, client;
100
+ try {
101
+ client = is_sqlite ? db : await db.getClient();
102
+ await client.query(`BEGIN;`);
103
+ if (!is_sqlite) {
104
+ await client.query(`SET LOCAL search_path TO "${db.getTenantSchema()}";`);
105
+ await client.query(
106
+ `SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;`
107
+ );
108
+ }
109
+ qres = await client.query(sql, phValues);
110
+ } catch (e) {
111
+ throw e;
112
+ } finally {
113
+ await client.query(`ROLLBACK;`);
114
+ if (!is_sqlite) client.release(true);
105
115
  }
106
- const qres = await client.query(sql, phValues);
107
-
108
- await client.query(`ROLLBACK;`);
109
-
110
- if (!is_sqlite) client.release(true);
111
116
  switch (output_type) {
112
117
  case "HTML":
113
118
  const template = _.template(html_code || "", {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/sql",
3
- "version": "0.4.2",
3
+ "version": "0.4.4",
4
4
  "description": "Actions and views based on SQL",
5
5
  "main": "index.js",
6
6
  "dependencies": {