@saltcorn/history-control 0.2.1 → 0.2.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.
- package/package.json +1 -1
- package/table-provider.js +30 -39
package/package.json
CHANGED
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,38 @@ const configuration_workflow = (req) =>
|
|
|
34
35
|
},
|
|
35
36
|
],
|
|
36
37
|
});
|
|
37
|
-
const runQuery = async (cfg,
|
|
38
|
+
const runQuery = async (cfg, whereFull, opts) => {
|
|
39
|
+
if (whereFull?._fts?.fields) {
|
|
40
|
+
whereFull._fts.fields = whereFull?._fts?.fields.filter(
|
|
41
|
+
(f) => f.name !== "_version_id" && f.name !== "_deleted"
|
|
42
|
+
);
|
|
43
|
+
}
|
|
38
44
|
const table = Table.findOne({ name: cfg.table });
|
|
39
|
-
const wheres = [];
|
|
40
|
-
let phIndex = 1;
|
|
41
|
-
const phValues = [];
|
|
42
45
|
|
|
43
46
|
const schemaPrefix = db.getTenantSchemaPrefix();
|
|
47
|
+
const { _is_latest, _deleted, ...whereRest } = whereFull;
|
|
48
|
+
|
|
49
|
+
let { where, values } = mkWhere(whereRest || {});
|
|
44
50
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
}
|
|
51
|
+
if (_is_latest) {
|
|
52
|
+
where = `${
|
|
53
|
+
where ? where + " and" : "where"
|
|
54
|
+
} h._version = (select max(ih._version) from ${schemaPrefix}"${db.sqlsanitize(
|
|
55
|
+
table.name
|
|
56
|
+
)}__history" ih where ih.id = h.id)`;
|
|
77
57
|
}
|
|
58
|
+
if (_deleted === false || _deleted === "false")
|
|
59
|
+
where = `${
|
|
60
|
+
where ? where + " and " : "where"
|
|
61
|
+
} exists(select id from ${schemaPrefix}"${db.sqlsanitize(
|
|
62
|
+
table.name
|
|
63
|
+
)}" t where t.id = h.id)`;
|
|
64
|
+
else if (_deleted)
|
|
65
|
+
where = `${
|
|
66
|
+
where ? where + " and " : "where"
|
|
67
|
+
} not exists(select id from ${schemaPrefix}"${db.sqlsanitize(
|
|
68
|
+
table.name
|
|
69
|
+
)}" t where t.id = h.id)`;
|
|
78
70
|
|
|
79
71
|
const sql = `select
|
|
80
72
|
_version || '_'|| id as _version_id,
|
|
@@ -85,10 +77,9 @@ const runQuery = async (cfg, where, opts) => {
|
|
|
85
77
|
table.name
|
|
86
78
|
)}" t where t.id = h.id) as _deleted,
|
|
87
79
|
* from ${schemaPrefix}"${db.sqlsanitize(table.name)}__history" h ${
|
|
88
|
-
|
|
80
|
+
where.length ? ` ${where}` : ""
|
|
89
81
|
}`;
|
|
90
|
-
|
|
91
|
-
return await db.query(sql, phValues);
|
|
82
|
+
return await db.query(sql, values);
|
|
92
83
|
};
|
|
93
84
|
module.exports = {
|
|
94
85
|
"History for database table": {
|