@saltcorn/history-control 0.1.0 → 0.2.0

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/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const { features } = require("@saltcorn/data/db/state");
2
+ const Table = require("@saltcorn/data/models/table");
2
3
 
3
4
  // user subscribe action
4
5
  const actions = {
@@ -18,9 +19,46 @@ const actions = {
18
19
  return { reload_page: true };
19
20
  },
20
21
  },
22
+ restore_from_history: {
23
+ requireRow: true,
24
+ run: async ({ table, row, user }) => {
25
+ if (table.provider_name !== "History for database table")
26
+ return {
27
+ error:
28
+ "Only use this action on tables provided by History for database table",
29
+ };
30
+ const real_table = Table.findOne({ name: table.provider_cfg?.table });
31
+ if (!real_table) return { error: "Table not found" };
32
+ if (!real_table.versioned)
33
+ return { error: "History not enabled for table" };
34
+ if (row._deleted) {
35
+ const insRow = {};
36
+ for (const field of real_table.fields) {
37
+ insRow[field.name] = row[field.name];
38
+ }
39
+ await real_table.insertRow(insRow);
40
+ } else {
41
+ const updRow = {};
42
+ for (const field of real_table.fields) {
43
+ if (!field.primary_key) updRow[field.name] = row[field.name];
44
+ }
45
+ await real_table.updateRow(
46
+ updRow,
47
+ row[real_table.pk_name],
48
+ user,
49
+ false,
50
+ undefined,
51
+ row._version
52
+ );
53
+ }
54
+
55
+ return { reload_page: true };
56
+ },
57
+ },
21
58
  };
22
59
 
23
60
  module.exports = {
24
61
  sc_plugin_api_version: 1,
25
62
  actions: features?.table_undo ? actions : undefined,
63
+ table_providers: require("./table-provider.js"),
26
64
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/history-control",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Allow users to interact with row history",
5
5
  "main": "index.js",
6
6
  "dependencies": {
@@ -0,0 +1,130 @@
1
+ const db = require("@saltcorn/data/db");
2
+ const { eval_expression } = require("@saltcorn/data/models/expression");
3
+ const Workflow = require("@saltcorn/data/models/workflow");
4
+ const Form = require("@saltcorn/data/models/form");
5
+ const FieldRepeat = require("@saltcorn/data/models/fieldrepeat");
6
+ const Field = require("@saltcorn/data/models/field");
7
+ const Table = require("@saltcorn/data/models/table");
8
+ const { getState } = require("@saltcorn/data/db/state");
9
+ const { mkTable } = require("@saltcorn/markup");
10
+ const { pre, code } = require("@saltcorn/markup/tags");
11
+
12
+ const configuration_workflow = (req) =>
13
+ new Workflow({
14
+ steps: [
15
+ {
16
+ name: "query",
17
+ form: async () => {
18
+ const tables = await Table.find({ versioned: true });
19
+ return new Form({
20
+ fields: [
21
+ {
22
+ name: "table",
23
+ label: "Table",
24
+ type: "String",
25
+ required: true,
26
+ attributes: {
27
+ options: tables.map((t) => t.name),
28
+ },
29
+ sublabel: "Select a versioned table",
30
+ },
31
+ ],
32
+ });
33
+ },
34
+ },
35
+ ],
36
+ });
37
+ const runQuery = async (cfg, where, opts) => {
38
+ const table = Table.findOne({ name: cfg.table });
39
+ const wheres = [];
40
+ let phIndex = 1;
41
+ const phValues = [];
42
+
43
+ const schemaPrefix = db.getTenantSchemaPrefix();
44
+
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
+ }
77
+ }
78
+
79
+ const sql = `select
80
+ _version || '_'|| id as _version_id,
81
+ _version = (select max(ih._version) from ${schemaPrefix}"${db.sqlsanitize(
82
+ table.name
83
+ )}__history" ih where ih.id = h.id) as _is_latest,
84
+ not exists(select id from ${schemaPrefix}"${db.sqlsanitize(
85
+ table.name
86
+ )}" t where t.id = h.id) as _deleted,
87
+ * from ${schemaPrefix}"${db.sqlsanitize(table.name)}__history" h ${
88
+ wheres.length ? ` where ${wheres.join(" AND")}` : ""
89
+ }`;
90
+
91
+ return await db.query(sql, phValues);
92
+ };
93
+ module.exports = {
94
+ "History for database table": {
95
+ configuration_workflow,
96
+ fields: (cfg) => {
97
+ if (!cfg?.table) return [];
98
+
99
+ const table = Table.findOne({ name: cfg.table });
100
+ return [
101
+ { name: "_version_id", type: "String", primary_key: true },
102
+ ...table.fields.map((f) => {
103
+ f.primary_key = false;
104
+ f.validator = undefined;
105
+ if (f.is_fkey) f.type = "Integer";
106
+ else f.type = f.type?.name || f.type;
107
+ return f;
108
+ }),
109
+ { name: "_version", label: "Version", type: "Integer" },
110
+ { name: "_is_latest", label: "Is latest", type: "Bool" },
111
+ { name: "_deleted", label: "Deleted", type: "Bool" },
112
+ { name: "_time", label: "Time", type: "Date" },
113
+ { name: "_userid", label: "User ID", type: "Integer" },
114
+ {
115
+ name: "_restore_of_version",
116
+ label: "Restore of version",
117
+ type: "Integer",
118
+ },
119
+ ];
120
+ },
121
+ get_table: (cfg) => {
122
+ return {
123
+ getRows: async (where, opts) => {
124
+ const qres = await runQuery(cfg, where, opts);
125
+ return qres.rows;
126
+ },
127
+ };
128
+ },
129
+ },
130
+ };