@aaqu/fromcubes-db 0.0.0-stage → 0.1.1
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 +201 -0
- package/NOTICE +24 -0
- package/README.md +442 -2
- package/SECURITY.md +75 -0
- package/THIRD-PARTY-NOTICES.md +151 -0
- package/examples/Quick Start/All Databases.json +1719 -0
- package/examples/Quick Start/ClickHouse.json +278 -0
- package/examples/Quick Start/CockroachDB.json +280 -0
- package/examples/Quick Start/Firebird.json +278 -0
- package/examples/Quick Start/MariaDB.json +281 -0
- package/examples/Quick Start/MySQL.json +281 -0
- package/examples/Quick Start/Oracle.json +280 -0
- package/examples/Quick Start/PostgreSQL.json +280 -0
- package/examples/Quick Start/SQL Server.json +282 -0
- package/examples/Quick Start/SQLite.json +278 -0
- package/examples/Quick Start/TiDB.json +281 -0
- package/examples/Quick Start/YugabyteDB.json +280 -0
- package/examples/SQLite Sensor Log With Portal.json +391 -0
- package/examples/Select With Parameters.json +130 -0
- package/index.js +32 -0
- package/lib/adapters/clickhouse.js +254 -0
- package/lib/adapters/firebird.js +416 -0
- package/lib/adapters/index.js +37 -0
- package/lib/adapters/mssql.js +178 -0
- package/lib/adapters/mysql.js +171 -0
- package/lib/adapters/oracle.js +272 -0
- package/lib/adapters/postgres.js +161 -0
- package/lib/adapters/read.js +57 -0
- package/lib/adapters/sqlite.js +193 -0
- package/lib/adapters/time.js +89 -0
- package/lib/bound.js +29 -0
- package/lib/editor-api.js +134 -0
- package/lib/editor-preview.js +191 -0
- package/lib/errors.js +66 -0
- package/lib/params.js +966 -0
- package/lib/pool.js +391 -0
- package/lib/settings.js +302 -0
- package/lib/sql/compile.js +160 -0
- package/lib/sql/dialect.js +194 -0
- package/lib/sql/parse.js +202 -0
- package/lib/sql/types.js +1071 -0
- package/lib/sqlite-files.js +137 -0
- package/nodes/connection.html +728 -0
- package/nodes/connection.js +179 -0
- package/nodes/editor/ark-pixel.LICENSE.txt +96 -0
- package/nodes/editor/ark-pixel.woff2 +0 -0
- package/nodes/editor/mark.svg +5 -0
- package/nodes/editor/sql-editor.css +604 -0
- package/nodes/editor/sql-editor.js +1490 -0
- package/nodes/editor/sql-model.js +203 -0
- package/nodes/icons/fromcubes-db.svg +24 -0
- package/nodes/query.html +662 -0
- package/nodes/query.js +195 -0
- package/package.json +81 -4
package/nodes/query.js
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { STATES } = require("../lib/pool");
|
|
4
|
+
const { bindParam } = require("../lib/params");
|
|
5
|
+
const { registerEditorApi, offerMessage, forgetNode, ASSET_BASE } = require("../lib/editor-api");
|
|
6
|
+
|
|
7
|
+
module.exports = function (RED) {
|
|
8
|
+
// The SQL Editor (nodes/editor/): its files, Monaco, the preview and capture.
|
|
9
|
+
registerEditorApi(RED);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Runs the SQL typed into the edit dialog. Values only ever arrive through
|
|
13
|
+
* the parameter list and are bound by the driver, so no message content can
|
|
14
|
+
* change the shape of the statement.
|
|
15
|
+
*/
|
|
16
|
+
function FromcubesDbQueryNode(config) {
|
|
17
|
+
RED.nodes.createNode(this, config);
|
|
18
|
+
const node = this;
|
|
19
|
+
|
|
20
|
+
node.sql = config.sql || "";
|
|
21
|
+
node.params = Array.isArray(config.params) ? config.params : [];
|
|
22
|
+
node.output = config.output || "rows";
|
|
23
|
+
node.target = config.target || "payload";
|
|
24
|
+
node.timeoutMs = config.timeoutMs;
|
|
25
|
+
|
|
26
|
+
const connection = RED.nodes.getNode(config.connection);
|
|
27
|
+
if (!connection || typeof connection.acquire !== "function") {
|
|
28
|
+
node.status({ fill: "red", shape: "ring", text: "no connection configured" });
|
|
29
|
+
node.error("No database connection configured");
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const pool = connection.acquire();
|
|
34
|
+
let busy = 0;
|
|
35
|
+
|
|
36
|
+
// A ring for the shared connection, a dot for what this node did. Several
|
|
37
|
+
// query nodes share one pool, so the pool reaching READY paints every one
|
|
38
|
+
// of them at once — and with a filled dot that reads as "I just ran a
|
|
39
|
+
// query" on nodes that ran nothing. Node-RED's own convention separates the
|
|
40
|
+
// two: hollow is idle, filled is activity.
|
|
41
|
+
const showIdleStatus = (state, detail) => {
|
|
42
|
+
if (busy > 0) return;
|
|
43
|
+
if (state === STATES.READY) node.status({ fill: "green", shape: "ring", text: "connected" });
|
|
44
|
+
else if (state === STATES.FAILED) {
|
|
45
|
+
node.status({ fill: "red", shape: "ring", text: detail ? detail.message : "disconnected" });
|
|
46
|
+
} else if (state === STATES.CLOSED) node.status({});
|
|
47
|
+
else node.status({ fill: "grey", shape: "ring", text: state });
|
|
48
|
+
};
|
|
49
|
+
// Keeping the unsubscribe handle is what stops listeners from piling up on
|
|
50
|
+
// the shared pool across redeploys.
|
|
51
|
+
const unsubscribe = pool.onStatus(showIdleStatus);
|
|
52
|
+
|
|
53
|
+
if (node.sql.trim() === "") {
|
|
54
|
+
node.status({ fill: "red", shape: "ring", text: "no SQL configured" });
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
node.on("input", function (msg, send, done) {
|
|
58
|
+
send =
|
|
59
|
+
send ||
|
|
60
|
+
function (...args) {
|
|
61
|
+
node.send(...args);
|
|
62
|
+
};
|
|
63
|
+
const fail = done || ((err) => node.error(err, msg));
|
|
64
|
+
|
|
65
|
+
// Before anything sets the result on it: the SQL Editor wants the input.
|
|
66
|
+
offerMessage(node.id, msg, RED.util.cloneMessage);
|
|
67
|
+
|
|
68
|
+
busy++;
|
|
69
|
+
node.status({ fill: "blue", shape: "dot", text: "querying" });
|
|
70
|
+
|
|
71
|
+
resolveParams(node.params, msg)
|
|
72
|
+
.then((values) => pool.query(node.sql, values, { timeoutMs: toNumber(node.timeoutMs) }))
|
|
73
|
+
.then((result) => {
|
|
74
|
+
deliver(result, msg, send);
|
|
75
|
+
node.status({
|
|
76
|
+
fill: "green",
|
|
77
|
+
shape: "dot",
|
|
78
|
+
// null: the database does not say how many rows a statement
|
|
79
|
+
// changed (ClickHouse UPDATE/DELETE) — "done", not a made-up number.
|
|
80
|
+
text:
|
|
81
|
+
result.rowCount === null || result.rowCount === undefined
|
|
82
|
+
? "done"
|
|
83
|
+
: `${result.rowCount} row${result.rowCount === 1 ? "" : "s"}`,
|
|
84
|
+
});
|
|
85
|
+
if (done) done();
|
|
86
|
+
})
|
|
87
|
+
.catch((err) => {
|
|
88
|
+
node.status({ fill: "red", shape: "ring", text: err.message });
|
|
89
|
+
fail(err);
|
|
90
|
+
})
|
|
91
|
+
.finally(() => {
|
|
92
|
+
busy = Math.max(0, busy - 1);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
node.on("close", function (removed, done) {
|
|
97
|
+
const finish = typeof done === "function" ? done : removed;
|
|
98
|
+
unsubscribe();
|
|
99
|
+
forgetNode(node.id);
|
|
100
|
+
node.status({});
|
|
101
|
+
// Releasing drops the pool's reference count; the last node out closes it.
|
|
102
|
+
pool
|
|
103
|
+
.release()
|
|
104
|
+
.catch((err) => node.error(`Error releasing connection: ${err.message}`))
|
|
105
|
+
.then(() => finish());
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
function deliver(result, msg, send) {
|
|
109
|
+
if (node.output === "msgPerRow") {
|
|
110
|
+
// One message per row, each a clone, so downstream mutation of one row
|
|
111
|
+
// cannot corrupt the others.
|
|
112
|
+
for (const row of result.rows) {
|
|
113
|
+
const out = RED.util.cloneMessage(msg);
|
|
114
|
+
RED.util.setMessageProperty(out, node.target, row, true);
|
|
115
|
+
out.rowCount = result.rowCount;
|
|
116
|
+
if (result.insertId !== undefined) out.insertId = result.insertId;
|
|
117
|
+
send(out);
|
|
118
|
+
}
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
let value;
|
|
123
|
+
if (node.output === "single") value = result.rows.length > 0 ? result.rows[0] : null;
|
|
124
|
+
else if (node.output === "count") value = result.rowCount;
|
|
125
|
+
else value = result.rows;
|
|
126
|
+
|
|
127
|
+
RED.util.setMessageProperty(msg, node.target, value, true);
|
|
128
|
+
msg.rowCount = result.rowCount;
|
|
129
|
+
// MySQL and SQLite report the generated key in the same result as the
|
|
130
|
+
// INSERT, so it is the row this statement created. Reading it with a
|
|
131
|
+
// second query node would not be — that may take another pooled
|
|
132
|
+
// connection and hand back someone else's id.
|
|
133
|
+
if (result.insertId !== undefined) msg.insertId = result.insertId;
|
|
134
|
+
send(msg);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Turns the parameter rows from the dialog into a plain value bag. Each row
|
|
139
|
+
* is a standard typedInput, so msg./flow./global./env./JSONata all work.
|
|
140
|
+
*/
|
|
141
|
+
function resolveParams(params, msg) {
|
|
142
|
+
return Promise.all(
|
|
143
|
+
params.map(
|
|
144
|
+
(param) =>
|
|
145
|
+
new Promise((resolve, reject) => {
|
|
146
|
+
const name = String(param.name || "").trim();
|
|
147
|
+
if (name === "") {
|
|
148
|
+
reject(new Error("A parameter is missing its name"));
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
RED.util.evaluateNodeProperty(param.value, param.type || "str", node, msg, (err, value) => {
|
|
152
|
+
if (err) {
|
|
153
|
+
reject(new Error(`Parameter :${name} — ${err.message}`));
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
// param.as is the declared SQL-side type, if the user set one.
|
|
157
|
+
// The product comes from the connection, so `varchar(50)`
|
|
158
|
+
// means 50 characters on PostgreSQL and 50 bytes on SQL
|
|
159
|
+
// Server, as each of them defines it.
|
|
160
|
+
try {
|
|
161
|
+
resolve([name, bindParam(name, value, declaredType(param), connection.dialect)]);
|
|
162
|
+
} catch (coerceErr) {
|
|
163
|
+
reject(coerceErr);
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
}),
|
|
167
|
+
),
|
|
168
|
+
).then((entries) => Object.fromEntries(entries));
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* The dialog keeps the type name and its arguments in two fields, so that the
|
|
174
|
+
* picker can stay a picker. `varchar` + `50` is the type `varchar(50)`.
|
|
175
|
+
*/
|
|
176
|
+
function declaredType(param) {
|
|
177
|
+
const as = param.as;
|
|
178
|
+
if (!as || as === "auto") return as;
|
|
179
|
+
const args = param.asArgs === undefined ? "" : String(param.asArgs).trim();
|
|
180
|
+
return args === "" ? as : `${as}(${args})`;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function toNumber(value) {
|
|
184
|
+
if (value === undefined || value === null || value === "") return undefined;
|
|
185
|
+
const n = Number(value);
|
|
186
|
+
return Number.isFinite(n) ? n : undefined;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
RED.nodes.registerType("fromcubes-db-query", FromcubesDbQueryNode, {
|
|
190
|
+
settings: {
|
|
191
|
+
// Where the editor loads the SQL Editor and Monaco from (versioned path).
|
|
192
|
+
fromcubesDbQueryAssetBase: { value: ASSET_BASE, exportable: true },
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,83 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aaqu/fromcubes-db",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Fromcubes DB: Node-RED nodes for PostgreSQL, CockroachDB, YugabyteDB, MySQL, MariaDB, TiDB, SQL Server, Oracle, SQLite, Firebird and ClickHouse — injection-safe parameter binding and leak-free connection pooling",
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"node-red",
|
|
9
|
+
"fromcubes",
|
|
10
|
+
"postgres",
|
|
11
|
+
"postgresql",
|
|
12
|
+
"cockroachdb",
|
|
13
|
+
"yugabytedb",
|
|
14
|
+
"mysql",
|
|
15
|
+
"mariadb",
|
|
16
|
+
"tidb",
|
|
17
|
+
"mssql",
|
|
18
|
+
"sqlserver",
|
|
19
|
+
"sql-server",
|
|
20
|
+
"oracle",
|
|
21
|
+
"oracledb",
|
|
22
|
+
"sqlite",
|
|
23
|
+
"firebird",
|
|
24
|
+
"clickhouse",
|
|
25
|
+
"sql",
|
|
26
|
+
"database",
|
|
27
|
+
"db",
|
|
28
|
+
"query",
|
|
29
|
+
"sql-injection",
|
|
30
|
+
"connection-pool"
|
|
31
|
+
],
|
|
32
|
+
"node-red": {
|
|
33
|
+
"version": ">=5.0.0",
|
|
34
|
+
"nodes": {
|
|
35
|
+
"fromcubes-db-connection": "nodes/connection.js",
|
|
36
|
+
"fromcubes-db-query": "nodes/query.js"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"index.js",
|
|
41
|
+
"lib",
|
|
42
|
+
"nodes",
|
|
43
|
+
"examples",
|
|
44
|
+
"README.md",
|
|
45
|
+
"SECURITY.md",
|
|
46
|
+
"THIRD-PARTY-NOTICES.md",
|
|
47
|
+
"LICENSE",
|
|
48
|
+
"NOTICE"
|
|
49
|
+
],
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": ">=24"
|
|
52
|
+
},
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"@clickhouse/client": "1.25.0",
|
|
55
|
+
"monaco-editor": "^0.58.0",
|
|
56
|
+
"mssql": "^12.7.0",
|
|
57
|
+
"mysql2": "^3.23.2",
|
|
58
|
+
"node-firebird": "2.21.0",
|
|
59
|
+
"oracledb": "^26.0.0",
|
|
60
|
+
"pg": "^8.22.0"
|
|
61
|
+
},
|
|
62
|
+
"repository": {
|
|
63
|
+
"type": "git",
|
|
64
|
+
"url": "git+https://github.com/Aaqu/fromcubes-modules.git",
|
|
65
|
+
"directory": "packages/fromcubes-db"
|
|
66
|
+
},
|
|
67
|
+
"bugs": {
|
|
68
|
+
"url": "https://github.com/Aaqu/fromcubes-modules/issues"
|
|
69
|
+
},
|
|
70
|
+
"homepage": "https://aaqu.github.io/fromcubes-modules/db/",
|
|
71
|
+
"funding": {
|
|
72
|
+
"type": "github",
|
|
73
|
+
"url": "https://github.com/sponsors/Aaqu"
|
|
74
|
+
},
|
|
75
|
+
"license": "Apache-2.0",
|
|
76
|
+
"author": {
|
|
77
|
+
"name": "Aaqu",
|
|
78
|
+
"url": "https://github.com/Aaqu"
|
|
79
|
+
},
|
|
80
|
+
"publishConfig": {
|
|
81
|
+
"access": "public"
|
|
82
|
+
}
|
|
83
|
+
}
|