@saltcorn/sql 0.1.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/LICENSE +21 -0
- package/README.md +14 -0
- package/index.js +140 -0
- package/package.json +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Saltcorn
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# sql
|
|
2
|
+
|
|
3
|
+
Actions and views based on SQL
|
|
4
|
+
|
|
5
|
+
### SQLView
|
|
6
|
+
|
|
7
|
+
Use this view to create HTML code views of the results of arbitrary SQL queries
|
|
8
|
+
|
|
9
|
+
1. Create a view of this type.
|
|
10
|
+
2. Set output type = Table or JSON to begin with
|
|
11
|
+
3. Start by creating your SQL query with no qualifiers (which will come from the URL query state). The preview table should update as you type
|
|
12
|
+
4. When you are happy with the SQL query, switch to output type = HTML
|
|
13
|
+
5. Create your HTML cod, use `{{ rows }}` to access rows. For instance if your query is an aggregation with a single row result (e.g. `SELECT COUNT(*) FROM...`), access this with `{{ rows[0].count }}`, for example `<h2>{{ rows[0].count }}</h2>`. You can also loop, e.g. `{{# for(const row of rows) { }}`
|
|
14
|
+
6. When you are happy with both you are SQL and HTML code, Think about whether you need any parameters from the state. List these comma-separated, in order and use in the SQL code as `$1`, `$2` etc. Example SQL code: `select * from _sc_config where key = $1;`
|
package/index.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
const db = require("@saltcorn/data/db");
|
|
2
|
+
const Form = require("@saltcorn/data/models/form");
|
|
3
|
+
const Field = require("@saltcorn/data/models/field");
|
|
4
|
+
const Table = require("@saltcorn/data/models/table");
|
|
5
|
+
const FieldRepeat = require("@saltcorn/data/models/fieldrepeat");
|
|
6
|
+
const Workflow = require("@saltcorn/data/models/workflow");
|
|
7
|
+
const { eval_expression } = require("@saltcorn/data/models/expression");
|
|
8
|
+
const {
|
|
9
|
+
text,
|
|
10
|
+
div,
|
|
11
|
+
h5,
|
|
12
|
+
style,
|
|
13
|
+
a,
|
|
14
|
+
script,
|
|
15
|
+
pre,
|
|
16
|
+
domReady,
|
|
17
|
+
i,
|
|
18
|
+
text_attr,
|
|
19
|
+
} = require("@saltcorn/markup/tags");
|
|
20
|
+
const { mkTable } = require("@saltcorn/markup");
|
|
21
|
+
const { readState } = require("@saltcorn/data/plugin-helper");
|
|
22
|
+
|
|
23
|
+
const _ = require("underscore");
|
|
24
|
+
|
|
25
|
+
const configuration_workflow = () =>
|
|
26
|
+
new Workflow({
|
|
27
|
+
steps: [
|
|
28
|
+
{
|
|
29
|
+
name: "views",
|
|
30
|
+
form: async (context) => {
|
|
31
|
+
return new Form({
|
|
32
|
+
fields: [
|
|
33
|
+
{
|
|
34
|
+
name: "sql",
|
|
35
|
+
label: "SQL",
|
|
36
|
+
input_type: "code",
|
|
37
|
+
attributes: { mode: "text/x-sql" },
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
name: "state_parameters",
|
|
41
|
+
label: "State parameters",
|
|
42
|
+
sublabel:
|
|
43
|
+
"Comma separated list of state variables from URL querystring to use as SQL query parameters",
|
|
44
|
+
type: "String",
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
name: "output_type",
|
|
48
|
+
label: "Output type",
|
|
49
|
+
type: "String",
|
|
50
|
+
required: true,
|
|
51
|
+
attributes: { options: ["Table", "JSON", "HTML"] },
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
name: "html_code",
|
|
55
|
+
label: "HTML Code",
|
|
56
|
+
input_type: "code",
|
|
57
|
+
attributes: { mode: "text/html" },
|
|
58
|
+
showIf: { output_type: "HTML" },
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
input_type: "section_header",
|
|
62
|
+
label: " ",
|
|
63
|
+
sublabel: div(
|
|
64
|
+
"Use handlebars to access query result in the <code>rows</code> variable. Example: <code>{{#each rows}}<h1>{{this.name}}</h1>{{/each}}</code>"
|
|
65
|
+
),
|
|
66
|
+
showIf: { row_count: "Many" },
|
|
67
|
+
},
|
|
68
|
+
],
|
|
69
|
+
});
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const get_state_fields = () => [];
|
|
76
|
+
|
|
77
|
+
const run = async (
|
|
78
|
+
table_id,
|
|
79
|
+
viewname,
|
|
80
|
+
{ sql, output_type, state_parameters, html_code },
|
|
81
|
+
state,
|
|
82
|
+
extraArgs
|
|
83
|
+
) => {
|
|
84
|
+
const is_sqlite = db.isSQLite;
|
|
85
|
+
|
|
86
|
+
const phValues = [];
|
|
87
|
+
(state_parameters || "")
|
|
88
|
+
.split(",")
|
|
89
|
+
.filter((s) => s)
|
|
90
|
+
.forEach((sp0) => {
|
|
91
|
+
const sp = sp0.trim();
|
|
92
|
+
if (typeof state[sp] === "undefined") phValues.push(null);
|
|
93
|
+
else phValues.push(state[sp]);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
const client = is_sqlite ? db : await db.getClient();
|
|
97
|
+
await client.query(`BEGIN;`);
|
|
98
|
+
if (!is_sqlite) {
|
|
99
|
+
await client.query(`SET LOCAL search_path TO "${db.getTenantSchema()}";`);
|
|
100
|
+
await client.query(`SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;`);
|
|
101
|
+
}
|
|
102
|
+
const qres = await client.query(sql, phValues);
|
|
103
|
+
|
|
104
|
+
await client.query(`ROLLBACK`);
|
|
105
|
+
|
|
106
|
+
if (!is_sqlite) client.release(true);
|
|
107
|
+
switch (output_type) {
|
|
108
|
+
case "HTML":
|
|
109
|
+
const template = _.template(html_code || "", {
|
|
110
|
+
evaluate: /\{\{#(.+?)\}\}/g,
|
|
111
|
+
interpolate: /\{\{([^#].+?)\}\}/g,
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
return template({ rows: qres.rows });
|
|
115
|
+
|
|
116
|
+
case "JSON":
|
|
117
|
+
return `<pre>${JSON.stringify(qres.rows, null, 2)}</pre>`;
|
|
118
|
+
|
|
119
|
+
default: //Table
|
|
120
|
+
return mkTable(
|
|
121
|
+
qres.fields.map((field) => ({ label: field.name, key: field.name })),
|
|
122
|
+
qres.rows
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
module.exports = {
|
|
128
|
+
sc_plugin_api_version: 1,
|
|
129
|
+
plugin_name: "sql",
|
|
130
|
+
viewtemplates: [
|
|
131
|
+
{
|
|
132
|
+
name: "SQLView",
|
|
133
|
+
display_state_form: false,
|
|
134
|
+
tableless: true,
|
|
135
|
+
get_state_fields,
|
|
136
|
+
configuration_workflow,
|
|
137
|
+
run,
|
|
138
|
+
},
|
|
139
|
+
],
|
|
140
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@saltcorn/sql",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Actions and views based on SQL",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"@saltcorn/markup": "^0.6.0",
|
|
8
|
+
"@saltcorn/data": "^0.6.0",
|
|
9
|
+
"underscore": "1.13.6"
|
|
10
|
+
},
|
|
11
|
+
"author": "Tom Nielsen",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"eslintConfig": {
|
|
14
|
+
"extends": "eslint:recommended",
|
|
15
|
+
"parserOptions": {
|
|
16
|
+
"ecmaVersion": 2020
|
|
17
|
+
},
|
|
18
|
+
"env": {
|
|
19
|
+
"node": true,
|
|
20
|
+
"es6": true
|
|
21
|
+
},
|
|
22
|
+
"rules": {
|
|
23
|
+
"no-unused-vars": "off",
|
|
24
|
+
"no-case-declarations": "off",
|
|
25
|
+
"no-empty": "warn",
|
|
26
|
+
"no-fallthrough": "warn"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|