feat: add rqlite database support

This commit is contained in:
t8y2
2026-06-05 01:02:07 +08:00
parent a799958328
commit 0887b96236
34 changed files with 804 additions and 12 deletions
+55 -2
View File
@@ -42,6 +42,17 @@ interface PoolEntry {
timer: ReturnType<typeof setTimeout>;
}
interface RqliteResult {
columns?: string[];
values?: unknown[][];
rows_affected?: number;
error?: string;
}
interface RqliteResponse {
results?: RqliteResult[];
}
const pools = new Map<string, PoolEntry>();
const proxyTunnels = new Map<string, { server: Server; port: number }>();
@@ -365,6 +376,7 @@ async function mysqlQuery(config: ConnectionConfig, sql: string, params?: unknow
async function query(config: ConnectionConfig, sql: string, params?: unknown[], options?: QueryOptions): Promise<QueryResult> {
if (config.db_type === "sqlite") return sqliteQuery(config, sql, options);
if (config.db_type === "rqlite") return rqliteQuery(config, sql, options);
if (isMysqlType(config.db_type)) return mysqlQuery(config, sql, params, options);
return pgQuery(config, sql, params, options);
}
@@ -398,6 +410,47 @@ function sqliteQuery(config: ConnectionConfig, sql: string, options?: QueryOptio
}
}
async function rqliteQuery(config: ConnectionConfig, sql: string, options?: QueryOptions): Promise<QueryResult> {
const isReader = /^\s*(?:--[^\n]*\n|\s|\/\*[\s\S]*?\*\/)*(select|pragma|explain|with)\b/i.test(sql);
const endpoint = isReader ? "/db/query" : "/db/execute";
const result = await rqliteRequest(config, endpoint, sql);
if (isReader) {
const columns = result.columns ?? [];
const rows = (result.values ?? []).slice(0, resolveMaxRows(options)).map((row) => {
const record: Record<string, unknown> = {};
columns.forEach((column, index) => {
record[column] = row[index];
});
return record;
});
return { columns, rows, row_count: rows.length };
}
return { columns: [], rows: [], row_count: result.rows_affected ?? 0 };
}
async function rqliteRequest(config: ConnectionConfig, endpoint: "/db/query" | "/db/execute", sql: string): Promise<RqliteResult> {
const { host, port } = await connectionEndpoint(config);
const scheme = config.ssl ? "https" : "http";
const params = (config.url_params || "").trim().replace(/^\?/, "");
const url = `${scheme}://${host}:${port}${endpoint}${params ? `?${params}` : ""}`;
const headers: Record<string, string> = { "content-type": "application/json" };
if (config.username) {
headers.authorization = `Basic ${Buffer.from(`${config.username}:${config.password || ""}`).toString("base64")}`;
}
const response = await fetch(url, {
method: "POST",
headers,
body: JSON.stringify([sql]),
});
const text = await response.text();
if (!response.ok) throw new Error(`rqlite error (${response.status}): ${text}`);
const payload = JSON.parse(text) as RqliteResponse;
const result = payload.results?.[0];
if (!result) throw new Error("rqlite returned no result");
if (result.error) throw new Error(`rqlite error: ${result.error}`);
return result;
}
export async function executeQuery(config: ConnectionConfig, sql: string, options?: QueryOptions): Promise<QueryResult> {
if (config.db_type === "mongodb") {
const find = parseMongoFindCommand(sql);
@@ -451,7 +504,7 @@ export async function listTables(config: ConnectionConfig, schema?: string): Pro
});
return collections.map((name) => ({ name, type: "COLLECTION" }));
}
if (config.db_type === "sqlite") {
if (config.db_type === "sqlite" || config.db_type === "rqlite") {
const result = await query(
config,
`SELECT name, type FROM sqlite_master WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite_%' ORDER BY name`,
@@ -484,7 +537,7 @@ export async function describeTable(config: ConnectionConfig, table: string, sch
const result = await mongoFindDocuments(config, table, 0, 20, "{}");
return inferMongoColumns(result.documents);
}
if (config.db_type === "sqlite") {
if (config.db_type === "sqlite" || config.db_type === "rqlite") {
const result = await query(config, `PRAGMA table_info(${quoteSqliteIdentifier(table)})`);
return result.rows.map((r) => ({
name: String(r.name || ""),
+1
View File
@@ -9,6 +9,7 @@ export const DIRECT_QUERY_TYPES = [
"doris",
"starrocks",
"sqlite",
"rqlite",
"gaussdb",
"opengauss",
] as const;
@@ -0,0 +1,111 @@
import assert from "node:assert/strict";
import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
import test from "node:test";
import type { ConnectionConfig } from "../src/connections.js";
import { describeTable, executeQuery, listTables } from "../src/database.js";
function rqliteConfig(port: number): ConnectionConfig {
return {
id: "rqlite-test",
name: "local-rqlite",
db_type: "rqlite",
host: "127.0.0.1",
port,
username: "dbx",
password: "secret",
database: "main",
ssh_enabled: false,
ssl: false,
};
}
async function withRqliteServer(handler: (req: IncomingMessage, res: ServerResponse, body: string) => void) {
const server = createServer((req, res) => {
let body = "";
req.setEncoding("utf8");
req.on("data", (chunk) => {
body += chunk;
});
req.on("end", () => handler(req, res, body));
});
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
const address = server.address();
assert.ok(address && typeof address === "object");
return {
port: address.port,
close: () => new Promise<void>((resolve, reject) => server.close((err) => (err ? reject(err) : resolve()))),
};
}
function json(res: ServerResponse, body: unknown) {
res.writeHead(200, { "content-type": "application/json" });
res.end(JSON.stringify(body));
}
test("executes rqlite query through the HTTP API", async () => {
const server = await withRqliteServer((req, res, body) => {
assert.equal(req.url, "/db/query");
assert.equal(req.headers.authorization, "Basic ZGJ4OnNlY3JldA==");
assert.deepEqual(JSON.parse(body), ["select id, name from users"]);
json(res, { results: [{ columns: ["id", "name"], values: [[1, "Ada"], [2, "Linus"]] }] });
});
try {
const result = await executeQuery(rqliteConfig(server.port), "select id, name from users", { maxRows: 1 });
assert.deepEqual(result, { columns: ["id", "name"], rows: [{ id: 1, name: "Ada" }], row_count: 1 });
} finally {
await server.close();
}
});
test("lists rqlite tables and describes columns", async () => {
const server = await withRqliteServer((_req, res, body) => {
const sql = JSON.parse(body)[0] as string;
if (sql.includes("sqlite_master")) {
json(res, { results: [{ columns: ["name", "type"], values: [["users", "table"], ["active_users", "view"]] }] });
return;
}
if (sql.includes("PRAGMA table_info")) {
json(res, {
results: [
{
columns: ["cid", "name", "type", "notnull", "dflt_value", "pk"],
values: [
[0, "id", "INTEGER", 1, null, 1],
[1, "name", "TEXT", 0, "'unknown'", 0],
],
},
],
});
return;
}
json(res, { results: [{ columns: [], values: [] }] });
});
try {
assert.deepEqual(await listTables(rqliteConfig(server.port)), [
{ name: "users", type: "table" },
{ name: "active_users", type: "view" },
]);
assert.deepEqual(await describeTable(rqliteConfig(server.port), "users"), [
{
name: "id",
data_type: "INTEGER",
is_nullable: false,
column_default: null,
is_primary_key: true,
comment: null,
},
{
name: "name",
data_type: "TEXT",
is_nullable: true,
column_default: "'unknown'",
is_primary_key: false,
comment: null,
},
]);
} finally {
await server.close();
}
});