@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
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Where SQLite databases live when Node-RED runs them: one folder,
|
|
5
|
+
* `<userDir>/fromcubes-db/`, created on first use. A connection names a
|
|
6
|
+
* database — `sensors` — and never a path, so the same flow opens the same
|
|
7
|
+
* file whether Node-RED was started from a shell, systemd or Docker, and no
|
|
8
|
+
* connection can reach a file outside that folder.
|
|
9
|
+
*
|
|
10
|
+
* The adapter itself still takes a plain `file`: used as a library, outside
|
|
11
|
+
* Node-RED, the caller decides where its database is.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const fs = require("node:fs");
|
|
15
|
+
const path = require("node:path");
|
|
16
|
+
const { ParameterError } = require("./errors");
|
|
17
|
+
|
|
18
|
+
const DIRECTORY = "fromcubes-db";
|
|
19
|
+
const EXTENSIONS = [".db", ".sqlite", ".sqlite3"];
|
|
20
|
+
const DEFAULT_EXTENSION = ".db";
|
|
21
|
+
const MEMORY = ":memory:";
|
|
22
|
+
|
|
23
|
+
// Letters, digits, and . _ - inside; no separator, no leading dot (hidden
|
|
24
|
+
// files, `..`), nothing a filesystem would read as something else.
|
|
25
|
+
const NAME = /^[A-Za-z0-9](?:[A-Za-z0-9._-]{0,98}[A-Za-z0-9])?$/;
|
|
26
|
+
// Windows refuses these as file names whatever the extension.
|
|
27
|
+
const RESERVED = /^(?:con|prn|aux|nul|com\d|lpt\d)(?:\.|$)/i;
|
|
28
|
+
|
|
29
|
+
/** The folder every database of this Node-RED instance lives in. */
|
|
30
|
+
function databaseDirectory(userDir) {
|
|
31
|
+
if (typeof userDir !== "string" || userDir.trim() === "") {
|
|
32
|
+
throw new ParameterError("SQLite needs Node-RED's userDir to know where its databases live", {
|
|
33
|
+
dialect: "sqlite",
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return path.join(userDir, DIRECTORY);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The file name for a database name: `sensors` -> `sensors.db`, while
|
|
41
|
+
* `sensors.sqlite` keeps its own extension. Throws on anything that is not a
|
|
42
|
+
* plain name, so a path never gets as far as path.join.
|
|
43
|
+
*
|
|
44
|
+
* @param {string} name
|
|
45
|
+
* @returns {string}
|
|
46
|
+
*/
|
|
47
|
+
function fileNameFor(name) {
|
|
48
|
+
const trimmed = typeof name === "string" ? name.trim() : "";
|
|
49
|
+
if (!NAME.test(trimmed) || trimmed.includes("..") || RESERVED.test(trimmed)) {
|
|
50
|
+
throw new ParameterError(
|
|
51
|
+
`SQLite database "${trimmed}" is not a plain name — use letters, digits, . _ - (e.g. sensors), not a path`,
|
|
52
|
+
{ dialect: "sqlite" },
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
const hasExtension = EXTENSIONS.includes(path.extname(trimmed).toLowerCase());
|
|
56
|
+
return hasExtension ? trimmed : trimmed + DEFAULT_EXTENSION;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Database name -> absolute file inside the folder, or `:memory:`.
|
|
61
|
+
*
|
|
62
|
+
* @param {string} name
|
|
63
|
+
* @param {string} userDir
|
|
64
|
+
* @returns {{ file: string, directory?: string }}
|
|
65
|
+
*/
|
|
66
|
+
function resolveDatabase(name, userDir) {
|
|
67
|
+
if (typeof name === "string" && name.trim() === MEMORY) return { file: MEMORY };
|
|
68
|
+
const directory = databaseDirectory(userDir);
|
|
69
|
+
const file = path.join(directory, fileNameFor(name));
|
|
70
|
+
// Belt and braces: the name check already rules this out.
|
|
71
|
+
if (path.dirname(file) !== directory) {
|
|
72
|
+
throw new ParameterError(`SQLite database "${name}" would land outside ${directory}`, { dialect: "sqlite" });
|
|
73
|
+
}
|
|
74
|
+
return { file, directory };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Creates the folder if it is missing (owner-only, it holds data) and refuses
|
|
79
|
+
* a database file that is a symbolic link — the one way left to point a name
|
|
80
|
+
* at a file elsewhere.
|
|
81
|
+
*/
|
|
82
|
+
function prepareDatabase({ file, directory }) {
|
|
83
|
+
if (!directory) return;
|
|
84
|
+
fs.mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
85
|
+
let stat = null;
|
|
86
|
+
try {
|
|
87
|
+
stat = fs.lstatSync(file);
|
|
88
|
+
} catch (err) {
|
|
89
|
+
if (err.code !== "ENOENT") throw err;
|
|
90
|
+
}
|
|
91
|
+
if (stat && !stat.isFile()) {
|
|
92
|
+
throw new ParameterError(`SQLite database ${path.basename(file)} is not a regular file`, { dialect: "sqlite" });
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The databases already in the folder, for the edit dialog's picker. Regular
|
|
98
|
+
* files with a database extension only: no links, no `-wal`/`-shm` siblings.
|
|
99
|
+
*
|
|
100
|
+
* @param {string} userDir
|
|
101
|
+
* @returns {{ directory: string, databases: { name: string, file: string, size: number, modified: string }[] }}
|
|
102
|
+
*/
|
|
103
|
+
function listDatabases(userDir) {
|
|
104
|
+
const directory = databaseDirectory(userDir);
|
|
105
|
+
let entries = [];
|
|
106
|
+
try {
|
|
107
|
+
entries = fs.readdirSync(directory, { withFileTypes: true });
|
|
108
|
+
} catch (err) {
|
|
109
|
+
if (err.code !== "ENOENT") throw err;
|
|
110
|
+
}
|
|
111
|
+
const databases = [];
|
|
112
|
+
for (const entry of entries) {
|
|
113
|
+
if (!entry.isFile()) continue;
|
|
114
|
+
const ext = path.extname(entry.name).toLowerCase();
|
|
115
|
+
if (!EXTENSIONS.includes(ext) || !NAME.test(entry.name)) continue;
|
|
116
|
+
const stat = fs.statSync(path.join(directory, entry.name));
|
|
117
|
+
databases.push({
|
|
118
|
+
// The name a connection uses: the default extension is left off.
|
|
119
|
+
name: ext === DEFAULT_EXTENSION ? entry.name.slice(0, -ext.length) : entry.name,
|
|
120
|
+
file: entry.name,
|
|
121
|
+
size: stat.size,
|
|
122
|
+
modified: stat.mtime.toISOString(),
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
databases.sort((a, b) => a.name.localeCompare(b.name));
|
|
126
|
+
return { directory, databases };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
module.exports = {
|
|
130
|
+
DIRECTORY,
|
|
131
|
+
MEMORY,
|
|
132
|
+
databaseDirectory,
|
|
133
|
+
fileNameFor,
|
|
134
|
+
resolveDatabase,
|
|
135
|
+
prepareDatabase,
|
|
136
|
+
listDatabases,
|
|
137
|
+
};
|