@saltcorn/history-control 0.2.1 → 0.2.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 +1 -1
  2. package/table-provider.js +25 -39
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/history-control",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Allow users to interact with row history",
5
5
  "main": "index.js",
6
6
  "dependencies": {
package/table-provider.js CHANGED
@@ -8,6 +8,7 @@ const Table = require("@saltcorn/data/models/table");
8
8
  const { getState } = require("@saltcorn/data/db/state");
9
9
  const { mkTable } = require("@saltcorn/markup");
10
10
  const { pre, code } = require("@saltcorn/markup/tags");
11
+ const { mkWhere } = require("@saltcorn/db-common/internal");
11
12
 
12
13
  const configuration_workflow = (req) =>
13
14
  new Workflow({
@@ -34,47 +35,33 @@ const configuration_workflow = (req) =>
34
35
  },
35
36
  ],
36
37
  });
37
- const runQuery = async (cfg, where, opts) => {
38
+ const runQuery = async (cfg, whereFull, opts) => {
38
39
  const table = Table.findOne({ name: cfg.table });
39
- const wheres = [];
40
- let phIndex = 1;
41
- const phValues = [];
42
40
 
43
41
  const schemaPrefix = db.getTenantSchemaPrefix();
42
+ const { _is_latest, _deleted, ...whereRest } = whereFull;
44
43
 
45
- for (const k of Object.keys(where)) {
46
- if (k === "_is_latest" && where[k]) {
47
- wheres.push(
48
- `h._version = (select max(ih._version) from ${schemaPrefix}"${db.sqlsanitize(
49
- table.name
50
- )}__history" ih where ih.id = h.id)`
51
- );
52
- continue;
53
- }
54
- if (k === "_deleted") {
55
- if (where[k])
56
- wheres.push(
57
- `not exists(select id from ${schemaPrefix}"${db.sqlsanitize(
58
- table.name
59
- )}" t where t.id = h.id)`
60
- );
61
- else
62
- wheres.push(
63
- `exists(select id from ${schemaPrefix}"${db.sqlsanitize(
64
- table.name
65
- )}" t where t.id = h.id)`
66
- );
67
- continue;
68
- }
69
- const f = table.getField(k);
70
- if (f) {
71
- wheres.push(
72
- where[k]?.ilike ? `"${k}" ILIKE $${phIndex}` : `"${k}" = $${phIndex}`
73
- );
74
- phValues.push(where[k]?.ilike ? where[k]?.ilike : where[k]);
75
- phIndex += 1;
76
- }
44
+ let { where, values } = mkWhere(whereRest || {});
45
+
46
+ if (_is_latest) {
47
+ where = `${
48
+ where ? where + " and" : "where"
49
+ } h._version = (select max(ih._version) from ${schemaPrefix}"${db.sqlsanitize(
50
+ table.name
51
+ )}__history" ih where ih.id = h.id)`;
77
52
  }
53
+ if (_deleted === false || _deleted === "false")
54
+ where = `${
55
+ where ? where + " and " : "where"
56
+ } exists(select id from ${schemaPrefix}"${db.sqlsanitize(
57
+ table.name
58
+ )}" t where t.id = h.id)`;
59
+ else if (_deleted)
60
+ where = `${
61
+ where ? where + " and " : "where"
62
+ } not exists(select id from ${schemaPrefix}"${db.sqlsanitize(
63
+ table.name
64
+ )}" t where t.id = h.id)`;
78
65
 
79
66
  const sql = `select
80
67
  _version || '_'|| id as _version_id,
@@ -85,10 +72,9 @@ const runQuery = async (cfg, where, opts) => {
85
72
  table.name
86
73
  )}" t where t.id = h.id) as _deleted,
87
74
  * from ${schemaPrefix}"${db.sqlsanitize(table.name)}__history" h ${
88
- wheres.length ? ` where ${wheres.join(" AND")}` : ""
75
+ where.length ? ` ${where}` : ""
89
76
  }`;
90
-
91
- return await db.query(sql, phValues);
77
+ return await db.query(sql, values);
92
78
  };
93
79
  module.exports = {
94
80
  "History for database table": {