@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.
- 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/lib/pool.js
ADDED
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { adapterFor } = require("./adapters");
|
|
4
|
+
const { compile } = require("./sql/compile");
|
|
5
|
+
const { parseSql } = require("./sql/parse");
|
|
6
|
+
const {
|
|
7
|
+
DbError,
|
|
8
|
+
ConnectionError,
|
|
9
|
+
PoolClosedError,
|
|
10
|
+
QueueOverflowError,
|
|
11
|
+
TimeoutError,
|
|
12
|
+
ParameterError,
|
|
13
|
+
} = require("./errors");
|
|
14
|
+
|
|
15
|
+
const DEFAULTS = {
|
|
16
|
+
poolMin: 0,
|
|
17
|
+
poolMax: 10,
|
|
18
|
+
idleTimeoutMs: 30_000,
|
|
19
|
+
connectTimeoutMs: 15_000,
|
|
20
|
+
queryTimeoutMs: 30_000,
|
|
21
|
+
queueMax: 100,
|
|
22
|
+
undefinedAsNull: false,
|
|
23
|
+
/** Longest wait for in-flight queries during close; Node-RED allows 15 s. */
|
|
24
|
+
drainMs: 10_000,
|
|
25
|
+
/** Reconnect backoff bounds, applied on demand — no background timers. */
|
|
26
|
+
retryMinMs: 1_000,
|
|
27
|
+
retryMaxMs: 30_000,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const STATES = {
|
|
31
|
+
IDLE: "idle",
|
|
32
|
+
CONNECTING: "connecting",
|
|
33
|
+
READY: "ready",
|
|
34
|
+
FAILED: "failed",
|
|
35
|
+
CLOSING: "closing",
|
|
36
|
+
CLOSED: "closed",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Owns one driver pool and everything that keeps a Node-RED redeploy from
|
|
41
|
+
* leaking it:
|
|
42
|
+
*
|
|
43
|
+
* - reference counted, so ten query nodes on one connection share one pool
|
|
44
|
+
* - connects lazily, on the first query
|
|
45
|
+
* - tracks in-flight queries and drains them before closing
|
|
46
|
+
* - reconnects with exponential backoff, on demand rather than on a timer,
|
|
47
|
+
* because a stray timer is exactly what survives an unload
|
|
48
|
+
* - refuses new work once closing, instead of resurrecting the pool
|
|
49
|
+
*/
|
|
50
|
+
class ManagedPool {
|
|
51
|
+
constructor(config) {
|
|
52
|
+
this.config = normalizeConfig(config);
|
|
53
|
+
// config.adapter is an injection point for tests and for embedding a custom
|
|
54
|
+
// driver; production paths resolve it from the dialect.
|
|
55
|
+
this.adapter = this.config.adapter || adapterFor(this.config.dialect);
|
|
56
|
+
|
|
57
|
+
this.state = STATES.IDLE;
|
|
58
|
+
this.refs = 0;
|
|
59
|
+
this._closing = null;
|
|
60
|
+
|
|
61
|
+
// Admission control — see _acquireSlot.
|
|
62
|
+
this._active = 0;
|
|
63
|
+
this._waiters = [];
|
|
64
|
+
|
|
65
|
+
this._pool = null;
|
|
66
|
+
this._connecting = null;
|
|
67
|
+
this._inflight = new Set();
|
|
68
|
+
this._parsed = new Map(); // sql text -> parsed statement
|
|
69
|
+
this._lastError = null;
|
|
70
|
+
this._failures = 0;
|
|
71
|
+
this._retryAt = 0;
|
|
72
|
+
this._listeners = new Set();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Called by a node that wants to use this connection. */
|
|
76
|
+
retain() {
|
|
77
|
+
this.refs++;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Called when a node closes. The pool shuts down once nobody holds it, which
|
|
82
|
+
* is what makes a redeploy release sockets instead of stacking pools.
|
|
83
|
+
*/
|
|
84
|
+
async release() {
|
|
85
|
+
this.refs = Math.max(0, this.refs - 1);
|
|
86
|
+
if (this.refs === 0 && (this.state === STATES.READY || this.state === STATES.CONNECTING)) {
|
|
87
|
+
await this.close();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Subscribe to state changes; returns an unsubscribe function. */
|
|
92
|
+
onStatus(listener) {
|
|
93
|
+
this._listeners.add(listener);
|
|
94
|
+
return () => this._listeners.delete(listener);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
_emit(state, detail) {
|
|
98
|
+
this.state = state;
|
|
99
|
+
for (const listener of this._listeners) {
|
|
100
|
+
try {
|
|
101
|
+
listener(state, detail);
|
|
102
|
+
} catch {
|
|
103
|
+
// A broken status handler must not take the query path down with it.
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
get inflightCount() {
|
|
109
|
+
return this._inflight.size;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Runs a statement. `sql` is the text typed into the node; `values` come from
|
|
114
|
+
* msg. They meet only inside compile(), which binds every value as a
|
|
115
|
+
* parameter.
|
|
116
|
+
*/
|
|
117
|
+
async query(sql, values, options = {}) {
|
|
118
|
+
if (this.state === STATES.CLOSING || this.state === STATES.CLOSED) {
|
|
119
|
+
throw new PoolClosedError("Connection is shutting down", { dialect: this.config.dialect });
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const parsed = this._parse(sql);
|
|
123
|
+
const compiled = compile(parsed, values || {}, this.config.dialect, {
|
|
124
|
+
undefinedAsNull: options.undefinedAsNull ?? this.config.undefinedAsNull,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
const timeoutMs = options.timeoutMs ?? this.config.queryTimeoutMs;
|
|
128
|
+
|
|
129
|
+
// The timeout covers connecting and waiting for a free slot, not just the
|
|
130
|
+
// statement — from the caller's side that whole span is "the query".
|
|
131
|
+
const run = this._withTimeout(this._run(compiled), timeoutMs);
|
|
132
|
+
|
|
133
|
+
this._inflight.add(run);
|
|
134
|
+
try {
|
|
135
|
+
const result = await run;
|
|
136
|
+
// Rows are handed on exactly as the driver reported them, column names
|
|
137
|
+
// included — Oracle's upper case is Oracle's, not ours to rewrite.
|
|
138
|
+
return { ...result, sql: compiled.text };
|
|
139
|
+
} catch (err) {
|
|
140
|
+
// Errors this layer raised — a timeout, a full queue, a closed pool, an
|
|
141
|
+
// already-translated connect failure — must not go through the driver's
|
|
142
|
+
// translator a second time. It would not recognise the shape and would
|
|
143
|
+
// downgrade every one of them to QueryError.
|
|
144
|
+
const translated = err instanceof DbError ? err : this.adapter.translateError(err);
|
|
145
|
+
if (translated instanceof ConnectionError) this._markBroken(translated);
|
|
146
|
+
throw translated;
|
|
147
|
+
} finally {
|
|
148
|
+
this._inflight.delete(run);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async _run(compiled) {
|
|
153
|
+
const pool = await this._ensureReady();
|
|
154
|
+
await this._acquireSlot();
|
|
155
|
+
try {
|
|
156
|
+
return await this.adapter.execute(pool, compiled);
|
|
157
|
+
} finally {
|
|
158
|
+
this._releaseSlot();
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Admission control, in front of whatever the driver does.
|
|
164
|
+
*
|
|
165
|
+
* Only `pg`'s pool queues without any bound, `mysql2` and `oracledb` take a
|
|
166
|
+
* queue limit and `mssql` and `node:sqlite` take neither — so the "bounded
|
|
167
|
+
* queue" the connection dialog promises was only true on two dialects. Doing
|
|
168
|
+
* it here makes the limit mean the same thing everywhere, and gives the
|
|
169
|
+
* timeout something to cover.
|
|
170
|
+
*/
|
|
171
|
+
_acquireSlot() {
|
|
172
|
+
if (this._active < this.config.poolMax) {
|
|
173
|
+
this._active++;
|
|
174
|
+
return Promise.resolve();
|
|
175
|
+
}
|
|
176
|
+
if (this._waiters.length >= this.config.queueMax) {
|
|
177
|
+
return Promise.reject(
|
|
178
|
+
new QueueOverflowError(
|
|
179
|
+
`Too many queries waiting (${this._waiters.length}); the queue limit is ${this.config.queueMax}`,
|
|
180
|
+
{ dialect: this.config.dialect },
|
|
181
|
+
),
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
return new Promise((resolve, reject) => {
|
|
185
|
+
this._waiters.push({ resolve, reject });
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Hands the slot to the next waiter, or gives it back to the pool. */
|
|
190
|
+
_releaseSlot() {
|
|
191
|
+
const next = this._waiters.shift();
|
|
192
|
+
if (next) next.resolve();
|
|
193
|
+
else this._active = Math.max(0, this._active - 1);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Queries running right now, and queries waiting for a slot. */
|
|
197
|
+
get stats() {
|
|
198
|
+
return {
|
|
199
|
+
state: this.state,
|
|
200
|
+
active: this._active,
|
|
201
|
+
waiting: this._waiters.length,
|
|
202
|
+
inflight: this._inflight.size,
|
|
203
|
+
refs: this.refs,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Parses once per statement text — the node's SQL does not change per message. */
|
|
208
|
+
_parse(sql) {
|
|
209
|
+
const cached = this._parsed.get(sql);
|
|
210
|
+
if (cached) return cached;
|
|
211
|
+
|
|
212
|
+
const parsed = parseSql(sql, this.config.dialect);
|
|
213
|
+
// The cache is keyed by node-configured SQL, so it is bounded in practice.
|
|
214
|
+
// The guard is here for the API being used programmatically.
|
|
215
|
+
if (this._parsed.size > 500) this._parsed.clear();
|
|
216
|
+
this._parsed.set(sql, parsed);
|
|
217
|
+
return parsed;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async _ensureReady() {
|
|
221
|
+
if (this.state === STATES.READY && this._pool) return this._pool;
|
|
222
|
+
if (this.state === STATES.CLOSING || this.state === STATES.CLOSED) {
|
|
223
|
+
throw new PoolClosedError("Connection is shutting down", { dialect: this.config.dialect });
|
|
224
|
+
}
|
|
225
|
+
// Single-flight: ten messages arriving at once open one pool, not ten.
|
|
226
|
+
if (this._connecting) return this._connecting;
|
|
227
|
+
|
|
228
|
+
if (this.state === STATES.FAILED && Date.now() < this._retryAt) {
|
|
229
|
+
throw this._lastError;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
this._emit(STATES.CONNECTING);
|
|
233
|
+
this._connecting = this._connect()
|
|
234
|
+
.then((pool) => {
|
|
235
|
+
this._pool = pool;
|
|
236
|
+
this._failures = 0;
|
|
237
|
+
this._lastError = null;
|
|
238
|
+
this._emit(STATES.READY);
|
|
239
|
+
return pool;
|
|
240
|
+
})
|
|
241
|
+
.catch((err) => {
|
|
242
|
+
const translated = this.adapter.translateError(err);
|
|
243
|
+
this._failures++;
|
|
244
|
+
this._lastError = translated;
|
|
245
|
+
this._retryAt = Date.now() + this._backoffMs();
|
|
246
|
+
this._emit(STATES.FAILED, translated);
|
|
247
|
+
throw translated;
|
|
248
|
+
})
|
|
249
|
+
.finally(() => {
|
|
250
|
+
this._connecting = null;
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
return this._connecting;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
_connect() {
|
|
257
|
+
return this.adapter.createPool({
|
|
258
|
+
...this.config,
|
|
259
|
+
onPoolError: (err) => this._markBroken(err),
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** Exponential backoff with jitter, so a dead server is not hammered. */
|
|
264
|
+
_backoffMs() {
|
|
265
|
+
const base = Math.min(this.config.retryMaxMs, this.config.retryMinMs * 2 ** Math.min(this._failures - 1, 10));
|
|
266
|
+
return Math.round(base * (0.5 + Math.random() * 0.5));
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* The driver reported the pool is unusable. Drop it so the next query
|
|
271
|
+
* reconnects, rather than serving from a pool of dead sockets.
|
|
272
|
+
*/
|
|
273
|
+
_markBroken(err) {
|
|
274
|
+
if (this.state === STATES.CLOSING || this.state === STATES.CLOSED) return;
|
|
275
|
+
|
|
276
|
+
this._lastError = err;
|
|
277
|
+
this._failures++;
|
|
278
|
+
this._retryAt = Date.now() + this._backoffMs();
|
|
279
|
+
this._emit(STATES.FAILED, err);
|
|
280
|
+
|
|
281
|
+
const pool = this._pool;
|
|
282
|
+
this._pool = null;
|
|
283
|
+
if (pool) {
|
|
284
|
+
// Best effort: the pool is already broken, a failing close changes nothing.
|
|
285
|
+
Promise.resolve(this.adapter.closePool(pool)).catch(() => {});
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Rejects when the driver takes too long. The adapters also set native
|
|
291
|
+
* timeouts; this is the backstop for drivers that have none, and it always
|
|
292
|
+
* clears its timer so nothing keeps the event loop alive.
|
|
293
|
+
*/
|
|
294
|
+
_withTimeout(promise, timeoutMs) {
|
|
295
|
+
if (!timeoutMs || timeoutMs <= 0) return promise;
|
|
296
|
+
|
|
297
|
+
let timer;
|
|
298
|
+
const timeout = new Promise((_resolve, reject) => {
|
|
299
|
+
timer = setTimeout(() => {
|
|
300
|
+
reject(new TimeoutError(`Query exceeded ${timeoutMs} ms`, { dialect: this.config.dialect }));
|
|
301
|
+
}, timeoutMs);
|
|
302
|
+
if (timer.unref) timer.unref();
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
return Promise.race([promise, timeout]).finally(() => clearTimeout(timer));
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Waits for in-flight queries (bounded by drainMs), then closes the driver
|
|
310
|
+
* pool. Safe to call twice; always resolves so a node's done() is never lost.
|
|
311
|
+
*/
|
|
312
|
+
async close(options = {}) {
|
|
313
|
+
if (this.state === STATES.CLOSED) return;
|
|
314
|
+
if (this._closing) return this._closing;
|
|
315
|
+
|
|
316
|
+
this._closing = (async () => {
|
|
317
|
+
this._emit(STATES.CLOSING);
|
|
318
|
+
|
|
319
|
+
// Queries still waiting for a slot will never get one — telling them so
|
|
320
|
+
// now is what keeps the drain below from waiting out its whole timeout.
|
|
321
|
+
const waiters = this._waiters.splice(0);
|
|
322
|
+
for (const waiter of waiters) {
|
|
323
|
+
waiter.reject(new PoolClosedError("Connection is shutting down", { dialect: this.config.dialect }));
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const drainMs = options.drainMs ?? this.config.drainMs;
|
|
327
|
+
if (this._inflight.size > 0) {
|
|
328
|
+
await Promise.race([Promise.allSettled([...this._inflight]), sleep(drainMs)]);
|
|
329
|
+
}
|
|
330
|
+
this._inflight.clear();
|
|
331
|
+
|
|
332
|
+
const pool = this._pool;
|
|
333
|
+
this._pool = null;
|
|
334
|
+
this._parsed.clear();
|
|
335
|
+
|
|
336
|
+
if (pool) {
|
|
337
|
+
try {
|
|
338
|
+
await this.adapter.closePool(pool);
|
|
339
|
+
} catch {
|
|
340
|
+
// Nothing useful to do: the node is going away either way.
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
this._emit(STATES.CLOSED);
|
|
345
|
+
this._listeners.clear();
|
|
346
|
+
})();
|
|
347
|
+
|
|
348
|
+
return this._closing;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function sleep(ms) {
|
|
353
|
+
return new Promise((resolve) => {
|
|
354
|
+
const timer = setTimeout(resolve, ms);
|
|
355
|
+
if (timer.unref) timer.unref();
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
function normalizeConfig(config) {
|
|
360
|
+
if (!config || typeof config !== "object") {
|
|
361
|
+
throw new ParameterError("Connection config is required");
|
|
362
|
+
}
|
|
363
|
+
const merged = { ...DEFAULTS, ...config };
|
|
364
|
+
|
|
365
|
+
for (const key of [
|
|
366
|
+
"poolMin",
|
|
367
|
+
"poolMax",
|
|
368
|
+
"idleTimeoutMs",
|
|
369
|
+
"connectTimeoutMs",
|
|
370
|
+
"queryTimeoutMs",
|
|
371
|
+
"queueMax",
|
|
372
|
+
"drainMs",
|
|
373
|
+
]) {
|
|
374
|
+
merged[key] = toPositiveInt(merged[key], DEFAULTS[key], key);
|
|
375
|
+
}
|
|
376
|
+
if (merged.poolMax < 1) merged.poolMax = 1;
|
|
377
|
+
if (merged.poolMin > merged.poolMax) merged.poolMin = merged.poolMax;
|
|
378
|
+
|
|
379
|
+
return merged;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function toPositiveInt(value, fallback, key) {
|
|
383
|
+
if (value === undefined || value === null || value === "") return fallback;
|
|
384
|
+
const n = typeof value === "number" ? value : Number.parseInt(String(value), 10);
|
|
385
|
+
if (!Number.isFinite(n) || n < 0) {
|
|
386
|
+
throw new ParameterError(`${key} must be a non-negative number, got ${JSON.stringify(value)}`);
|
|
387
|
+
}
|
|
388
|
+
return n;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
module.exports = { ManagedPool, STATES, DEFAULTS };
|
package/lib/settings.js
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { resolveDatabase } = require("./sqlite-files");
|
|
4
|
+
const { ParameterError } = require("./errors");
|
|
5
|
+
const { byDatabase } = require("./sql/dialect");
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Per-dialect connection settings.
|
|
9
|
+
*
|
|
10
|
+
* Every database wants different things — a Postgres SSL mode, a SQL Server
|
|
11
|
+
* instance name, an Oracle connect descriptor. Hiding them all behind one
|
|
12
|
+
* "advanced JSON" box means the user has to know each driver's config shape by
|
|
13
|
+
* heart. Here each dialect declares its own fields, the editor shows only the
|
|
14
|
+
* ones that apply, and this module turns them into driver configuration.
|
|
15
|
+
*
|
|
16
|
+
* FIELDS describes the same fields the dialog in nodes/connection.html shows,
|
|
17
|
+
* keyed by engine. Products that share an engine share its fields until one of
|
|
18
|
+
* them declares its own.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const FIELDS = {
|
|
22
|
+
postgres: [
|
|
23
|
+
{
|
|
24
|
+
key: "pgSsl",
|
|
25
|
+
label: "SSL",
|
|
26
|
+
type: "select",
|
|
27
|
+
default: "disable",
|
|
28
|
+
options: [
|
|
29
|
+
["disable", "off"],
|
|
30
|
+
["require", "require (no certificate check)"],
|
|
31
|
+
["verify-ca", "verify CA"],
|
|
32
|
+
["verify-full", "verify CA and hostname"],
|
|
33
|
+
],
|
|
34
|
+
},
|
|
35
|
+
{ key: "pgCa", label: "CA certificate", type: "text", default: "", dependsOn: "pgSsl" },
|
|
36
|
+
{ key: "pgSchema", label: "Schema", type: "text", default: "", placeholder: "public" },
|
|
37
|
+
{
|
|
38
|
+
key: "appName",
|
|
39
|
+
label: "Application name",
|
|
40
|
+
type: "text",
|
|
41
|
+
default: "",
|
|
42
|
+
placeholder: "node-red",
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
mysql: [
|
|
46
|
+
{ key: "mySsl", label: "SSL", type: "checkbox", default: false },
|
|
47
|
+
{
|
|
48
|
+
key: "mySslVerify",
|
|
49
|
+
label: "Verify certificate",
|
|
50
|
+
type: "checkbox",
|
|
51
|
+
default: true,
|
|
52
|
+
dependsOn: "mySsl",
|
|
53
|
+
},
|
|
54
|
+
{ key: "myCharset", label: "Charset", type: "text", default: "", placeholder: "utf8mb4" },
|
|
55
|
+
{ key: "myTimezone", label: "Timezone", type: "text", default: "", placeholder: "Z (UTC)" },
|
|
56
|
+
],
|
|
57
|
+
mssql: [
|
|
58
|
+
{ key: "msEncrypt", label: "Encrypt", type: "checkbox", default: true },
|
|
59
|
+
{ key: "msTrustCert", label: "Trust server certificate", type: "checkbox", default: false },
|
|
60
|
+
{
|
|
61
|
+
key: "msInstance",
|
|
62
|
+
label: "Instance name",
|
|
63
|
+
type: "text",
|
|
64
|
+
default: "",
|
|
65
|
+
placeholder: "SQLEXPRESS",
|
|
66
|
+
},
|
|
67
|
+
{ key: "msDomain", label: "Domain (NTLM)", type: "text", default: "" },
|
|
68
|
+
{
|
|
69
|
+
key: "appName",
|
|
70
|
+
label: "Application name",
|
|
71
|
+
type: "text",
|
|
72
|
+
default: "",
|
|
73
|
+
placeholder: "node-red",
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
sqlite: [
|
|
77
|
+
{
|
|
78
|
+
key: "sqliteFile",
|
|
79
|
+
label: "Database file",
|
|
80
|
+
type: "text",
|
|
81
|
+
default: "",
|
|
82
|
+
placeholder: "sensors or :memory:",
|
|
83
|
+
},
|
|
84
|
+
{ key: "sqliteReadOnly", label: "Read only", type: "checkbox", default: false },
|
|
85
|
+
{ key: "sqliteWal", label: "WAL journal", type: "checkbox", default: true },
|
|
86
|
+
{ key: "sqliteForeignKeys", label: "Enforce foreign keys", type: "checkbox", default: true },
|
|
87
|
+
{ key: "sqliteBusyTimeoutMs", label: "Busy timeout", type: "text", default: 5000 },
|
|
88
|
+
],
|
|
89
|
+
firebird: [{ key: "fbRole", label: "Role", type: "text", default: "", placeholder: "none" }],
|
|
90
|
+
clickhouse: [{ key: "chTls", label: "HTTPS", type: "checkbox", default: false }],
|
|
91
|
+
oracle: [
|
|
92
|
+
{
|
|
93
|
+
key: "oraMode",
|
|
94
|
+
label: "Connect using",
|
|
95
|
+
type: "select",
|
|
96
|
+
default: "easy",
|
|
97
|
+
options: [
|
|
98
|
+
["easy", "host, port and service name"],
|
|
99
|
+
["descriptor", "full connect string / TNS alias"],
|
|
100
|
+
],
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
key: "oraConnectString",
|
|
104
|
+
label: "Connect string",
|
|
105
|
+
type: "text",
|
|
106
|
+
default: "",
|
|
107
|
+
placeholder: "host:1521/FREEPDB1",
|
|
108
|
+
dependsOn: "oraMode",
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
key: "oraIdentifier",
|
|
112
|
+
label: "Identifier is",
|
|
113
|
+
type: "select",
|
|
114
|
+
default: "service",
|
|
115
|
+
options: [
|
|
116
|
+
["service", "service name"],
|
|
117
|
+
["sid", "SID"],
|
|
118
|
+
],
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const APP_NAME = "fromcubes-db";
|
|
124
|
+
|
|
125
|
+
function truthy(value) {
|
|
126
|
+
return value === true || value === "true";
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function text(value) {
|
|
130
|
+
return typeof value === "string" ? value.trim() : "";
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function postgres(config) {
|
|
134
|
+
const driver = {};
|
|
135
|
+
const options = {};
|
|
136
|
+
|
|
137
|
+
const mode = config.pgSsl || "disable";
|
|
138
|
+
if (mode !== "disable") {
|
|
139
|
+
const ca = text(config.pgCa);
|
|
140
|
+
options.ssl = {
|
|
141
|
+
rejectUnauthorized: mode !== "require",
|
|
142
|
+
// verify-full is the default once a CA is checked; pg only skips the
|
|
143
|
+
// hostname check when explicitly told to.
|
|
144
|
+
...(mode === "verify-ca" ? { checkServerIdentity: () => undefined } : {}),
|
|
145
|
+
...(ca ? { ca } : {}),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const schema = text(config.pgSchema);
|
|
150
|
+
// libpq-style startup options; the driver forwards them verbatim.
|
|
151
|
+
if (schema) options.options = `-c search_path=${schema}`;
|
|
152
|
+
|
|
153
|
+
options.application_name = text(config.appName) || APP_NAME;
|
|
154
|
+
|
|
155
|
+
driver.options = options;
|
|
156
|
+
return driver;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function mysql(config) {
|
|
160
|
+
const options = {};
|
|
161
|
+
|
|
162
|
+
if (truthy(config.mySsl)) {
|
|
163
|
+
options.ssl = { rejectUnauthorized: config.mySslVerify !== false };
|
|
164
|
+
}
|
|
165
|
+
const charset = text(config.myCharset);
|
|
166
|
+
if (charset) options.charset = charset;
|
|
167
|
+
|
|
168
|
+
const timezone = text(config.myTimezone);
|
|
169
|
+
if (timezone) options.timezone = timezone;
|
|
170
|
+
|
|
171
|
+
return { options };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function mssql(config) {
|
|
175
|
+
const nested = {
|
|
176
|
+
encrypt: config.msEncrypt !== false,
|
|
177
|
+
trustServerCertificate: truthy(config.msTrustCert),
|
|
178
|
+
appName: text(config.appName) || APP_NAME,
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const instance = text(config.msInstance);
|
|
182
|
+
if (instance) nested.instanceName = instance;
|
|
183
|
+
|
|
184
|
+
const options = { options: nested };
|
|
185
|
+
|
|
186
|
+
const domain = text(config.msDomain);
|
|
187
|
+
if (domain) options.domain = domain;
|
|
188
|
+
|
|
189
|
+
return { options };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function oracle(config) {
|
|
193
|
+
if (config.oraMode === "descriptor") {
|
|
194
|
+
const connectString = text(config.oraConnectString);
|
|
195
|
+
if (!connectString) {
|
|
196
|
+
throw new ParameterError("Oracle connect string is required in descriptor mode", {
|
|
197
|
+
dialect: "oracle",
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
return { connectString };
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const host = text(config.host);
|
|
204
|
+
const port = config.port || 1521;
|
|
205
|
+
const identifier = text(config.database);
|
|
206
|
+
if (!host || !identifier) {
|
|
207
|
+
throw new ParameterError("Oracle needs a host and a service name (or SID)", {
|
|
208
|
+
dialect: "oracle",
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Easy Connect uses "/" for a service name and ":" for a SID.
|
|
213
|
+
const separator = config.oraIdentifier === "sid" ? ":" : "/";
|
|
214
|
+
return { connectString: `${host}:${port}${separator}${identifier}` };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* The dialog names a database, not a file: `sensors` is
|
|
219
|
+
* `<userDir>/fromcubes-db/sensors.db` (see lib/sqlite-files.js).
|
|
220
|
+
*/
|
|
221
|
+
function sqlite(config, context = {}) {
|
|
222
|
+
const name = text(config.sqliteFile) || text(config.database);
|
|
223
|
+
if (!name) {
|
|
224
|
+
throw new ParameterError("SQLite needs a database name (or :memory:)", { dialect: "sqlite" });
|
|
225
|
+
}
|
|
226
|
+
const { file, directory } = resolveDatabase(name, context.userDir);
|
|
227
|
+
|
|
228
|
+
const busy = Number(config.sqliteBusyTimeoutMs);
|
|
229
|
+
|
|
230
|
+
return {
|
|
231
|
+
file,
|
|
232
|
+
directory,
|
|
233
|
+
readOnly: truthy(config.sqliteReadOnly),
|
|
234
|
+
// The checkboxes default to on, so only an explicit false turns them off.
|
|
235
|
+
wal: config.sqliteWal !== false && config.sqliteWal !== "false",
|
|
236
|
+
foreignKeys: config.sqliteForeignKeys !== false && config.sqliteForeignKeys !== "false",
|
|
237
|
+
busyTimeoutMs: Number.isFinite(busy) && busy >= 0 ? busy : 5000,
|
|
238
|
+
options: {},
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Keyed by engine and read through byDatabase, so MariaDB or CockroachDB can
|
|
243
|
+
// take over its own builder later without every caller learning about it.
|
|
244
|
+
/** The database is a path or an alias on the server, as Firebird's own tools take it. */
|
|
245
|
+
function firebird(config) {
|
|
246
|
+
const role = text(config.fbRole);
|
|
247
|
+
return role ? { role } : {};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** HTTP interface; HTTPS when asked (port 8443 by convention). */
|
|
251
|
+
function clickhouse(config) {
|
|
252
|
+
return config.chTls ? { tls: true } : {};
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const BUILDERS = { postgres, mysql, mssql, oracle, sqlite, firebird, clickhouse };
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Turns the dialect-specific dialog fields into driver configuration.
|
|
259
|
+
* Anything the user typed into Advanced JSON is merged last and wins, so the
|
|
260
|
+
* escape hatch stays open for options no field covers.
|
|
261
|
+
*
|
|
262
|
+
* @param {string} dialect
|
|
263
|
+
* @param {Record<string, unknown>} config the node's configuration
|
|
264
|
+
* @param {Record<string, unknown>} [advanced] parsed Advanced JSON
|
|
265
|
+
* @param {{ userDir?: string }} [context] where Node-RED keeps its files (SQLite)
|
|
266
|
+
*/
|
|
267
|
+
function driverConfigFor(dialect, config, advanced, context = {}) {
|
|
268
|
+
const build = byDatabase(BUILDERS, dialect);
|
|
269
|
+
if (!build) throw new ParameterError(`Unknown dialect: ${dialect}`);
|
|
270
|
+
|
|
271
|
+
const base = build(config, context);
|
|
272
|
+
if (!advanced || Object.keys(advanced).length === 0) return base;
|
|
273
|
+
|
|
274
|
+
// Advanced JSON *is* driver configuration — the same shape each driver's own
|
|
275
|
+
// documentation uses. It merges key by key, so adding one option does not
|
|
276
|
+
// discard what the dialog set.
|
|
277
|
+
return { ...base, options: mergeOptions(base.options, advanced) };
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function isPlainObject(value) {
|
|
281
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Merges one level deep, but only where both sides are plain objects.
|
|
286
|
+
* `options.options` is a libpq string on Postgres and a settings object on SQL
|
|
287
|
+
* Server — spreading the string would turn it into a map of characters.
|
|
288
|
+
*/
|
|
289
|
+
function mergeOptions(base, extra) {
|
|
290
|
+
if (!isPlainObject(base)) return extra;
|
|
291
|
+
if (!isPlainObject(extra)) return extra === undefined ? base : extra;
|
|
292
|
+
|
|
293
|
+
const out = { ...base };
|
|
294
|
+
for (const key of Object.keys(extra)) {
|
|
295
|
+
const left = base[key];
|
|
296
|
+
const right = extra[key];
|
|
297
|
+
out[key] = isPlainObject(left) && isPlainObject(right) ? { ...left, ...right } : right;
|
|
298
|
+
}
|
|
299
|
+
return out;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
module.exports = { driverConfigFor, FIELDS };
|