@aaqu/fromcubes-db 0.0.0-stage → 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.
Files changed (54) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +24 -0
  3. package/README.md +442 -2
  4. package/SECURITY.md +75 -0
  5. package/THIRD-PARTY-NOTICES.md +151 -0
  6. package/examples/Quick Start/All Databases.json +1719 -0
  7. package/examples/Quick Start/ClickHouse.json +278 -0
  8. package/examples/Quick Start/CockroachDB.json +280 -0
  9. package/examples/Quick Start/Firebird.json +278 -0
  10. package/examples/Quick Start/MariaDB.json +281 -0
  11. package/examples/Quick Start/MySQL.json +281 -0
  12. package/examples/Quick Start/Oracle.json +280 -0
  13. package/examples/Quick Start/PostgreSQL.json +280 -0
  14. package/examples/Quick Start/SQL Server.json +282 -0
  15. package/examples/Quick Start/SQLite.json +278 -0
  16. package/examples/Quick Start/TiDB.json +281 -0
  17. package/examples/Quick Start/YugabyteDB.json +280 -0
  18. package/examples/SQLite Sensor Log With Portal.json +391 -0
  19. package/examples/Select With Parameters.json +130 -0
  20. package/index.js +32 -0
  21. package/lib/adapters/clickhouse.js +254 -0
  22. package/lib/adapters/firebird.js +416 -0
  23. package/lib/adapters/index.js +37 -0
  24. package/lib/adapters/mssql.js +178 -0
  25. package/lib/adapters/mysql.js +171 -0
  26. package/lib/adapters/oracle.js +272 -0
  27. package/lib/adapters/postgres.js +161 -0
  28. package/lib/adapters/read.js +57 -0
  29. package/lib/adapters/sqlite.js +193 -0
  30. package/lib/adapters/time.js +89 -0
  31. package/lib/bound.js +29 -0
  32. package/lib/editor-api.js +134 -0
  33. package/lib/editor-preview.js +191 -0
  34. package/lib/errors.js +66 -0
  35. package/lib/params.js +966 -0
  36. package/lib/pool.js +391 -0
  37. package/lib/settings.js +302 -0
  38. package/lib/sql/compile.js +160 -0
  39. package/lib/sql/dialect.js +194 -0
  40. package/lib/sql/parse.js +202 -0
  41. package/lib/sql/types.js +1071 -0
  42. package/lib/sqlite-files.js +137 -0
  43. package/nodes/connection.html +728 -0
  44. package/nodes/connection.js +179 -0
  45. package/nodes/editor/ark-pixel.LICENSE.txt +96 -0
  46. package/nodes/editor/ark-pixel.woff2 +0 -0
  47. package/nodes/editor/mark.svg +5 -0
  48. package/nodes/editor/sql-editor.css +604 -0
  49. package/nodes/editor/sql-editor.js +1490 -0
  50. package/nodes/editor/sql-model.js +203 -0
  51. package/nodes/icons/fromcubes-db.svg +24 -0
  52. package/nodes/query.html +662 -0
  53. package/nodes/query.js +195 -0
  54. package/package.json +81 -4
