@dbx-app/node-core 0.4.6 → 0.4.7
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 -661
- package/dist/backend.d.ts +1 -0
- package/dist/backend.js +2 -1
- package/dist/connections.d.ts +16 -8
- package/dist/connections.js +87 -20
- package/dist/database.d.ts +5 -0
- package/dist/database.js +107 -18
- package/dist/diagnostics.d.ts +2 -2
- package/dist/diagnostics.js +4 -0
- package/dist/web-backend.js +17 -2
- package/package.json +3 -3
package/dist/backend.d.ts
CHANGED
|
@@ -8,5 +8,6 @@ export interface Backend {
|
|
|
8
8
|
listTables(config: ConnectionConfig, schema?: string): Promise<TableInfo[]>;
|
|
9
9
|
describeTable(config: ConnectionConfig, table: string, schema?: string): Promise<ColumnInfo[]>;
|
|
10
10
|
executeQuery(config: ConnectionConfig, sql: string, options?: QueryOptions): Promise<QueryResult>;
|
|
11
|
+
close?(): Promise<void>;
|
|
11
12
|
}
|
|
12
13
|
export declare function createBackend(env?: NodeJS.ProcessEnv): Promise<Backend>;
|
package/dist/backend.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { addConnection as desktopAddConnection, findConnection as desktopFindConnection, loadConnections as desktopLoadConnections, removeConnection as desktopRemoveConnection, } from "./connections.js";
|
|
2
|
-
import { describeTable as desktopDescribeTable, executeQuery as desktopExecuteQuery, listTables as desktopListTables, } from "./database.js";
|
|
2
|
+
import { closeDatabaseResources as desktopCloseDatabaseResources, describeTable as desktopDescribeTable, executeQuery as desktopExecuteQuery, listTables as desktopListTables, } from "./database.js";
|
|
3
3
|
export async function createBackend(env = process.env) {
|
|
4
4
|
if (env.DBX_WEB_URL) {
|
|
5
5
|
return await import("./web-backend.js");
|
|
@@ -12,5 +12,6 @@ export async function createBackend(env = process.env) {
|
|
|
12
12
|
listTables: desktopListTables,
|
|
13
13
|
describeTable: desktopDescribeTable,
|
|
14
14
|
executeQuery: desktopExecuteQuery,
|
|
15
|
+
close: desktopCloseDatabaseResources,
|
|
15
16
|
};
|
|
16
17
|
}
|
package/dist/connections.d.ts
CHANGED
|
@@ -9,14 +9,7 @@ export interface ConnectionConfig {
|
|
|
9
9
|
password: string;
|
|
10
10
|
database?: string;
|
|
11
11
|
url_params?: string;
|
|
12
|
-
|
|
13
|
-
ssh_tunnels?: SshTunnelConfig[];
|
|
14
|
-
proxy_enabled?: boolean;
|
|
15
|
-
proxy_type?: "socks5" | "http";
|
|
16
|
-
proxy_host?: string;
|
|
17
|
-
proxy_port?: number;
|
|
18
|
-
proxy_username?: string;
|
|
19
|
-
proxy_password?: string;
|
|
12
|
+
transport_layers?: TransportLayerConfig[];
|
|
20
13
|
ssl: boolean;
|
|
21
14
|
ca_cert_path?: string;
|
|
22
15
|
oracle_connection_type?: "service_name" | "sid";
|
|
@@ -27,6 +20,11 @@ export interface ConnectionConfig {
|
|
|
27
20
|
redis_sentinel_password?: string;
|
|
28
21
|
redis_sentinel_tls?: boolean;
|
|
29
22
|
}
|
|
23
|
+
export type TransportLayerConfig = ({
|
|
24
|
+
type: "ssh";
|
|
25
|
+
} & SshTunnelConfig) | ({
|
|
26
|
+
type: "proxy";
|
|
27
|
+
} & ProxyTunnelConfig);
|
|
30
28
|
export interface SshTunnelConfig {
|
|
31
29
|
id: string;
|
|
32
30
|
name?: string;
|
|
@@ -40,6 +38,16 @@ export interface SshTunnelConfig {
|
|
|
40
38
|
connect_timeout_secs?: number;
|
|
41
39
|
expose_lan?: boolean;
|
|
42
40
|
}
|
|
41
|
+
export interface ProxyTunnelConfig {
|
|
42
|
+
id: string;
|
|
43
|
+
name?: string;
|
|
44
|
+
enabled?: boolean;
|
|
45
|
+
proxy_type?: "socks5" | "http";
|
|
46
|
+
host: string;
|
|
47
|
+
port: number;
|
|
48
|
+
username?: string;
|
|
49
|
+
password?: string;
|
|
50
|
+
}
|
|
43
51
|
export interface ConnectionStoreOptions {
|
|
44
52
|
path?: string;
|
|
45
53
|
}
|
package/dist/connections.js
CHANGED
|
@@ -37,6 +37,74 @@ function getSecret(db, connectionId, key) {
|
|
|
37
37
|
.get(connectionId, key);
|
|
38
38
|
return row?.secret ?? "";
|
|
39
39
|
}
|
|
40
|
+
function transportLayerSecretSegment(index, layer) {
|
|
41
|
+
return layer.id?.trim() || String(index);
|
|
42
|
+
}
|
|
43
|
+
function transportLayerSshPasswordKey(index, layer) {
|
|
44
|
+
return `transport_layers.${transportLayerSecretSegment(index, layer)}.ssh_password`;
|
|
45
|
+
}
|
|
46
|
+
function transportLayerSshKeyPassphraseKey(index, layer) {
|
|
47
|
+
return `transport_layers.${transportLayerSecretSegment(index, layer)}.ssh_key_passphrase`;
|
|
48
|
+
}
|
|
49
|
+
function transportLayerProxyPasswordKey(index, layer) {
|
|
50
|
+
return `transport_layers.${transportLayerSecretSegment(index, layer)}.proxy_password`;
|
|
51
|
+
}
|
|
52
|
+
function normalizeTransportLayers(config) {
|
|
53
|
+
if (Array.isArray(config.transport_layers) && config.transport_layers.length > 0)
|
|
54
|
+
return config.transport_layers;
|
|
55
|
+
const layers = [];
|
|
56
|
+
if (config.ssh_enabled && Array.isArray(config.ssh_tunnels) && config.ssh_tunnels.length > 0) {
|
|
57
|
+
layers.push(...config.ssh_tunnels.map((hop) => ({ type: "ssh", ...hop })));
|
|
58
|
+
}
|
|
59
|
+
else if (config.ssh_enabled && config.ssh_host) {
|
|
60
|
+
layers.push({
|
|
61
|
+
type: "ssh",
|
|
62
|
+
id: "legacy",
|
|
63
|
+
enabled: true,
|
|
64
|
+
host: config.ssh_host,
|
|
65
|
+
port: config.ssh_port || 22,
|
|
66
|
+
user: config.ssh_user || "",
|
|
67
|
+
password: config.ssh_password || "",
|
|
68
|
+
key_path: config.ssh_key_path || "",
|
|
69
|
+
key_passphrase: config.ssh_key_passphrase || "",
|
|
70
|
+
connect_timeout_secs: config.ssh_connect_timeout_secs || 5,
|
|
71
|
+
expose_lan: !!config.ssh_expose_lan,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
if (config.proxy_enabled && config.proxy_host) {
|
|
75
|
+
layers.push({
|
|
76
|
+
type: "proxy",
|
|
77
|
+
id: "legacy-proxy",
|
|
78
|
+
enabled: true,
|
|
79
|
+
proxy_type: config.proxy_type || "socks5",
|
|
80
|
+
host: config.proxy_host,
|
|
81
|
+
port: config.proxy_port || 1080,
|
|
82
|
+
username: config.proxy_username || "",
|
|
83
|
+
password: config.proxy_password || "",
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return layers;
|
|
87
|
+
}
|
|
88
|
+
function hydrateTransportLayerSecrets(db, config, connectionId) {
|
|
89
|
+
config.transport_layers = normalizeTransportLayers(config);
|
|
90
|
+
config.transport_layers.forEach((layer, index) => {
|
|
91
|
+
if (layer.type === "ssh") {
|
|
92
|
+
layer.password ||=
|
|
93
|
+
getSecret(db, connectionId, transportLayerSshPasswordKey(index, layer)) ||
|
|
94
|
+
(layer.id === "legacy" ? getSecret(db, connectionId, "ssh_password") : getSecret(db, connectionId, `ssh_tunnels.${layer.id || index}.password`));
|
|
95
|
+
layer.key_passphrase ||=
|
|
96
|
+
getSecret(db, connectionId, transportLayerSshKeyPassphraseKey(index, layer)) ||
|
|
97
|
+
(layer.id === "legacy"
|
|
98
|
+
? getSecret(db, connectionId, "ssh_key_passphrase")
|
|
99
|
+
: getSecret(db, connectionId, `ssh_tunnels.${layer.id || index}.key_passphrase`));
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
layer.password ||=
|
|
103
|
+
getSecret(db, connectionId, transportLayerProxyPasswordKey(index, layer)) ||
|
|
104
|
+
(layer.id === "legacy-proxy" ? getSecret(db, connectionId, "proxy_password") : "");
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
40
108
|
export async function loadConnections(options = {}) {
|
|
41
109
|
const path = options.path ?? defaultDbPath();
|
|
42
110
|
if (!existsSync(path))
|
|
@@ -51,8 +119,7 @@ export async function loadConnections(options = {}) {
|
|
|
51
119
|
config.id = row.id;
|
|
52
120
|
if (!config.password)
|
|
53
121
|
config.password = getSecret(db, row.id, "password");
|
|
54
|
-
|
|
55
|
-
config.proxy_password = getSecret(db, row.id, "proxy_password");
|
|
122
|
+
hydrateTransportLayerSecrets(db, config, row.id);
|
|
56
123
|
if (!config.redis_sentinel_password) {
|
|
57
124
|
config.redis_sentinel_password = getSecret(db, row.id, "redis_sentinel_password");
|
|
58
125
|
}
|
|
@@ -135,21 +202,11 @@ export async function addConnection(config) {
|
|
|
135
202
|
password: "",
|
|
136
203
|
database: normalized.database ?? null,
|
|
137
204
|
color: null,
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
ssh_key_path: "",
|
|
144
|
-
ssh_key_passphrase: "",
|
|
145
|
-
ssh_expose_lan: false,
|
|
146
|
-
ssh_tunnels: normalized.ssh_tunnels ?? [],
|
|
147
|
-
proxy_enabled: normalized.proxy_enabled ?? false,
|
|
148
|
-
proxy_type: normalized.proxy_type ?? "socks5",
|
|
149
|
-
proxy_host: normalized.proxy_host ?? "",
|
|
150
|
-
proxy_port: normalized.proxy_port ?? 1080,
|
|
151
|
-
proxy_username: normalized.proxy_username ?? "",
|
|
152
|
-
proxy_password: "",
|
|
205
|
+
transport_layers: normalizeTransportLayers(normalized).map((layer) => {
|
|
206
|
+
if (layer.type === "ssh")
|
|
207
|
+
return { ...layer, password: "", key_passphrase: "" };
|
|
208
|
+
return { ...layer, password: "" };
|
|
209
|
+
}),
|
|
153
210
|
ssl: normalized.ssl ?? false,
|
|
154
211
|
sysdba: false,
|
|
155
212
|
oracle_connection_type: normalized.oracle_connection_type ?? null,
|
|
@@ -167,9 +224,19 @@ export async function addConnection(config) {
|
|
|
167
224
|
if (normalized.password) {
|
|
168
225
|
db.prepare("INSERT INTO connection_secrets (connection_id, key, secret) VALUES (?, ?, ?)").run(id, "password", normalized.password);
|
|
169
226
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
227
|
+
normalizeTransportLayers(normalized).forEach((layer, index) => {
|
|
228
|
+
if (layer.type === "ssh") {
|
|
229
|
+
if (layer.password) {
|
|
230
|
+
db.prepare("INSERT INTO connection_secrets (connection_id, key, secret) VALUES (?, ?, ?)").run(id, transportLayerSshPasswordKey(index, layer), layer.password);
|
|
231
|
+
}
|
|
232
|
+
if (layer.key_passphrase) {
|
|
233
|
+
db.prepare("INSERT INTO connection_secrets (connection_id, key, secret) VALUES (?, ?, ?)").run(id, transportLayerSshKeyPassphraseKey(index, layer), layer.key_passphrase);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
else if (layer.password) {
|
|
237
|
+
db.prepare("INSERT INTO connection_secrets (connection_id, key, secret) VALUES (?, ?, ?)").run(id, transportLayerProxyPasswordKey(index, layer), layer.password);
|
|
238
|
+
}
|
|
239
|
+
});
|
|
173
240
|
if (normalized.redis_sentinel_password) {
|
|
174
241
|
db.prepare("INSERT INTO connection_secrets (connection_id, key, secret) VALUES (?, ?, ?)").run(id, "redis_sentinel_password", normalized.redis_sentinel_password);
|
|
175
242
|
}
|
package/dist/database.d.ts
CHANGED
|
@@ -20,6 +20,7 @@ export interface QueryOptions {
|
|
|
20
20
|
maxRows?: number;
|
|
21
21
|
timeoutMs?: number;
|
|
22
22
|
}
|
|
23
|
+
export declare function closeDatabaseResources(): Promise<void>;
|
|
23
24
|
export declare function executeQuery(config: ConnectionConfig, sql: string, options?: QueryOptions): Promise<QueryResult>;
|
|
24
25
|
export declare function listTables(config: ConnectionConfig, schema?: string): Promise<TableInfo[]>;
|
|
25
26
|
export declare function describeTable(config: ConnectionConfig, table: string, schema?: string): Promise<ColumnInfo[]>;
|
|
@@ -40,6 +41,9 @@ interface MongoAggregateCommand {
|
|
|
40
41
|
collection: string;
|
|
41
42
|
pipeline: string;
|
|
42
43
|
}
|
|
44
|
+
interface MongoGetIndexesCommand {
|
|
45
|
+
collection: string;
|
|
46
|
+
}
|
|
43
47
|
export type MongoWriteCommand = {
|
|
44
48
|
kind: "insert";
|
|
45
49
|
collection: string;
|
|
@@ -59,6 +63,7 @@ export type MongoWriteCommand = {
|
|
|
59
63
|
export declare function parseMongoFindCommand(input: string): MongoFindCommand | null;
|
|
60
64
|
export declare function parseMongoCountDocumentsCommand(input: string): MongoCountDocumentsCommand | null;
|
|
61
65
|
export declare function parseMongoAggregateCommand(input: string): MongoAggregateCommand | null;
|
|
66
|
+
export declare function parseMongoGetIndexesCommand(input: string): MongoGetIndexesCommand | null;
|
|
62
67
|
export declare function mongoAggregateWriteStage(pipelineJson: string): "$out" | "$merge" | null;
|
|
63
68
|
export declare function parseMongoWriteCommand(input: string): MongoWriteCommand | null;
|
|
64
69
|
export declare function evaluateMongoWriteSafety(command: MongoWriteCommand, options: {
|
package/dist/database.js
CHANGED
|
@@ -15,6 +15,7 @@ function poolKey(config) {
|
|
|
15
15
|
}
|
|
16
16
|
function evictPool(key, entry) {
|
|
17
17
|
pools.delete(key);
|
|
18
|
+
clearTimeout(entry.timer);
|
|
18
19
|
if (entry.type === "pg") {
|
|
19
20
|
entry.pool.end().catch(() => { });
|
|
20
21
|
}
|
|
@@ -26,6 +27,26 @@ function resetIdleTimer(key, entry) {
|
|
|
26
27
|
clearTimeout(entry.timer);
|
|
27
28
|
entry.timer = setTimeout(() => evictPool(key, entry), IDLE_TIMEOUT_MS);
|
|
28
29
|
}
|
|
30
|
+
export async function closeDatabaseResources() {
|
|
31
|
+
const poolEntries = [...pools.entries()];
|
|
32
|
+
pools.clear();
|
|
33
|
+
await Promise.all(poolEntries.map(async ([, entry]) => {
|
|
34
|
+
clearTimeout(entry.timer);
|
|
35
|
+
if (entry.type === "pg") {
|
|
36
|
+
await entry.pool.end().catch(() => { });
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
await entry.pool.end().catch(() => { });
|
|
40
|
+
}
|
|
41
|
+
}));
|
|
42
|
+
const tunnels = [...proxyTunnels.values()];
|
|
43
|
+
proxyTunnels.clear();
|
|
44
|
+
await Promise.all(tunnels.map(({ server, sockets }) => new Promise((resolve) => {
|
|
45
|
+
for (const socket of sockets)
|
|
46
|
+
socket.destroy();
|
|
47
|
+
server.close(() => resolve());
|
|
48
|
+
})));
|
|
49
|
+
}
|
|
29
50
|
async function getPgPool(config) {
|
|
30
51
|
const key = poolKey(config);
|
|
31
52
|
const existing = pools.get(key);
|
|
@@ -67,15 +88,24 @@ async function getMysqlPool(config) {
|
|
|
67
88
|
resetIdleTimer(key, entry);
|
|
68
89
|
return pool;
|
|
69
90
|
}
|
|
91
|
+
function firstProxyLayer(config) {
|
|
92
|
+
return config.transport_layers?.find((layer) => layer.type === "proxy" && layer.enabled !== false && !!layer.host);
|
|
93
|
+
}
|
|
70
94
|
async function connectionEndpoint(config) {
|
|
71
|
-
|
|
95
|
+
const proxy = firstProxyLayer(config);
|
|
96
|
+
if (!proxy)
|
|
72
97
|
return { host: config.host, port: config.port };
|
|
73
98
|
const existing = proxyTunnels.get(config.id);
|
|
74
99
|
if (existing)
|
|
75
100
|
return { host: "127.0.0.1", port: existing.port };
|
|
101
|
+
const sockets = new Set();
|
|
76
102
|
const server = createServer((inbound) => {
|
|
77
|
-
|
|
103
|
+
sockets.add(inbound);
|
|
104
|
+
inbound.once("close", () => sockets.delete(inbound));
|
|
105
|
+
connectViaProxy(config, proxy)
|
|
78
106
|
.then((outbound) => {
|
|
107
|
+
sockets.add(outbound);
|
|
108
|
+
outbound.once("close", () => sockets.delete(outbound));
|
|
79
109
|
inbound.pipe(outbound);
|
|
80
110
|
outbound.pipe(inbound);
|
|
81
111
|
})
|
|
@@ -91,7 +121,7 @@ async function connectionEndpoint(config) {
|
|
|
91
121
|
reject(new Error("Failed to bind proxy tunnel"));
|
|
92
122
|
});
|
|
93
123
|
});
|
|
94
|
-
proxyTunnels.set(config.id, { server, port });
|
|
124
|
+
proxyTunnels.set(config.id, { server, port, sockets });
|
|
95
125
|
return { host: "127.0.0.1", port };
|
|
96
126
|
}
|
|
97
127
|
function buildConnectionUrl(config, endpoint) {
|
|
@@ -103,25 +133,25 @@ function buildConnectionUrl(config, endpoint) {
|
|
|
103
133
|
}
|
|
104
134
|
return `postgres://${encodeURIComponent(config.username)}:${encodeURIComponent(config.password)}@${endpoint.host}:${endpoint.port}/${db}${suffix}`;
|
|
105
135
|
}
|
|
106
|
-
function connectViaProxy(config) {
|
|
136
|
+
function connectViaProxy(config, proxy) {
|
|
107
137
|
return new Promise((resolve, reject) => {
|
|
108
|
-
const socket = netConnect(
|
|
138
|
+
const socket = netConnect(proxy.port || 1080, proxy.host || "127.0.0.1");
|
|
109
139
|
socket.once("error", reject);
|
|
110
140
|
socket.once("connect", () => {
|
|
111
|
-
if ((
|
|
112
|
-
httpConnect(socket, config, resolve, reject);
|
|
141
|
+
if ((proxy.proxy_type || "socks5") === "http") {
|
|
142
|
+
httpConnect(socket, config, proxy, resolve, reject);
|
|
113
143
|
}
|
|
114
144
|
else {
|
|
115
|
-
socks5Connect(socket, config, resolve, reject);
|
|
145
|
+
socks5Connect(socket, config, proxy, resolve, reject);
|
|
116
146
|
}
|
|
117
147
|
});
|
|
118
148
|
});
|
|
119
149
|
}
|
|
120
|
-
function httpConnect(socket, config, resolve, reject) {
|
|
150
|
+
function httpConnect(socket, config, proxy, resolve, reject) {
|
|
121
151
|
const target = `${config.host}:${config.port}`;
|
|
122
152
|
const lines = [`CONNECT ${target} HTTP/1.1`, `Host: ${target}`];
|
|
123
|
-
if (
|
|
124
|
-
const token = Buffer.from(`${
|
|
153
|
+
if (proxy.username || proxy.password) {
|
|
154
|
+
const token = Buffer.from(`${proxy.username || ""}:${proxy.password || ""}`).toString("base64");
|
|
125
155
|
lines.push(`Proxy-Authorization: Basic ${token}`);
|
|
126
156
|
}
|
|
127
157
|
socket.write(`${lines.join("\r\n")}\r\n\r\n`);
|
|
@@ -144,8 +174,8 @@ function httpConnect(socket, config, resolve, reject) {
|
|
|
144
174
|
resolve(socket);
|
|
145
175
|
});
|
|
146
176
|
}
|
|
147
|
-
function socks5Connect(socket, config, resolve, reject) {
|
|
148
|
-
const wantsAuth = !!(
|
|
177
|
+
function socks5Connect(socket, config, proxy, resolve, reject) {
|
|
178
|
+
const wantsAuth = !!(proxy.username || proxy.password);
|
|
149
179
|
socket.write(Buffer.from(wantsAuth ? [0x05, 0x02, 0x00, 0x02] : [0x05, 0x01, 0x00]));
|
|
150
180
|
socket.once("data", (method) => {
|
|
151
181
|
if (method.length < 2 || method[0] !== 0x05) {
|
|
@@ -154,8 +184,8 @@ function socks5Connect(socket, config, resolve, reject) {
|
|
|
154
184
|
return;
|
|
155
185
|
}
|
|
156
186
|
if (method[1] === 0x02) {
|
|
157
|
-
const user = Buffer.from(
|
|
158
|
-
const pass = Buffer.from(
|
|
187
|
+
const user = Buffer.from(proxy.username || "");
|
|
188
|
+
const pass = Buffer.from(proxy.password || "");
|
|
159
189
|
socket.write(Buffer.concat([Buffer.from([0x01, user.length]), user, Buffer.from([pass.length]), pass]));
|
|
160
190
|
socket.once("data", (auth) => {
|
|
161
191
|
if (auth.length < 2 || auth[1] !== 0x00) {
|
|
@@ -292,6 +322,8 @@ async function mysqlQuery(config, sql, params, options) {
|
|
|
292
322
|
async function query(config, sql, params, options) {
|
|
293
323
|
if (config.db_type === "sqlite")
|
|
294
324
|
return sqliteQuery(config, sql, options);
|
|
325
|
+
if (config.db_type === "rqlite")
|
|
326
|
+
return rqliteQuery(config, sql, options);
|
|
295
327
|
if (isMysqlType(config.db_type))
|
|
296
328
|
return mysqlQuery(config, sql, params, options);
|
|
297
329
|
return pgQuery(config, sql, params, options);
|
|
@@ -324,6 +356,48 @@ function sqliteQuery(config, sql, options) {
|
|
|
324
356
|
db.close();
|
|
325
357
|
}
|
|
326
358
|
}
|
|
359
|
+
async function rqliteQuery(config, sql, options) {
|
|
360
|
+
const isReader = /^\s*(?:--[^\n]*\n|\s|\/\*[\s\S]*?\*\/)*(select|pragma|explain|with)\b/i.test(sql);
|
|
361
|
+
const endpoint = isReader ? "/db/query" : "/db/execute";
|
|
362
|
+
const result = await rqliteRequest(config, endpoint, sql);
|
|
363
|
+
if (isReader) {
|
|
364
|
+
const columns = result.columns ?? [];
|
|
365
|
+
const rows = (result.values ?? []).slice(0, resolveMaxRows(options)).map((row) => {
|
|
366
|
+
const record = {};
|
|
367
|
+
columns.forEach((column, index) => {
|
|
368
|
+
record[column] = row[index];
|
|
369
|
+
});
|
|
370
|
+
return record;
|
|
371
|
+
});
|
|
372
|
+
return { columns, rows, row_count: rows.length };
|
|
373
|
+
}
|
|
374
|
+
return { columns: [], rows: [], row_count: result.rows_affected ?? 0 };
|
|
375
|
+
}
|
|
376
|
+
async function rqliteRequest(config, endpoint, sql) {
|
|
377
|
+
const { host, port } = await connectionEndpoint(config);
|
|
378
|
+
const scheme = config.ssl ? "https" : "http";
|
|
379
|
+
const params = (config.url_params || "").trim().replace(/^\?/, "");
|
|
380
|
+
const url = `${scheme}://${host}:${port}${endpoint}${params ? `?${params}` : ""}`;
|
|
381
|
+
const headers = { "content-type": "application/json" };
|
|
382
|
+
if (config.username) {
|
|
383
|
+
headers.authorization = `Basic ${Buffer.from(`${config.username}:${config.password || ""}`).toString("base64")}`;
|
|
384
|
+
}
|
|
385
|
+
const response = await fetch(url, {
|
|
386
|
+
method: "POST",
|
|
387
|
+
headers,
|
|
388
|
+
body: JSON.stringify([sql]),
|
|
389
|
+
});
|
|
390
|
+
const text = await response.text();
|
|
391
|
+
if (!response.ok)
|
|
392
|
+
throw new Error(`rqlite error (${response.status}): ${text}`);
|
|
393
|
+
const payload = JSON.parse(text);
|
|
394
|
+
const result = payload.results?.[0];
|
|
395
|
+
if (!result)
|
|
396
|
+
throw new Error("rqlite returned no result");
|
|
397
|
+
if (result.error)
|
|
398
|
+
throw new Error(`rqlite error: ${result.error}`);
|
|
399
|
+
return result;
|
|
400
|
+
}
|
|
327
401
|
export async function executeQuery(config, sql, options) {
|
|
328
402
|
if (config.db_type === "mongodb") {
|
|
329
403
|
const find = parseMongoFindCommand(sql);
|
|
@@ -344,6 +418,11 @@ export async function executeQuery(config, sql, options) {
|
|
|
344
418
|
const result = await withTimeout(mongoAggregateDocuments(config, aggregate.collection, aggregate.pipeline, resolveMaxRows(options)), resolveTimeoutMs(options));
|
|
345
419
|
return mongoDocumentsToQueryResult(result.documents.slice(0, resolveMaxRows(options)), result.total);
|
|
346
420
|
}
|
|
421
|
+
const getIndexes = parseMongoGetIndexesCommand(sql);
|
|
422
|
+
if (getIndexes) {
|
|
423
|
+
const result = await withTimeout(mongoAggregateDocuments(config, getIndexes.collection, '[{"$indexStats":{}}]', resolveMaxRows(options)), resolveTimeoutMs(options));
|
|
424
|
+
return mongoDocumentsToQueryResult(result.documents.slice(0, resolveMaxRows(options)), result.total);
|
|
425
|
+
}
|
|
347
426
|
const write = parseMongoWriteCommand(sql);
|
|
348
427
|
if (write) {
|
|
349
428
|
const safety = evaluateMongoWriteSafety(write, sqlSafetyFromEnv());
|
|
@@ -352,7 +431,7 @@ export async function executeQuery(config, sql, options) {
|
|
|
352
431
|
const affected = await withTimeout(executeMongoWrite(config, write), resolveTimeoutMs(options));
|
|
353
432
|
return { columns: [], rows: [], row_count: affected };
|
|
354
433
|
}
|
|
355
|
-
throw new Error("Use MongoDB shell-style commands, for example: db.projects.find({}).limit(100), db.projects.countDocuments({}), db.projects.insertOne({...}), db.projects.updateOne({...}, {$set: {...}}), or db.projects.deleteOne({...})");
|
|
434
|
+
throw new Error("Use MongoDB shell-style commands, for example: db.projects.find({}).limit(100), db.projects.countDocuments({}), db.projects.getIndexes(), db.projects.insertOne({...}), db.projects.updateOne({...}, {$set: {...}}), or db.projects.deleteOne({...})");
|
|
356
435
|
}
|
|
357
436
|
if (isDirectQueryType(config.db_type)) {
|
|
358
437
|
return query(config, sql, undefined, options);
|
|
@@ -373,7 +452,7 @@ export async function listTables(config, schema) {
|
|
|
373
452
|
});
|
|
374
453
|
return collections.map((name) => ({ name, type: "COLLECTION" }));
|
|
375
454
|
}
|
|
376
|
-
if (config.db_type === "sqlite") {
|
|
455
|
+
if (config.db_type === "sqlite" || config.db_type === "rqlite") {
|
|
377
456
|
const result = await query(config, `SELECT name, type FROM sqlite_master WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite_%' ORDER BY name`);
|
|
378
457
|
return result.rows.map((r) => ({ name: String(r.name || ""), type: String(r.type || "table") }));
|
|
379
458
|
}
|
|
@@ -399,7 +478,7 @@ export async function describeTable(config, table, schema) {
|
|
|
399
478
|
const result = await mongoFindDocuments(config, table, 0, 20, "{}");
|
|
400
479
|
return inferMongoColumns(result.documents);
|
|
401
480
|
}
|
|
402
|
-
if (config.db_type === "sqlite") {
|
|
481
|
+
if (config.db_type === "sqlite" || config.db_type === "rqlite") {
|
|
403
482
|
const result = await query(config, `PRAGMA table_info(${quoteSqliteIdentifier(table)})`);
|
|
404
483
|
return result.rows.map((r) => ({
|
|
405
484
|
name: String(r.name || ""),
|
|
@@ -598,6 +677,16 @@ export function parseMongoAggregateCommand(input) {
|
|
|
598
677
|
return null;
|
|
599
678
|
return Array.isArray(JSON.parse(pipeline)) ? { collection: target.collection, pipeline } : null;
|
|
600
679
|
}
|
|
680
|
+
export function parseMongoGetIndexesCommand(input) {
|
|
681
|
+
const source = input.trim().replace(/;$/, "").trim();
|
|
682
|
+
const target = parseCollectionMethodTarget(source, "getIndexes");
|
|
683
|
+
if (!target)
|
|
684
|
+
return null;
|
|
685
|
+
const args = parseMethodArgs(source, target.methodCallIndex);
|
|
686
|
+
if (!args || args.some((arg) => arg.trim()))
|
|
687
|
+
return null;
|
|
688
|
+
return { collection: target.collection };
|
|
689
|
+
}
|
|
601
690
|
export function mongoAggregateWriteStage(pipelineJson) {
|
|
602
691
|
try {
|
|
603
692
|
const pipeline = JSON.parse(pipelineJson);
|
package/dist/diagnostics.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export declare const DIRECT_QUERY_TYPES: readonly ["postgres", "redshift", "mysql", "doris", "starrocks", "sqlite", "gaussdb", "opengauss"];
|
|
1
|
+
export declare const DIRECT_QUERY_TYPES: readonly ["postgres", "redshift", "mysql", "doris", "starrocks", "sqlite", "rqlite", "gaussdb", "kwdb", "opengauss"];
|
|
2
2
|
export type DirectQueryType = (typeof DIRECT_QUERY_TYPES)[number];
|
|
3
3
|
export declare function isDirectQueryType(dbType: string): dbType is DirectQueryType;
|
|
4
|
-
export declare const BRIDGE_REQUIRED_TYPES: readonly ["redis", "mongodb", "duckdb", "clickhouse", "sqlserver", "oracle", "elasticsearch", "dameng", "kingbase", "highgo", "vastbase", "goldendb", "yashandb", "databricks", "saphana", "teradata", "vertica", "firebird", "exasol", "oceanbase-oracle", "gbase", "tdengine", "h2", "snowflake", "trino", "hive", "db2", "informix", "iris", "neo4j", "cassandra", "bigquery", "kylin", "sundb", "xugu", "jdbc", "access"];
|
|
4
|
+
export declare const BRIDGE_REQUIRED_TYPES: readonly ["redis", "mongodb", "duckdb", "clickhouse", "sqlserver", "oracle", "elasticsearch", "dameng", "kingbase", "highgo", "vastbase", "goldendb", "databend", "yashandb", "databricks", "saphana", "teradata", "vertica", "firebird", "exasol", "oceanbase-oracle", "gbase", "tdengine", "iotdb", "h2", "snowflake", "trino", "hive", "db2", "informix", "iris", "neo4j", "cassandra", "bigquery", "kylin", "sundb", "xugu", "jdbc", "access"];
|
|
5
5
|
export interface DbxDiagnostics {
|
|
6
6
|
appDataDir: string;
|
|
7
7
|
dbPath: string;
|
package/dist/diagnostics.js
CHANGED
|
@@ -8,7 +8,9 @@ export const DIRECT_QUERY_TYPES = [
|
|
|
8
8
|
"doris",
|
|
9
9
|
"starrocks",
|
|
10
10
|
"sqlite",
|
|
11
|
+
"rqlite",
|
|
11
12
|
"gaussdb",
|
|
13
|
+
"kwdb",
|
|
12
14
|
"opengauss",
|
|
13
15
|
];
|
|
14
16
|
const DIRECT_QUERY_TYPE_SET = new Set(DIRECT_QUERY_TYPES);
|
|
@@ -28,6 +30,7 @@ export const BRIDGE_REQUIRED_TYPES = [
|
|
|
28
30
|
"highgo",
|
|
29
31
|
"vastbase",
|
|
30
32
|
"goldendb",
|
|
33
|
+
"databend",
|
|
31
34
|
"yashandb",
|
|
32
35
|
"databricks",
|
|
33
36
|
"saphana",
|
|
@@ -38,6 +41,7 @@ export const BRIDGE_REQUIRED_TYPES = [
|
|
|
38
41
|
"oceanbase-oracle",
|
|
39
42
|
"gbase",
|
|
40
43
|
"tdengine",
|
|
44
|
+
"iotdb",
|
|
41
45
|
"h2",
|
|
42
46
|
"snowflake",
|
|
43
47
|
"trino",
|
package/dist/web-backend.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { evaluateMongoAggregateSafety, evaluateMongoWriteSafety, inferMongoColumns, mongoDocumentsToQueryResult, parseMongoAggregateCommand, parseMongoCountDocumentsCommand, parseMongoFindCommand, parseMongoWriteCommand, } from "./database.js";
|
|
1
|
+
import { evaluateMongoAggregateSafety, evaluateMongoWriteSafety, inferMongoColumns, mongoDocumentsToQueryResult, parseMongoAggregateCommand, parseMongoCountDocumentsCommand, parseMongoFindCommand, parseMongoGetIndexesCommand, parseMongoWriteCommand, } from "./database.js";
|
|
2
2
|
import { sqlSafetyFromEnv } from "./sql-safety.js";
|
|
3
3
|
const baseUrl = process.env.DBX_WEB_URL.replace(/\/+$/, "");
|
|
4
4
|
const password = process.env.DBX_WEB_PASSWORD || "";
|
|
@@ -164,6 +164,21 @@ export async function executeQuery(config, sql, options) {
|
|
|
164
164
|
const result = (await res.json());
|
|
165
165
|
return mongoDocumentsToQueryResult(result.documents.slice(0, options?.maxRows ?? result.documents.length), result.total);
|
|
166
166
|
}
|
|
167
|
+
const getIndexes = parseMongoGetIndexesCommand(sql);
|
|
168
|
+
if (getIndexes) {
|
|
169
|
+
const res = await apiFetch("/api/mongo/aggregate-documents", {
|
|
170
|
+
method: "POST",
|
|
171
|
+
body: JSON.stringify({
|
|
172
|
+
connectionId: config.id,
|
|
173
|
+
database: config.database || "",
|
|
174
|
+
collection: getIndexes.collection,
|
|
175
|
+
pipelineJson: '[{"$indexStats":{}}]',
|
|
176
|
+
maxRows: options?.maxRows ?? 100,
|
|
177
|
+
}),
|
|
178
|
+
});
|
|
179
|
+
const result = (await res.json());
|
|
180
|
+
return mongoDocumentsToQueryResult(result.documents.slice(0, options?.maxRows ?? result.documents.length), result.total);
|
|
181
|
+
}
|
|
167
182
|
const write = parseMongoWriteCommand(sql);
|
|
168
183
|
if (write) {
|
|
169
184
|
const safety = evaluateMongoWriteSafety(write, sqlSafetyFromEnv());
|
|
@@ -172,7 +187,7 @@ export async function executeQuery(config, sql, options) {
|
|
|
172
187
|
const affected = await executeMongoWrite(config, write);
|
|
173
188
|
return { columns: [], rows: [], row_count: affected };
|
|
174
189
|
}
|
|
175
|
-
throw new Error("Use MongoDB shell-style commands, for example: db.projects.find({}).limit(100), db.projects.countDocuments({}), db.projects.insertOne({...}), db.projects.updateOne({...}, {$set: {...}}), or db.projects.deleteOne({...})");
|
|
190
|
+
throw new Error("Use MongoDB shell-style commands, for example: db.projects.find({}).limit(100), db.projects.countDocuments({}), db.projects.getIndexes(), db.projects.insertOne({...}), db.projects.updateOne({...}, {$set: {...}}), or db.projects.deleteOne({...})");
|
|
176
191
|
}
|
|
177
192
|
const res = await apiFetch("/api/query/execute", {
|
|
178
193
|
method: "POST",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dbx-app/node-core",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.7",
|
|
4
4
|
"description": "Shared Node.js database and DBX connection utilities for DBX CLI and MCP server",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"./schema-context": "./dist/schema-context.js",
|
|
23
23
|
"./sql-safety": "./dist/sql-safety.js"
|
|
24
24
|
},
|
|
25
|
-
"license": "
|
|
25
|
+
"license": "Apache-2.0",
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"better-sqlite3": "^12.9.0",
|
|
28
28
|
"keytar": "^7.9.0",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"typescript": "^5.8.3"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
|
-
"test": "
|
|
40
|
+
"test": "vitest run --config vitest.config.ts",
|
|
41
41
|
"build": "tsc"
|
|
42
42
|
}
|
|
43
43
|
}
|