package/README.md CHANGED
@@ -1,3 +1,443 @@
1
- # Temporary Holding Version
1
+ # @aaqu/fromcubes-db
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![npm version](https://img.shields.io/npm/v/%40aaqu%2Ffromcubes-db.svg)](https://www.npmjs.com/package/@aaqu/fromcubes-db)
4
+ [![npm downloads](https://img.shields.io/npm/dm/%40aaqu%2Ffromcubes-db.svg)](https://www.npmjs.com/package/@aaqu/fromcubes-db)
5
+ [![node](https://img.shields.io/node/v/%40aaqu%2Ffromcubes-db.svg)](https://www.npmjs.com/package/@aaqu/fromcubes-db)
6
+ [![Node-RED](https://img.shields.io/badge/Node--RED-%E2%89%A5%205.0-8f0000.svg)](https://nodered.org)
7
+ [![license](https://img.shields.io/npm/l/%40aaqu%2Ffromcubes-db.svg)](./LICENSE)
8
+ [![Sponsor](https://img.shields.io/badge/Sponsor-GitHub-ea4aaa?logo=githubsponsors&logoColor=white)](https://github.com/sponsors/Aaqu)
9
+
10
+ Node-RED nodes for **PostgreSQL, CockroachDB, YugabyteDB, MySQL, MariaDB, TiDB, Microsoft SQL
11
+ Server, Oracle, SQLite, Firebird and ClickHouse** behind one connection node and one query node.
12
+ Tested against PostgreSQL 19, CockroachDB 26.4, YugabyteDB 2026.1, MySQL 26.10, MariaDB 13.1, TiDB
13
+ 8.5, SQL Server 2025, Oracle 26ai, Firebird 5.0 and ClickHouse 26.11.
14
+
15
+ **[User guide](https://aaqu.github.io/fromcubes-modules/db/)** — installing, the first query, parameters, declared
16
+ types, every database's connection settings.
17
+
18
+ Each database is its own entry in the picker, with its own name, port and saved value — even
19
+ where two of them currently run on the same engine and the same driver:
20
+
21
+ | Database | Picked as | Engine today | Driver today |
22
+ | -------------------- | ------------- | ------------ | ------------------------------ |
23
+ | PostgreSQL | `postgres` | `postgres` | `pg` |
24
+ | CockroachDB | `cockroachdb` | `postgres` | `pg` — Postgres wire protocol |
25
+ | YugabyteDB | `yugabytedb` | `postgres` | `pg` — YSQL, port 5433 |
26
+ | MySQL | `mysql` | `mysql` | `mysql2` |
27
+ | MariaDB | `mariadb` | `mysql` | `mysql2` |
28
+ | TiDB | `tidb` | `mysql` | `mysql2` — MySQL protocol, 4000 |
29
+ | Microsoft SQL Server | `mssql` | `mssql` | `mssql` / tedious |
30
+ | Oracle | `oracle` | `oracle` | `oracledb`, Thin mode |
31
+ | SQLite | `sqlite` | `sqlite` | `node:sqlite`, built into Node |
32
+ | Firebird | `firebird` | `firebird` | `node-firebird`, pure JS, 3050 |
33
+ | ClickHouse | `clickhouse` | `clickhouse` | `@clickhouse/client`, HTTP, 8123 |
34
+
35
+ "Today" is the point: MariaDB is not MySQL and CockroachDB is not PostgreSQL, they merely
36
+ behave alike for now. Keeping them apart means the day one needs its own driver, settings or
37
+ binding rule, it gets one — without unpicking the choice from every saved flow first.
38
+
39
+ Every one of them has its own integration suite running the same contract against a real
40
+ server — CockroachDB, YugabyteDB, MariaDB and TiDB included, so "it speaks the same protocol" is
41
+ tested, not assumed. It is also how their differences were found: TiDB has no spatial types and
42
+ takes a vector as `[1,2.5]` text; YugabyteDB's `numeric` refuses `NaN` and `Infinity`.
43
+
44
+ Two things this package is strict about:
45
+
46
+ 1. **SQL injection has no way in.** The statement comes from the node's edit dialog and nothing
47
+ else. Values arrive through a parameter list and are bound by the driver. No message content
48
+ can change what a statement does.
49
+ 2. **Connections do not leak.** One pool per connection node, shared by every query node that
50
+ uses it, drained and closed on redeploy.
51
+
52
+ **[User guide](https://aaqu.github.io/fromcubes-modules/db/)** — install, first query, recipes, troubleshooting and what the package deliberately will not do. Source: `website/db/index.html` in the monorepo.
53
+
54
+ ## Install
55
+
56
+ Palette manager → install `@aaqu/fromcubes-db`, or:
57
+
58
+ ```bash
59
+ cd ~/.node-red
60
+ npm install @aaqu/fromcubes-db
61
+ ```
62
+
63
+ All drivers ship with the package, so nothing else needs installing. Oracle runs in Thin mode —
64
+ no Oracle Instant Client required — and SQLite uses `node:sqlite` from Node itself, so there is
65
+ no native build step anywhere.
66
+
67
+ **Node 24 or newer, Node-RED 5 or newer.** That is what CI runs and the only combination the
68
+ suites are executed against.
69
+
70
+ ## Use
71
+
72
+ Drop a **db query** node, create a connection, type SQL with `:name` placeholders, and map each
73
+ placeholder to a value:
74
+
75
+ ```sql
76
+ SELECT id, name, email
77
+ FROM users
78
+ WHERE tenant_id = :tenant
79
+ AND status = :status
80
+ ```
81
+
82
+ | Parameter | Source |
83
+ | --------- | ------------------------------ |
84
+ | `tenant` | `msg.payload.tenantId` |
85
+ | `status` | `active` (string) |
86
+
87
+ The parameter rows are normal Node-RED typedInputs — `msg`, `flow`, `global`, `env`, JSONata,
88
+ or a literal. The connection's user and password are not typedInputs, because a credential is
89
+ stored encrypted and outside the flow file; they take an environment variable through a source
90
+ picker of their own — see [User and password](#user-and-password).
91
+
92
+ Result lands on `msg.payload` (configurable), with `msg.rowCount` alongside.
93
+
94
+ The dialog is split in two: **Query** carries the statement and its parameters, **Connection**
95
+ carries what you set once — the connection itself, the result shape, the message property it
96
+ lands on, and the timeout.
97
+
98
+ ### Placeholders
99
+
100
+ | Syntax | Meaning |
101
+ | ----------- | ---------------------------------------------------------------------- |
102
+ | `:name` | One value: string, number, boolean, date, buffer or null. |
103
+ | `:...name` | An array expanded into a value list, for `IN (:...ids)`. |
104
+
105
+ Each database gets its own binding syntax automatically:
106
+
107
+ | Database | What the driver receives |
108
+ | ---------- | -------------------------------------------- |
109
+ | PostgreSQL | `WHERE id = $1 AND name = $2` |
110
+ | SQL Server | `WHERE id = @p1 AND name = @p2` |
111
+ | Oracle | `WHERE id = :p1 AND name = :p2` |
112
+ | MySQL | `WHERE id = ? AND name = ?` |
113
+ | SQLite | `WHERE id = ? AND name = ?` |
114
+
115
+ An empty `:...list` renders as `IN (NULL)` — valid SQL that matches nothing.
116
+
117
+ ### Parameter types
118
+
119
+ Each parameter row has a type select. With `auto` the value is bound as it arrives. Pick anything
120
+ else and the value is checked first, so a bad message fails naming the parameter instead of
121
+ surfacing as a driver error about a column you never mentioned.
122
+
123
+ | Type | Accepts | Rejects |
124
+ | ----------------- | ------------------------------------------------ | ------------------------------ |
125
+ | `string` | strings, numbers, booleans, Dates (→ ISO) | objects — use `json` |
126
+ | `number`/`integer`| numbers, numeric strings | `"abc"`, NaN, Infinity, booleans |
127
+ | `boolean` | `true`/`false`, `"yes"`/`"no"`, `1`/`0` | `"maybe"`, `2` |
128
+ | `date` | a Date, an ISO string, an epoch number | unparseable dates |
129
+ | `json` | objects, arrays, JSON strings | invalid JSON |
130
+ | `buffer` | Buffer, Uint8Array, base64 string | numbers |
131
+
132
+ `null` and `undefined` always pass through — whether a missing value is an error is decided by
133
+ **Bind missing parameters as NULL** on the connection.
134
+
135
+ For a spread parameter the check runs per element, so `IN (:...ids)` declared as `integer`
136
+ rejects the whole message when one id is junk, naming it: `Parameter :ids[1] — expected an
137
+ integer, got string "x"`.
138
+
139
+ ### Column types
140
+
141
+ Below the generic types the select also offers the column types of the database the connection
142
+ points at — `varchar`, `numeric`, `tinyint`, `varchar2` — with a small field beside it for the
143
+ length or precision. Then the value is checked against that type as the product defines it, and
144
+ never reaches the SQL text: the declaration is read before binding and nowhere else.
145
+
146
+ Each product's own limits apply, which is the point of picking one:
147
+
148
+ | Declared | On | Means |
149
+ | ---------------- | --------------------------- | ------------------------------------------------- |
150
+ | `varchar(50)` | PostgreSQL, MySQL, MariaDB, CockroachDB | 50 **characters** |
151
+ | `varchar(50)` | SQL Server | 50 **bytes** — `'ółć'` is 6 |
152
+ | `nvarchar(50)` | SQL Server | 50 UTF-16 units |
153
+ | `varchar2(50)` | Oracle | 50 bytes; `varchar2(50 char)` for characters |
154
+ | `tinyint` | MySQL | −128…127, or 0…255 with `unsigned` |
155
+ | `tinyint` | SQL Server | 0…255 |
156
+
157
+ Beyond length, the check covers what each type actually holds: integer ranges, `decimal(p,s)`
158
+ digit budgets, date ranges (`datetime` on SQL Server starts in 1753, MySQL's `TIMESTAMP` stops in
159
+ 2038 — and a fraction the column rounds away is checked as rounded), and the shapes of `uuid`,
160
+ `inet`, `macaddr`, `bit`, ranges and SQL Server's `time`.
161
+
162
+ **Vectors** are a column type on seven of the eleven: `vector(3)` on CockroachDB, MySQL 9, TiDB 8.4+
163
+ and MariaDB 11.7+, `vector(3, float32)` on SQL Server 2025 and Oracle 23ai+, `QBit(Float32, 3)` on
164
+ ClickHouse 25.10+
165
+ (the second argument is the storage format, checked against what that product has). The value is
166
+ an array of numbers, a typed array or its JSON text; a declared length must match exactly.
167
+
168
+ **The declared type also picks the bind type.** Left to themselves the drivers guess from the JS
169
+ value, and several guesses corrupt data rather than fail: SQL Server's driver sends every Date as
170
+ the legacy `DATETIME`, `pg` turns a json array into a PostgreSQL array, and `oracledb` reads any
171
+ object as a bind definition (`{val, dir, type}`) — so on Oracle an object always goes as JSON,
172
+ typed or not. The declaration still never reaches the SQL text.
173
+
174
+ **Dates are UTC everywhere.** A Date is an instant; a column without a time zone receives its UTC
175
+ wall clock and is read back the same way, whatever zone Node-RED runs in. Text without a zone
176
+ (`"2026-11-28 10:20"`) means that wall clock in UTC. MySQL and Oracle sessions are set to UTC
177
+ (Oracle with ISO `NLS_*` formats); a timestamp with more digits than a Date holds, or an offset a
178
+ Date would lose (`timestamptz`, `datetimeoffset`), travels as text so nothing is cut.
179
+
180
+ **Numbers a double cannot hold are kept exactly.** `bigint` is 19 digits, Oracle's `NUMBER` 38,
181
+ PostgreSQL's `numeric` a thousand — and `Number('9223372036854775807')` quietly returns
182
+ 9223372036854776000. A BigInt stays a BigInt and a numeric string stays a string, both of which
183
+ every driver here binds, and range checks compare in BigInt rather than in doubles.
184
+
185
+ SQLite is the exception worth knowing: it accepts `VARCHAR(5)` and then stores anything at all
186
+ in it. The length check there is ours, not the database's — which is exactly why it is useful.
187
+
188
+ [`docs/column-types.md`](https://github.com/Aaqu/fromcubes-modules/blob/main/packages/fromcubes-db/docs/column-types.md)
189
+ lists where each product documents its types and every limit collected
190
+ from it. The full catalogue is `lib/sql/types.js`, and the integration suite creates a column of
191
+ every type in it on a real server, so a name in the picker is a name the database has.
192
+
193
+ Placeholders inside string literals, comments and quoted identifiers are left alone, so
194
+ `created::text` on Postgres and `[a:b]` on SQL Server keep working.
195
+
196
+ ### Result shapes
197
+
198
+ | Result | `msg.payload` |
199
+ | ------------------ | --------------------------------- |
200
+ | all rows | array of row objects |
201
+ | first row only | one row object, or `null` |
202
+ | affected row count | number |
203
+ | one message per row| one message per row, each a clone |
204
+
205
+ ### Values read back
206
+
207
+ Rows keep the driver's shape except where that shape loses data — then the same value comes back
208
+ the same way from every database:
209
+
210
+ - **Exact numbers** are a JS number when the double *is* that number, and text otherwise:
211
+ `BIGINT` 42 is `42`, 9223372036854775807 is `"9223372036854775807"`. MySQL, MariaDB and Oracle
212
+ used to round those silently (…993 arrived as …992); PostgreSQL's `BIGINT` used to be text
213
+ even for 42. `NUMERIC`/`DECIMAL` stay text on PostgreSQL, CockroachDB, MySQL and MariaDB; Oracle's
214
+ `NUMBER` follows the same rule as integers.
215
+ - **Vectors** are arrays of numbers (CockroachDB, MySQL, Oracle). TiDB reports a vector column as
216
+ plain text, so it arrives as its `[1,2.5]` text there.
217
+ - **Binary** is a `Buffer` everywhere, SQLite included; **CLOB** is text, not a stream object.
218
+ - **SQLite** reads an `INTEGER` past 2⁵³ (which used to fail the whole row) and parses columns
219
+ declared `JSON`.
220
+
221
+ Two the drivers give no way to fix — cast in the SQL when it matters:
222
+
223
+ | Database | Column | Arrives as | For the exact value |
224
+ | ---------- | ----------------------- | ---------------------------------- | -------------------------------- |
225
+ | SQL Server | `decimal`, `money` | a double — 38 digits become 17 | `CAST(c AS varchar(50))` |
226
+ | SQL Server | `json`, `vector` | text | `JSON.parse` in the flow |
227
+ | MariaDB | `vector` | its float32 bytes | `VEC_ToText(c)` |
228
+
229
+ ### Column names
230
+
231
+ Rows come through exactly as the driver reported them — the package does not rewrite column
232
+ names. Note that Oracle reports them in UPPER CASE (`FULL_NAME`) where the others report what
233
+ you wrote (`full_name`). Moving a flow between the two, alias the columns in the SQL:
234
+
235
+ ```sql
236
+ SELECT full_name AS "fullName" FROM users
237
+ ```
238
+
239
+ ### On the canvas
240
+
241
+ Query nodes carry the database they run against in small type above the node — `PostgreSQL`,
242
+ `SQLite` — so a node can show both its own name and where it goes. The checkbox under the
243
+ database picker on the connection turns that off for every node using it.
244
+
245
+ ### SQL Editor
246
+
247
+ A full-screen editor for every query node in the flow: **SQL Editor** in Node-RED's status bar,
248
+ or the button above the SQL in a query node's dialog. Every query node sits in a tree by flow
249
+ tab; the SQL opens in Monaco (served from this package, no CDN).
250
+
251
+ Under the SQL is the **Input** panel:
252
+
253
+ - **Sample message** — type one, or **Capture next message** to copy the next message the
254
+ deployed node receives. Samples stay in this browser (localStorage), never in `flows.json`.
255
+ - **Fields** — the sample's fields as `msg` paths. Click one to write `:name` at the cursor,
256
+ bound to that field. Typing `:` offers the node's parameters and the sample's fields.
257
+ - **Parameters** — source, declared type and what each resolves to against the sample, with
258
+ the same check a deployed node runs. A placeholder with no parameter is underlined in the
259
+ SQL and gets a **bind** button.
260
+
261
+ **What the database receives** shows the compiled statement and its values apart from it.
262
+ Nothing runs: the preview uses the functions the node uses (parse, type check, compile), never
263
+ a database. `flow`, `global`, `env` and JSONata are read only when the flow runs — env never
264
+ reaches the browser.
265
+
266
+ Edits go onto the node with an undo entry (Ctrl/Cmd+Z), like a dialog save. Deploy stays
267
+ manual: the header button or Ctrl/Cmd+S. Esc closes without losing anything.
268
+
269
+ ### Examples
270
+
271
+ Import → Examples → `@aaqu/fromcubes-db`:
272
+
273
+ - **Quick Start → one per database** (PostgreSQL, CockroachDB, YugabyteDB, MySQL, MariaDB, TiDB, SQL Server,
274
+ Oracle, SQLite, Firebird, ClickHouse) — creates the table `fromcubes_demo` on deploy, then buttons insert a row
275
+ with declared types, read the latest ten and clear the table. Every result goes to the Debug sidebar and to the
276
+ Node-RED log. The tab's info has the `docker run` line for a matching local server and the user to enter (an
277
+ example cannot carry credentials).
278
+ - **Quick Start → All Databases** — the same rows for all eleven on one tab, driven by four buttons at once; a
279
+ server that is not running only fails its own row. MariaDB is on 3307 there, beside MySQL.
280
+
281
+ The Quick Start files are generated by `tools/build-examples.mjs` from one table of statements, so the combined
282
+ flow cannot drift from the single ones; each database's integration suite deploys its own file against a real
283
+ server.
284
+
285
+ - **Select With Parameters** — a typed `SELECT`, then an audit `INSERT` (PostgreSQL; edit the connection first).
286
+ - **SQLite Sensor Log With Portal** — no server needed. A simulated sensor writes into the SQLite database
287
+ `sensor-log` every 3 s; a [`@aaqu/fromcubes-react`](https://www.npmjs.com/package/@aaqu/fromcubes-react) page at
288
+ `/fromcubes/sensor-log` shows the rows live, adds a reading and clears the table. A value its declared type refuses
289
+ goes back to the page that sent it. `tests/nodes/examples.test.js` checks every example and runs this one's
290
+ statements on a real SQLite file.
291
+
292
+ ## Connection settings
293
+
294
+ Pick the database first — the dialog then shows only what that database accepts, instead of
295
+ expecting you to know each driver's config shape.
296
+
297
+ | Database | Fields |
298
+ | ---------- | --------------------------------------------------------------------------- |
299
+ | PostgreSQL | SSL mode (off / require / verify CA / verify CA and hostname), CA certificate, schema (`search_path`), application name |
300
+ | MySQL | SSL, verify certificate, charset, timezone |
301
+ | SQL Server | encrypt, trust server certificate, named instance, NTLM domain, application name |
302
+ | Oracle | host + port + service name, or a full connect string / TNS alias; service name vs SID |
303
+ | SQLite | database name (or `:memory:`), read only, WAL journal, foreign keys, busy timeout |
304
+
305
+ ### SQLite databases
306
+
307
+ A SQLite connection names a database, not a file: `sensors` is
308
+ `<userDir>/fromcubes-db/sensors.db` (usually `~/.node-red/fromcubes-db/`). The folder and the file
309
+ are created on the first query, and the dialog lists the databases already there, so several
310
+ connections can each use their own. Names are letters, digits and `. _ -`; `.sqlite` and
311
+ `.sqlite3` are kept, anything else gets `.db`. A path is refused, as is a database file replaced by
312
+ a link — nothing a connection names can be outside that folder. Back up the folder, back up every
313
+ database. Used as a library (below), `file` is still a plain path of your choosing.
314
+
315
+ ### User and password
316
+
317
+ Each is either a **value** — kept in Node-RED's encrypted credentials, outside the flow file, so
318
+ an exported flow never carries it — or the name of an **environment variable**, read when the
319
+ node starts:
320
+
321
+ | Source | What you type | Where it lives |
322
+ | ---------------------- | ------------------------ | ----------------------------- |
323
+ | `value` (default) | the user / the password | encrypted credentials file |
324
+ | `environment variable` | `POSTGRES_PASSWORD` | the process, group or flow env |
325
+
326
+ The variable is for a flow built before the login it will use exists — an image that ships to a
327
+ device provisioned with its own password, handed to the container through `--env-file`. The
328
+ variable *name* is a normal setting and does travel in an exported flow; the value never does,
329
+ because it is the device's rather than the flow's.
330
+
331
+ The name is resolved the way every other environment reference in Node-RED is, so a variable set
332
+ on the group or the enclosing flow is found before the process one. Unset when the node starts is
333
+ a hard stop: the node logs an error, shows red, and no pool is built — a login that is silently
334
+ `undefined` would otherwise surface much later as the server's own authentication failure.
335
+
336
+ The two sources are independent; a fixed user with the password from the environment is the usual
337
+ pairing. Leaving both alone is what every existing flow does, and it behaves as it always has.
338
+
339
+ **Advanced** stays as the escape hatch: JSON in the shape the driver's own documentation uses,
340
+ merged key by key over the fields above. Adding one option does not discard the rest.
341
+
342
+ ```json
343
+ { "keepAlive": true }
344
+ ```
345
+
346
+ ## Connection handling
347
+
348
+ A Node-RED redeploy tears every node down and builds it again. Without care that leaves open
349
+ sockets behind until the server runs out of connections. What this package does:
350
+
351
+ - **One pool per connection node**, reference counted. Ten query nodes on one connection share
352
+ one pool.
353
+ - **Lazy connect** — an unused connection node costs nothing.
354
+ - **Drain on close**: in-flight queries are awaited (up to 10 s, under Node-RED's 15 s limit),
355
+ then the pool closes and `done()` is called.
356
+ - **Bounded queue**: the node admits at most `poolMax` queries at a time and lets at most
357
+ `queueMax` wait. Past that they fail with `QueueOverflowError` instead of piling up in memory.
358
+ This is enforced here rather than left to the driver — `pg` has no queue limit at all, and
359
+ `mssql` and `node:sqlite` have none either.
360
+ - **Timeouts**: applied by the driver where it supports them (`statement_timeout`,
361
+ `requestTimeout`, `callTimeout`) plus a hard stop in the node.
362
+ - **Backoff on failure**, evaluated when a query arrives rather than on a background timer — a
363
+ stray timer is exactly what survives an unload.
364
+ - **No listener build-up**: every `pool.on('error')` and status subscription is removed on close.
365
+
366
+ ## Errors
367
+
368
+ Failures go through `done()`, so a **Catch** node picks them up. Constraint violations get the
369
+ same type whichever database raised them:
370
+
371
+ `UniqueViolationError`, `ForeignKeyViolationError`, `NotNullViolationError`,
372
+ `CheckViolationError`, `ConnectionError`, `TimeoutError`, `QueueOverflowError`,
373
+ `PoolClosedError`, `ParameterError`, `SqlSyntaxError`, `QueryError`.
374
+
375
+ The native code stays available on `err.driverCode` (SQLSTATE, `ORA-` number, SQL Server number).
376
+
377
+ ## Using it outside Node-RED
378
+
379
+ The core is a plain module:
380
+
381
+ ```js
382
+ const { ManagedPool } = require('@aaqu/fromcubes-db');
383
+
384
+ const pool = new ManagedPool({
385
+ dialect: 'postgres',
386
+ host: 'localhost',
387
+ database: 'app',
388
+ user: 'app',
389
+ password: '...',
390
+ });
391
+
392
+ const { rows } = await pool.query('SELECT * FROM users WHERE id = :id', { id: 5 });
393
+ await pool.close();
394
+ ```
395
+
396
+ ## Development
397
+
398
+ From the monorepo root ([fromcubes-modules](https://github.com/Aaqu/fromcubes-modules)):
399
+
400
+ ```bash
401
+ npm test # every package: parser, compiler, pool, and the nodes through node-red-node-test-helper
402
+ npm run test:integration # real databases in Docker (testcontainers)
403
+ npm run lint && npm run format:check
404
+ ```
405
+
406
+ Integration tests start one container per database (images in `tests/integration/images.js`;
407
+ `FCDB_IMAGE_MYSQL=mysql:8.4` and the like swap one). CI runs the newest line of each product and
408
+ the older ones still widely used — PostgreSQL 15, CockroachDB 25.4, MySQL 8.4 and 9.7, MariaDB
409
+ 10.11, 11.4, 11.8 and 12.3, SQL Server 2022 — where types a version lacks (MySQL `VECTOR` before 9.0,
410
+ SQL Server `json` before 2025) are skipped, and the picker marks them `9.0+` — SQLite needs none, it runs against a temp
411
+ file. SQL Server is amd64-only and runs under emulation on Apple Silicon; the Oracle image is
412
+ large and slow on first pull.
413
+
414
+ All eleven products run the same contract suite: binding and injection, spread lists, nulls and
415
+ empty strings, unicode, dates, binary, decimals, booleans, generated keys, and the fact that each
416
+ statement commits on its own. It also creates a column of every type in `lib/sql/types.js` on the
417
+ running server — 62 on ClickHouse, 54 on PostgreSQL and YugabyteDB, 43 on MariaDB, 39 on MySQL,
418
+ 38 on CockroachDB, 36 on SQL Server, 31 on TiDB, 28 on Oracle, 23 on Firebird, 11 on SQLite — so the picker cannot offer a type the database does not
419
+ have — then binds a value of each type through its declaration, reads it back and compares, sends
420
+ every catalogued limit (which must land) and the value just past it (which must be refused). And it reads each server's own list of its types (`pg_type`, `sys.types`, `dba_types`, MySQL's HELP tables, `RDB$TYPES`, `system.data_type_families`): every type listed there must be catalogued or left out with a written reason, and every alias must be stored by the server as the type it claims to be. On top of that, `flow.test.js` deploys real Node-RED nodes against a
421
+ real server and counts connections through `pg_stat_activity` — including across restarts — and
422
+ `tests/nodes/startup.test.js` starts a 144-node flow with eleven unreachable servers to prove that
423
+ start-up never waits for a database.
424
+
425
+ GitHub Actions runs them on every push and pull request touching the package, one job per
426
+ database (`.github/workflows/db-integration.yml`).
427
+
428
+ ```bash
429
+ npx vitest run --project db-integration packages/fromcubes-db/tests/integration/sqlite.test.js # one dialect
430
+ ```
431
+
432
+ To try it in a real editor:
433
+
434
+ ```bash
435
+ cd ~/.node-red && npm install /path/to/fromcubes-modules/packages/fromcubes-db && node-red
436
+ ```
437
+
438
+ ## License
439
+
440
+ Apache-2.0 — see `LICENSE` and `NOTICE`.
441
+
442
+ The node icon is the "database" icon from [Lucide](https://lucide.dev), used under the ISC
443
+ License. Its required notice is reproduced in `THIRD-PARTY-NOTICES.md`.
package/SECURITY.md ADDED
@@ -0,0 +1,75 @@
1
+ # Security model
2
+
3
+ ## What this package guarantees
4
+
5
+ **The SQL text sent to the driver depends only on the SQL typed into the node.** Values from
6
+ `msg`, `flow`, `global` or the environment are bound as parameters. They never become part of the
7
+ statement, whatever they contain.
8
+
9
+ The compiler enforces this by construction: it walks a parsed statement and emits either literal
10
+ text from the SQL, or a placeholder. There is no code path that turns a value into SQL text. The
11
+ unit suite asserts it for a list of hostile payloads across all four dialects — the emitted text
12
+ is compared byte for byte against a benign baseline.
13
+
14
+ ## What it deliberately does not offer
15
+
16
+ **There is no "take the SQL from `msg`" option.** That switch is the usual way injection gets into
17
+ a Node-RED flow: a function node builds a string, a database node runs it, and one unescaped
18
+ value from an HTTP request is enough. Since the statement can only come from the dialog, the
19
+ attack has nowhere to enter.
20
+
21
+ Consequence: statements whose *shape* varies (a column name chosen at runtime, an optional
22
+ `ORDER BY`) cannot be built by this node. Model them as separate query nodes selected by a switch
23
+ node, or with SQL that handles both cases:
24
+
25
+ ```sql
26
+ SELECT * FROM users
27
+ WHERE (:name IS NULL OR name = :name)
28
+ ORDER BY id
29
+ ```
30
+
31
+ Identifiers — table and column names — are never parameterisable in SQL, in any database. If you
32
+ ever need a dynamic one, pick it from a fixed allow-list in a function node and route to the
33
+ matching query node. Never interpolate it.
34
+
35
+ ## Credentials
36
+
37
+ User and password live in Node-RED's credential store, encrypted at rest in
38
+ `~/.node-red/<flows>_cred.json`, never in the flow file. Passwords are not sent to the editor.
39
+ They are never copied into `msg` and never logged.
40
+
41
+ Exported flows contain the connection host, port and database, but no credentials.
42
+
43
+ ## Transport
44
+
45
+ Each dialect has its own fields in the connection dialog:
46
+
47
+ - **SQL Server**: encryption is on by default. A self-signed certificate — what a local or
48
+ container instance uses — has to be accepted explicitly with **Trust the server certificate**.
49
+ - **PostgreSQL**: the **SSL** select. `require` encrypts but does not check the certificate,
50
+ which stops passive sniffing but not an active man in the middle; `verify-ca` and
51
+ `verify-full` check it, against the CA you paste in if given. `verify-full` also checks the
52
+ hostname and is the only setting that resists an attacker who can redirect your traffic.
53
+ - **MySQL**: **Use SSL**, with certificate verification on unless you turn it off.
54
+ - **Oracle**: Thin mode supports TLS through the connect descriptor.
55
+
56
+ `multipleStatements` is off on MySQL and never enabled. One statement per query node means a
57
+ value cannot escalate into a second command even if the driver were tricked.
58
+
59
+ ## Reporting a vulnerability
60
+
61
+ Open a GitHub issue for anything already public. For an unreported flaw in the parameter binding
62
+ path, contact the maintainer privately first.
63
+
64
+ ## Threat model boundaries
65
+
66
+ This package protects the statement. It does not, and cannot:
67
+
68
+ - decide whether the *user of your flow* is allowed to see a row — that is your query's `WHERE`
69
+ clause and your database's permissions;
70
+ - stop a flow from sending secrets downstream once a query has returned them;
71
+ - protect against a compromised Node-RED instance, which can read credentials from its own
72
+ credential store by design.
73
+
74
+ Give the connection a database account with the narrowest rights that the flow needs. A flow that
75
+ only reads should not connect as an account that can `DROP`.
@@ -0,0 +1,151 @@
1
+ # Third-party notices
2
+
3
+ This package is licensed under Apache-2.0 (see `LICENSE`). It bundles the
4
+ material listed below under the licences reproduced here.
5
+
6
+ The database drivers (`pg`, `mysql2`, `mssql`, `oracledb`) are ordinary npm
7
+ dependencies — they are installed alongside this package rather than copied into
8
+ it, and each carries its own licence in `node_modules`. SQLite support uses
9
+ `node:sqlite`, part of Node.js itself, so nothing is bundled for it.
10
+
11
+ ---
12
+
13
+ ## Lucide — `nodes/icons/fromcubes-db.svg`
14
+
15
+ The node icon is a composite. The cube is the fromcubes mark, traced from the
16
+ project's own artwork by `tools/build-icon.mjs` and not third-party material.
17
+ Beside it sits the **database** icon from [Lucide](https://lucide.dev),
18
+ recoloured white to match the Node-RED palette convention and scaled; its path
19
+ data is otherwise unchanged.
20
+
21
+ ISC requires this notice to travel with every copy.
22
+
23
+ ```
24
+ ISC License
25
+
26
+ Copyright (c) for portions of Lucide are held by Cole Bemis 2013-2022 as part of
27
+ Feather (MIT). All other copyright (c) for Lucide are held by Lucide Contributors
28
+ 2022.
29
+
30
+ Permission to use, copy, modify, and/or distribute this software for any purpose
31
+ with or without fee is hereby granted, provided that the above copyright notice
32
+ and this permission notice appear in all copies.
33
+
34
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
35
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
36
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
37
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
38
+ OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
39
+ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
40
+ THIS SOFTWARE.
41
+ ```
42
+
43
+ ---
44
+
45
+ ## Ark Pixel — `nodes/editor/ark-pixel.woff2`
46
+
47
+ The SQL Editor's brand lockup (the same lockup as the React Editor's) is set in **Ark Pixel 10px proportional** by TakWolf,
48
+ release 2026.08.11, shipped unmodified. The file is the project's own `latin`
49
+ build (the editor's chrome is Latin-only), taken from the upstream release
50
+ rather than subsetted here, so it stays byte-identical to what TakWolf ships. The SIL Open Font License requires its text to travel with the font,
51
+ so the full licence is also kept beside it as
52
+ `nodes/editor/ark-pixel.LICENSE.txt`.
53
+
54
+ ```
55
+ Ark Pixel Font
56
+ https://github.com/TakWolf/ark-pixel-font
57
+
58
+ Copyright (c) 2021, TakWolf (https://takwolf.com).
59
+
60
+ This Font Software is licensed under the SIL Open Font License, Version 1.1.
61
+ This license is copied below, and is also available with a FAQ at:
62
+ https://openfontlicense.org
63
+
64
+
65
+ -----------------------------------------------------------
66
+ SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
67
+ -----------------------------------------------------------
68
+
69
+ PREAMBLE
70
+ The goals of the Open Font License (OFL) are to stimulate worldwide
71
+ development of collaborative font projects, to support the font creation
72
+ efforts of academic and linguistic communities, and to provide a free and
73
+ open framework in which fonts may be shared and improved in partnership
74
+ with others.
75
+
76
+ The OFL allows the licensed fonts to be used, studied, modified and
77
+ redistributed freely as long as they are not sold by themselves. The
78
+ fonts, including any derivative works, can be bundled, embedded,
79
+ redistributed and/or sold with any software provided that any reserved
80
+ names are not used by derivative works. The fonts and derivatives,
81
+ however, cannot be released under any other type of license. The
82
+ requirement for fonts to remain under this license does not apply
83
+ to any document created using the fonts or their derivatives.
84
+
85
+ DEFINITIONS
86
+ "Font Software" refers to the set of files released by the Copyright
87
+ Holder(s) under this license and clearly marked as such. This may
88
+ include source files, build scripts and documentation.
89
+
90
+ "Reserved Font Name" refers to any names specified as such after the
91
+ copyright statement(s).
92
+
93
+ "Original Version" refers to the collection of Font Software components as
94
+ distributed by the Copyright Holder(s).
95
+
96
+ "Modified Version" refers to any derivative made by adding to, deleting,
97
+ or substituting -- in part or in whole -- any of the components of the
98
+ Original Version, by changing formats or by porting the Font Software to a
99
+ new environment.
100
+
101
+ "Author" refers to any designer, engineer, programmer, technical
102
+ writer or other person who contributed to the Font Software.
103
+
104
+ PERMISSION & CONDITIONS
105
+ Permission is hereby granted, free of charge, to any person obtaining
106
+ a copy of the Font Software, to use, study, copy, merge, embed, modify,
107
+ redistribute, and sell modified and unmodified copies of the Font
108
+ Software, subject to the following conditions:
109
+
110
+ 1) Neither the Font Software nor any of its individual components,
111
+ in Original or Modified Versions, may be sold by itself.
112
+
113
+ 2) Original or Modified Versions of the Font Software may be bundled,
114
+ redistributed and/or sold with any software, provided that each copy
115
+ contains the above copyright notice and this license. These can be
116
+ included either as stand-alone text files, human-readable headers or
117
+ in the appropriate machine-readable metadata fields within text or
118
+ binary files as long as those fields can be easily viewed by the user.
119
+
120
+ 3) No Modified Version of the Font Software may use the Reserved Font
121
+ Name(s) unless explicit written permission is granted by the corresponding
122
+ Copyright Holder. This restriction only applies to the primary font name as
123
+ presented to the users.
124
+
125
+ 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
126
+ Software shall not be used to promote, endorse or advertise any
127
+ Modified Version, except to acknowledge the contribution(s) of the
128
+ Copyright Holder(s) and the Author(s) or with their explicit written
129
+ permission.
130
+
131
+ 5) The Font Software, modified or unmodified, in part or in whole,
132
+ must be distributed entirely under this license, and must not be
133
+ distributed under any other license. The requirement for fonts to
134
+ remain under this license does not apply to any document created
135
+ using the Font Software.
136
+
137
+ TERMINATION
138
+ This license becomes null and void if any of the above conditions are
139
+ not met.
140
+
141
+ DISCLAIMER
142
+ THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
143
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
144
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
145
+ OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
146
+ COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
147
+ INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
148
+ DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
149
+ FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
150
+ OTHER DEALINGS IN THE FONT SOFTWARE.
151
+ ```