diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index df8925be..7cd33e6a 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -61,6 +61,22 @@ jobs:
--health-timeout=5s
--health-retries=5
+ clickhouse:
+ image: clickhouse/clickhouse-server:latest
+ env:
+ CLICKHOUSE_USER: testuser
+ CLICKHOUSE_PASSWORD: testpass
+ CLICKHOUSE_DB: testdb
+ CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT: 1
+ ports:
+ - 8123:8123
+ - 9000:9000
+ options: >-
+ --health-cmd="wget -q --spider http://localhost:8123/ping || exit 1"
+ --health-interval=10s
+ --health-timeout=5s
+ --health-retries=5
+
env:
REAL_POSTGRES_HOST: localhost
REAL_POSTGRES_PORT: 5432
@@ -93,6 +109,13 @@ jobs:
REAL_MARIADB_DATABASE: testdb
REAL_MARIADB_SSL: "false"
+ REAL_CLICKHOUSE_HOST: localhost
+ REAL_CLICKHOUSE_PORT: 8123
+ REAL_CLICKHOUSE_USER: testuser
+ REAL_CLICKHOUSE_PASSWORD: testpass
+ REAL_CLICKHOUSE_DATABASE: testdb
+ REAL_CLICKHOUSE_SSL: "false"
+
# SQLite — no env vars needed; tests use a temp file
steps:
@@ -127,6 +150,10 @@ jobs:
run: |
mysql -h 127.0.0.1 -P 3307 -u root -prootpass testdb < bridge/scripts/seed-mariadb.sql
+ - name: Seed ClickHouse database
+ run: |
+ node bridge/scripts/seed-clickhouse.cjs
+
- name: Run tests
run: |
cd bridge
diff --git a/AGENTS.md b/AGENTS.md
index 3f50bbd0..83baa99a 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -24,7 +24,7 @@ RelWave uses a **Bridge Architecture** to balance performance, security, and dev
### Bridge (Node.js)
- **Runtime:** Node.js (bundled via `pkg`)
-- **Database Drivers:** `pg` (PostgreSQL), `mysql2` (MySQL/MariaDB), `better-sqlite3` (SQLite), `@duckdb/node-api` (DuckDB)
+- **Database Drivers:** `pg` (PostgreSQL), `mysql2` (MySQL/MariaDB), `better-sqlite3` (SQLite), `@duckdb/node-api` (DuckDB), `@clickhouse/client` (ClickHouse)
- **Version Control:** `simple-git`
- **Logging:** `pino`
- **Security:** `@napi-rs/keyring` for encrypted credential storage
diff --git a/FEATURES.md b/FEATURES.md
index 2b14dec8..0103873a 100644
--- a/FEATURES.md
+++ b/FEATURES.md
@@ -2,7 +2,7 @@
This document provides a comprehensive breakdown of all features and capabilities in RelWave. For installation and setup instructions, see the main [README](README.md).
-**Tech Stack:** Tauri + React 19 + TypeScript + Tailwind CSS + shadcn/ui + React Query + ReactFlow + Recharts + CodeMirror + better-sqlite3 + DuckDB
+**Tech Stack:** Tauri + React 19 + TypeScript + Tailwind CSS + shadcn/ui + React Query + ReactFlow + Recharts + CodeMirror + better-sqlite3 + DuckDB + ClickHouse
---
@@ -30,6 +30,18 @@ This document provides a comprehensive breakdown of all features and capabilitie
| MariaDB | Host/port + SSL | ✅ | ✅ | ✅ | ✅ | ✅ |
| SQLite | Local file path | ✅ | ✅ | ✅ | ✅ | ✅ |
| DuckDB | Local file path | ✅ | ✅ | ✅ | ✅ | ✅ |
+| ClickHouse | Host/port + SSL | ✅ | ✅ | ✅ | ✅ | ✅ |
+
+### ClickHouse-Specific Features
+
+- **Column-oriented analytical engine** — powered by `@clickhouse/client` over high-performance HTTP/HTTPS interface
+- **Flexible connection modes** — connect via standard HTTP (`8123`) or secure HTTPS/TLS (`8443`), with custom user credentials and databases
+- **URL & Parameter parsing** — parses `clickhouse://` and `clickhouses://` connection strings seamlessly
+- **Introspection & Key detection** — queries `system.tables`, `system.columns`, and `system.data_skipping_indices` with `ENGINE = MergeTree()` awareness and sorting/primary key recognition
+- **Cancelable streaming queries** — streams large analytical query result sets with `AbortController` support
+- **Lightweight mutations** — executes `ALTER TABLE ... UPDATE` and `ALTER TABLE ... DELETE` with synchronous mutation options (`mutations_sync = 1`)
+- **DDL & Schema migrations** — automated `schema_migrations` table tracking (`MergeTree() ORDER BY version`), table creations, and column migrations
+- **Local & Docker discovery** — automatically scans ports `8123`/`8124` and detects ClickHouse Docker containers
### DuckDB-Specific Features
diff --git a/README.md b/README.md
index 3b42efa5..a8c5b13b 100644
--- a/README.md
+++ b/README.md
@@ -40,7 +40,7 @@ _RelWave brings the power of native Git versioning, visual ER diagrams, and seam
Native Core
- Direct connections via native drivers (PostgreSQL, MySQL, SQLite, DuckDB). Zero browser overhead. Pure speed.
+ Direct connections via native drivers (PostgreSQL, MySQL, SQLite, DuckDB, ClickHouse). Zero browser overhead. Pure speed.
@@ -133,7 +133,7 @@ RelWave uses a **three-layer bridge architecture**. React owns the user experien
-The request path is **React UI -> Tauri command -> Node.js JSON-RPC handler -> service -> database, Git, local storage, monitoring, or LLM provider**. Responses return through the same boundary in reverse. Long-running SQL queries additionally stream `query.result`, `query.progress`, and `query.done` notifications back to the React client. The Node.js bridge contains the database connectors (PostgreSQL, MySQL/MariaDB, SQLite, DuckDB), Git service, AI services, monitoring services, project stores, credential access, and SSH tunnel handling.
+The request path is **React UI -> Tauri command -> Node.js JSON-RPC handler -> service -> database, Git, local storage, monitoring, or LLM provider**. Responses return through the same boundary in reverse. Long-running SQL queries additionally stream `query.result`, `query.progress`, and `query.done` notifications back to the React client. The Node.js bridge contains the database connectors (PostgreSQL, MySQL/MariaDB, SQLite, DuckDB, ClickHouse), Git service, AI services, monitoring services, project stores, credential access, and SSH tunnel handling.
diff --git a/bridge/.env.example b/bridge/.env.example
new file mode 100644
index 00000000..d56d33b2
--- /dev/null
+++ b/bridge/.env.example
@@ -0,0 +1,32 @@
+# Bridge test and local development environment
+# Copy this file to .env and adjust values if you need custom database endpoints.
+
+REAL_POSTGRES_HOST=localhost
+REAL_POSTGRES_PORT=5432
+REAL_POSTGRES_USER=testuser
+REAL_POSTGRES_PASSWORD=testpass
+REAL_POSTGRES_DATABASE=testdb
+REAL_POSTGRES_SSL=false
+REAL_POSTGRES_SSLMODE=disable
+
+REAL_MYSQL_HOST=localhost
+REAL_MYSQL_PORT=3306
+REAL_MYSQL_USER=testuser
+REAL_MYSQL_PASSWORD=testpass
+REAL_MYSQL_DATABASE=testdb
+REAL_MYSQL_SSL=false
+
+REAL_MARIADB_HOST=localhost
+REAL_MARIADB_PORT=3307
+REAL_MARIADB_USER=testuser
+REAL_MARIADB_PASSWORD=testpass
+REAL_MARIADB_DATABASE=testdb
+REAL_MARIADB_SSL=false
+
+REAL_CLICKHOUSE_HOST=localhost
+REAL_CLICKHOUSE_PORT=8123
+REAL_CLICKHOUSE_USER=testuser
+REAL_CLICKHOUSE_PASSWORD=testpass
+REAL_CLICKHOUSE_DATABASE=testdb
+REAL_CLICKHOUSE_SSL=false
+
diff --git a/bridge/.gitignore b/bridge/.gitignore
index 20ca7eb4..c07dd57b 100644
--- a/bridge/.gitignore
+++ b/bridge/.gitignore
@@ -2,4 +2,5 @@ node_modules
dist
coverage
.env
-.env*
\ No newline at end of file
+.env.*
+!.env.example
\ No newline at end of file
diff --git a/bridge/__tests__/clickhouseUrl.test.ts b/bridge/__tests__/clickhouseUrl.test.ts
new file mode 100644
index 00000000..ac157769
--- /dev/null
+++ b/bridge/__tests__/clickhouseUrl.test.ts
@@ -0,0 +1,95 @@
+import { cleanClickHouseUrl, parseClickHouseUrl } from "../src/utils/clickhouseUrl";
+import { describe, expect, it } from "@jest/globals";
+
+describe("ClickHouse URL Utilities", () => {
+ describe("cleanClickHouseUrl", () => {
+ it("should handle empty or undefined input", () => {
+ expect(cleanClickHouseUrl("")).toBe("");
+ expect(cleanClickHouseUrl(undefined)).toBe("");
+ expect(cleanClickHouseUrl(" ")).toBe("");
+ });
+
+ it("should strip markdown links", () => {
+ expect(
+ cleanClickHouseUrl(
+ "[https://demo.clickhouse.cloud:8443](https://demo.clickhouse.cloud:8443)",
+ ),
+ ).toBe("https://demo.clickhouse.cloud:8443");
+ expect(cleanClickHouseUrl("[https://demo.clickhouse.cloud:8443]()")).toBe(
+ "https://demo.clickhouse.cloud:8443",
+ );
+ });
+
+ it("should strip enclosing single/double quotes and backticks", () => {
+ expect(cleanClickHouseUrl("'https://demo.clickhouse.cloud:8443'")).toBe(
+ "https://demo.clickhouse.cloud:8443",
+ );
+ expect(cleanClickHouseUrl('"https://demo.clickhouse.cloud:8443"')).toBe(
+ "https://demo.clickhouse.cloud:8443",
+ );
+ expect(cleanClickHouseUrl("`https://demo.clickhouse.cloud:8443`")).toBe(
+ "https://demo.clickhouse.cloud:8443",
+ );
+ });
+
+ it("should convert clickhouses:// protocol to https://", () => {
+ expect(cleanClickHouseUrl("clickhouses://demo.clickhouse.cloud:8443")).toBe(
+ "https://demo.clickhouse.cloud:8443",
+ );
+ });
+
+ it("should convert clickhouse:// protocol properly based on SSL / port", () => {
+ expect(cleanClickHouseUrl("clickhouse://demo.clickhouse.cloud:8443")).toBe(
+ "https://demo.clickhouse.cloud:8443",
+ );
+ expect(cleanClickHouseUrl("clickhouse://localhost:8123")).toBe("http://localhost:8123");
+ });
+
+ it("should prepend https:// for clickhouse.cloud or port 8443 if no scheme provided", () => {
+ expect(cleanClickHouseUrl("demo.clickhouse.cloud:8443")).toBe(
+ "https://demo.clickhouse.cloud:8443",
+ );
+ expect(cleanClickHouseUrl("demo.clickhouse.cloud")).toBe("https://demo.clickhouse.cloud");
+ });
+
+ it("should prepend http:// for localhost:8123 if no scheme provided", () => {
+ expect(cleanClickHouseUrl("localhost:8123")).toBe("http://localhost:8123");
+ });
+
+ it("should strip trailing slashes", () => {
+ expect(cleanClickHouseUrl("https://demo.clickhouse.cloud:8443/")).toBe(
+ "https://demo.clickhouse.cloud:8443",
+ );
+ });
+ });
+
+ describe("parseClickHouseUrl", () => {
+ it("should parse standard cloud url", () => {
+ const parsed = parseClickHouseUrl("https://of8remuy3t.ap-south-1.aws.clickhouse.cloud:8443");
+ expect(parsed.host).toBe("of8remuy3t.ap-south-1.aws.clickhouse.cloud");
+ expect(parsed.port).toBe(8443);
+ expect(parsed.ssl).toBe(true);
+ expect(parsed.database).toBe("default");
+ });
+
+ it("should parse url with embedded user and password", () => {
+ const parsed = parseClickHouseUrl(
+ "https://myuser:secret123@of8remuy3t.ap-south-1.aws.clickhouse.cloud:8443/analytics",
+ );
+ expect(parsed.host).toBe("of8remuy3t.ap-south-1.aws.clickhouse.cloud");
+ expect(parsed.port).toBe(8443);
+ expect(parsed.ssl).toBe(true);
+ expect(parsed.user).toBe("myuser");
+ expect(parsed.password).toBe("secret123");
+ expect(parsed.database).toBe("analytics");
+ });
+
+ it("should parse localhost http url", () => {
+ const parsed = parseClickHouseUrl("http://localhost:8123/default");
+ expect(parsed.host).toBe("localhost");
+ expect(parsed.port).toBe(8123);
+ expect(parsed.ssl).toBe(false);
+ expect(parsed.database).toBe("default");
+ });
+ });
+});
diff --git a/bridge/__tests__/connectors/clickhouse.batch.test.ts b/bridge/__tests__/connectors/clickhouse.batch.test.ts
new file mode 100644
index 00000000..2abef9c6
--- /dev/null
+++ b/bridge/__tests__/connectors/clickhouse.batch.test.ts
@@ -0,0 +1,307 @@
+import { describe, expect, it, jest, beforeEach } from "@jest/globals";
+
+const mockQuery = jest.fn();
+const mockCommand = jest.fn();
+const mockInsert = jest.fn();
+const mockClose = jest.fn();
+
+jest.mock("@clickhouse/client", () => ({
+ createClient: jest.fn(() => ({
+ query: mockQuery,
+ command: mockCommand,
+ insert: mockInsert,
+ close: mockClose,
+ })),
+}));
+
+import * as clickhouseConnector from "../../src/connectors/clickhouse";
+
+const mockConfig: clickhouseConnector.ClickHouseConfig = {
+ host: "localhost",
+ port: 8123,
+ user: "default",
+ password: "",
+ database: "testdb",
+};
+
+describe("ClickHouse getSchemaMetadataBatch unit tests", () => {
+ beforeEach(async () => {
+ clickhouseConnector.clickhouseCache.clearAll();
+ await clickhouseConnector.closeAllClients();
+ mockQuery.mockReset();
+ mockCommand.mockReset();
+ mockInsert.mockReset();
+ mockClose.mockReset();
+ });
+
+ it("should assemble batch metadata correctly when batch queries succeed", async () => {
+ mockQuery.mockImplementation(async ({ query }: any) => {
+ const q = String(query);
+
+ // listTables query
+ if (q.includes("FROM system.tables")) {
+ return {
+ json: async () => [
+ { schema: "testdb", name: "users", type: "BASE TABLE" },
+ { schema: "testdb", name: "orders", type: "BASE TABLE" },
+ { schema: "testdb", name: "products", type: "BASE TABLE" },
+ ],
+ };
+ }
+
+ // Batch columns query
+ if (q.includes("FROM system.columns") && !q.includes("is_in_primary_key = 1")) {
+ return {
+ json: async () => [
+ {
+ table: "users",
+ name: "id",
+ type: "UInt64",
+ not_nullable: 1,
+ default_value: null,
+ is_in_primary_key: 1,
+ is_in_sorting_key: 1,
+ comment: "User ID",
+ },
+ {
+ table: "users",
+ name: "email",
+ type: "String",
+ not_nullable: 1,
+ default_value: null,
+ is_primary_key: 0,
+ is_in_sorting_key: 0,
+ comment: null,
+ },
+ {
+ table: "orders",
+ name: "id",
+ type: "UInt64",
+ not_nullable: 1,
+ default_value: null,
+ is_in_primary_key: 1,
+ is_in_sorting_key: 1,
+ comment: null,
+ },
+ {
+ table: "products",
+ name: "id",
+ type: "UInt64",
+ not_nullable: 1,
+ default_value: null,
+ is_in_primary_key: 1,
+ is_in_sorting_key: 1,
+ comment: null,
+ },
+ ],
+ };
+ }
+
+ // Batch primary keys query
+ if (q.includes("is_in_primary_key = 1")) {
+ return {
+ json: async () => [
+ { table: "users", column_name: "id" },
+ { table: "orders", column_name: "id" },
+ { table: "products", column_name: "id" },
+ ],
+ };
+ }
+
+ // Batch indexes query
+ if (q.includes("system.data_skipping_indices")) {
+ return {
+ json: async () => [
+ {
+ table: "orders",
+ index_name: "idx_orders_created",
+ column_name: "created_at",
+ index_type: "minmax",
+ },
+ ],
+ };
+ }
+
+ return { json: async () => [] };
+ });
+
+ const result = await clickhouseConnector.getSchemaMetadataBatch(mockConfig, "testdb");
+ expect(result.tables instanceof Map).toBe(true);
+ expect(result.tables.size).toBe(3);
+
+ const userTable = result.tables.get("users");
+ expect(userTable).toBeDefined();
+ expect(userTable!.columns).toHaveLength(2);
+ expect(userTable!.columns[0].name).toBe("id");
+ expect(userTable!.columns[0].is_primary_key).toBe(true);
+ expect(userTable!.primaryKeys).toHaveLength(1);
+ expect(userTable!.primaryKeys[0].column_name).toBe("id");
+
+ const orderTable = result.tables.get("orders");
+ expect(orderTable).toBeDefined();
+ expect(orderTable!.indexes).toHaveLength(1);
+ expect(orderTable!.indexes[0].index_name).toBe("idx_orders_created");
+ });
+
+ it("should handle backticked `table` key name in batch query results", async () => {
+ mockQuery.mockImplementation(async ({ query }: any) => {
+ const q = String(query);
+
+ if (q.includes("FROM system.tables")) {
+ return {
+ json: async () => [{ schema: "testdb", name: "users", type: "BASE TABLE" }],
+ };
+ }
+
+ if (q.includes("FROM system.columns") && !q.includes("is_in_primary_key = 1")) {
+ return {
+ json: async () => [
+ {
+ "`table`": "users",
+ name: "id",
+ type: "UInt64",
+ not_nullable: 1,
+ default_value: null,
+ is_in_primary_key: 1,
+ is_in_sorting_key: 1,
+ },
+ ],
+ };
+ }
+
+ if (q.includes("is_in_primary_key = 1")) {
+ return {
+ json: async () => [{ "`table`": "users", column_name: "id" }],
+ };
+ }
+
+ return { json: async () => [] };
+ });
+
+ const result = await clickhouseConnector.getSchemaMetadataBatch(mockConfig, "testdb");
+ expect(result.tables.size).toBe(1);
+ const userTable = result.tables.get("users");
+ expect(userTable).toBeDefined();
+ expect(userTable!.columns).toHaveLength(1);
+ expect(userTable!.primaryKeys).toHaveLength(1);
+ });
+
+ it("should fallback to per-table lookups if batch queries fail", async () => {
+ mockQuery.mockImplementation(async ({ query }: any) => {
+ const q = String(query);
+
+ // listTables query
+ if (q.includes("FROM system.tables")) {
+ return {
+ json: async () => [
+ { schema: "testdb", name: "student", type: "BASE TABLE" },
+ { schema: "testdb", name: "persons", type: "BASE TABLE" },
+ { schema: "testdb", name: "TestTable", type: "BASE TABLE" },
+ ],
+ };
+ }
+
+ // Batch queries fail
+ if (q.includes("ORDER BY `table`")) {
+ throw new Error("Batch query failed");
+ }
+
+ // Fallback single table column queries:
+ if (
+ q.includes("WHERE database = {target_schema: String} AND `table` = {target_table: String}")
+ ) {
+ return {
+ json: async () => [
+ {
+ name: "id",
+ type: "UInt32",
+ not_nullable: 1,
+ default_value: null,
+ is_in_primary_key: 1,
+ is_in_sorting_key: 1,
+ comment: null,
+ },
+ {
+ name: "name",
+ type: "String",
+ not_nullable: 1,
+ default_value: null,
+ is_in_primary_key: 0,
+ is_in_sorting_key: 0,
+ comment: null,
+ },
+ ],
+ };
+ }
+
+ // Fallback single table PK queries:
+ if (q.includes("is_in_primary_key = 1 OR is_in_sorting_key = 1")) {
+ return {
+ json: async () => [{ column_name: "id" }],
+ };
+ }
+
+ return { json: async () => [] };
+ });
+
+ const result = await clickhouseConnector.getSchemaMetadataBatch(mockConfig, "testdb");
+ expect(result.tables.size).toBe(3);
+ expect(result.tables.has("student")).toBe(true);
+ expect(result.tables.has("persons")).toBe(true);
+ expect(result.tables.has("TestTable")).toBe(true);
+
+ const studentMeta = result.tables.get("student");
+ expect(studentMeta).toBeDefined();
+ expect(studentMeta!.columns).toHaveLength(2);
+ expect(studentMeta!.primaryKeys[0].column_name).toBe("id");
+ });
+
+ it("should filter tables when array of table names is passed", async () => {
+ mockQuery.mockImplementation(async ({ query }: any) => {
+ const q = String(query);
+
+ if (q.includes("FROM system.tables")) {
+ return {
+ json: async () => [
+ { schema: "testdb", name: "student", type: "BASE TABLE" },
+ { schema: "testdb", name: "persons", type: "BASE TABLE" },
+ { schema: "testdb", name: "TestTable", type: "BASE TABLE" },
+ ],
+ };
+ }
+
+ if (q.includes("FROM system.columns") && !q.includes("is_in_primary_key = 1")) {
+ return {
+ json: async () => [
+ { table: "student", name: "id", type: "UInt32", not_nullable: 1, is_in_primary_key: 1 },
+ { table: "persons", name: "id", type: "UInt32", not_nullable: 1, is_in_primary_key: 1 },
+ {
+ table: "TestTable",
+ name: "id",
+ type: "UInt32",
+ not_nullable: 1,
+ is_in_primary_key: 1,
+ },
+ ],
+ };
+ }
+
+ if (q.includes("is_in_primary_key = 1")) {
+ return {
+ json: async () => [
+ { table: "student", column_name: "id" },
+ { table: "persons", column_name: "id" },
+ { table: "TestTable", column_name: "id" },
+ ],
+ };
+ }
+
+ return { json: async () => [] };
+ });
+
+ const result = await clickhouseConnector.getSchemaMetadataBatch(mockConfig, ["student"]);
+ expect(result.tables.size).toBe(1);
+ expect(result.tables.has("student")).toBe(true);
+ expect(result.tables.has("persons")).toBe(false);
+ });
+});
diff --git a/bridge/__tests__/connectors/clickhouse.cache.test.ts b/bridge/__tests__/connectors/clickhouse.cache.test.ts
new file mode 100644
index 00000000..3381c0cd
--- /dev/null
+++ b/bridge/__tests__/connectors/clickhouse.cache.test.ts
@@ -0,0 +1,130 @@
+import { clickhouseCache } from "../../src/connectors/clickhouse";
+import { describe, it, expect, beforeEach } from "@jest/globals";
+import { ClickHouseConfig } from "../../src/types/clickhouse";
+
+const mockConnection: ClickHouseConfig = {
+ host: "localhost",
+ port: 8123,
+ user: "testuser",
+ password: "testpass",
+ database: "testdb",
+};
+
+const mockConnection2: ClickHouseConfig = {
+ host: "localhost",
+ port: 8123,
+ user: "testuser",
+ password: "testpass",
+ database: "otherdb",
+};
+
+describe("ClickhouseCacheManager", () => {
+ beforeEach(() => {
+ clickhouseCache.clearAll();
+ });
+
+ describe("Connection Key Generation & Isolation", () => {
+ it("should generate consistent keys and isolate different databases", () => {
+ const tables1 = [{ schema: "testdb", name: "users", type: "BASE TABLE" }];
+ const tables2 = [{ schema: "otherdb", name: "orders", type: "BASE TABLE" }];
+
+ clickhouseCache.setTableList(mockConnection, tables1, "testdb");
+ clickhouseCache.setTableList(mockConnection2, tables2, "otherdb");
+
+ expect(clickhouseCache.getTableList(mockConnection, "testdb")).toEqual(tables1);
+ expect(clickhouseCache.getTableList(mockConnection2, "otherdb")).toEqual(tables2);
+ });
+ });
+
+ describe("Table List Cache", () => {
+ const mockTables = [
+ { schema: "testdb", name: "users", type: "BASE TABLE" },
+ { schema: "testdb", name: "orders", type: "BASE TABLE" },
+ ];
+
+ it("should cache and retrieve table list", () => {
+ clickhouseCache.setTableList(mockConnection, mockTables, "testdb");
+ const cached = clickhouseCache.getTableList(mockConnection, "testdb");
+ expect(cached).toEqual(mockTables);
+ });
+
+ it("should return null for non-existent cache entry", () => {
+ const cached = clickhouseCache.getTableList(mockConnection, "nonexistent");
+ expect(cached).toBeNull();
+ });
+ });
+
+ describe("Table Details Cache", () => {
+ const mockColumns = [
+ {
+ name: "id",
+ type: "UInt64",
+ not_nullable: true,
+ default_value: null,
+ is_primary_key: true,
+ is_foreign_key: false,
+ comment: null,
+ },
+ {
+ name: "email",
+ type: "String",
+ not_nullable: true,
+ default_value: null,
+ is_primary_key: false,
+ is_foreign_key: false,
+ comment: null,
+ },
+ ];
+
+ it("should cache and retrieve table details", () => {
+ clickhouseCache.setTableDetails(mockConnection, "testdb", "users", mockColumns);
+ const cached = clickhouseCache.getTableDetails(mockConnection, "testdb", "users");
+ expect(cached).toEqual(mockColumns);
+ });
+
+ it("should clear specific table cache", () => {
+ clickhouseCache.setTableDetails(mockConnection, "testdb", "users", mockColumns);
+ clickhouseCache.clearTableCache(mockConnection, "testdb", "users");
+ expect(clickhouseCache.getTableDetails(mockConnection, "testdb", "users")).toBeNull();
+ });
+ });
+
+ describe("DB Stats Cache", () => {
+ const mockStats = {
+ total_tables: 3,
+ total_rows: 1000,
+ total_db_size_mb: 2.5,
+ };
+
+ it("should cache and retrieve db stats", () => {
+ clickhouseCache.setDBStats(mockConnection, mockStats);
+ const cached = clickhouseCache.getDBStats(mockConnection);
+ expect(cached).toEqual(mockStats);
+ });
+ });
+
+ describe("Clear for Connection and Clear All", () => {
+ it("should clear only the target connection cache", () => {
+ const tables1 = [{ schema: "testdb", name: "users", type: "BASE TABLE" }];
+ const tables2 = [{ schema: "otherdb", name: "orders", type: "BASE TABLE" }];
+
+ clickhouseCache.setTableList(mockConnection, tables1, "testdb");
+ clickhouseCache.setTableList(mockConnection2, tables2, "otherdb");
+
+ clickhouseCache.clearForConnection(mockConnection);
+
+ expect(clickhouseCache.getTableList(mockConnection, "testdb")).toBeNull();
+ expect(clickhouseCache.getTableList(mockConnection2, "otherdb")).toEqual(tables2);
+ });
+
+ it("should clear all caches on clearAll", () => {
+ clickhouseCache.setTableList(
+ mockConnection,
+ [{ schema: "testdb", name: "users", type: "BASE TABLE" }],
+ "testdb",
+ );
+ clickhouseCache.clearAll();
+ expect(clickhouseCache.getTableList(mockConnection, "testdb")).toBeNull();
+ });
+ });
+});
diff --git a/bridge/__tests__/connectors/clickhouse.test.ts b/bridge/__tests__/connectors/clickhouse.test.ts
new file mode 100644
index 00000000..d0da846a
--- /dev/null
+++ b/bridge/__tests__/connectors/clickhouse.test.ts
@@ -0,0 +1,264 @@
+import { describe, expect, test, jest, afterAll } from "@jest/globals";
+import * as clickhouseConnector from "../../src/connectors/clickhouse";
+
+const invalidConfig: clickhouseConnector.ClickHouseConfig = {
+ host: "nonexistent.invalid.host",
+ port: 8123,
+ user: "testuser",
+ password: "wrongpassword",
+ database: "testdb",
+};
+
+const validConfig: clickhouseConnector.ClickHouseConfig = {
+ host: process.env.REAL_CLICKHOUSE_HOST || "127.0.0.1",
+ port: Number(process.env.REAL_CLICKHOUSE_PORT || 8123),
+ user: process.env.REAL_CLICKHOUSE_USER || "testuser",
+ password: process.env.REAL_CLICKHOUSE_PASSWORD || "testpass",
+ database: process.env.REAL_CLICKHOUSE_DATABASE || "testdb",
+ ssl: process.env.REAL_CLICKHOUSE_SSL === "true",
+};
+
+describe("ClickHouse Connector", () => {
+ jest.setTimeout(20000);
+
+ afterAll(async () => {
+ await clickhouseConnector.closeAllClients();
+ });
+
+ test("Should Fail to Connect to ClickHouse with Invalid Config", async () => {
+ const connection = await clickhouseConnector.testConnection(invalidConfig);
+ expect(connection.ok).toBe(false);
+ expect(connection.status).toBe("disconnected");
+ });
+
+ test("Should Connect to ClickHouse Database", async () => {
+ const connection = await clickhouseConnector.testConnection(validConfig);
+ expect(connection).toStrictEqual({
+ message: "Connection successful",
+ status: "connected",
+ ok: true,
+ });
+ });
+
+ test("Should Get Database Statistics", async () => {
+ const stats = await clickhouseConnector.getDBStats(validConfig);
+ expect(stats).toHaveProperty("total_tables");
+ expect(stats).toHaveProperty("total_rows");
+ expect(stats).toHaveProperty("total_db_size_mb");
+ expect(stats.total_tables).toBeGreaterThanOrEqual(1);
+ });
+
+ test("Should Get List of Schemas / Databases", async () => {
+ const schemas = await clickhouseConnector.listSchemas(validConfig);
+ expect(Array.isArray(schemas)).toBe(true);
+ expect(schemas.some((s) => s.name === validConfig.database)).toBe(true);
+ });
+
+ test("Should Get List of Tables in testdb", async () => {
+ const tables = await clickhouseConnector.listTables(validConfig, validConfig.database);
+ expect(Array.isArray(tables)).toBe(true);
+ const tableNames = tables.map((t) => t.name);
+ expect(tableNames).toContain("persons");
+ expect(tableNames).toContain("student");
+ expect(tableNames).toContain("TestTable");
+ });
+
+ test("Should Get Table Details for student Table", async () => {
+ const details = await clickhouseConnector.getTableDetails(
+ validConfig,
+ validConfig.database,
+ "student",
+ );
+ expect(Array.isArray(details)).toBe(true);
+ expect(details.length).toBeGreaterThanOrEqual(3);
+
+ const colNames = details.map((c) => c.name);
+ expect(colNames).toContain("id");
+ expect(colNames).toContain("name");
+ expect(colNames).toContain("address");
+
+ const idCol = details.find((c) => c.name === "id");
+ expect(idCol?.is_primary_key).toBe(true);
+ });
+
+ test("Should Get Table Primary Keys for student Table", async () => {
+ const pks = await clickhouseConnector.listPrimaryKeys(
+ validConfig,
+ validConfig.database,
+ "student",
+ );
+ expect(Array.isArray(pks)).toBe(true);
+ expect(pks.length).toBeGreaterThanOrEqual(1);
+ expect(pks[0].column_name).toBe("id");
+ });
+
+ test("Should Fetch Table Data from persons", async () => {
+ const result = await clickhouseConnector.fetchTableData(
+ validConfig,
+ validConfig.database,
+ "persons",
+ 10,
+ 1,
+ );
+ expect(result).toHaveProperty("total");
+ expect(result.total).toBeGreaterThanOrEqual(3);
+ expect(Array.isArray(result.rows)).toBe(true);
+ expect(result.rows.length).toBeGreaterThanOrEqual(3);
+ expect(result.rows[0]).toHaveProperty("id");
+ expect(result.rows[0]).toHaveProperty("name");
+ expect(result.rows[0]).toHaveProperty("email");
+ });
+
+ test("Should Stream Query on student Table", async () => {
+ const rows: any[] = [];
+ let doneCalled = false;
+
+ const { promise } = clickhouseConnector.streamQueryCancelable(
+ validConfig,
+ `SELECT * FROM ${validConfig.database}.student ORDER BY id;`,
+ 10,
+ (batch) => {
+ rows.push(...batch);
+ },
+ () => {
+ doneCalled = true;
+ },
+ );
+
+ await promise;
+
+ expect(doneCalled).toBe(true);
+ expect(rows.length).toBeGreaterThanOrEqual(3);
+ expect(rows[0]).toHaveProperty("name");
+ });
+
+ test("Should Insert, Update, and Delete Rows", async () => {
+ const testId = 88888;
+ const testName = "CRUD Integration Test";
+ const updatedName = "Updated CRUD Test";
+
+ // 1. Insert
+ const inserted = await clickhouseConnector.insertRow(
+ validConfig,
+ validConfig.database,
+ "TestTable",
+ { id: testId, name: testName },
+ );
+ expect(inserted).toHaveProperty("id", testId);
+
+ // Verify insertion
+ const dataAfterInsert = await clickhouseConnector.fetchTableData(
+ validConfig,
+ validConfig.database,
+ "TestTable",
+ 50,
+ 1,
+ );
+ const foundInserted = dataAfterInsert.rows.find((r) => Number(r.id) === testId);
+ expect(foundInserted).toBeDefined();
+ expect(foundInserted.name).toBe(testName);
+
+ // 2. Update
+ await clickhouseConnector.updateRow(
+ validConfig,
+ validConfig.database,
+ "TestTable",
+ "id",
+ testId,
+ { name: updatedName },
+ );
+
+ const dataAfterUpdate = await clickhouseConnector.fetchTableData(
+ validConfig,
+ validConfig.database,
+ "TestTable",
+ 50,
+ 1,
+ );
+ const foundUpdated = dataAfterUpdate.rows.find((r) => Number(r.id) === testId);
+ expect(foundUpdated).toBeDefined();
+ expect(foundUpdated.name).toBe(updatedName);
+
+ // 3. Delete
+ const deleted = await clickhouseConnector.deleteRow(
+ validConfig,
+ validConfig.database,
+ "TestTable",
+ "id",
+ testId,
+ );
+ expect(deleted).toBe(true);
+
+ const dataAfterDelete = await clickhouseConnector.fetchTableData(
+ validConfig,
+ validConfig.database,
+ "TestTable",
+ 50,
+ 1,
+ );
+ const foundDeleted = dataAfterDelete.rows.find((r) => Number(r.id) === testId);
+ expect(foundDeleted).toBeUndefined();
+ });
+
+ test("Should Search Table by Keyword", async () => {
+ const searchResult = await clickhouseConnector.searchTable(
+ validConfig,
+ validConfig.database,
+ "persons",
+ "John",
+ );
+ expect(searchResult.total).toBeGreaterThanOrEqual(1);
+ expect(searchResult.rows.some((r) => r.name.includes("John"))).toBe(true);
+ });
+
+ test("Should Get Schema Metadata Batch for ER Diagram", async () => {
+ const batch = await clickhouseConnector.getSchemaMetadataBatch(
+ validConfig,
+ validConfig.database,
+ );
+ expect(batch.tables instanceof Map).toBe(true);
+ expect(batch.tables.size).toBeGreaterThanOrEqual(3);
+
+ const studentMeta = batch.tables.get("student");
+ expect(studentMeta).toBeDefined();
+ expect(studentMeta!.columns.length).toBeGreaterThanOrEqual(3);
+ expect(studentMeta!.primaryKeys.length).toBeGreaterThanOrEqual(1);
+ expect(studentMeta!.primaryKeys[0].column_name).toBe("id");
+ });
+
+ test("Should Return Empty Arrays for Foreign Keys and Unique Constraints", async () => {
+ const fks = await clickhouseConnector.listForeignKeys(
+ validConfig,
+ validConfig.database,
+ "student",
+ );
+ expect(fks).toEqual([]);
+
+ const uqs = await clickhouseConnector.listUniqueConstraints(
+ validConfig,
+ validConfig.database,
+ "student",
+ );
+ expect(uqs).toEqual([]);
+
+ const checks = await clickhouseConnector.listCheckConstraints(
+ validConfig,
+ validConfig.database,
+ "student",
+ );
+ expect(checks).toEqual([]);
+ });
+
+ test("Should Parse and Split Multi-statement Migrations", () => {
+ const sql = `
+ -- First statement
+ CREATE TABLE \`testdb\`.\`temp_1\` (id UInt32) ENGINE = MergeTree() ORDER BY id;
+ -- Second statement
+ CREATE TABLE \`testdb\`.\`temp_2\` (id UInt32) ENGINE = MergeTree() ORDER BY id;
+ `;
+ const stmts = clickhouseConnector.splitClickHouseStatements(sql);
+ expect(stmts).toHaveLength(2);
+ expect(stmts[0]).toContain("`testdb`.`temp_1`");
+ expect(stmts[1]).toContain("`testdb`.`temp_2`");
+ });
+});
diff --git a/bridge/__tests__/discoveryService.test.ts b/bridge/__tests__/discoveryService.test.ts
index 40f9e475..ae078d6f 100644
--- a/bridge/__tests__/discoveryService.test.ts
+++ b/bridge/__tests__/discoveryService.test.ts
@@ -34,6 +34,16 @@ describe("DiscoveryService", () => {
expect(name).not.toContain("-3306");
});
+ test("should not add port suffix for default ClickHouse port", () => {
+ const name = service.generateFunName("clickhouse", 8123);
+ expect(name).not.toContain("-8123");
+ });
+
+ test("should add port suffix for non-default ClickHouse port", () => {
+ const name = service.generateFunName("clickhouse", 8124);
+ expect(name).toMatch(/^[a-z]+-[a-z]+-8124$/);
+ });
+
test("should add port suffix for non-default MySQL port", () => {
const name = service.generateFunName("mysql", 3307);
expect(name).toMatch(/^[a-z]+-[a-z]+-3307$/);
@@ -54,7 +64,7 @@ describe("DiscoveryService", () => {
// Access private method via prototype for testing
const extractCredentials = (
svc: DiscoveryService,
- dbType: "postgresql" | "mysql" | "mariadb",
+ dbType: "postgresql" | "mysql" | "mariadb" | "clickhouse",
envVars: Map,
) => {
return (svc as any).extractCredentialsFromEnv(dbType, envVars);
@@ -181,6 +191,45 @@ describe("DiscoveryService", () => {
expect(result.password).toBe("mysqlrootpass");
});
});
+
+ describe("ClickHouse", () => {
+ test("should extract ClickHouse credentials with CLICKHOUSE_USER", () => {
+ const envVars = new Map([
+ ["CLICKHOUSE_USER", "chuser"],
+ ["CLICKHOUSE_PASSWORD", "chpass"],
+ ["CLICKHOUSE_DB", "analytics"],
+ ]);
+
+ const result = extractCredentials(service, "clickhouse", envVars);
+
+ expect(result.user).toBe("chuser");
+ expect(result.password).toBe("chpass");
+ expect(result.database).toBe("analytics");
+ });
+
+ test("should fall back to CLICKHOUSE_DEFAULT_USER and CLICKHOUSE_DEFAULT_PASSWORD", () => {
+ const envVars = new Map([
+ ["CLICKHOUSE_DEFAULT_USER", "defaultadmin"],
+ ["CLICKHOUSE_DEFAULT_PASSWORD", "secret123"],
+ ]);
+
+ const result = extractCredentials(service, "clickhouse", envVars);
+
+ expect(result.user).toBe("defaultadmin");
+ expect(result.password).toBe("secret123");
+ expect(result.database).toBe("default");
+ });
+
+ test("should use defaults when ClickHouse env vars are missing", () => {
+ const envVars = new Map();
+
+ const result = extractCredentials(service, "clickhouse", envVars);
+
+ expect(result.user).toBe("default");
+ expect(result.password).toBe("");
+ expect(result.database).toBe("default");
+ });
+ });
});
describe("getDefaultPort (private)", () => {
@@ -200,6 +249,10 @@ describe("DiscoveryService", () => {
expect(getDefaultPort(service, "mariadb")).toBe(3306);
});
+ test("should return 8123 for clickhouse", () => {
+ expect(getDefaultPort(service, "clickhouse")).toBe(8123);
+ });
+
test("should return 0 for unknown type", () => {
expect(getDefaultPort(service, "unknown")).toBe(0);
});
@@ -222,6 +275,10 @@ describe("DiscoveryService", () => {
expect(getDefaultUser(service, "mariadb")).toBe("root");
});
+ test("should return default for clickhouse", () => {
+ expect(getDefaultUser(service, "clickhouse")).toBe("default");
+ });
+
test("should return empty string for unknown type", () => {
expect(getDefaultUser(service, "unknown")).toBe("");
});
@@ -244,6 +301,10 @@ describe("DiscoveryService", () => {
expect(getDefaultDatabase(service, "mariadb")).toBe("mysql");
});
+ test("should return default for clickhouse", () => {
+ expect(getDefaultDatabase(service, "clickhouse")).toBe("default");
+ });
+
test("should return empty string for unknown type", () => {
expect(getDefaultDatabase(service, "unknown")).toBe("");
});
@@ -321,9 +382,20 @@ describe("DiscoveryService", () => {
defaultDatabase: "mysql",
};
+ const clickhouseDb: DiscoveredDatabase = {
+ type: "clickhouse",
+ host: "localhost",
+ port: 8123,
+ source: "local",
+ suggestedName: "clickhouse-db",
+ defaultUser: "default",
+ defaultDatabase: "default",
+ };
+
expect(pgDb.type).toBe("postgresql");
expect(mysqlDb.type).toBe("mysql");
expect(mariaDb.type).toBe("mariadb");
+ expect(clickhouseDb.type).toBe("clickhouse");
});
});
@@ -340,7 +412,7 @@ describe("DiscoveryService", () => {
for (const db of result) {
expect(db.type).toBeDefined();
- expect(["postgresql", "mysql", "mariadb"]).toContain(db.type);
+ expect(["postgresql", "mysql", "mariadb", "clickhouse"]).toContain(db.type);
expect(db.host).toBeDefined();
expect(typeof db.port).toBe("number");
expect(db.source).toBeDefined();
diff --git a/bridge/__tests__/monitoringHandlers.test.ts b/bridge/__tests__/monitoringHandlers.test.ts
index 7ab2906d..6291c421 100644
--- a/bridge/__tests__/monitoringHandlers.test.ts
+++ b/bridge/__tests__/monitoringHandlers.test.ts
@@ -91,6 +91,34 @@ describe("MonitoringHandlers", () => {
});
});
+ test("returns monitoring snapshot for a ClickHouse database", async () => {
+ const conn = { host: "127.0.0.1", port: 8123 };
+ const snapshot = {
+ databaseType: DBType.CLICKHOUSE,
+ sampledAt: "2026-05-16T00:00:00.000Z",
+ health: { ok: true, latencyMs: 5 },
+ connections: { active: 3, max: 100, usagePct: 3 },
+ throughput: { qps: 15, totalQueries: 5000 },
+ cacheHitRatio: 95.5,
+ activeQueries: [],
+ };
+
+ (dbService.getDatabaseConnection as any).mockResolvedValue({
+ conn,
+ dbType: DBType.CLICKHOUSE,
+ });
+ (monitoringService.getSnapshot as any).mockResolvedValue(snapshot);
+
+ await handlers.handleGetSnapshot({ id: "ch-1" }, 10);
+
+ expect(dbService.getDatabaseConnection).toHaveBeenCalledWith("ch-1");
+ expect(monitoringService.getSnapshot).toHaveBeenCalledWith("ch-1", conn, DBType.CLICKHOUSE);
+ expect(rpc.sendResponse).toHaveBeenCalledWith(10, {
+ ok: true,
+ data: snapshot,
+ });
+ });
+
test("returns MONITORING_ERROR when snapshot generation fails", async () => {
(dbService.getDatabaseConnection as any).mockResolvedValue({
conn: {},
diff --git a/bridge/__tests__/monitoringService.test.ts b/bridge/__tests__/monitoringService.test.ts
new file mode 100644
index 00000000..abb7e49e
--- /dev/null
+++ b/bridge/__tests__/monitoringService.test.ts
@@ -0,0 +1,193 @@
+import { describe, expect, test, jest, beforeEach } from "@jest/globals";
+import { DBType } from "../src/types";
+import { ClickHouseConfig } from "../src/types/clickhouse";
+
+const mockClickHouseClient = {
+ query: jest.fn() as any,
+};
+
+jest.mock("../src/connectors/clickhouse", () => ({
+ getClient: jest.fn(() => mockClickHouseClient),
+}));
+
+import { MonitoringService } from "../src/services/monitoringService";
+
+describe("MonitoringService", () => {
+ let service: MonitoringService;
+
+ beforeEach(() => {
+ jest.clearAllMocks();
+ service = new MonitoringService();
+ });
+
+ test("throws for unsupported database types like sqlite or duckdb", async () => {
+ await expect(service.getSnapshot("db-1", {}, DBType.SQLITE)).rejects.toThrow(
+ "Monitoring is not supported for sqlite",
+ );
+ await expect(service.getSnapshot("db-2", {}, DBType.DUCKDB)).rejects.toThrow(
+ "Monitoring is not supported for duckdb",
+ );
+ });
+
+ test("generates complete ClickHouse monitoring snapshot on success", async () => {
+ const config: ClickHouseConfig = {
+ host: "localhost",
+ port: 8123,
+ user: "default",
+ database: "default",
+ };
+
+ mockClickHouseClient.query.mockImplementation((params: any) => {
+ const q = params.query;
+ if (q.includes("SELECT 1;")) {
+ return Promise.resolve({
+ json: async () => [{ 1: 1 }],
+ });
+ }
+ if (q.includes("FROM system.metrics")) {
+ return Promise.resolve({
+ json: async () => [
+ { metric: "TCPConnection", value: 3 },
+ { metric: "HTTPConnection", value: 2 },
+ { metric: "Query", value: 1 },
+ ],
+ });
+ }
+ if (q.includes("FROM system.settings")) {
+ return Promise.resolve({
+ json: async () => [{ value: "200" }],
+ });
+ }
+ if (q.includes("FROM system.events")) {
+ return Promise.resolve({
+ json: async () => [
+ { event: "Query", value: 1000 },
+ { event: "MarkCacheHits", value: 950 },
+ { event: "MarkCacheMisses", value: 50 },
+ ],
+ });
+ }
+ if (q.includes("FROM system.processes")) {
+ return Promise.resolve({
+ json: async () => [
+ {
+ id: "query-abc",
+ user: "analyst",
+ query: "SELECT count() FROM logs",
+ state: "running",
+ durationSeconds: 3.45,
+ },
+ ],
+ });
+ }
+ return Promise.resolve({ json: async () => [] });
+ });
+
+ const snapshot = await service.getSnapshot("ch-1", config, DBType.CLICKHOUSE);
+
+ expect(snapshot.databaseType).toBe(DBType.CLICKHOUSE);
+ expect(snapshot.health.ok).toBe(true);
+ expect(typeof snapshot.health.latencyMs).toBe("number");
+ expect(snapshot.connections).toEqual({
+ active: 5,
+ max: 200,
+ usagePct: 2.5,
+ });
+ expect(snapshot.throughput.totalQueries).toBe(1000);
+ expect(snapshot.throughput.qps).toBe(0); // first sample
+ expect(snapshot.cacheHitRatio).toBe(95); // 950 / (950 + 50) * 100
+ expect(snapshot.activeQueries).toEqual([
+ {
+ id: "query-abc",
+ user: "analyst",
+ query: "SELECT count() FROM logs",
+ state: "running",
+ durationSeconds: 3.45,
+ },
+ ]);
+ });
+
+ test("calculates QPS correctly on subsequent snapshot calls", async () => {
+ const config: ClickHouseConfig = {
+ host: "localhost",
+ port: 8123,
+ };
+
+ mockClickHouseClient.query.mockImplementation((params: any) => {
+ const q = params.query;
+ if (q.includes("FROM system.events")) {
+ return Promise.resolve({
+ json: async () => [{ event: "Query", value: 1000 }],
+ });
+ }
+ return Promise.resolve({ json: async () => [] });
+ });
+
+ await service.getSnapshot("ch-qps", config, DBType.CLICKHOUSE);
+
+ // Mock next sample with 100 additional queries
+ mockClickHouseClient.query.mockImplementation((params: any) => {
+ const q = params.query;
+ if (q.includes("FROM system.events")) {
+ return Promise.resolve({
+ json: async () => [{ event: "Query", value: 1100 }],
+ });
+ }
+ return Promise.resolve({ json: async () => [] });
+ });
+
+ const secondSnapshot = await service.getSnapshot("ch-qps", config, DBType.CLICKHOUSE);
+ expect(secondSnapshot.throughput.totalQueries).toBe(1100);
+ expect(secondSnapshot.throughput.qps).toBeGreaterThanOrEqual(0);
+ });
+
+ test("handles partial metric query failures gracefully with defaults", async () => {
+ const config: ClickHouseConfig = { host: "localhost" };
+
+ mockClickHouseClient.query.mockImplementation((params: any) => {
+ const q = params.query;
+ if (q.includes("SELECT 1;")) {
+ return Promise.resolve({ json: async () => [{ 1: 1 }] });
+ }
+ // Settings and processes fail (e.g. permission denied)
+ if (q.includes("FROM system.settings") || q.includes("FROM system.processes")) {
+ return Promise.reject(new Error("Permission denied"));
+ }
+ if (q.includes("FROM system.metrics")) {
+ return Promise.resolve({
+ json: async () => [{ metric: "TCPConnection", value: 1 }],
+ });
+ }
+ if (q.includes("FROM system.events")) {
+ return Promise.resolve({
+ json: async () => [{ event: "Query", value: 50 }],
+ });
+ }
+ return Promise.resolve({ json: async () => [] });
+ });
+
+ const snapshot = await service.getSnapshot("ch-partial", config, DBType.CLICKHOUSE);
+
+ expect(snapshot.health.ok).toBe(true);
+ expect(snapshot.connections.active).toBe(1);
+ expect(snapshot.connections.max).toBe(100); // fallback default
+ expect(snapshot.activeQueries).toEqual([]); // fallback gracefully
+ expect(snapshot.cacheHitRatio).toBeNull();
+ });
+
+ test("returns empty snapshot when ClickHouse connection fails", async () => {
+ const config: ClickHouseConfig = { host: "unreachable" };
+
+ mockClickHouseClient.query.mockRejectedValue(new Error("Connection refused") as any);
+
+ const snapshot = await service.getSnapshot("ch-down", config, DBType.CLICKHOUSE);
+
+ expect(snapshot.databaseType).toBe(DBType.CLICKHOUSE);
+ expect(snapshot.health.ok).toBe(false);
+ expect(snapshot.health.latencyMs).toBeNull();
+ expect(snapshot.health.message).toContain("Connection refused");
+ expect(snapshot.connections).toEqual({ active: 0, max: 0, usagePct: 0 });
+ expect(snapshot.throughput).toEqual({ qps: 0, totalQueries: 0 });
+ expect(snapshot.activeQueries).toEqual([]);
+ });
+});
diff --git a/bridge/__tests__/monitoringWebSocketServer.test.ts b/bridge/__tests__/monitoringWebSocketServer.test.ts
index 667c0cf6..fd27983b 100644
--- a/bridge/__tests__/monitoringWebSocketServer.test.ts
+++ b/bridge/__tests__/monitoringWebSocketServer.test.ts
@@ -171,4 +171,39 @@ describe("MonitoringWebSocketServer", () => {
expect(setIntervalSpy).toHaveBeenCalledWith(expect.any(Function), 2000);
expect(clearIntervalSpy).not.toHaveBeenCalled();
});
+
+ test("sends snapshots over websocket for clickhouse database", async () => {
+ server.start();
+ const wsServer = mockWebSocketServerInstances[0];
+ const socket = createMockSocket();
+
+ (dbService.getDatabaseConnection as any).mockResolvedValue({
+ conn: { host: "localhost", port: 8123 },
+ dbType: DBType.CLICKHOUSE,
+ });
+ (monitoringService.getSnapshot as any).mockResolvedValue({
+ databaseType: DBType.CLICKHOUSE,
+ sampledAt: "2026-05-16T00:00:00.000Z",
+ health: { ok: true, latencyMs: 8 },
+ connections: { active: 2, max: 100, usagePct: 2 },
+ throughput: { qps: 20, totalQueries: 500 },
+ cacheHitRatio: 90.0,
+ activeQueries: [],
+ });
+
+ await wsServer.emit("connection", socket, {
+ url: "/monitoring?dbId=ch-1",
+ } as any);
+
+ expect(dbService.getDatabaseConnection).toHaveBeenCalledWith("ch-1");
+ expect(monitoringService.getSnapshot).toHaveBeenCalledWith(
+ "ch-1",
+ { host: "localhost", port: 8123 },
+ DBType.CLICKHOUSE,
+ );
+ expect(JSON.parse(socket.send.mock.calls[0][0] as string)).toMatchObject({
+ type: "snapshot",
+ data: expect.objectContaining({ databaseType: DBType.CLICKHOUSE }),
+ });
+ });
});
diff --git a/bridge/docker-compose.test.yml b/bridge/docker-compose.test.yml
index 1a045cc0..190aab41 100644
--- a/bridge/docker-compose.test.yml
+++ b/bridge/docker-compose.test.yml
@@ -49,6 +49,24 @@ services:
timeout: 5s
retries: 5
+ clickhouse:
+ image: clickhouse/clickhouse-server:latest
+ environment:
+ CLICKHOUSE_USER: testuser
+ CLICKHOUSE_PASSWORD: testpass
+ CLICKHOUSE_DB: testdb
+ CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT: 1
+ ports:
+ - "8123:8123"
+ - "9000:9000"
+ volumes:
+ - ./scripts/seed-clickhouse.sql:/docker-entrypoint-initdb.d/seed.sql
+ healthcheck:
+ test: ["CMD-SHELL", "wget -q --spider http://localhost:8123/ping || exit 1"]
+ interval: 10s
+ timeout: 5s
+ retries: 5
+
ssh-server:
image: linuxserver/openssh-server
environment:
diff --git a/bridge/package.json b/bridge/package.json
index fb8d5bb8..a733202b 100644
--- a/bridge/package.json
+++ b/bridge/package.json
@@ -14,10 +14,12 @@
"build:pkg:linux": "npm run build && npm run build:bundle-for-pkg && npm run rebuild:native && npm run copy:native && npx @yao-pkg/pkg dist/index.bundled.cjs --target node22-linux-x64 --output ../src-tauri/resources/bridge-x86_64-unknown-linux-gnu",
"test": "jest",
"test:watch": "jest --watchAll --detectOpenHandles",
- "seed:duckdb": "ts-node seed.ts"
+ "seed:duckdb": "ts-node seed.ts",
+ "seed:clickhouse": "ts-node seed-clickhouse.ts"
},
"dependencies": {
"@anthropic-ai/sdk": "^0.100.1",
+ "@clickhouse/client": "^1.24.1",
"@duckdb/node-api": "1.5.6-r.1",
"@google/generative-ai": "^0.24.1",
"@jest/globals": "^30.2.0",
diff --git a/bridge/pnpm-lock.yaml b/bridge/pnpm-lock.yaml
index 19f915da..47fc76ed 100644
--- a/bridge/pnpm-lock.yaml
+++ b/bridge/pnpm-lock.yaml
@@ -11,6 +11,9 @@ importers:
'@anthropic-ai/sdk':
specifier: ^0.100.1
version: 0.100.1(zod@4.4.3)
+ '@clickhouse/client':
+ specifier: ^1.24.1
+ version: 1.24.1
'@duckdb/node-api':
specifier: 1.5.6-r.1
version: 1.5.6-r.1
@@ -283,6 +286,10 @@ packages:
'@bcoe/v8-coverage@0.2.3':
resolution: {integrity: sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==}
+ '@clickhouse/client@1.24.1':
+ resolution: {integrity: sha512-1kjKJ4uuD4J9JlRU4CpwEGfecstvtoGreBorhh7CxgB7r7PL54DCLD6gajKZhLCri84XlyD3CUxEm6rYkKC+dA==}
+ engines: {node: '>=20'}
+
'@cspotcode/source-map-support@0.8.1':
resolution: {integrity: sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==}
engines: {node: '>=12'}
@@ -2578,6 +2585,8 @@ snapshots:
'@bcoe/v8-coverage@0.2.3': {}
+ '@clickhouse/client@1.24.1': {}
+
'@cspotcode/source-map-support@0.8.1':
dependencies:
'@jridgewell/trace-mapping': 0.3.9
diff --git a/bridge/scripts/seed-clickhouse.cjs b/bridge/scripts/seed-clickhouse.cjs
new file mode 100644
index 00000000..f1da0f05
--- /dev/null
+++ b/bridge/scripts/seed-clickhouse.cjs
@@ -0,0 +1,70 @@
+const fs = require("fs");
+const path = require("path");
+const { createClient } = require("@clickhouse/client");
+
+function wait(ms) {
+ return new Promise((resolve) => setTimeout(resolve, ms));
+}
+
+async function seed() {
+ const host = process.env.REAL_CLICKHOUSE_HOST || "127.0.0.1";
+ const port = process.env.REAL_CLICKHOUSE_PORT || "8123";
+ const user = process.env.REAL_CLICKHOUSE_USER || "testuser";
+ const password = process.env.REAL_CLICKHOUSE_PASSWORD || "testpass";
+ const url = process.env.REAL_CLICKHOUSE_URL || `http://${host}:${port}`;
+
+ const client = createClient({
+ url,
+ username: user,
+ password,
+ database: "default",
+ request_timeout: 10000,
+ });
+
+ let connected = false;
+ for (let attempt = 1; attempt <= 15; attempt++) {
+ try {
+ const ping = await client.ping();
+ if (ping.success) {
+ connected = true;
+ break;
+ }
+ } catch {
+ // Retry after delay
+ }
+ await wait(1000);
+ }
+
+ if (!connected) {
+ throw new Error(`ClickHouse at ${url} was not reachable after 15 attempts.`);
+ }
+
+ const sqlPath = path.join(__dirname, "seed-clickhouse.sql");
+ const sql = fs.readFileSync(sqlPath, "utf-8");
+
+ const cleanSql = sql
+ .split("\n")
+ .map((line) => {
+ const trimmed = line.trim();
+ if (trimmed.startsWith("--")) return "";
+ return line;
+ })
+ .join("\n");
+
+ const statements = cleanSql
+ .split(";")
+ .map((s) => s.trim())
+ .filter((s) => s.length > 0);
+
+ for (const statement of statements) {
+ await client.command({ query: statement });
+ }
+
+ await client.close();
+ console.log("ClickHouse test database seeded successfully.");
+}
+
+seed().catch((err) => {
+ console.error("Failed to seed ClickHouse:", err);
+ process.exit(1);
+});
diff --git a/bridge/scripts/seed-clickhouse.sql b/bridge/scripts/seed-clickhouse.sql
new file mode 100644
index 00000000..a0a82729
--- /dev/null
+++ b/bridge/scripts/seed-clickhouse.sql
@@ -0,0 +1,42 @@
+-- ClickHouse seed script for test databases
+-- Mounted to /docker-entrypoint-initdb.d/seed.sql
+
+CREATE DATABASE IF NOT EXISTS testdb;
+
+CREATE TABLE IF NOT EXISTS testdb.TestTable (
+ id UInt32,
+ name String,
+ created_at DateTime DEFAULT now()
+) ENGINE = MergeTree()
+ORDER BY id;
+
+CREATE TABLE IF NOT EXISTS testdb.persons (
+ id UInt32,
+ name String,
+ email Nullable(String),
+ created_at DateTime DEFAULT now()
+) ENGINE = MergeTree()
+ORDER BY id;
+
+CREATE TABLE IF NOT EXISTS testdb.student (
+ id UInt32,
+ name String,
+ address Nullable(String),
+ created_at DateTime DEFAULT now()
+) ENGINE = MergeTree()
+ORDER BY id;
+
+INSERT INTO testdb.TestTable (id, name) VALUES
+ (1, 'Test Item 1'),
+ (2, 'Test Item 2'),
+ (3, 'Test Item 3');
+
+INSERT INTO testdb.persons (id, name, email) VALUES
+ (1, 'John Doe', 'john@example.com'),
+ (2, 'Jane Smith', 'jane@example.com'),
+ (3, 'Bob Wilson', 'bob@example.com');
+
+INSERT INTO testdb.student (id, name, address) VALUES
+ (1, 'Alice Johnson', '123 Main St'),
+ (2, 'Charlie Brown', '456 Oak Ave'),
+ (3, 'Diana Ross', '789 Pine Rd');
diff --git a/bridge/seed-clickhouse.ts b/bridge/seed-clickhouse.ts
new file mode 100644
index 00000000..41bfe9e4
--- /dev/null
+++ b/bridge/seed-clickhouse.ts
@@ -0,0 +1,433 @@
+import { createClient } from "@clickhouse/client";
+import { cleanClickHouseUrl } from "./src/utils/clickhouseUrl";
+
+declare const process: {
+ argv: string[];
+ env: Record;
+ exit(code?: number): never;
+ stdout: {
+ write(chunk: string): boolean;
+ };
+};
+
+interface ScaleConfig {
+ users: number;
+ products: number;
+ orders: number;
+ events: number;
+ metrics: number;
+}
+
+const SCALE_PRESETS: Record = {
+ small: {
+ users: 10_000,
+ products: 1_000,
+ orders: 25_000,
+ events: 50_000,
+ metrics: 20_000,
+ },
+ medium: {
+ users: 50_000,
+ products: 5_000,
+ orders: 150_000,
+ events: 300_000,
+ metrics: 100_000,
+ },
+ large: {
+ users: 100_000,
+ products: 10_000,
+ orders: 300_000,
+ events: 600_000,
+ metrics: 250_000,
+ },
+ huge: {
+ users: 500_000,
+ products: 50_000,
+ orders: 1_500_000,
+ events: 3_000_000,
+ metrics: 1_000_000,
+ },
+};
+
+function parseArgs(): {
+ url: string;
+ username: string;
+ password?: string;
+ database: string;
+ scale: string;
+ drop: boolean;
+} {
+ const args = process.argv.slice(2);
+ let url = process.env.CLICKHOUSE_URL || "https://of8remuy3t.ap-south-1.aws.clickhouse.cloud:8443";
+ let username = process.env.CLICKHOUSE_USER || "default";
+ let password = process.env.CLICKHOUSE_PASSWORD || "HpD2b9hQ5KW.3";
+ let database = process.env.CLICKHOUSE_DATABASE || "default";
+ let scale = "medium";
+ let drop = true;
+
+ for (let i = 0; i < args.length; i++) {
+ const arg = args[i];
+ if (arg === "--help" || arg === "-h") {
+ console.log(`
+ClickHouse High-Performance Seed Tool for RelWave
+==================================================
+Usage:
+ npx ts-node seed-clickhouse.ts [options]
+ npm run seed:clickhouse -- [options]
+
+Options:
+ --url, -u ClickHouse URL (default: cloud cluster or CLICKHOUSE_URL)
+ --user ClickHouse username (default: default)
+ --password, -p ClickHouse password
+ --database, -d Target database name (default: default)
+ --scale, -s Volume preset: small | medium | large | huge (default: medium)
+ - small: ~106,000 rows
+ - medium: ~605,000 rows
+ - large: ~1,260,000 rows
+ - huge: ~6,050,000 rows
+ --no-drop Do not drop existing tables before inserting
+ --help, -h Show this help message
+
+Examples:
+ npm run seed:clickhouse
+ npm run seed:clickhouse -- --scale small
+ npm run seed:clickhouse -- --url http://localhost:8123 --database test_db
+ `);
+ process.exit(0);
+ } else if (arg === "--url" || arg === "-u") {
+ url = args[++i];
+ } else if (arg === "--user") {
+ username = args[++i];
+ } else if (arg === "--password" || arg === "-p") {
+ password = args[++i];
+ } else if (arg === "--database" || arg === "-d") {
+ database = args[++i];
+ } else if (arg === "--scale" || arg === "-s") {
+ scale = args[++i]?.toLowerCase() || "medium";
+ } else if (arg === "--no-drop") {
+ drop = false;
+ }
+ }
+
+ return {
+ url: cleanClickHouseUrl(url),
+ username,
+ password,
+ database,
+ scale,
+ drop,
+ };
+}
+
+async function main() {
+ const { url, username, password, database, scale, drop } = parseArgs();
+ const config = SCALE_PRESETS[scale] || SCALE_PRESETS.medium;
+ const totalPlanned =
+ config.users + config.products + config.orders + config.events + config.metrics;
+
+ console.log("\n=======================================================");
+ console.log(" RelWave ClickHouse Benchmark & Seed Generator");
+ console.log("=======================================================");
+ console.log(`Target URL: ${url}`);
+ console.log(`Database: ${database}`);
+ console.log(`User: ${username}`);
+ console.log(`Preset: ${scale.toUpperCase()}`);
+ console.log(`Planned Rows:`);
+ console.log(` - users: ${config.users.toLocaleString()}`);
+ console.log(` - products: ${config.products.toLocaleString()}`);
+ console.log(` - orders: ${config.orders.toLocaleString()}`);
+ console.log(` - analytics_events: ${config.events.toLocaleString()}`);
+ console.log(` - server_metrics: ${config.metrics.toLocaleString()}`);
+ console.log(` - Total: ~${totalPlanned.toLocaleString()}`);
+ console.log("-------------------------------------------------------\n");
+
+ const startTime = Date.now();
+ const client = createClient({
+ url,
+ username,
+ password,
+ database,
+ request_timeout: 60_000,
+ });
+
+ console.log("1. Verifying ClickHouse connection...");
+ const ping = await client.ping();
+ if (!ping.success) {
+ throw new Error("Failed to ping ClickHouse server. Please check URL & credentials.");
+ }
+ console.log(" Connected successfully!\n");
+
+ if (drop) {
+ console.log("2. Dropping existing tables (clean slate)...");
+ const dropTables = [
+ "server_metrics",
+ "analytics_events",
+ "orders",
+ "products",
+ "users",
+ "schema_migrations",
+ ];
+ for (const tbl of dropTables) {
+ await client.command({
+ query: `DROP TABLE IF EXISTS ${database}.${tbl}`,
+ });
+ }
+ console.log(" Existing tables dropped.\n");
+ }
+
+ console.log("3. Creating tables (MergeTree engines)...");
+
+ // Users table
+ await client.command({
+ query: `
+ CREATE TABLE IF NOT EXISTS ${database}.users (
+ id UInt64,
+ name String,
+ email String,
+ role LowCardinality(String),
+ country LowCardinality(String),
+ is_active UInt8,
+ signup_date Date32,
+ created_at DateTime
+ ) ENGINE = MergeTree()
+ ORDER BY id
+ `,
+ });
+
+ // Products table
+ await client.command({
+ query: `
+ CREATE TABLE IF NOT EXISTS ${database}.products (
+ id UInt64,
+ name String,
+ category LowCardinality(String),
+ price Decimal(10, 2),
+ stock UInt32,
+ rating Float32,
+ created_at DateTime
+ ) ENGINE = MergeTree()
+ ORDER BY id
+ `,
+ });
+
+ // Orders table with date partitioning
+ await client.command({
+ query: `
+ CREATE TABLE IF NOT EXISTS ${database}.orders (
+ id UInt64,
+ user_id UInt64,
+ product_id UInt64,
+ quantity UInt16,
+ total_amount Decimal(10, 2),
+ status LowCardinality(String),
+ payment_method LowCardinality(String),
+ order_date DateTime
+ ) ENGINE = MergeTree()
+ PARTITION BY toYYYYMM(order_date)
+ ORDER BY (order_date, id)
+ `,
+ });
+
+ // Analytics events table (typical ClickHouse workload)
+ await client.command({
+ query: `
+ CREATE TABLE IF NOT EXISTS ${database}.analytics_events (
+ id UInt64,
+ user_id UInt64,
+ event_type LowCardinality(String),
+ url String,
+ device LowCardinality(String),
+ ip_address String,
+ duration_ms UInt32,
+ event_time DateTime
+ ) ENGINE = MergeTree()
+ PARTITION BY toYYYYMM(event_time)
+ ORDER BY (event_type, event_time, id)
+ `,
+ });
+
+ // Server metrics timeseries table
+ await client.command({
+ query: `
+ CREATE TABLE IF NOT EXISTS ${database}.server_metrics (
+ timestamp DateTime,
+ hostname LowCardinality(String),
+ cpu_usage Float32,
+ memory_usage Float32,
+ disk_io_read UInt64,
+ disk_io_write UInt64
+ ) ENGINE = MergeTree()
+ ORDER BY (hostname, timestamp)
+ `,
+ });
+
+ // Schema migrations table for RelWave migration tracking
+ await client.command({
+ query: `
+ CREATE TABLE IF NOT EXISTS ${database}.schema_migrations (
+ version String,
+ name String,
+ applied_at DateTime DEFAULT now(),
+ checksum String
+ ) ENGINE = MergeTree()
+ ORDER BY version
+ `,
+ });
+
+ console.log(" All tables created.\n");
+
+ console.log("4. Generating data with ClickHouse vectorized execution engine...");
+
+ // Seed users
+ process.stdout.write(` -> Inserting ${config.users.toLocaleString()} users... `);
+ const uStart = Date.now();
+ await client.command({
+ query: `
+ INSERT INTO ${database}.users
+ SELECT
+ number + 1 AS id,
+ concat('User_', toString(number + 1)) AS name,
+ concat('user_', toString(number + 1), '@example.com') AS email,
+ ['admin', 'member', 'viewer', 'editor'][(number % 4) + 1] AS role,
+ ['US', 'IN', 'DE', 'GB', 'CA', 'FR', 'JP', 'AU'][(number % 8) + 1] AS country,
+ toUInt8(number % 5 != 0) AS is_active,
+ toDate(today() - (number % 365)) AS signup_date,
+ toDateTime(now() - (number % 86400)) AS created_at
+ FROM numbers(${config.users})
+ `,
+ });
+ console.log(`done in ${Date.now() - uStart}ms`);
+
+ // Seed products
+ process.stdout.write(` -> Inserting ${config.products.toLocaleString()} products... `);
+ const pStart = Date.now();
+ await client.command({
+ query: `
+ INSERT INTO ${database}.products
+ SELECT
+ number + 1 AS id,
+ concat('Product ', toString(number + 1)) AS name,
+ ['Electronics', 'Books', 'Clothing', 'Home', 'Toys', 'Sports', 'Garden', 'Automotive'][(number % 8) + 1] AS category,
+ toDecimal64(10.0 + ((number % 500) * 1.5), 2) AS price,
+ toUInt32((number % 100) * 5) AS stock,
+ toFloat32(round(3.0 + ((number % 20) * 0.1), 1)) AS rating,
+ toDateTime(now() - ((number % 1000) * 86400)) AS created_at
+ FROM numbers(${config.products})
+ `,
+ });
+ console.log(`done in ${Date.now() - pStart}ms`);
+
+ // Seed orders
+ process.stdout.write(` -> Inserting ${config.orders.toLocaleString()} orders... `);
+ const oStart = Date.now();
+ await client.command({
+ query: `
+ INSERT INTO ${database}.orders
+ SELECT
+ number + 1 AS id,
+ (number % ${config.users}) + 1 AS user_id,
+ (number % ${config.products}) + 1 AS product_id,
+ toUInt16((number % 5) + 1) AS quantity,
+ toDecimal64(((number % 5) + 1) * (15.0 + (number % 100)), 2) AS total_amount,
+ ['pending', 'processing', 'completed', 'cancelled', 'refunded'][(number % 5) + 1] AS status,
+ ['credit_card', 'paypal', 'apple_pay', 'crypto', 'bank_transfer'][(number % 5) + 1] AS payment_method,
+ toDateTime(now() - ((number % 300) * 86400)) AS order_date
+ FROM numbers(${config.orders})
+ `,
+ });
+ console.log(`done in ${Date.now() - oStart}ms`);
+
+ // Seed analytics_events
+ process.stdout.write(` -> Inserting ${config.events.toLocaleString()} analytics events... `);
+ const eStart = Date.now();
+ await client.command({
+ query: `
+ INSERT INTO ${database}.analytics_events
+ SELECT
+ number + 1 AS id,
+ (number % ${config.users}) + 1 AS user_id,
+ ['page_view', 'click', 'scroll', 'add_to_cart', 'search', 'checkout'][(number % 6) + 1] AS event_type,
+ ['/home', '/products', '/cart', '/checkout', '/profile', '/settings'][(number % 6) + 1] AS url,
+ ['mobile', 'desktop', 'tablet'][(number % 3) + 1] AS device,
+ concat('192.168.', toString(number % 255), '.', toString((number * 7) % 255)) AS ip_address,
+ toUInt32((number % 3000) + 50) AS duration_ms,
+ toDateTime(now() - ((number % 10000) * 60)) AS event_time
+ FROM numbers(${config.events})
+ `,
+ });
+ console.log(`done in ${Date.now() - eStart}ms`);
+
+ // Seed server_metrics
+ process.stdout.write(` -> Inserting ${config.metrics.toLocaleString()} server metrics... `);
+ const mStart = Date.now();
+ await client.command({
+ query: `
+ INSERT INTO ${database}.server_metrics
+ SELECT
+ toDateTime(now() - (number * 10)) AS timestamp,
+ ['prod-api-1', 'prod-api-2', 'prod-db-1', 'worker-1', 'cache-1'][(number % 5) + 1] AS hostname,
+ toFloat32(round(10.0 + ((number % 80) * 0.9), 2)) AS cpu_usage,
+ toFloat32(round(30.0 + ((number % 60) * 0.8), 2)) AS memory_usage,
+ toUInt64(number * 1024) AS disk_io_read,
+ toUInt64(number * 512) AS disk_io_write
+ FROM numbers(${config.metrics})
+ `,
+ });
+ console.log(`done in ${Date.now() - mStart}ms`);
+
+ // Seed schema_migrations baseline
+ await client.command({
+ query: `
+ INSERT INTO ${database}.schema_migrations (version, name, checksum)
+ VALUES ('20261014000000', 'initial_clickhouse_seed', 'clickhouse_seed_baseline')
+ `,
+ });
+
+ const durationMs = Date.now() - startTime;
+
+ // Retrieve verification stats
+ console.log("\n5. Verifying seeded table metrics from ClickHouse system catalog...");
+ const statsRes = await client.query({
+ query: `
+ SELECT
+ table,
+ formatReadableQuantity(sum(rows)) AS total_rows,
+ formatReadableSize(sum(data_compressed_bytes)) AS compressed_size,
+ formatReadableSize(sum(data_uncompressed_bytes)) AS uncompressed_size
+ FROM system.parts
+ WHERE database = '${database}'
+ AND active = 1
+ AND table IN ('users', 'products', 'orders', 'analytics_events', 'server_metrics', 'schema_migrations')
+ GROUP BY table
+ ORDER BY table
+ `,
+ format: "JSONEachRow",
+ });
+ const rows = (await statsRes.json()) as Array<{
+ table: string;
+ total_rows: string;
+ compressed_size: string;
+ uncompressed_size: string;
+ }>;
+
+ console.log("\n=======================================================");
+ console.log(" ClickHouse Seeding Completed Successfully! 🎉");
+ console.log("=======================================================");
+ console.log(`Execution Time: ${durationMs} ms (${(durationMs / 1000).toFixed(2)}s)`);
+ console.log(`Target: ${url}`);
+ console.log(`Database: ${database}`);
+ console.log("\nTable Breakdown:");
+ for (const r of rows) {
+ console.log(
+ ` - ${r.table.padEnd(20)}: ${r.total_rows.padStart(10)} rows | ${r.compressed_size.padStart(10)} compressed (${r.uncompressed_size} uncompressed)`,
+ );
+ }
+ console.log("=======================================================\n");
+
+ await client.close();
+}
+
+main().catch((err) => {
+ console.error("\n[Error] ClickHouse seed generation failed:", err);
+ process.exit(1);
+});
diff --git a/bridge/src/ai/providers/groq.provider.ts b/bridge/src/ai/providers/groq.provider.ts
index 06142c32..5adc381c 100644
--- a/bridge/src/ai/providers/groq.provider.ts
+++ b/bridge/src/ai/providers/groq.provider.ts
@@ -13,7 +13,14 @@ import {
parseChartRecommendation,
} from "../prompts/chart-recommendation";
-const DEFAULT_MODEL = "llama-3.3-70b-versatile";
+const DEFAULT_MODEL = "openai/gpt-oss-20b";
+const FALLBACK_MODELS = [
+ "openai/gpt-oss-20b",
+ "qwen/qwen3.8-27b",
+ "openai/gpt-oss-120b",
+ "llama-3.1-8b-instant",
+ "llama-3.3-70b-versatile",
+];
export class GroqProvider implements AIProvider {
private client: Groq;
@@ -36,7 +43,27 @@ export class GroqProvider implements AIProvider {
});
return res.choices[0]?.message?.content ?? "";
} catch (err) {
- throw classifyError(err, "groq");
+ const classified = classifyError(err, "groq");
+ if (classified.code === "INVALID_MODEL") {
+ // Attempt fallback to a supported chat model so ongoing requests don't fail
+ for (const fallback of FALLBACK_MODELS) {
+ if (fallback === this.model) continue;
+ try {
+ const res = await this.client.chat.completions.create({
+ model: fallback,
+ messages: [
+ { role: "system", content: system },
+ { role: "user", content: user },
+ ],
+ max_tokens: 4096,
+ });
+ return res.choices[0]?.message?.content ?? "";
+ } catch {
+ // Continue to next fallback
+ }
+ }
+ }
+ throw classified;
}
}
@@ -57,6 +84,25 @@ export class GroqProvider implements AIProvider {
}
async testConnection(): Promise {
+ let availableChatModels: string[] = [];
+ try {
+ const modelsRes = await this.client.models.list();
+ const nonChatKeywords = [
+ "whisper",
+ "prompt-guard",
+ "orpheus",
+ "safeguard",
+ "vision",
+ "embed",
+ ];
+ availableChatModels = (modelsRes.data ?? [])
+ .map((m) => m.id)
+ .filter((id) => !nonChatKeywords.some((kw) => id.toLowerCase().includes(kw)));
+ } catch (err) {
+ // If models.list fails with invalid key or network issue, classify and throw
+ throw classifyError(err, "groq");
+ }
+
try {
await this.client.chat.completions.create({
model: this.model,
@@ -65,7 +111,12 @@ export class GroqProvider implements AIProvider {
});
return "";
} catch (err) {
- throw classifyError(err, "groq");
+ const classified = classifyError(err, "groq");
+ if (classified.code === "INVALID_MODEL") {
+ const sample = availableChatModels.slice(0, 3).join(", ");
+ return `Connected to Groq (API key verified)! However, model '${this.model}' is not accessible on your account. Available models include: ${sample || "openai/gpt-oss-20b, qwen/qwen3.8-27b"}. Please select one in settings.`;
+ }
+ throw classified;
}
}
diff --git a/bridge/src/ai/providers/mistral.provider.ts b/bridge/src/ai/providers/mistral.provider.ts
index 976e92e2..3844bffd 100644
--- a/bridge/src/ai/providers/mistral.provider.ts
+++ b/bridge/src/ai/providers/mistral.provider.ts
@@ -63,6 +63,15 @@ export class MistralProvider implements AIProvider {
}
async testConnection(): Promise {
+ // 1. Verify credentials and network connectivity via models.list()
+ // This confirms the API key is genuine without consuming chat tokens or triggering chat RPM limits.
+ try {
+ await this.client.models.list();
+ } catch (err) {
+ throw classifyError(err, "mistral");
+ }
+
+ // 2. Test chat completion endpoint with the selected model
try {
await this.client.chat.complete({
model: this.model,
@@ -71,7 +80,14 @@ export class MistralProvider implements AIProvider {
});
return "";
} catch (err) {
- throw classifyError(err, "mistral");
+ const classified = classifyError(err, "mistral");
+ if (classified.code === "RATE_LIMIT") {
+ return "Connected to Mistral (API key verified)! Note: Chat completions are currently rate-limited (code 1300). Free tier accounts have strict quotas (1 req/sec). Please wait a moment or check your plan at console.mistral.ai.";
+ }
+ if (classified.code === "INVALID_MODEL") {
+ return `Connected to Mistral (API key verified)! However, model '${this.model}' is not accessible on your account. Please select 'Mistral Small' or check console.mistral.ai.`;
+ }
+ throw classified;
}
}
diff --git a/bridge/src/ai/providers/types.ts b/bridge/src/ai/providers/types.ts
index 1aef5d3f..3c707950 100644
--- a/bridge/src/ai/providers/types.ts
+++ b/bridge/src/ai/providers/types.ts
@@ -48,56 +48,132 @@ export class AIError extends Error {
readonly code: AIErrorCode;
readonly provider: string;
readonly originalMessage: string;
+ readonly rawMessage?: string;
- constructor(code: AIErrorCode, provider: string, message: string) {
+ constructor(code: AIErrorCode, provider: string, message: string, rawMessage?: string) {
super(`[${provider}] ${message}`);
this.name = "AIError";
this.code = code;
this.provider = provider;
this.originalMessage = message;
+ this.rawMessage = rawMessage;
}
}
+function capitalize(str: string): string {
+ if (!str) return "";
+ return str.charAt(0).toUpperCase() + str.slice(1);
+}
+
+function extractCleanMessage(raw: string): string {
+ try {
+ const jsonMatch = raw.match(/\{[\s\S]*\}/);
+ if (jsonMatch) {
+ const parsed = JSON.parse(jsonMatch[0]);
+ if (parsed.error?.message) {
+ return parsed.error.message;
+ }
+ if (parsed.message) {
+ return parsed.message;
+ }
+ }
+ } catch {
+ // Ignore JSON parsing errors
+ }
+
+ return raw
+ .replace(/^API error occurred:\s*/i, "")
+ .replace(/^Status \d+\s*(Body:\s*)?/i, "")
+ .replace(/^\d{3}\s+/i, "")
+ .trim();
+}
+
/**
* Classify a raw SDK/network error into an AIErrorCode.
*/
export function classifyError(err: unknown, provider: string): AIError {
- const msg = err instanceof Error ? err.message : String(err);
- const lower = msg.toLowerCase();
+ const rawMsg = err instanceof Error ? err.message : String(err);
+ const lower = rawMsg.toLowerCase();
+ const cleanMsg = extractCleanMessage(rawMsg);
+ const provName = capitalize(provider);
if (
lower.includes("api key") ||
lower.includes("apikey") ||
lower.includes("authentication") ||
lower.includes("unauthorized") ||
+ lower.includes("invalid x-api-key") ||
lower.includes("401")
) {
- return new AIError("INVALID_API_KEY", provider, msg);
+ return new AIError(
+ "INVALID_API_KEY",
+ provider,
+ `Invalid or unauthorized API key for ${provName}. Please check your credentials in the ${provName} console.`,
+ rawMsg,
+ );
}
+
if (
lower.includes("rate limit") ||
lower.includes("429") ||
- lower.includes("too many requests")
+ lower.includes("too many requests") ||
+ lower.includes("rate_limited") ||
+ lower.includes("quota")
) {
- return new AIError("RATE_LIMIT", provider, msg);
+ let friendly = `Rate limit exceeded for ${provName}. Please wait a moment before trying again.`;
+ if (provider.toLowerCase() === "mistral") {
+ friendly = `Mistral rate limit exceeded (code 1300). Free tier accounts are limited to 1 request/second and monthly limits. Please wait a moment or upgrade to pay-as-you-go at console.mistral.ai.`;
+ } else if (provider.toLowerCase() === "groq") {
+ friendly = `Groq rate limit exceeded. Please wait a moment or check your account limits at console.groq.com.`;
+ }
+ return new AIError("RATE_LIMIT", provider, friendly, rawMsg);
}
+
if (lower.includes("timeout") || lower.includes("timed out")) {
- return new AIError("TIMEOUT", provider, msg);
+ return new AIError(
+ "TIMEOUT",
+ provider,
+ `Request to ${provName} timed out. Please check your network connection and try again.`,
+ rawMsg,
+ );
}
+
if (
lower.includes("econnrefused") ||
lower.includes("enotfound") ||
lower.includes("fetch failed") ||
lower.includes("network")
) {
- return new AIError("NETWORK_FAILURE", provider, msg);
+ return new AIError(
+ "NETWORK_FAILURE",
+ provider,
+ `Unable to reach ${provName} servers. Please check your internet connection or proxy settings.`,
+ rawMsg,
+ );
}
- if (lower.includes("model") && (lower.includes("not found") || lower.includes("invalid"))) {
- return new AIError("INVALID_MODEL", provider, msg);
+
+ if (
+ lower.includes("model") &&
+ (lower.includes("not found") ||
+ lower.includes("invalid") ||
+ lower.includes("does not exist") ||
+ lower.includes("do not have access"))
+ ) {
+ let friendly = `The selected model is not available for ${provName} (${cleanMsg}). Please select a different model in AI settings.`;
+ if (provider.toLowerCase() === "groq") {
+ friendly = `The selected model is not accessible on your Groq tier or organization (${cleanMsg}). Please switch model in settings (e.g., GPT-OSS 20B or Qwen 3.8 27B) or check console.groq.com.`;
+ }
+ return new AIError("INVALID_MODEL", provider, friendly, rawMsg);
}
+
if (lower.includes("unavailable") || lower.includes("503") || lower.includes("overloaded")) {
- return new AIError("PROVIDER_UNAVAILABLE", provider, msg);
+ return new AIError(
+ "PROVIDER_UNAVAILABLE",
+ provider,
+ `${provName} service is temporarily unavailable or overloaded. Please try again shortly.`,
+ rawMsg,
+ );
}
- return new AIError("UNKNOWN", provider, msg);
+ return new AIError("UNKNOWN", provider, cleanMsg || rawMsg, rawMsg);
}
diff --git a/bridge/src/connectors/clickhouse.ts b/bridge/src/connectors/clickhouse.ts
new file mode 100644
index 00000000..3cf10f39
--- /dev/null
+++ b/bridge/src/connectors/clickhouse.ts
@@ -0,0 +1,1321 @@
+import { createClient, type ClickHouseClient } from "@clickhouse/client";
+import { ClickHouseConfig, ClickHouseSchemaMetadataBatch } from "../types/clickhouse";
+export type { ClickHouseConfig, ClickHouseSchemaMetadataBatch };
+import {
+ TableInfo,
+ PrimaryKeyInfo,
+ ForeignKeyInfo,
+ IndexInfo,
+ UniqueConstraintInfo,
+ CheckConstraintInfo,
+ ColumnDetail,
+ DBStats,
+ SchemaInfo,
+ AppliedMigration,
+} from "../types/common";
+import { CacheEntry } from "../types/cache";
+import { SchemaFile } from "../services/projectStore";
+import crypto from "crypto";
+import fs from "fs";
+
+import {
+ CLICKHOUSE_LIST_SCHEMAS,
+ CLICKHOUSE_LIST_TABLES,
+ CLICKHOUSE_LIST_TABLES_BY_SCHEMA,
+ CLICKHOUSE_LIST_COLUMNS,
+ CLICKHOUSE_BATCH_LIST_COLUMNS,
+} from "../queries/clickhouse/schema";
+import {
+ CLICKHOUSE_GET_PRIMARY_KEYS,
+ CLICKHOUSE_BATCH_GET_PRIMARY_KEYS,
+ CLICKHOUSE_GET_INDEXES,
+ CLICKHOUSE_BATCH_GET_INDEXES,
+} from "../queries/clickhouse/constraints";
+import { CLICKHOUSE_GET_DB_STATS, CLICKHOUSE_GET_DB_SIZE } from "../queries/clickhouse/stats";
+import { cleanClickHouseUrl } from "../utils/clickhouseUrl";
+import logger from "../services/logger";
+
+export function resolveClickHouseUrl(cfg: ClickHouseConfig): string {
+ if (cfg.url && cfg.url.trim()) {
+ return cleanClickHouseUrl(cfg.url);
+ }
+ if (cfg.host && (cfg.host.startsWith("http://") || cfg.host.startsWith("https://"))) {
+ return cleanClickHouseUrl(cfg.host);
+ }
+ const isSsl = cfg.ssl || cfg.port === 8443 || cfg.port === 443;
+ const protocol = isSsl ? "https" : "http";
+ const port = cfg.port || (isSsl ? 8443 : 8123);
+ return `${protocol}://${cfg.host || "localhost"}:${port}`;
+}
+
+// ============================================
+// CACHING SYSTEM FOR CLICKHOUSE CONNECTOR
+// ============================================
+
+const CACHE_TTL = 30_000;
+const STATS_CACHE_TTL = 10_000;
+const SCHEMA_CACHE_TTL = 60_000;
+
+class ClickHouseCacheManager {
+ private tableListCache = new Map>();
+ private primaryKeysCache = new Map>();
+ private dbStatsCache = new Map>();
+ private schemasCache = new Map>();
+ private tableDetailsCache = new Map>();
+
+ private getConfigKey(cfg: ClickHouseConfig): string {
+ const url = resolveClickHouseUrl(cfg);
+ return `${url}:${cfg.user || "default"}:${cfg.database || "default"}`;
+ }
+
+ private getTableKey(cfg: ClickHouseConfig, schema: string, table: string): string {
+ return `${this.getConfigKey(cfg)}:${schema}:${table}`;
+ }
+
+ private isValid(entry: CacheEntry | undefined): boolean {
+ if (!entry) return false;
+ return Date.now() - entry.timestamp < entry.ttl;
+ }
+
+ getTableList(cfg: ClickHouseConfig, schema?: string): TableInfo[] | null {
+ const key = `${this.getConfigKey(cfg)}:${schema || "all"}`;
+ const entry = this.tableListCache.get(key);
+ return this.isValid(entry) ? entry!.data : null;
+ }
+ setTableList(cfg: ClickHouseConfig, data: TableInfo[], schema?: string): void {
+ const key = `${this.getConfigKey(cfg)}:${schema || "all"}`;
+ this.tableListCache.set(key, { data, timestamp: Date.now(), ttl: CACHE_TTL });
+ }
+
+ getPrimaryKeys(cfg: ClickHouseConfig, schema: string, table: string): PrimaryKeyInfo[] | null {
+ const entry = this.primaryKeysCache.get(this.getTableKey(cfg, schema, table));
+ return this.isValid(entry) ? entry!.data : null;
+ }
+ setPrimaryKeys(
+ cfg: ClickHouseConfig,
+ schema: string,
+ table: string,
+ data: PrimaryKeyInfo[],
+ ): void {
+ this.primaryKeysCache.set(this.getTableKey(cfg, schema, table), {
+ data,
+ timestamp: Date.now(),
+ ttl: CACHE_TTL,
+ });
+ }
+
+ getDBStats(cfg: ClickHouseConfig): DBStats | null {
+ const entry = this.dbStatsCache.get(this.getConfigKey(cfg));
+ return this.isValid(entry) ? entry!.data : null;
+ }
+ setDBStats(cfg: ClickHouseConfig, data: DBStats): void {
+ this.dbStatsCache.set(this.getConfigKey(cfg), {
+ data,
+ timestamp: Date.now(),
+ ttl: STATS_CACHE_TTL,
+ });
+ }
+
+ getSchemas(cfg: ClickHouseConfig): SchemaInfo[] | null {
+ const entry = this.schemasCache.get(this.getConfigKey(cfg));
+ return this.isValid(entry) ? entry!.data : null;
+ }
+ setSchemas(cfg: ClickHouseConfig, data: SchemaInfo[]): void {
+ this.schemasCache.set(this.getConfigKey(cfg), {
+ data,
+ timestamp: Date.now(),
+ ttl: SCHEMA_CACHE_TTL,
+ });
+ }
+
+ getTableDetails(cfg: ClickHouseConfig, schema: string, table: string): ColumnDetail[] | null {
+ const entry = this.tableDetailsCache.get(this.getTableKey(cfg, schema, table));
+ return this.isValid(entry) ? entry!.data : null;
+ }
+ setTableDetails(
+ cfg: ClickHouseConfig,
+ schema: string,
+ table: string,
+ data: ColumnDetail[],
+ ): void {
+ this.tableDetailsCache.set(this.getTableKey(cfg, schema, table), {
+ data,
+ timestamp: Date.now(),
+ ttl: CACHE_TTL,
+ });
+ }
+
+ clearForConnection(cfg: ClickHouseConfig): void {
+ const configKey = this.getConfigKey(cfg);
+ for (const cache of [this.tableListCache, this.primaryKeysCache, this.tableDetailsCache]) {
+ for (const [key] of cache) {
+ if (key.startsWith(configKey)) cache.delete(key);
+ }
+ }
+ this.dbStatsCache.delete(configKey);
+ this.schemasCache.delete(configKey);
+ }
+
+ clearTableCache(cfg: ClickHouseConfig, schema: string, table: string): void {
+ const key = this.getTableKey(cfg, schema, table);
+ this.primaryKeysCache.delete(key);
+ this.tableDetailsCache.delete(key);
+ }
+
+ clearAll(): void {
+ this.tableListCache.clear();
+ this.primaryKeysCache.clear();
+ this.dbStatsCache.clear();
+ this.schemasCache.clear();
+ this.tableDetailsCache.clear();
+ }
+}
+
+export const clickhouseCache = new ClickHouseCacheManager();
+
+// ============================================
+// CLIENT CONNECTION POOL
+// ============================================
+
+const clientPool = new Map();
+
+function getClientKey(cfg: ClickHouseConfig): string {
+ const url = resolveClickHouseUrl(cfg);
+ return `${url}:${cfg.user || "default"}:${cfg.database || "default"}`;
+}
+
+export function getClient(cfg: ClickHouseConfig): ClickHouseClient {
+ const key = getClientKey(cfg);
+ let client = clientPool.get(key);
+ if (!client) {
+ const url = resolveClickHouseUrl(cfg);
+
+ client = createClient({
+ url,
+ username: cfg.user || "default",
+ password: cfg.password || "",
+ database: cfg.database || "default",
+ request_timeout: 30_000,
+ clickhouse_settings: {
+ wait_end_of_query: 1,
+ },
+ });
+
+ clientPool.set(key, client);
+ }
+ return client;
+}
+
+export async function closeClient(cfg: ClickHouseConfig): Promise {
+ const key = getClientKey(cfg);
+ const client = clientPool.get(key);
+ if (client) {
+ try {
+ await client.close();
+ } catch {
+ // ignore
+ }
+ clientPool.delete(key);
+ }
+}
+
+export async function closeAllClients(): Promise {
+ for (const [key, client] of clientPool.entries()) {
+ try {
+ await client.close();
+ } catch {
+ // ignore
+ }
+ clientPool.delete(key);
+ }
+}
+
+function formatValue(val: any): string {
+ if (val === null || val === undefined) return "NULL";
+ if (typeof val === "number" || typeof val === "boolean") return String(val);
+ if (val instanceof Date) return `'${val.toISOString().replace("T", " ").replace("Z", "")}'`;
+ return `'${String(val).replace(/'/g, "\\'")}'`;
+}
+
+// ============================================
+// CORE CONNECTOR OPERATIONS
+// ============================================
+
+export async function testConnection(
+ cfg: ClickHouseConfig,
+): Promise<{ ok: boolean; message?: string; status: "connected" | "disconnected" }> {
+ const url = resolveClickHouseUrl(cfg);
+
+ const tempClient = createClient({
+ url,
+ username: cfg.user || "default",
+ password: cfg.password || "",
+ database: cfg.database || "default",
+ request_timeout: 30_000,
+ });
+
+ try {
+ const pingResult = await tempClient.ping();
+ if (!pingResult.success) {
+ const errMsg = (pingResult as { error?: Error }).error?.message || "Ping failed";
+ return {
+ ok: false,
+ status: "disconnected",
+ message: errMsg,
+ };
+ }
+
+ // Ping verifies server is reachable; now verify credentials & permissions
+ await tempClient.query({
+ query: "SELECT 1",
+ format: "JSONEachRow",
+ });
+
+ return { ok: true, status: "connected", message: "Connection successful" };
+ } catch (err: any) {
+ return {
+ ok: false,
+ status: "disconnected",
+ message: (err as Error).message || "Connection failed",
+ };
+ } finally {
+ try {
+ await tempClient.close();
+ } catch {}
+ }
+}
+
+export async function listTables(cfg: ClickHouseConfig, schemaName?: string): Promise {
+ const cached = clickhouseCache.getTableList(cfg, schemaName);
+ if (cached) return cached;
+
+ const client = getClient(cfg);
+ const query = schemaName ? CLICKHOUSE_LIST_TABLES_BY_SCHEMA : CLICKHOUSE_LIST_TABLES;
+ const query_params = schemaName ? { target_schema: schemaName, schema: schemaName } : undefined;
+
+ const res = await client.query({
+ query,
+ query_params,
+ format: "JSONEachRow",
+ });
+ const data = (await res.json()) as any[];
+
+ const tables: TableInfo[] = data.map((row) => ({
+ schema: row.schema,
+ name: row.name,
+ type: row.type || "BASE TABLE",
+ }));
+
+ clickhouseCache.setTableList(cfg, tables, schemaName);
+ return tables;
+}
+
+export async function listPrimaryKeys(
+ cfg: ClickHouseConfig,
+ schemaNameOrTable: string,
+ tableNameOrSchema?: string,
+): Promise {
+ let schema = cfg.database || "default";
+ let table = schemaNameOrTable;
+
+ if (tableNameOrSchema) {
+ schema = schemaNameOrTable || cfg.database || "default";
+ table = tableNameOrSchema;
+ }
+
+ const cached = clickhouseCache.getPrimaryKeys(cfg, schema, table);
+ if (cached) return cached;
+
+ const client = getClient(cfg);
+ const res = await client.query({
+ query: CLICKHOUSE_GET_PRIMARY_KEYS,
+ query_params: {
+ target_schema: schema,
+ target_table: table,
+ schema,
+ table,
+ },
+ format: "JSONEachRow",
+ });
+ const data = (await res.json()) as any[];
+
+ let pks: PrimaryKeyInfo[] = data.map((r) => ({
+ column_name: r.column_name,
+ }));
+
+ // If no primary key or sorting key is defined, fall back to the first column
+ if (pks.length === 0) {
+ try {
+ const cols = await getTableDetails(cfg, schema, table);
+ if (cols.length > 0) {
+ pks = [{ column_name: cols[0].name }];
+ }
+ } catch {}
+ }
+
+ clickhouseCache.setPrimaryKeys(cfg, schema, table, pks);
+ return pks;
+}
+
+export async function listForeignKeys(
+ _cfg: ClickHouseConfig,
+ _schemaNameOrTable: string,
+ _tableNameOrSchema?: string,
+): Promise {
+ // ClickHouse is an analytical OLAP database and does not enforce foreign keys
+ return [];
+}
+
+export async function listIndexes(
+ cfg: ClickHouseConfig,
+ schemaNameOrTable: string,
+ tableNameOrSchema?: string,
+): Promise {
+ let schema = cfg.database || "default";
+ let table = schemaNameOrTable;
+
+ if (tableNameOrSchema) {
+ schema = schemaNameOrTable || cfg.database || "default";
+ table = tableNameOrSchema;
+ }
+
+ const client = getClient(cfg);
+
+ try {
+ const res = await client.query({
+ query: CLICKHOUSE_GET_INDEXES,
+ query_params: {
+ target_schema: schema,
+ target_table: table,
+ schema,
+ table,
+ },
+ format: "JSONEachRow",
+ });
+ const data = (await res.json()) as any[];
+
+ return data.map((r) => ({
+ table_name: table,
+ index_name: r.index_name,
+ column_name: r.column_name,
+ is_unique: false,
+ is_primary: false,
+ index_type: r.index_type,
+ }));
+ } catch {
+ return [];
+ }
+}
+
+export async function listUniqueConstraints(
+ _cfg: ClickHouseConfig,
+ _schemaNameOrTable: string,
+ _tableNameOrSchema?: string,
+): Promise {
+ return [];
+}
+
+export async function listCheckConstraints(
+ _cfg: ClickHouseConfig,
+ _schemaNameOrTable: string,
+ _tableNameOrSchema?: string,
+): Promise {
+ return [];
+}
+
+export async function getDBStats(cfg: ClickHouseConfig): Promise {
+ const cached = clickhouseCache.getDBStats(cfg);
+ if (cached) return cached;
+
+ const client = getClient(cfg);
+ const database = cfg.database || "default";
+
+ let total_tables = 0;
+ let total_rows = 0;
+ let total_db_size_mb = 0;
+
+ try {
+ const statsRes = await client.query({
+ query: CLICKHOUSE_GET_DB_STATS,
+ query_params: { database },
+ format: "JSONEachRow",
+ });
+ const statsData = (await statsRes.json()) as any[];
+ if (statsData.length > 0) {
+ total_tables = Number(statsData[0].total_tables) || 0;
+ total_rows = Number(statsData[0].total_rows) || 0;
+ }
+ } catch {}
+
+ try {
+ const sizeRes = await client.query({
+ query: CLICKHOUSE_GET_DB_SIZE,
+ query_params: { database },
+ format: "JSONEachRow",
+ });
+ const sizeData = (await sizeRes.json()) as any[];
+ if (sizeData.length > 0) {
+ total_db_size_mb = Number(sizeData[0].total_db_size_mb) || 0;
+ }
+ } catch {}
+
+ const stats: DBStats = {
+ total_tables,
+ total_rows,
+ total_db_size_mb,
+ };
+
+ clickhouseCache.setDBStats(cfg, stats);
+ return stats;
+}
+
+export async function listSchemas(cfg: ClickHouseConfig): Promise {
+ const cached = clickhouseCache.getSchemas(cfg);
+ if (cached) return cached;
+
+ const client = getClient(cfg);
+ const res = await client.query({
+ query: CLICKHOUSE_LIST_SCHEMAS,
+ format: "JSONEachRow",
+ });
+ const data = (await res.json()) as any[];
+
+ const schemas: SchemaInfo[] = data.map((r) => ({ name: r.name }));
+ clickhouseCache.setSchemas(cfg, schemas);
+ return schemas;
+}
+
+export async function listSchemaNames(cfg: ClickHouseConfig): Promise {
+ const schemas = await listSchemas(cfg);
+ return schemas.map((s) => s.name);
+}
+
+export async function getTableDetails(
+ cfg: ClickHouseConfig,
+ schemaNameOrTable: string,
+ tableNameOrSchema?: string,
+): Promise {
+ let schema = cfg.database || "default";
+ let table = schemaNameOrTable;
+
+ if (tableNameOrSchema) {
+ schema = schemaNameOrTable || cfg.database || "default";
+ table = tableNameOrSchema;
+ }
+
+ const cached = clickhouseCache.getTableDetails(cfg, schema, table);
+ if (cached) return cached;
+
+ const client = getClient(cfg);
+ const res = await client.query({
+ query: CLICKHOUSE_LIST_COLUMNS,
+ query_params: {
+ target_schema: schema,
+ target_table: table,
+ schema,
+ table,
+ },
+ format: "JSONEachRow",
+ });
+ const data = (await res.json()) as any[];
+
+ const columns: ColumnDetail[] = data.map((r) => ({
+ name: r.name,
+ type: r.type,
+ not_nullable: Boolean(r.not_nullable),
+ default_value: r.default_value || null,
+ is_primary_key: Boolean(r.is_in_primary_key || r.is_in_sorting_key),
+ is_foreign_key: false,
+ comment: r.comment || null,
+ }));
+
+ clickhouseCache.setTableDetails(cfg, schema, table, columns);
+ return columns;
+}
+
+export async function getSchemaMetadataBatch(
+ cfg: ClickHouseConfig,
+ schemaNameOrTables?: string | string[],
+ maybeSchemaName?: string,
+): Promise {
+ let schema = cfg.database || "default";
+ let filterTables: Set | null = null;
+
+ if (Array.isArray(schemaNameOrTables)) {
+ filterTables = new Set(schemaNameOrTables);
+ if (maybeSchemaName) schema = maybeSchemaName;
+ } else if (typeof schemaNameOrTables === "string") {
+ schema = schemaNameOrTables;
+ }
+
+ const client = getClient(cfg);
+
+ const tablesMap = new Map<
+ string,
+ {
+ columns: ColumnDetail[];
+ primaryKeys: PrimaryKeyInfo[];
+ foreignKeys: ForeignKeyInfo[];
+ indexes: IndexInfo[];
+ uniqueConstraints: UniqueConstraintInfo[];
+ checkConstraints: CheckConstraintInfo[];
+ }
+ >();
+
+ function getOrCreateTable(tableName: string) {
+ let entry = tablesMap.get(tableName);
+ if (!entry) {
+ entry = {
+ columns: [],
+ primaryKeys: [],
+ foreignKeys: [],
+ indexes: [],
+ uniqueConstraints: [],
+ checkConstraints: [],
+ };
+ tablesMap.set(tableName, entry);
+ }
+ return entry;
+ }
+
+ // Pre-seed tables if filterTables was explicitly provided
+ if (filterTables) {
+ for (const tbl of filterTables) {
+ getOrCreateTable(tbl);
+ }
+ }
+
+ // 1. Discover tables in target schema
+ try {
+ const tables = await listTables(cfg, schema);
+ for (const t of tables) {
+ if (!filterTables || filterTables.has(t.name)) {
+ getOrCreateTable(t.name);
+ }
+ }
+ } catch (err: any) {
+ logger.warn(
+ { err: err?.message, schema },
+ "ClickHouse getSchemaMetadataBatch listTables failed",
+ );
+ }
+
+ // Fallback table discovery if empty and no specific table filter was provided
+ if (tablesMap.size === 0 && !filterTables) {
+ try {
+ const allTables = await listTables(cfg);
+ for (const t of allTables) {
+ if (!schema || t.schema === schema) {
+ getOrCreateTable(t.name);
+ }
+ }
+ } catch (err: any) {
+ logger.warn(
+ { err: err?.message, schema },
+ "ClickHouse getSchemaMetadataBatch allTables fallback failed",
+ );
+ }
+ }
+
+ // 2. Batch fetch columns
+ try {
+ const colRes = await client.query({
+ query: CLICKHOUSE_BATCH_LIST_COLUMNS,
+ query_params: { target_schema: schema, schema },
+ format: "JSONEachRow",
+ });
+ const colData = (await colRes.json()) as any[];
+
+ for (const r of colData) {
+ const tbl = r.table || r["`table`"] || r.table_name;
+ if (!tbl || (filterTables && !filterTables.has(tbl))) continue;
+ const entry = getOrCreateTable(tbl);
+ entry.columns.push({
+ name: r.name,
+ type: r.type,
+ not_nullable: Boolean(r.not_nullable),
+ default_value: r.default_value || null,
+ is_primary_key: Boolean(r.is_in_primary_key || r.is_in_sorting_key),
+ is_foreign_key: false,
+ comment: r.comment || null,
+ });
+ }
+ } catch (err: any) {
+ logger.warn({ err: err?.message, schema }, "ClickHouse CLICKHOUSE_BATCH_LIST_COLUMNS failed");
+ }
+
+ // 3. Batch fetch primary keys
+ try {
+ const pkRes = await client.query({
+ query: CLICKHOUSE_BATCH_GET_PRIMARY_KEYS,
+ query_params: { target_schema: schema, schema },
+ format: "JSONEachRow",
+ });
+ const pkData = (await pkRes.json()) as any[];
+
+ for (const r of pkData) {
+ const tbl = r.table || r["`table`"] || r.table_name;
+ if (!tbl || (filterTables && !filterTables.has(tbl))) continue;
+ const entry = getOrCreateTable(tbl);
+ entry.primaryKeys.push({ column_name: r.column_name });
+ }
+ } catch (err: any) {
+ logger.warn(
+ { err: err?.message, schema },
+ "ClickHouse CLICKHOUSE_BATCH_GET_PRIMARY_KEYS failed",
+ );
+ }
+
+ // 4. Batch fetch secondary indexes
+ try {
+ const idxRes = await client.query({
+ query: CLICKHOUSE_BATCH_GET_INDEXES,
+ query_params: { target_schema: schema, schema },
+ format: "JSONEachRow",
+ });
+ const idxData = (await idxRes.json()) as any[];
+
+ for (const r of idxData) {
+ const tbl = r.table || r["`table`"] || r.table_name;
+ if (!tbl || (filterTables && !filterTables.has(tbl))) continue;
+ const entry = getOrCreateTable(tbl);
+ entry.indexes.push({
+ table_name: tbl,
+ index_name: r.index_name,
+ column_name: r.column_name,
+ is_unique: false,
+ is_primary: false,
+ index_type: r.index_type,
+ });
+ }
+ } catch (err: any) {
+ logger.debug(
+ { err: err?.message, schema },
+ "ClickHouse CLICKHOUSE_BATCH_GET_INDEXES skipped/failed",
+ );
+ }
+
+ // 5. Direct fallback: If tablesMap is still empty after batch queries, query system.tables directly
+ if (tablesMap.size === 0) {
+ try {
+ const directTablesRes = await client.query({
+ query: `SELECT name FROM system.tables WHERE database = {target_schema: String} ORDER BY name`,
+ query_params: { target_schema: schema, schema },
+ format: "JSONEachRow",
+ });
+ const directRows = (await directTablesRes.json()) as any[];
+ for (const row of directRows) {
+ if (!filterTables || filterTables.has(row.name)) {
+ getOrCreateTable(row.name);
+ }
+ }
+ } catch (err: any) {
+ logger.warn({ err: err?.message, schema }, "ClickHouse direct system.tables query failed");
+ }
+ }
+
+ // 6. Guaranteed population: Ensure every table in tablesMap has columns and primary keys populated
+ for (const [tableName, entry] of tablesMap.entries()) {
+ if (entry.columns.length === 0) {
+ try {
+ const fallbackCols = await getTableDetails(cfg, schema, tableName);
+ if (fallbackCols && fallbackCols.length > 0) {
+ entry.columns = fallbackCols;
+ }
+ } catch (err: any) {
+ logger.warn({ err: err?.message, schema, tableName }, "Fallback getTableDetails failed");
+ }
+ }
+ if (entry.primaryKeys.length === 0) {
+ try {
+ const fallbackPks = await listPrimaryKeys(cfg, schema, tableName);
+ if (fallbackPks && fallbackPks.length > 0) {
+ entry.primaryKeys = fallbackPks;
+ }
+ } catch (err: any) {
+ logger.debug({ err: err?.message, schema, tableName }, "Fallback listPrimaryKeys failed");
+ }
+ }
+ }
+
+ return { tables: tablesMap };
+}
+
+export function streamQueryCancelable(
+ cfg: ClickHouseConfig,
+ sql: string,
+ batchSize: number = 1000,
+ onBatch: (rows: any[], columns: { name: string }[]) => Promise | void,
+ onDone?: () => void,
+): { promise: Promise; cancel: () => Promise } {
+ let cancelled = false;
+ const abortController = new AbortController();
+
+ const promise = (async () => {
+ const client = getClient(cfg);
+ const resultSet = await client.query({
+ query: sql,
+ format: "JSONEachRow",
+ abort_signal: abortController.signal,
+ });
+
+ if (cancelled) return;
+
+ let columns: { name: string }[] | null = null;
+ const buffer: any[] = [];
+
+ const flush = async () => {
+ if (buffer.length === 0) return;
+ const batch = buffer.splice(0, buffer.length);
+ await onBatch(batch, columns || []);
+ };
+
+ const stream = resultSet.stream();
+ for await (const rows of stream) {
+ if (cancelled) break;
+ for (const row of rows as any[]) {
+ if (cancelled) break;
+ const jsonObj = typeof row.json === "function" ? row.json() : row;
+ if (!columns && jsonObj && typeof jsonObj === "object") {
+ columns = Object.keys(jsonObj).map((k) => ({ name: k }));
+ }
+ buffer.push(jsonObj);
+ if (buffer.length >= batchSize) {
+ await flush();
+ }
+ }
+ }
+
+ if (!cancelled) {
+ await flush();
+ if (onDone) onDone();
+ }
+ })();
+
+ async function cancel() {
+ cancelled = true;
+ abortController.abort();
+ }
+
+ return { promise, cancel };
+}
+
+export async function fetchTableData(
+ cfg: ClickHouseConfig,
+ schemaName: string,
+ tableName: string,
+ limit: number,
+ page: number,
+ sortBy?: string,
+ sortOrder: "ASC" | "DESC" = "ASC",
+): Promise<{ rows: any[]; total: number }> {
+ const client = getClient(cfg);
+ const offset = Math.max(0, (page - 1) * limit);
+
+ // Total rows
+ const countRes = await client.query({
+ query: `SELECT count() AS total FROM \`${schemaName}\`.\`${tableName}\``,
+ format: "JSONEachRow",
+ });
+ const countData = (await countRes.json()) as any[];
+ const total = countData.length > 0 ? Number(countData[0].total) : 0;
+
+ // Data rows
+ const orderClause = sortBy ? ` ORDER BY \`${sortBy}\` ${sortOrder}` : "";
+ const dataRes = await client.query({
+ query: `SELECT * FROM \`${schemaName}\`.\`${tableName}\`${orderClause} LIMIT ${limit} OFFSET ${offset}`,
+ format: "JSONEachRow",
+ });
+ const rows = (await dataRes.json()) as any[];
+
+ return { rows, total };
+}
+
+export async function createTable(
+ cfg: ClickHouseConfig,
+ schemaName: string,
+ tableName: string,
+ columns: any[],
+ primaryKey?: string | string[],
+ ifNotExists: boolean = true,
+): Promise {
+ const client = getClient(cfg);
+ const colDefs = columns
+ .map((col) => {
+ let def = `\`${col.name}\` ${col.type}`;
+ if (col.default_value) def += ` DEFAULT ${col.default_value}`;
+ return def;
+ })
+ .join(",\n ");
+
+ const pkCols = Array.isArray(primaryKey) ? primaryKey : primaryKey ? [primaryKey] : [];
+ const orderClause = pkCols.length > 0 ? pkCols.map((c) => `\`${c}\``).join(", ") : "tuple()";
+ const ifNotExistsClause = ifNotExists ? "IF NOT EXISTS " : "";
+
+ const sql = `CREATE TABLE ${ifNotExistsClause}\`${schemaName}\`.\`${tableName}\` (
+ ${colDefs}
+) ENGINE = MergeTree() ORDER BY (${orderClause});`;
+
+ await client.command({ query: sql });
+ clickhouseCache.clearForConnection(cfg);
+}
+
+export async function createIndexes(
+ cfg: ClickHouseConfig,
+ schemaName: string,
+ tableName: string,
+ indexes: any[],
+): Promise {
+ const client = getClient(cfg);
+ for (const idx of indexes) {
+ const cols = Array.isArray(idx.columns) ? idx.columns.join(", ") : idx.column;
+ const type = idx.type || "minmax";
+ const granularity = idx.granularity || 1;
+ await client.command({
+ query: `ALTER TABLE \`${schemaName}\`.\`${tableName}\` ADD INDEX \`${idx.name}\` (${cols}) TYPE ${type} GRANULARITY ${granularity}`,
+ });
+ }
+ clickhouseCache.clearTableCache(cfg, schemaName, tableName);
+}
+
+export async function alterTable(
+ cfg: ClickHouseConfig,
+ schemaName: string,
+ tableName: string,
+ alterations: any[],
+): Promise {
+ const client = getClient(cfg);
+ for (const alt of alterations) {
+ let clause = "";
+ if (alt.type === "ADD_COLUMN") {
+ clause = `ADD COLUMN \`${alt.columnName}\` ${alt.columnDef}`;
+ } else if (alt.type === "DROP_COLUMN") {
+ clause = `DROP COLUMN \`${alt.columnName}\``;
+ } else if (alt.type === "RENAME_COLUMN") {
+ clause = `RENAME COLUMN \`${alt.oldName}\` TO \`${alt.newName}\``;
+ } else if (alt.type === "ALTER_COLUMN" || alt.type === "MODIFY_COLUMN") {
+ clause = `MODIFY COLUMN \`${alt.columnName}\` ${alt.newDef}`;
+ } else if (alt.type === "RENAME_TABLE") {
+ await client.command({
+ query: `RENAME TABLE \`${schemaName}\`.\`${tableName}\` TO \`${schemaName}\`.\`${alt.newName}\``,
+ });
+ continue;
+ }
+ if (clause) {
+ await client.command({
+ query: `ALTER TABLE \`${schemaName}\`.\`${tableName}\` ${clause}`,
+ });
+ }
+ }
+ clickhouseCache.clearForConnection(cfg);
+}
+
+export async function dropTable(
+ cfg: ClickHouseConfig,
+ schemaName: string,
+ tableName: string,
+ ifExists: boolean = true,
+): Promise {
+ const client = getClient(cfg);
+ const ifExistsClause = ifExists ? "IF EXISTS " : "";
+ await client.command({
+ query: `DROP TABLE ${ifExistsClause}\`${schemaName}\`.\`${tableName}\``,
+ });
+ clickhouseCache.clearForConnection(cfg);
+}
+
+export async function ensureMigrationTable(
+ cfg: ClickHouseConfig,
+ schemaName?: string,
+): Promise {
+ const schema = schemaName || cfg.database || "default";
+ const client = getClient(cfg);
+ await client.command({
+ query: `CREATE DATABASE IF NOT EXISTS \`${schema}\``,
+ });
+ await client.command({
+ query: `CREATE TABLE IF NOT EXISTS \`${schema}\`.schema_migrations (
+ version String,
+ name String,
+ checksum String,
+ applied_at DateTime DEFAULT now()
+ ) ENGINE = MergeTree() ORDER BY version;`,
+ });
+}
+
+export async function hasAnyMigrations(
+ cfg: ClickHouseConfig,
+ schemaName?: string,
+): Promise {
+ const schema = schemaName || cfg.database || "default";
+ const client = getClient(cfg);
+ try {
+ const res = await client.query({
+ query: `SELECT count() AS cnt FROM \`${schema}\`.schema_migrations`,
+ format: "JSONEachRow",
+ });
+ const data = (await res.json()) as any[];
+ return data.length > 0 && Number(data[0].cnt) > 0;
+ } catch (err: any) {
+ if (String(err).includes("doesn't exist") || String(err).includes("UNKNOWN_TABLE")) {
+ return false;
+ }
+ throw err;
+ }
+}
+
+export async function insertBaseline(
+ cfg: ClickHouseConfig,
+ version: string,
+ name: string,
+ checksum: string,
+ schemaName?: string,
+): Promise {
+ await ensureMigrationTable(cfg, schemaName);
+ const schema = schemaName || cfg.database || "default";
+ const client = getClient(cfg);
+ await client.insert({
+ table: `\`${schema}\`.schema_migrations`,
+ values: [{ version, name, checksum }],
+ format: "JSONEachRow",
+ });
+}
+
+export async function baselineIfNeeded(
+ cfg: ClickHouseConfig,
+ migrationsDir: string,
+ snapshot?: SchemaFile,
+ schemaName?: string,
+): Promise<{ baselined: boolean; version?: string }> {
+ await ensureMigrationTable(cfg, schemaName);
+ const hasMigrations = await hasAnyMigrations(cfg, schemaName);
+ if (hasMigrations) return { baselined: false };
+
+ const version = Date.now().toString();
+ const name = "baseline_existing_schema";
+
+ const fakeSnapshot = snapshot || {
+ version: 2,
+ projectId: "",
+ databaseId: "",
+ dialect: "clickhouse",
+ schemas: [],
+ cachedAt: "",
+ relwaveVersion: "",
+ schemaHash: "",
+ };
+
+ const { writeBaselineMigration } = await import("../utils/baselineMigration");
+ const filePath = writeBaselineMigration(migrationsDir, version, name, fakeSnapshot as any);
+ const checksum = crypto.createHash("sha256").update(fs.readFileSync(filePath)).digest("hex");
+
+ await insertBaseline(cfg, version, name, checksum, schemaName);
+ return { baselined: true, version };
+}
+
+export async function listAppliedMigrations(
+ cfg: ClickHouseConfig,
+ schemaName?: string,
+): Promise {
+ const schema = schemaName || cfg.database || "default";
+ await ensureMigrationTable(cfg, schema);
+ const client = getClient(cfg);
+
+ try {
+ const res = await client.query({
+ query: `SELECT version, name, checksum, toString(applied_at) AS applied_at FROM \`${schema}\`.schema_migrations ORDER BY version ASC`,
+ format: "JSONEachRow",
+ });
+ const rows = (await res.json()) as any[];
+ return rows.map((r) => ({
+ version: r.version,
+ name: r.name,
+ checksum: r.checksum || "",
+ applied_at: new Date(r.applied_at),
+ }));
+ } catch {
+ return [];
+ }
+}
+
+export function splitClickHouseStatements(sql: string): string[] {
+ const cleanSql = sql
+ .split("\n")
+ .map((line) => {
+ const trimmed = line.trim();
+ if (trimmed.startsWith("--")) return "";
+ return line;
+ })
+ .join("\n");
+
+ return cleanSql
+ .split(";")
+ .map((s) => s.trim())
+ .map((s) => {
+ if (/^\s*DROP\s+TABLE\b/i.test(s)) {
+ return s.replace(/\s+(?:CASCADE|RESTRICT)\s*$/i, "").trim();
+ }
+ return s;
+ })
+ .filter((s) => s.length > 0);
+}
+
+export async function connectToDatabase(
+ cfg: ClickHouseConfig,
+ connectionId: string,
+ options?: { readOnly?: boolean },
+) {
+ let baselineResult = { baselined: false };
+ const { projectStoreInstance } = await import("../services/projectStore");
+ const { loadLocalMigrations } = await import("../utils/baselineMigration");
+ const migrationsDir = await projectStoreInstance.resolveMigrationsDir(connectionId);
+ if (!fs.existsSync(migrationsDir)) {
+ fs.mkdirSync(migrationsDir, { recursive: true });
+ }
+
+ if (!options?.readOnly) {
+ let snapshot: any = undefined;
+ try {
+ const project = await projectStoreInstance.getProjectByDatabaseId(connectionId);
+ if (project) {
+ snapshot = (await projectStoreInstance.getSchema(project.id)) || undefined;
+ }
+ } catch {}
+ baselineResult = await baselineIfNeeded(cfg, migrationsDir, snapshot);
+ }
+
+ const schema = await listSchemas(cfg);
+ const localMigrations = await loadLocalMigrations(migrationsDir);
+ const appliedMigrations = await listAppliedMigrations(cfg);
+
+ return {
+ baselined: baselineResult.baselined,
+ schema,
+ migrations: {
+ local: localMigrations,
+ applied: appliedMigrations,
+ },
+ };
+}
+
+export async function applyMigration(
+ cfg: ClickHouseConfig,
+ migrationFilePath: string,
+ schemaName?: string,
+): Promise {
+ const schema = schemaName || cfg.database || "default";
+ await ensureMigrationTable(cfg, schema);
+ const { readMigrationFile } = await import("../utils/migrationFileReader");
+ const migration = readMigrationFile(migrationFilePath);
+
+ const client = getClient(cfg);
+ const stmts = splitClickHouseStatements(migration.upSQL);
+
+ for (const stmt of stmts) {
+ await client.command({ query: stmt });
+ }
+
+ await client.insert({
+ table: `\`${schema}\`.schema_migrations`,
+ values: [{ version: migration.version, name: migration.name, checksum: migration.checksum }],
+ format: "JSONEachRow",
+ });
+ clickhouseCache.clearForConnection(cfg);
+}
+
+export async function rollbackMigration(
+ cfg: ClickHouseConfig,
+ version: string,
+ migrationFilePath: string,
+ schemaName?: string,
+): Promise {
+ const schema = schemaName || cfg.database || "default";
+ await ensureMigrationTable(cfg, schema);
+ const { readMigrationFile } = await import("../utils/migrationFileReader");
+ const migration = readMigrationFile(migrationFilePath);
+
+ const client = getClient(cfg);
+ const stmts = splitClickHouseStatements(migration.downSQL);
+
+ if (stmts.length === 0) {
+ throw new Error(
+ `Cannot rollback migration "${migration.name}": no executable SQL found in DOWN section.`,
+ );
+ }
+
+ for (const stmt of stmts) {
+ await client.command({ query: stmt });
+ }
+
+ await client.command({
+ query: `ALTER TABLE \`${schema}\`.schema_migrations DELETE WHERE version = '${version.replace(/'/g, "\\'")}'`,
+ clickhouse_settings: {
+ mutations_sync: "1",
+ },
+ });
+ clickhouseCache.clearForConnection(cfg);
+}
+
+export async function insertRow(
+ cfg: ClickHouseConfig,
+ schemaName: string,
+ tableName: string,
+ row: Record,
+): Promise {
+ const client = getClient(cfg);
+ await client.insert({
+ table: `\`${schemaName}\`.\`${tableName}\``,
+ values: [row],
+ format: "JSONEachRow",
+ });
+ clickhouseCache.clearTableCache(cfg, schemaName, tableName);
+ return row;
+}
+
+export async function updateRow(
+ cfg: ClickHouseConfig,
+ schemaName: string,
+ tableName: string,
+ primaryKeyColumnOrObj: string | { column: string; value: any } | Record,
+ primaryKeyValueOrRow?: any,
+ rowData?: Record,
+): Promise {
+ const client = getClient(cfg);
+ let updates: Record = {};
+ let whereClause = "";
+
+ if (rowData !== undefined && typeof primaryKeyColumnOrObj === "string") {
+ // Called as: (cfg, schemaName, tableName, primaryKeyColumn, primaryKeyValue, rowData)
+ updates = rowData;
+ whereClause = `\`${primaryKeyColumnOrObj}\` = ${formatValue(primaryKeyValueOrRow)}`;
+ } else if (typeof primaryKeyColumnOrObj === "object" && primaryKeyColumnOrObj !== null) {
+ if ("column" in primaryKeyColumnOrObj && "value" in primaryKeyColumnOrObj) {
+ whereClause = `\`${primaryKeyColumnOrObj.column}\` = ${formatValue(primaryKeyColumnOrObj.value)}`;
+ } else {
+ whereClause = Object.entries(primaryKeyColumnOrObj)
+ .map(([col, val]) => `\`${col}\` = ${formatValue(val)}`)
+ .join(" AND ");
+ }
+ updates = primaryKeyValueOrRow || {};
+ } else if (
+ typeof primaryKeyColumnOrObj === "string" &&
+ primaryKeyValueOrRow &&
+ typeof primaryKeyValueOrRow === "object"
+ ) {
+ updates = primaryKeyValueOrRow;
+ whereClause = `\`${primaryKeyColumnOrObj}\` = ${formatValue(rowData)}`;
+ }
+
+ const updateEntries = Object.entries(updates);
+ if (updateEntries.length === 0) {
+ throw new Error("No data provided for update");
+ }
+ if (!whereClause) {
+ throw new Error("Primary key condition required for update");
+ }
+
+ const sets = updateEntries.map(([col, val]) => `\`${col}\` = ${formatValue(val)}`).join(", ");
+
+ await client.command({
+ query: `ALTER TABLE \`${schemaName}\`.\`${tableName}\` UPDATE ${sets} WHERE ${whereClause}`,
+ clickhouse_settings: {
+ mutations_sync: "1",
+ },
+ });
+ clickhouseCache.clearTableCache(cfg, schemaName, tableName);
+
+ try {
+ const fetchRes = await client.query({
+ query: `SELECT * FROM \`${schemaName}\`.\`${tableName}\` WHERE ${whereClause} LIMIT 1`,
+ format: "JSONEachRow",
+ });
+ const fetched = (await fetchRes.json()) as any[];
+ return fetched[0] || updates;
+ } catch {
+ return updates;
+ }
+}
+
+export async function deleteRow(
+ cfg: ClickHouseConfig,
+ schemaName: string,
+ tableName: string,
+ primaryKeyColumnOrObj: string | { column: string; value: any } | Record,
+ primaryKeyValue?: any,
+): Promise {
+ const client = getClient(cfg);
+ let whereClause = "";
+
+ if (typeof primaryKeyColumnOrObj === "string") {
+ whereClause = `\`${primaryKeyColumnOrObj}\` = ${formatValue(primaryKeyValue)}`;
+ } else if (typeof primaryKeyColumnOrObj === "object" && primaryKeyColumnOrObj !== null) {
+ if ("column" in primaryKeyColumnOrObj && "value" in primaryKeyColumnOrObj) {
+ whereClause = `\`${primaryKeyColumnOrObj.column}\` = ${formatValue(primaryKeyColumnOrObj.value)}`;
+ } else {
+ whereClause = Object.entries(primaryKeyColumnOrObj)
+ .map(([col, val]) => `\`${col}\` = ${formatValue(val)}`)
+ .join(" AND ");
+ }
+ }
+
+ if (!whereClause) {
+ throw new Error("Primary key condition required for delete");
+ }
+
+ await client.command({
+ query: `ALTER TABLE \`${schemaName}\`.\`${tableName}\` DELETE WHERE ${whereClause}`,
+ clickhouse_settings: {
+ mutations_sync: "1",
+ },
+ });
+ clickhouseCache.clearTableCache(cfg, schemaName, tableName);
+ return true;
+}
+
+export async function searchTable(
+ cfg: ClickHouseConfig,
+ schemaName: string,
+ tableName: string,
+ searchTerm: string,
+ column?: string,
+ page: number = 1,
+ pageSize: number = 50,
+): Promise<{ rows: any[]; total: number }> {
+ const client = getClient(cfg);
+ const offset = Math.max(0, (page - 1) * pageSize);
+ const escapedTerm = searchTerm.replace(/'/g, "\\'");
+
+ let whereConditions = "";
+ if (column && column.trim()) {
+ whereConditions = `\`${column.trim()}\` ILIKE '%${escapedTerm}%'`;
+ } else {
+ const cols = await getTableDetails(cfg, schemaName, tableName);
+ const textCols = cols.filter(
+ (c) =>
+ c.type.toLowerCase().includes("string") ||
+ c.type.toLowerCase().includes("char") ||
+ c.type.toLowerCase().includes("text"),
+ );
+
+ if (textCols.length === 0) {
+ return fetchTableData(cfg, schemaName, tableName, pageSize, page);
+ }
+
+ whereConditions = textCols.map((c) => `\`${c.name}\` ILIKE '%${escapedTerm}%'`).join(" OR ");
+ }
+
+ const countRes = await client.query({
+ query: `SELECT count() AS total FROM \`${schemaName}\`.\`${tableName}\` WHERE ${whereConditions}`,
+ format: "JSONEachRow",
+ });
+ const countData = (await countRes.json()) as any[];
+ const total = countData.length > 0 ? Number(countData[0].total) : 0;
+
+ const dataRes = await client.query({
+ query: `SELECT * FROM \`${schemaName}\`.\`${tableName}\` WHERE ${whereConditions} LIMIT ${pageSize} OFFSET ${offset}`,
+ format: "JSONEachRow",
+ });
+ const rows = (await dataRes.json()) as any[];
+
+ return { rows, total };
+}
diff --git a/bridge/src/handlers/aiHandlers.ts b/bridge/src/handlers/aiHandlers.ts
index 6155b3ba..65acd12b 100644
--- a/bridge/src/handlers/aiHandlers.ts
+++ b/bridge/src/handlers/aiHandlers.ts
@@ -40,13 +40,26 @@ export class AIHandlers {
async handleTestConnection(params: AITestConnectionParams, id: number | string) {
try {
const provider = this.aiService.resolveProvider(params.settings);
- await provider.testConnection();
- this.rpc.sendResponse(id, { ok: true, data: { connected: true } });
+ const warning = await provider.testConnection();
+ this.rpc.sendResponse(id, {
+ ok: true,
+ data: {
+ connected: true,
+ warning: warning || undefined,
+ },
+ });
} catch (err) {
this.logger?.warn({ err }, "ai.testConnection failed");
const msg = err instanceof AIError ? err.originalMessage : String(err);
const code = err instanceof AIError ? err.code : "UNKNOWN";
- this.rpc.sendError(id, { code, message: msg });
+ this.rpc.sendResponse(id, {
+ ok: true,
+ data: {
+ connected: false,
+ code,
+ message: msg,
+ },
+ });
}
}
@@ -284,7 +297,7 @@ export class AIHandlers {
// ── Settings persistence (reads/writes ai-settings.json) ──────────────
- async handleLoadSettings(_params: unknown, id: number | string) {
+ handleLoadSettings(_params: unknown, id: number | string) {
try {
ensureDir(CONFIG_FOLDER);
if (!fsSync.existsSync(AI_SETTINGS_FILE)) {
@@ -292,7 +305,7 @@ export class AIHandlers {
this.rpc.sendResponse(id, { ok: true, data: {} });
return;
}
- const raw = await fs.readFile(AI_SETTINGS_FILE, "utf-8");
+ const raw = fsSync.readFileSync(AI_SETTINGS_FILE, "utf-8");
const settings = JSON.parse(raw) as AISettings;
this.rpc.sendResponse(id, { ok: true, data: settings });
} catch (err: any) {
@@ -302,13 +315,17 @@ export class AIHandlers {
}
}
- async handleSaveSettings(params: { settings: AISettings }, id: number | string) {
+ handleSaveSettings(params: { settings: AISettings }, id: number | string) {
try {
ensureDir(CONFIG_FOLDER);
- await fs.writeFile(AI_SETTINGS_FILE, JSON.stringify(params.settings, null, 2), "utf-8");
+ fsSync.writeFileSync(AI_SETTINGS_FILE, JSON.stringify(params.settings, null, 2), "utf-8");
// On non-Windows platforms, restrict file permissions (contains API keys)
if (process.platform !== "win32") {
- await fs.chmod(AI_SETTINGS_FILE, 0o600);
+ try {
+ fsSync.chmodSync(AI_SETTINGS_FILE, 0o600);
+ } catch {
+ // ignore chmod errors on unsupported filesystems
+ }
}
this.rpc.sendResponse(id, { ok: true, data: { saved: true } });
} catch (err: any) {
diff --git a/bridge/src/handlers/migrationHandlers.ts b/bridge/src/handlers/migrationHandlers.ts
index 32d4b2c3..e893ee89 100644
--- a/bridge/src/handlers/migrationHandlers.ts
+++ b/bridge/src/handlers/migrationHandlers.ts
@@ -98,7 +98,14 @@ export class MigrationHandlers {
async handleGenerateDropMigration(params: any, id: number | string) {
try {
- const { dbId, schemaName, tableName, mode = "RESTRICT" } = params || {};
+ const {
+ dbId,
+ schemaName,
+ tableName,
+ mode = "RESTRICT",
+ columns: passedColumns,
+ foreignKeys: passedForeignKeys,
+ } = params || {};
if (!dbId || !schemaName || !tableName) {
return this.rpc.sendError(id, {
@@ -107,16 +114,77 @@ export class MigrationHandlers {
});
}
- const { dbType } = await this.dbService.getDatabaseConnection(dbId);
+ const { conn, dbType } = await this.dbService.getDatabaseConnection(dbId);
const migrationsDir = await projectStoreInstance.resolveMigrationsDir(dbId);
- // Generate migration file
+ let columns = passedColumns;
+ let foreignKeys = passedForeignKeys;
+
+ // 1. Introspect columns and foreign keys from live database before dropping
+ if (!columns || !Array.isArray(columns) || columns.length === 0) {
+ try {
+ const connector = getConnector(dbType) as any;
+ if (typeof connector.getTableDetails === "function") {
+ const tableDetails = await connector.getTableDetails(conn, schemaName, tableName);
+ if (tableDetails && tableDetails.length > 0) {
+ columns = tableDetails.map((col: any) => ({
+ name: col.name,
+ type: col.type,
+ not_nullable: Boolean(col.not_nullable),
+ default_value: col.default_value,
+ is_primary_key: Boolean(col.is_primary_key),
+ }));
+ }
+ }
+ if (
+ (!foreignKeys || foreignKeys.length === 0) &&
+ typeof connector.listForeignKeys === "function"
+ ) {
+ foreignKeys = await connector.listForeignKeys(conn, schemaName, tableName);
+ }
+ } catch (inspectErr) {
+ this.logger?.warn(
+ { err: inspectErr },
+ "Could not inspect table before drop migration generation",
+ );
+ }
+ }
+
+ // 2. If table was already dropped or live inspection failed, fall back to project schema snapshot
+ if (!columns || columns.length === 0) {
+ try {
+ const project = await projectStoreInstance.getProjectByDatabaseId(dbId);
+ if (project) {
+ const schemaFile = await projectStoreInstance.getSchema(project.id);
+ const schemaGroup = schemaFile?.schemas?.find((s: any) => s.name === schemaName);
+ const tableSnap = schemaGroup?.tables?.find((t: any) => t.name === tableName);
+ if (tableSnap?.columns?.length) {
+ columns = tableSnap.columns.map((c: any) => ({
+ name: c.name,
+ type: c.type,
+ not_nullable: !c.nullable,
+ default_value: c.defaultValue,
+ is_primary_key: Boolean(c.isPrimaryKey),
+ }));
+ }
+ }
+ } catch (snapErr) {
+ this.logger?.warn(
+ { err: snapErr },
+ "Could not load schema snapshot before drop migration generation",
+ );
+ }
+ }
+
+ // Generate migration file with reverse CREATE TABLE in downSQL
const { generateDropTableMigration, writeMigrationFile } =
await import("../utils/migrationGenerator");
const migration = generateDropTableMigration({
schemaName,
tableName,
mode,
+ columns,
+ foreignKeys,
dbType,
});
@@ -175,13 +243,37 @@ export class MigrationHandlers {
const project = await projectStoreInstance.getProjectByDatabaseId(dbId);
if (project) {
+ // Refresh schema from DB so project schema.json stays up-to-date with newly created tables/columns
+ let schemaHash = "";
+ try {
+ const dbSchema = (await this.queryExecutor.listSchemas(conn, dbType)) as any;
+ const liveSchemas = dbSchema?.schemas || [];
+ const crypto = await import("crypto");
+ schemaHash = crypto
+ .createHash("sha256")
+ .update(JSON.stringify(liveSchemas))
+ .digest("hex");
+ await projectStoreInstance.saveSchema(project.id, liveSchemas, schemaHash, dbType);
+ this.rpc?.sendNotification?.("project.schema_changed", {
+ projectId: project.id,
+ newHash: schemaHash,
+ });
+ } catch (schemaErr) {
+ this.logger?.warn(
+ { err: schemaErr },
+ "Failed to auto-refresh project schema after migration apply",
+ );
+ }
+
// Update Migration Lock first so it gets included in the git sync commit
const appliedMigrations = await connector.listAppliedMigrations(conn);
- const schemaFile = await projectStoreInstance.getSchema(project.id);
- const schemaHash = (schemaFile as any)?.schemaHash || "";
+ if (!schemaHash) {
+ const schemaFile = await projectStoreInstance.getSchema(project.id);
+ schemaHash = (schemaFile as any)?.schemaHash || "";
+ }
- const appliedVersions = appliedMigrations.map((m) => m.version);
+ const appliedVersions = appliedMigrations.map((m: any) => m.version);
await writeMigrationLock(dbId, schemaHash, appliedVersions);
const projectDir = await projectStoreInstance.resolveProjectDir(project.id);
@@ -193,6 +285,9 @@ export class MigrationHandlers {
"Git sync failed after migration apply",
);
}
+ try {
+ await gitServiceInstance.syncSchemaFile(projectDir);
+ } catch {}
}
}
} catch (syncErr) {
@@ -258,14 +353,46 @@ export class MigrationHandlers {
const { writeMigrationLock } = await import("../services/migrationLock");
const project = await projectStoreInstance.getProjectByDatabaseId(dbId);
if (project) {
- const schemaFile = await projectStoreInstance.getSchema(project.id);
- const schemaHash = (schemaFile as any)?.schemaHash || "";
+ let schemaHash = "";
+ try {
+ const dbSchema = (await this.queryExecutor.listSchemas(conn, dbType)) as any;
+ const liveSchemas = dbSchema?.schemas || [];
+ const crypto = await import("crypto");
+ schemaHash = crypto
+ .createHash("sha256")
+ .update(JSON.stringify(liveSchemas))
+ .digest("hex");
+ await projectStoreInstance.saveSchema(project.id, liveSchemas, schemaHash, dbType);
+ this.rpc?.sendNotification?.("project.schema_changed", {
+ projectId: project.id,
+ newHash: schemaHash,
+ });
+ } catch (schemaErr) {
+ this.logger?.warn(
+ { err: schemaErr },
+ "Failed to auto-refresh project schema after applyMigrations",
+ );
+ }
+
+ if (!schemaHash) {
+ const schemaFile = await projectStoreInstance.getSchema(project.id);
+ schemaHash = (schemaFile as any)?.schemaHash || "";
+ }
const updatedApplied = await connector.listAppliedMigrations(conn);
await writeMigrationLock(
dbId,
schemaHash,
updatedApplied.map((m: any) => m.version),
);
+
+ const projectDir = await projectStoreInstance.resolveProjectDir(project.id);
+ if (projectDir) {
+ try {
+ const { gitServiceInstance } = await import("../services/gitService");
+ await gitServiceInstance.syncMigrationFiles(projectDir);
+ await gitServiceInstance.syncSchemaFile(projectDir);
+ } catch {}
+ }
}
} catch (syncErr) {
this.logger?.error({ err: syncErr }, "Lock hook failed");
@@ -385,11 +512,34 @@ export class MigrationHandlers {
const project = await projectStoreInstance.getProjectByDatabaseId(dbId);
if (project) {
+ let schemaHash = "";
+ try {
+ const dbSchema = (await this.queryExecutor.listSchemas(conn, dbType)) as any;
+ const liveSchemas = dbSchema?.schemas || [];
+ const crypto = await import("crypto");
+ schemaHash = crypto
+ .createHash("sha256")
+ .update(JSON.stringify(liveSchemas))
+ .digest("hex");
+ await projectStoreInstance.saveSchema(project.id, liveSchemas, schemaHash, dbType);
+ this.rpc?.sendNotification?.("project.schema_changed", {
+ projectId: project.id,
+ newHash: schemaHash,
+ });
+ } catch (schemaErr) {
+ this.logger?.warn(
+ { err: schemaErr },
+ "Failed to auto-refresh project schema after migration rollback",
+ );
+ }
+
// Update Migration Lock first so it gets included in the git sync commit
const appliedMigrations = await connector.listAppliedMigrations(conn);
- const schemaFile = await projectStoreInstance.getSchema(project.id);
- const schemaHash = (schemaFile as any)?.schemaHash || "";
+ if (!schemaHash) {
+ const schemaFile = await projectStoreInstance.getSchema(project.id);
+ schemaHash = (schemaFile as any)?.schemaHash || "";
+ }
const appliedVersions = appliedMigrations.map((m) => m.version);
await writeMigrationLock(dbId, schemaHash, appliedVersions);
@@ -403,6 +553,9 @@ export class MigrationHandlers {
"Git sync failed after migration rollback",
);
}
+ try {
+ await gitServiceInstance.syncSchemaFile(projectDir);
+ } catch {}
}
}
} catch (syncErr) {
diff --git a/bridge/src/queries/clickhouse/constraints.ts b/bridge/src/queries/clickhouse/constraints.ts
new file mode 100644
index 00000000..edfd3e87
--- /dev/null
+++ b/bridge/src/queries/clickhouse/constraints.ts
@@ -0,0 +1,54 @@
+/**
+ * ClickHouse Constraint & Index Queries
+ */
+
+/**
+ * List primary key / sorting key columns for a table.
+ */
+export const CLICKHOUSE_GET_PRIMARY_KEYS = `
+ SELECT name AS column_name
+ FROM system.columns
+ WHERE database = {target_schema: String}
+ AND \`table\` = {target_table: String}
+ AND (is_in_primary_key = 1 OR is_in_sorting_key = 1)
+ ORDER BY position;
+`;
+
+/**
+ * Batch get primary keys for all tables in a schema.
+ */
+export const CLICKHOUSE_BATCH_GET_PRIMARY_KEYS = `
+ SELECT \`table\` AS \`table\`, name AS column_name
+ FROM system.columns
+ WHERE database = {target_schema: String}
+ AND (is_in_primary_key = 1 OR is_in_sorting_key = 1)
+ ORDER BY \`table\`, position;
+`;
+
+/**
+ * List secondary (data skipping) indexes for a table.
+ */
+export const CLICKHOUSE_GET_INDEXES = `
+ SELECT
+ name AS index_name,
+ expr AS column_name,
+ type AS index_type
+ FROM system.data_skipping_indices
+ WHERE database = {target_schema: String}
+ AND \`table\` = {target_table: String}
+ ORDER BY name;
+`;
+
+/**
+ * Batch get indexes for all tables in a schema.
+ */
+export const CLICKHOUSE_BATCH_GET_INDEXES = `
+ SELECT
+ \`table\` AS \`table\`,
+ name AS index_name,
+ expr AS column_name,
+ type AS index_type
+ FROM system.data_skipping_indices
+ WHERE database = {target_schema: String}
+ ORDER BY \`table\`, name;
+`;
diff --git a/bridge/src/queries/clickhouse/migrations.ts b/bridge/src/queries/clickhouse/migrations.ts
new file mode 100644
index 00000000..ca129304
--- /dev/null
+++ b/bridge/src/queries/clickhouse/migrations.ts
@@ -0,0 +1,29 @@
+/**
+ * ClickHouse Migration Queries
+ */
+
+export const CLICKHOUSE_CREATE_MIGRATION_TABLE = (schema: string) => `
+ CREATE TABLE IF NOT EXISTS \`${schema}\`.schema_migrations (
+ version String,
+ name String,
+ applied_at DateTime DEFAULT now()
+ ) ENGINE = MergeTree() ORDER BY version;
+`;
+
+export const CLICKHOUSE_CHECK_MIGRATIONS_EXIST = (schema: string) => `
+ SELECT count() AS cnt
+ FROM \`${schema}\`.schema_migrations;
+`;
+
+export const CLICKHOUSE_LIST_APPLIED_MIGRATIONS = (schema: string) => `
+ SELECT
+ version,
+ name,
+ toString(applied_at) AS applied_at
+ FROM \`${schema}\`.schema_migrations
+ ORDER BY version ASC;
+`;
+
+export const CLICKHOUSE_DELETE_MIGRATION = (schema: string) => `
+ ALTER TABLE \`${schema}\`.schema_migrations DELETE WHERE version = {version: String};
+`;
diff --git a/bridge/src/queries/clickhouse/schema.ts b/bridge/src/queries/clickhouse/schema.ts
new file mode 100644
index 00000000..a1eac422
--- /dev/null
+++ b/bridge/src/queries/clickhouse/schema.ts
@@ -0,0 +1,75 @@
+/**
+ * ClickHouse Schema-related Queries
+ */
+
+/**
+ * List all user databases/schemas (excludes internal system schemas).
+ */
+export const CLICKHOUSE_LIST_SCHEMAS = `
+ SELECT name
+ FROM system.databases
+ WHERE name NOT IN ('system', 'information_schema', 'INFORMATION_SCHEMA')
+ ORDER BY name;
+`;
+
+/**
+ * List all tables across all non-system databases.
+ */
+export const CLICKHOUSE_LIST_TABLES = `
+ SELECT
+ database AS schema,
+ name,
+ if(engine LIKE '%View%', 'VIEW', 'BASE TABLE') AS type
+ FROM system.tables
+ WHERE database NOT IN ('system', 'information_schema', 'INFORMATION_SCHEMA')
+ ORDER BY database, name;
+`;
+
+/**
+ * List tables filtered by schema/database.
+ */
+export const CLICKHOUSE_LIST_TABLES_BY_SCHEMA = `
+ SELECT
+ database AS schema,
+ name,
+ if(engine LIKE '%View%', 'VIEW', 'BASE TABLE') AS type
+ FROM system.tables
+ WHERE database = {target_schema: String}
+ ORDER BY name;
+`;
+
+/**
+ * List all column details for a specific table.
+ */
+export const CLICKHOUSE_LIST_COLUMNS = `
+ SELECT
+ name,
+ type,
+ if(type LIKE 'Nullable(%)', 0, 1) AS not_nullable,
+ default_expression AS default_value,
+ is_in_primary_key,
+ is_in_sorting_key,
+ comment
+ FROM system.columns
+ WHERE database = {target_schema: String} AND \`table\` = {target_table: String}
+ ORDER BY position;
+`;
+
+/**
+ * Batch query column details for multiple tables.
+ */
+export const CLICKHOUSE_BATCH_LIST_COLUMNS = `
+ SELECT
+ database AS schema,
+ \`table\` AS \`table\`,
+ name,
+ type,
+ if(type LIKE 'Nullable(%)', 0, 1) AS not_nullable,
+ default_expression AS default_value,
+ is_in_primary_key,
+ is_in_sorting_key,
+ comment
+ FROM system.columns
+ WHERE database = {target_schema: String}
+ ORDER BY \`table\`, position;
+`;
diff --git a/bridge/src/queries/clickhouse/stats.ts b/bridge/src/queries/clickhouse/stats.ts
new file mode 100644
index 00000000..96460590
--- /dev/null
+++ b/bridge/src/queries/clickhouse/stats.ts
@@ -0,0 +1,24 @@
+/**
+ * ClickHouse Statistics Queries
+ */
+
+/**
+ * Get total tables and total rows for a database.
+ */
+export const CLICKHOUSE_GET_DB_STATS = `
+ SELECT
+ count() AS total_tables,
+ coalesce(sum(total_rows), 0) AS total_rows
+ FROM system.tables
+ WHERE database = {database: String};
+`;
+
+/**
+ * Get active data size on disk in MB for a database.
+ */
+export const CLICKHOUSE_GET_DB_SIZE = `
+ SELECT
+ coalesce(round(sum(bytes_on_disk) / (1024 * 1024), 2), 0) AS total_db_size_mb
+ FROM system.parts
+ WHERE database = {database: String} AND active = 1;
+`;
diff --git a/bridge/src/services/aiCacheService.ts b/bridge/src/services/aiCacheService.ts
index 0ef66487..a1873417 100644
--- a/bridge/src/services/aiCacheService.ts
+++ b/bridge/src/services/aiCacheService.ts
@@ -114,7 +114,7 @@ function resolveModelName(settings: AISettings): string {
case "gemini":
return settings.geminiModel ?? "gemini-1.5-flash";
case "groq":
- return settings.groqModel ?? "llama-3.3-70b-versatile";
+ return settings.groqModel ?? "openai/gpt-oss-20b";
case "mistral":
return settings.mistralModel ?? "mistral-small-latest";
case "ollama":
diff --git a/bridge/src/services/connectionBuilder.ts b/bridge/src/services/connectionBuilder.ts
index 7816eb6c..3e91e98d 100644
--- a/bridge/src/services/connectionBuilder.ts
+++ b/bridge/src/services/connectionBuilder.ts
@@ -1,6 +1,7 @@
import { DBType, DatabaseConfig } from "../types";
import { SQLiteConfig } from "../types/sqlite";
import { isWindowsDriveRootPath, normalizeSQLitePath } from "../utils/sqlitePath";
+import { parseClickHouseUrl } from "../utils/clickhouseUrl";
import { sshTunnelServiceInstance, TunnelInfo } from "./sshTunnelService";
export interface BuildResult {
@@ -32,13 +33,34 @@ export class ConnectionBuilder {
};
}
+ if (dbType === DBType.CLICKHOUSE) {
+ const rawTarget = db.url || db.host;
+ if (!rawTarget) {
+ throw new Error("ClickHouse connection requires a URL or host");
+ }
+ const parsed = parseClickHouseUrl(rawTarget);
+ return {
+ config: {
+ url: parsed.url,
+ host: db.host || parsed.host,
+ port: db.port || parsed.port,
+ user: db.user || "default",
+ password: pwd ?? undefined,
+ database: db.database || parsed.database || "default",
+ ssl: db.ssl !== undefined ? db.ssl : parsed.ssl,
+ } as any,
+ };
+ }
+
+ const defaultPort = dbType === DBType.MYSQL || dbType === DBType.MARIADB ? 3306 : 5432;
+
const base: DatabaseConfig = {
host: db.host,
- port: db.port || (dbType === DBType.MYSQL || dbType === DBType.MARIADB ? 3306 : 5432),
- user: db.user,
+ port: db.port || defaultPort,
+ user: db.user || "",
password: pwd ?? undefined,
ssl: db.ssl,
- database: db.database,
+ database: db.database || "",
ssh: db.ssh,
};
@@ -77,4 +99,8 @@ export class ConnectionBuilder {
static async buildDuckDBConnection(db: any): Promise {
return this.buildConnection(db, null, DBType.DUCKDB);
}
+
+ static async buildClickHouseConnection(db: any, pwd: string | null): Promise {
+ return this.buildConnection(db, pwd, DBType.CLICKHOUSE);
+ }
}
diff --git a/bridge/src/services/connectorRegistry.ts b/bridge/src/services/connectorRegistry.ts
index 286215f7..fe84ca45 100644
--- a/bridge/src/services/connectorRegistry.ts
+++ b/bridge/src/services/connectorRegistry.ts
@@ -12,13 +12,15 @@ import * as mysqlConnector from "../connectors/mysql";
import * as mariadbConnector from "../connectors/mariadb";
import * as sqliteConnector from "../connectors/sqlite";
import * as duckdbConnector from "../connectors/duckdb";
+import * as clickhouseConnector from "../connectors/clickhouse";
export type Connector =
| typeof postgresConnector
| typeof mysqlConnector
| typeof mariadbConnector
| typeof sqliteConnector
- | typeof duckdbConnector;
+ | typeof duckdbConnector
+ | typeof clickhouseConnector;
const registry: Record = {
[DBType.POSTGRES]: postgresConnector,
@@ -26,6 +28,7 @@ const registry: Record = {
[DBType.MARIADB]: mariadbConnector,
[DBType.SQLITE]: sqliteConnector,
[DBType.DUCKDB]: duckdbConnector,
+ [DBType.CLICKHOUSE]: clickhouseConnector,
};
/**
diff --git a/bridge/src/services/databaseService.ts b/bridge/src/services/databaseService.ts
index bd481c4b..e23bf6ba 100644
--- a/bridge/src/services/databaseService.ts
+++ b/bridge/src/services/databaseService.ts
@@ -62,9 +62,12 @@ export class DatabaseService {
const isFileBasedDB = ["sqlite", "duckdb"].includes(
(payload.type as string | undefined)?.toLowerCase() || "",
);
+ const isClickHouse = (payload.type as string | undefined)?.toLowerCase() === "clickhouse";
const required = isFileBasedDB
? ["name", "database", "type"]
- : ["name", "host", "port", "user", "database", "type"];
+ : isClickHouse
+ ? ["name", "type"]
+ : ["name", "host", "port", "user", "database", "type"];
for (const field of required) {
if (!payload[field]) throw new Error(`Missing required field: ${field}`);
}
@@ -84,6 +87,21 @@ export class DatabaseService {
...payload,
database: (config as any).path,
};
+ } else if (isClickHouse) {
+ if (!payload.url && !payload.host) {
+ throw new Error("Missing required field: url");
+ }
+ const { parseClickHouseUrl } = await import("../utils/clickhouseUrl");
+ const parsed = parseClickHouseUrl(payload.url || payload.host);
+ payload = {
+ ...payload,
+ url: parsed.url,
+ host: payload.host || parsed.host,
+ port: payload.port || parsed.port,
+ user: payload.user || "default",
+ database: payload.database || parsed.database || "default",
+ ssl: payload.ssl !== undefined ? payload.ssl : parsed.ssl,
+ };
}
const res = await dbStoreInstance.addDB(payload as Parameters[0]);
@@ -104,6 +122,7 @@ export class DatabaseService {
const isFileBasedDB = ["sqlite", "duckdb"].includes(
(payload.type as string | undefined)?.toLowerCase() || "",
);
+ const isClickHouse = (payload.type as string | undefined)?.toLowerCase() === "clickhouse";
// Extract SSH credentials
const sshPwd = payload.ssh?.password;
@@ -113,6 +132,23 @@ export class DatabaseService {
delete payload.ssh.passphrase;
}
+ if (isClickHouse && (payload.url || payload.host)) {
+ const { parseClickHouseUrl } = await import("../utils/clickhouseUrl");
+ const parsed = parseClickHouseUrl(payload.url || payload.host);
+ payload = {
+ ...payload,
+ url: parsed.url,
+ host: payload.host || parsed.host,
+ port: payload.port || parsed.port,
+ database: payload.database || parsed.database || "default",
+ ssl: payload.ssl !== undefined ? payload.ssl : parsed.ssl,
+ };
+ }
+ if (payload.ssh) {
+ delete payload.ssh.password;
+ delete payload.ssh.passphrase;
+ }
+
if (isFileBasedDB || typeof payload.database === "string") {
const current = await dbStoreInstance.getDB(id);
const currentIsFileBasedDB = ["sqlite", "duckdb"].includes(
diff --git a/bridge/src/services/dbStore.ts b/bridge/src/services/dbStore.ts
index 516cdc29..b1b3fb8c 100644
--- a/bridge/src/services/dbStore.ts
+++ b/bridge/src/services/dbStore.ts
@@ -178,6 +178,7 @@ type DBMeta = {
user: string;
database: string;
type?: string;
+ url?: string;
credentialId?: string;
notes?: string;
tags?: string[];
@@ -507,6 +508,7 @@ export class DbStore {
user: string;
database: string;
type?: string;
+ url?: string;
ssl?: boolean;
sslmode?: string;
password?: string;
@@ -526,6 +528,7 @@ export class DbStore {
user: payload.user,
database: payload.database,
type: payload.type,
+ url: payload.url,
ssl: payload.ssl,
sslmode: payload.sslmode,
credentialId,
diff --git a/bridge/src/services/discoveryService.ts b/bridge/src/services/discoveryService.ts
index 238821b6..4edf15db 100644
--- a/bridge/src/services/discoveryService.ts
+++ b/bridge/src/services/discoveryService.ts
@@ -16,6 +16,7 @@ const DATABASE_PORTS: Record = {
postgresql: [5432, 5433, 5434],
mysql: [3306, 3307, 3308],
mariadb: [3306, 3307, 3308],
+ clickhouse: [8123, 8124],
};
// Fun adjectives and nouns for generating database names
@@ -90,7 +91,7 @@ const NOUNS = [
];
export interface DiscoveredDatabase {
- type: "postgresql" | "mysql" | "mariadb";
+ type: "postgresql" | "mysql" | "mariadb" | "clickhouse";
host: string;
port: number;
source: "local" | "docker";
@@ -124,6 +125,8 @@ export class DiscoveryService {
case "mysql":
case "mariadb":
return 3306;
+ case "clickhouse":
+ return 8123;
default:
return 0;
}
@@ -139,6 +142,8 @@ export class DiscoveryService {
case "mysql":
case "mariadb":
return "root";
+ case "clickhouse":
+ return "default";
default:
return "";
}
@@ -154,6 +159,8 @@ export class DiscoveryService {
case "mysql":
case "mariadb":
return "mysql";
+ case "clickhouse":
+ return "default";
default:
return "";
}
@@ -206,12 +213,14 @@ export class DiscoveryService {
scannedPorts.add(key);
// Determine the actual type based on the port
- let actualType: "postgresql" | "mysql" | "mariadb" = dbType as any;
+ let actualType: "postgresql" | "mysql" | "mariadb" | "clickhouse" = dbType as any;
if (port >= 5432 && port <= 5434) {
actualType = "postgresql";
} else if (port >= 3306 && port <= 3308) {
// Can't distinguish MySQL from MariaDB by port alone
actualType = "mysql";
+ } else if (port === 8123 || port === 8124) {
+ actualType = "clickhouse";
}
discovered.push({
@@ -263,7 +272,7 @@ export class DiscoveryService {
* Extract database credentials from container environment variables
*/
private extractCredentialsFromEnv(
- dbType: "postgresql" | "mysql" | "mariadb",
+ dbType: "postgresql" | "mysql" | "mariadb" | "clickhouse",
envVars: Map,
): { user: string; password: string; database: string } {
if (dbType === "postgresql") {
@@ -284,6 +293,13 @@ export class DiscoveryService {
password,
database: envVars.get("MYSQL_DATABASE") || "mysql",
};
+ } else if (dbType === "clickhouse") {
+ return {
+ user: envVars.get("CLICKHOUSE_USER") || envVars.get("CLICKHOUSE_DEFAULT_USER") || "default",
+ password:
+ envVars.get("CLICKHOUSE_PASSWORD") || envVars.get("CLICKHOUSE_DEFAULT_PASSWORD") || "",
+ database: envVars.get("CLICKHOUSE_DB") || "default",
+ };
} else {
// MariaDB - similar to MySQL
const user = envVars.get("MARIADB_USER") || envVars.get("MYSQL_USER") || "root";
@@ -317,13 +333,15 @@ export class DiscoveryService {
const [containerName, image, ports] = line.split("|");
// Determine database type from image name
- let dbType: "postgresql" | "mysql" | "mariadb" | null = null;
+ let dbType: "postgresql" | "mysql" | "mariadb" | "clickhouse" | null = null;
if (image.includes("postgres") || image.includes("pg") || image.includes("postgresql")) {
dbType = "postgresql";
} else if (image.includes("mariadb")) {
dbType = "mariadb";
} else if (image.includes("mysql")) {
dbType = "mysql";
+ } else if (image.includes("clickhouse")) {
+ dbType = "clickhouse";
}
if (dbType && ports) {
diff --git a/bridge/src/services/monitoringService.ts b/bridge/src/services/monitoringService.ts
index 0b0fa828..80400ee2 100644
--- a/bridge/src/services/monitoringService.ts
+++ b/bridge/src/services/monitoringService.ts
@@ -3,8 +3,10 @@ import mysql, { PoolConnection, RowDataPacket } from "mysql2/promise";
import { DBType } from "../types";
import { MySQLConfig } from "../types/mysql";
import { PGConfig } from "../types/postgres";
+import { ClickHouseConfig } from "../types/clickhouse";
import { createPoolConfig as createMySQLPoolConfig } from "../connectors/mysql";
import { createPoolConfig as createMariaDBPoolConfig, MariaDBConfig } from "../connectors/mariadb";
+import { getClient as getClickHouseClient } from "../connectors/clickhouse";
export interface MonitoringActiveQuery {
id: string | number;
@@ -44,7 +46,7 @@ export class MonitoringService {
private previousTotals = new Map();
async getSnapshot(dbId: string, conn: any, dbType: DBType): Promise {
- if (![DBType.POSTGRES, DBType.MYSQL, DBType.MARIADB].includes(dbType)) {
+ if (![DBType.POSTGRES, DBType.MYSQL, DBType.MARIADB, DBType.CLICKHOUSE].includes(dbType)) {
throw new Error(`Monitoring is not supported for ${dbType}`);
}
@@ -56,6 +58,10 @@ export class MonitoringService {
return this.getMySQLLikeSnapshot(dbId, conn as MySQLConfig, dbType);
}
+ if (dbType === DBType.CLICKHOUSE) {
+ return this.getClickHouseSnapshot(dbId, conn as ClickHouseConfig);
+ }
+
throw new Error(`Monitoring is not supported for ${dbType}`);
}
@@ -223,6 +229,150 @@ export class MonitoringService {
}
}
+ private async getClickHouseSnapshot(
+ dbId: string,
+ cfg: ClickHouseConfig,
+ ): Promise {
+ const startedAt = Date.now();
+ try {
+ const client = getClickHouseClient(cfg);
+ await client.query({ query: "SELECT 1;", format: "JSONEachRow" });
+ const latencyMs = Date.now() - startedAt;
+
+ const [metricsSettled, settingsSettled, eventsSettled, processesSettled] =
+ await Promise.allSettled([
+ client
+ .query({
+ query:
+ "SELECT metric, toInt64(value) AS value FROM system.metrics WHERE metric IN ('TCPConnection', 'HTTPConnection', 'Query');",
+ format: "JSONEachRow",
+ })
+ .then(
+ async (res) =>
+ (await res.json()) as Array<{ metric: string; value: string | number }>,
+ ),
+
+ client
+ .query({
+ query:
+ "SELECT value FROM system.settings WHERE name = 'max_concurrent_queries' LIMIT 1;",
+ format: "JSONEachRow",
+ })
+ .then(async (res) => (await res.json()) as Array<{ value: string | number }>),
+
+ client
+ .query({
+ query:
+ "SELECT event, toUInt64(value) AS value FROM system.events WHERE event IN ('Query', 'MarkCacheHits', 'MarkCacheMisses');",
+ format: "JSONEachRow",
+ })
+ .then(
+ async (res) => (await res.json()) as Array<{ event: string; value: string | number }>,
+ ),
+
+ client
+ .query({
+ query: `
+ SELECT
+ query_id AS id,
+ user,
+ query,
+ 'running' AS state,
+ round(elapsed, 2) AS durationSeconds
+ FROM system.processes
+ WHERE is_cancelled = 0 AND query NOT LIKE '%system.processes%'
+ ORDER BY elapsed DESC
+ LIMIT 25;
+ `,
+ format: "JSONEachRow",
+ })
+ .then(
+ async (res) =>
+ (await res.json()) as Array<{
+ id: string;
+ user: string;
+ query: string;
+ state: string;
+ durationSeconds: number;
+ }>,
+ ),
+ ]);
+
+ let active = 0;
+ if (metricsSettled.status === "fulfilled" && Array.isArray(metricsSettled.value)) {
+ for (const row of metricsSettled.value) {
+ if (row.metric === "TCPConnection" || row.metric === "HTTPConnection") {
+ active += Number(row.value ?? 0);
+ }
+ }
+ }
+ active = Math.max(1, active);
+
+ let max = 100;
+ if (
+ settingsSettled.status === "fulfilled" &&
+ Array.isArray(settingsSettled.value) &&
+ settingsSettled.value.length > 0
+ ) {
+ const parsedMax = Number(settingsSettled.value[0]?.value);
+ if (Number.isFinite(parsedMax) && parsedMax > 0) {
+ max = parsedMax;
+ }
+ }
+
+ let totalQueries = 0;
+ let markCacheHits = 0;
+ let markCacheMisses = 0;
+ let hasMarkCache = false;
+
+ if (eventsSettled.status === "fulfilled" && Array.isArray(eventsSettled.value)) {
+ for (const row of eventsSettled.value) {
+ if (row.event === "Query") {
+ totalQueries = Number(row.value ?? 0);
+ } else if (row.event === "MarkCacheHits") {
+ markCacheHits = Number(row.value ?? 0);
+ hasMarkCache = true;
+ } else if (row.event === "MarkCacheMisses") {
+ markCacheMisses = Number(row.value ?? 0);
+ hasMarkCache = true;
+ }
+ }
+ }
+
+ const totalCacheRequests = markCacheHits + markCacheMisses;
+ const cacheHitRatio =
+ hasMarkCache && totalCacheRequests > 0
+ ? Number(((markCacheHits / totalCacheRequests) * 100).toFixed(2))
+ : null;
+
+ const activeQueries: MonitoringActiveQuery[] =
+ processesSettled.status === "fulfilled" && Array.isArray(processesSettled.value)
+ ? processesSettled.value.map((row) => ({
+ id: String(row.id ?? ""),
+ user: String(row.user ?? ""),
+ query: String(row.query ?? ""),
+ state: String(row.state ?? "running"),
+ durationSeconds: Number(row.durationSeconds ?? 0),
+ }))
+ : [];
+
+ return {
+ databaseType: DBType.CLICKHOUSE,
+ sampledAt: new Date().toISOString(),
+ health: { ok: true, latencyMs },
+ connections: this.mapConnections(active, max),
+ throughput: {
+ qps: this.calculateRate(dbId, totalQueries),
+ totalQueries,
+ },
+ cacheHitRatio,
+ activeQueries,
+ };
+ } catch (error: any) {
+ return this.emptySnapshot(DBType.CLICKHOUSE, error?.message || String(error));
+ }
+ }
+
private mapConnections(active: number, max: number) {
return {
active,
diff --git a/bridge/src/services/monitoringWebSocketServer.ts b/bridge/src/services/monitoringWebSocketServer.ts
index 551c72af..3074b038 100644
--- a/bridge/src/services/monitoringWebSocketServer.ts
+++ b/bridge/src/services/monitoringWebSocketServer.ts
@@ -92,7 +92,7 @@ export class MonitoringWebSocketServer {
try {
const { conn, dbType } = await this.dbService.getDatabaseConnection(dbId);
- if (![DBType.POSTGRES, DBType.MYSQL, DBType.MARIADB].includes(dbType)) {
+ if (![DBType.POSTGRES, DBType.MYSQL, DBType.MARIADB, DBType.CLICKHOUSE].includes(dbType)) {
socket.send(
JSON.stringify({
type: "unsupported",
diff --git a/bridge/src/services/projectStore.ts b/bridge/src/services/projectStore.ts
index 2e5dcabd..6efb71db 100644
--- a/bridge/src/services/projectStore.ts
+++ b/bridge/src/services/projectStore.ts
@@ -73,7 +73,7 @@ export type SchemaFile = {
version: number; // bumped to 2
projectId: string;
databaseId: string;
- dialect: "postgresql" | "mysql" | "sqlite" | "unknown";
+ dialect: "postgresql" | "mysql" | "sqlite" | "duckdb" | "clickhouse" | "unknown";
schemas: SchemaSnapshot[];
cachedAt: string;
relwaveVersion: string;
diff --git a/bridge/src/services/queryExecutor.ts b/bridge/src/services/queryExecutor.ts
index 1fc22259..5a625ef7 100644
--- a/bridge/src/services/queryExecutor.ts
+++ b/bridge/src/services/queryExecutor.ts
@@ -56,6 +56,7 @@ export class QueryExecutor {
public mariadb = getConnector(DBType.MARIADB) as typeof import("../connectors/mariadb");
public sqlite = getConnector(DBType.SQLITE) as typeof import("../connectors/sqlite");
public duckdb = getConnector(DBType.DUCKDB) as typeof import("../connectors/duckdb");
+ public clickhouse = getConnector(DBType.CLICKHOUSE) as typeof import("../connectors/clickhouse");
async executeQuery(
params: QueryParams,
diff --git a/bridge/src/types/clickhouse.ts b/bridge/src/types/clickhouse.ts
new file mode 100644
index 00000000..2e27868d
--- /dev/null
+++ b/bridge/src/types/clickhouse.ts
@@ -0,0 +1,42 @@
+/**
+ * ClickHouse-specific Types
+ */
+
+import type {
+ ColumnDetail,
+ ForeignKeyInfo,
+ IndexInfo,
+ UniqueConstraintInfo,
+ CheckConstraintInfo,
+ PrimaryKeyInfo,
+} from "./common";
+
+/**
+ * ClickHouse connection configuration
+ */
+export type ClickHouseConfig = {
+ host: string;
+ port?: number;
+ user?: string;
+ password?: string;
+ database?: string;
+ ssl?: boolean;
+ url?: string;
+};
+
+/**
+ * ClickHouse schema metadata batch result
+ */
+export type ClickHouseSchemaMetadataBatch = {
+ tables: Map<
+ string,
+ {
+ columns: ColumnDetail[];
+ primaryKeys: PrimaryKeyInfo[];
+ foreignKeys: ForeignKeyInfo[];
+ indexes: IndexInfo[];
+ uniqueConstraints: UniqueConstraintInfo[];
+ checkConstraints: CheckConstraintInfo[];
+ }
+ >;
+};
diff --git a/bridge/src/types/index.ts b/bridge/src/types/index.ts
index 9c946fe1..b05ea952 100644
--- a/bridge/src/types/index.ts
+++ b/bridge/src/types/index.ts
@@ -15,6 +15,7 @@ export * from "./mysql";
export * from "./postgres";
export * from "./sqlite";
export * from "./duckdb";
+export * from "./clickhouse";
export * from "./ai";
export { SSHConfig } from "./common";
@@ -24,6 +25,7 @@ export enum DBType {
MARIADB = "mariadb",
SQLITE = "sqlite",
DUCKDB = "duckdb",
+ CLICKHOUSE = "clickhouse",
}
export type Rpc = {
@@ -40,6 +42,7 @@ export interface DatabaseConfig {
ssl?: boolean;
database: string;
ssh?: SSHConfig;
+ url?: string;
}
export interface QueryParams {
diff --git a/bridge/src/utils/baselineMigration.ts b/bridge/src/utils/baselineMigration.ts
index 55171026..cfcefea8 100644
--- a/bridge/src/utils/baselineMigration.ts
+++ b/bridge/src/utils/baselineMigration.ts
@@ -4,7 +4,7 @@ import path from "path";
import { SchemaFile } from "../services/projectStore";
function quoteIdent(name: string, dbType: string): string {
- if (dbType === "mysql" || dbType === "mariadb") {
+ if (dbType === "mysql" || dbType === "mariadb" || dbType === "clickhouse") {
return `\`${name.replace(/`/g, "``")}\``;
} else {
return `"${name.replace(/"/g, '""')}"`;
@@ -102,18 +102,25 @@ export function generateBaselineSQL(snapshot: SchemaFile, version: string, name:
return ` CONSTRAINT ${quoteIdent(fk.name, dbType)} FOREIGN KEY (${srcCols}) REFERENCES ${targetRef}(${tgtCols}) ON DELETE ${onDelete} ON UPDATE ${onUpdate}`;
});
- const allDefs = [...columnDefs, ...fkDefs].join(",\n");
-
- upSQL += `CREATE TABLE ${tableRef} (\n${allDefs}\n);\n\n`;
-
- // Indexes
- const indexesToCreate = table.indexes || [];
- for (const idx of indexesToCreate) {
- const idxCols = idx.columns.map((c) => quoteIdent(c, dbType)).join(", ");
- const uniqueStr = idx.unique ? "UNIQUE " : "";
- upSQL += `CREATE ${uniqueStr}INDEX ${quoteIdent(idx.name, dbType)} ON ${tableRef} (${idxCols});\n`;
+ if (dbType === "clickhouse") {
+ const pkCols = table.columns
+ .filter((c) => c.isPrimaryKey)
+ .map((c) => quoteIdent(c.name, dbType));
+ const orderClause = pkCols.length > 0 ? pkCols.join(", ") : "tuple()";
+ upSQL += `CREATE TABLE ${tableRef} (\n${columnDefs.join(",\n")}\n) ENGINE = MergeTree() ORDER BY (${orderClause});\n\n`;
+ } else {
+ const allDefs = [...columnDefs, ...fkDefs].join(",\n");
+ upSQL += `CREATE TABLE ${tableRef} (\n${allDefs}\n);\n\n`;
+
+ // Indexes
+ const indexesToCreate = table.indexes || [];
+ for (const idx of indexesToCreate) {
+ const idxCols = idx.columns.map((c) => quoteIdent(c, dbType)).join(", ");
+ const uniqueStr = idx.unique ? "UNIQUE " : "";
+ upSQL += `CREATE ${uniqueStr}INDEX ${quoteIdent(idx.name, dbType)} ON ${tableRef} (${idxCols});\n`;
+ }
+ if (indexesToCreate.length > 0) upSQL += "\n";
}
- if (indexesToCreate.length > 0) upSQL += "\n";
}
// Views
diff --git a/bridge/src/utils/clickhouseUrl.ts b/bridge/src/utils/clickhouseUrl.ts
new file mode 100644
index 00000000..f722ec4c
--- /dev/null
+++ b/bridge/src/utils/clickhouseUrl.ts
@@ -0,0 +1,79 @@
+/**
+ * Utility to clean and resolve ClickHouse URLs
+ */
+
+export function cleanClickHouseUrl(raw?: string): string {
+ let url = (raw || "").trim();
+ if (!url) return "";
+
+ // Strip markdown link brackets e.g. [https://...](https://...)
+ const mdMatch = url.match(/\[(.*?)\]\((.*?)\)/);
+ if (mdMatch) {
+ url = mdMatch[2] || mdMatch[1];
+ }
+
+ // Strip enclosing quotes
+ url = url.replace(/^['"`]+|['"`]+$/g, "").trim();
+
+ // If starts with clickhouses:// -> https://
+ if (url.startsWith("clickhouses://")) {
+ url = "https://" + url.slice("clickhouses://".length);
+ } else if (url.startsWith("clickhouse://")) {
+ const rest = url.slice("clickhouse://".length);
+ const isSsl =
+ rest.includes(":8443") || rest.includes(":443") || rest.includes("clickhouse.cloud");
+ url = (isSsl ? "https://" : "http://") + rest;
+ }
+
+ // If no scheme was provided, determine http vs https
+ if (!url.startsWith("http://") && !url.startsWith("https://")) {
+ const isSsl = url.includes(":8443") || url.includes(":443") || url.includes("clickhouse.cloud");
+ url = (isSsl ? "https://" : "http://") + url;
+ }
+
+ // Strip trailing slashes
+ url = url.replace(/\/+$/, "");
+
+ return url;
+}
+
+export function parseClickHouseUrl(raw?: string): {
+ url: string;
+ host: string;
+ port: number;
+ ssl: boolean;
+ database: string;
+ user?: string;
+ password?: string;
+} {
+ const cleanUrl = cleanClickHouseUrl(raw);
+ try {
+ const u = new URL(cleanUrl);
+ const ssl = u.protocol === "https:";
+ const defaultPort = ssl ? 8443 : 8123;
+ const port = u.port ? parseInt(u.port, 10) : defaultPort;
+ const database = decodeURIComponent(u.pathname.replace(/^\//, "")) || "default";
+ const user = decodeURIComponent(u.username) || undefined;
+ const password = decodeURIComponent(u.password) || undefined;
+
+ return {
+ url: cleanUrl,
+ host: u.hostname,
+ port,
+ ssl,
+ database,
+ user,
+ password,
+ };
+ } catch {
+ const isSsl = cleanUrl.includes(":8443") || cleanUrl.startsWith("https:");
+ const port = isSsl ? 8443 : 8123;
+ return {
+ url: cleanUrl,
+ host: cleanUrl,
+ port,
+ ssl: isSsl,
+ database: "default",
+ };
+ }
+}
diff --git a/bridge/src/utils/dbTypeDetector.ts b/bridge/src/utils/dbTypeDetector.ts
index 33fc9560..9df8046c 100644
--- a/bridge/src/utils/dbTypeDetector.ts
+++ b/bridge/src/utils/dbTypeDetector.ts
@@ -11,6 +11,7 @@ export class DBTypeDetector {
}
if (normalized.includes("sqlite")) return DBType.SQLITE;
if (normalized.includes("duckdb")) return DBType.DUCKDB;
+ if (normalized.includes("clickhouse")) return DBType.CLICKHOUSE;
}
return DBType.POSTGRES; // default
}
diff --git a/bridge/src/utils/migrationGenerator.ts b/bridge/src/utils/migrationGenerator.ts
index fe2db899..92465ab6 100644
--- a/bridge/src/utils/migrationGenerator.ts
+++ b/bridge/src/utils/migrationGenerator.ts
@@ -33,7 +33,7 @@ export function generateCreateTableMigration(params: {
tableName: string;
columns: any[];
foreignKeys?: any[];
- dbType: "mysql" | "postgres" | "mariadb" | "sqlite" | "duckdb";
+ dbType: "mysql" | "postgres" | "mariadb" | "sqlite" | "duckdb" | "clickhouse";
}): MigrationFile {
const { schemaName, tableName, columns, foreignKeys = [], dbType } = params;
const version = generateMigrationVersion();
@@ -44,30 +44,42 @@ export function generateCreateTableMigration(params: {
const columnDefs = columns
.map((col) => {
let def = ` ${quoteIdent(col.name, dbType)} ${col.type}`;
- if (col.not_nullable) def += " NOT NULL";
+ if (col.not_nullable && dbType !== "clickhouse") def += " NOT NULL";
if (col.default_value) def += ` DEFAULT ${col.default_value}`;
- if (col.is_primary_key) def += " PRIMARY KEY";
+ if (col.is_primary_key && dbType !== "clickhouse") def += " PRIMARY KEY";
return def;
})
.join(",\n");
// Build foreign key constraints
- const fkDefs = foreignKeys.map((fk) => {
- const onDelete = fk.on_delete || "NO ACTION";
- const onUpdate = fk.on_update || "NO ACTION";
- return ` CONSTRAINT ${quoteIdent(fk.constraint_name, dbType)} FOREIGN KEY (${quoteIdent(fk.source_column, dbType)}) REFERENCES ${quoteIdent(fk.target_schema, dbType)}.${quoteIdent(fk.target_table, dbType)}(${quoteIdent(fk.target_column, dbType)}) ON DELETE ${onDelete} ON UPDATE ${onUpdate}`;
- });
+ const fkDefs =
+ dbType === "clickhouse"
+ ? []
+ : foreignKeys.map((fk) => {
+ const onDelete = fk.on_delete || "NO ACTION";
+ const onUpdate = fk.on_update || "NO ACTION";
+ return ` CONSTRAINT ${quoteIdent(fk.constraint_name, dbType)} FOREIGN KEY (${quoteIdent(fk.source_column, dbType)}) REFERENCES ${quoteIdent(fk.target_schema, dbType)}.${quoteIdent(fk.target_table, dbType)}(${quoteIdent(fk.target_column, dbType)}) ON DELETE ${onDelete} ON UPDATE ${onUpdate}`;
+ });
const allDefs = [...[columnDefs], ...fkDefs].filter(Boolean).join(",\n");
- // For MySQL/MariaDB, use database.table format (schemas are databases)
+ // For MySQL/MariaDB/ClickHouse, use database.table format (schemas are databases)
// For Postgres, use schema.table format
const tableRef = `${quoteIdent(schemaName, dbType)}.${quoteIdent(tableName, dbType)}`;
// Generate UP SQL
- const upSQL = `CREATE TABLE ${tableRef} (
+ let upSQL = "";
+ if (dbType === "clickhouse") {
+ const pkCols = columns.filter((c) => c.is_primary_key).map((c) => quoteIdent(c.name, dbType));
+ const orderClause = pkCols.length > 0 ? pkCols.join(", ") : "tuple()";
+ upSQL = `CREATE TABLE ${tableRef} (
+${allDefs}
+) ENGINE = MergeTree() ORDER BY (${orderClause});`;
+ } else {
+ upSQL = `CREATE TABLE ${tableRef} (
${allDefs}
);`;
+ }
// Generate DOWN SQL
const downSQL = `DROP TABLE ${tableRef};`;
@@ -88,7 +100,7 @@ export function generateAlterTableMigration(params: {
schemaName: string;
tableName: string;
operations: any[];
- dbType: "mysql" | "postgres" | "mariadb" | "sqlite" | "duckdb";
+ dbType: "mysql" | "postgres" | "mariadb" | "sqlite" | "duckdb" | "clickhouse";
}): MigrationFile {
const { schemaName, tableName, operations, dbType } = params;
const version = generateMigrationVersion();
@@ -142,9 +154,19 @@ export function generateAlterTableMigration(params: {
break;
case "ALTER_TYPE":
- upStatements.push(
- `ALTER TABLE ${fullTableName} ${dbType === "postgres" ? "ALTER COLUMN" : "MODIFY"} ${quoteIdent(op.column_name, dbType)} ${dbType === "postgres" ? "TYPE " : ""}${op.new_type};`,
- );
+ if (dbType === "postgres") {
+ upStatements.push(
+ `ALTER TABLE ${fullTableName} ALTER COLUMN ${quoteIdent(op.column_name, dbType)} TYPE ${op.new_type};`,
+ );
+ } else if (dbType === "clickhouse") {
+ upStatements.push(
+ `ALTER TABLE ${fullTableName} MODIFY COLUMN ${quoteIdent(op.column_name, dbType)} ${op.new_type};`,
+ );
+ } else {
+ upStatements.push(
+ `ALTER TABLE ${fullTableName} MODIFY ${quoteIdent(op.column_name, dbType)} ${op.new_type};`,
+ );
+ }
downStatements.push(
`-- WARNING: Cannot automatically reverse ALTER TYPE. Manual intervention required.`,
);
@@ -242,10 +264,19 @@ export function generateAlterTableMigration(params: {
export function generateDropTableMigration(params: {
schemaName: string;
tableName: string;
- mode: "RESTRICT" | "DETACH_FKS" | "CASCADE";
- dbType: "mysql" | "postgres" | "mariadb" | "sqlite" | "duckdb";
+ mode?: "RESTRICT" | "DETACH_FKS" | "CASCADE";
+ columns?: any[];
+ foreignKeys?: any[];
+ dbType: "mysql" | "postgres" | "mariadb" | "sqlite" | "duckdb" | "clickhouse";
}): MigrationFile {
- const { schemaName, tableName, mode, dbType } = params;
+ const {
+ schemaName,
+ tableName,
+ mode = "RESTRICT",
+ columns = [],
+ foreignKeys = [],
+ dbType,
+ } = params;
const version = generateMigrationVersion();
const name = `drop_${tableName}_table`;
const filename = `${version}_${name}.sql`;
@@ -254,18 +285,33 @@ export function generateDropTableMigration(params: {
const fullTableName = `${quoteIdent(schemaName, dbType)}.${quoteIdent(tableName, dbType)}`;
let upSQL = "";
- if (mode === "CASCADE") {
- upSQL = `DROP TABLE ${fullTableName} CASCADE;`;
+ if (dbType === "clickhouse" || dbType === "sqlite") {
+ upSQL = `DROP TABLE IF EXISTS ${fullTableName};`;
+ } else if (mode === "CASCADE") {
+ upSQL = `DROP TABLE IF EXISTS ${fullTableName} CASCADE;`;
} else if (mode === "DETACH_FKS") {
upSQL = `-- First drop foreign key constraints, then drop table
-- WARNING: FK constraint names need to be specified manually
-DROP TABLE ${fullTableName};`;
+DROP TABLE IF EXISTS ${fullTableName};`;
} else {
- upSQL = `DROP TABLE ${fullTableName};`;
+ upSQL = `DROP TABLE IF EXISTS ${fullTableName};`;
}
- const downSQL = `-- WARNING: Cannot automatically reverse DROP TABLE.
+ let downSQL = "";
+ if (columns && columns.length > 0) {
+ const createParams = {
+ schemaName,
+ tableName,
+ columns,
+ foreignKeys,
+ dbType,
+ };
+ const createMigration = generateCreateTableMigration(createParams);
+ downSQL = createMigration.upSQL;
+ } else {
+ downSQL = `-- WARNING: Cannot automatically reverse DROP TABLE.
-- You must manually recreate the table with all its data, constraints, and indexes.`;
+ }
return {
version,
@@ -305,9 +351,9 @@ ${migration.downSQL}
*/
function quoteIdent(
name: string,
- dbType: "mysql" | "postgres" | "mariadb" | "sqlite" | "duckdb",
+ dbType: "mysql" | "postgres" | "mariadb" | "sqlite" | "duckdb" | "clickhouse",
): string {
- if (dbType === "mysql" || dbType === "mariadb") {
+ if (dbType === "mysql" || dbType === "mariadb" || dbType === "clickhouse") {
return `\`${name.replace(/`/g, "``")}\``;
} else {
return `"${name.replace(/"/g, '""')}"`;
diff --git a/index.html b/index.html
index 84ec2709..fd38402a 100644
--- a/index.html
+++ b/index.html
@@ -11,6 +11,13 @@
href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;500&family=IBM+Plex+Sans:wght@400;500;600&display=swap"
rel="stylesheet"
/>
+
+
+
RelWave
diff --git a/package.json b/package.json
index f75a34ba..094ae54c 100644
--- a/package.json
+++ b/package.json
@@ -15,7 +15,8 @@
"format:check": "prettier . --check",
"bridge:copy-native": "npm --prefix bridge run copy:native",
"bridge:package": "node scripts/package-bridge.mjs",
- "seed:duckdb": "npm --prefix bridge run seed:duckdb"
+ "seed:duckdb": "npm --prefix bridge run seed:duckdb",
+ "seed:clickhouse": "npm --prefix bridge run seed:clickhouse"
},
"dependencies": {
"@aptabase/tauri": "github:aptabase/tauri-plugin-aptabase",
diff --git a/src/App.css b/src/App.css
index c794dc43..8fd61f04 100644
--- a/src/App.css
+++ b/src/App.css
@@ -205,239 +205,6 @@ body,
--ring: oklch(0.72 0.2 10);
}
-/* ── Full-palette theme: Cyberpunk ─────────────────────────────────────── */
-/* These serve as a cascade-level backup. The hook also injects inline styles
- on :root so they take precedence over .dark rules without needing specificity
- tricks. */
-[data-theme-variant="cyberpunk"],
-.dark[data-theme-variant="cyberpunk"] {
- --background: #070b14;
- --foreground: #e2f0ff;
- --card: #0d1526;
- --card-foreground: #e2f0ff;
- --popover: #0d1526;
- --popover-foreground: #e2f0ff;
- --primary: #00f5ff;
- --primary-foreground: #030c12;
- --secondary: #0f1e35;
- --secondary-foreground: #a8cfef;
- --muted: #0d1a2e;
- --muted-foreground: #6a8faf;
- --accent: #ff00c8;
- --accent-foreground: #fff;
- --destructive: #ff3366;
- --border: #0e2540;
- --input: #0a1a2e;
- --ring: #00f5ff;
- --sidebar: #060d1c;
- --sidebar-foreground: #cde8ff;
- --sidebar-primary: #00f5ff;
- --sidebar-primary-foreground: #030c12;
- --sidebar-accent: #0f2240;
- --sidebar-accent-foreground: #00f5ff;
- --sidebar-border: rgba(0, 245, 255, 0.08);
- --sidebar-ring: #00f5ff;
- --card-shadow: 0 0 0 1px rgba(0, 245, 255, 0.06), 0 8px 32px rgba(0, 0, 0, 0.6);
- --card-shadow-hover: 0 0 0 1px rgba(0, 245, 255, 0.14), 0 12px 44px rgba(0, 0, 0, 0.7);
- --glass-bg: rgba(7, 11, 20, 0.85);
- --surface: #0a1220;
- --surface-elevated: #0d1526;
- --surface-subtle: #0f1d32;
- --border-subtle: rgba(0, 245, 255, 0.07);
-}
-
-/* Cyberpunk neon glow on focused/primary interactive elements */
-[data-theme-variant="cyberpunk"]
- button[data-slot="button"]:not([data-variant="ghost"]):not(
- [data-variant="outline"]
- ):focus-visible,
-[data-theme-variant="cyberpunk"] [role="button"]:focus-visible {
- box-shadow:
- 0 0 0 2px #00f5ff,
- 0 0 12px rgba(0, 245, 255, 0.4);
-}
-
-/* ── Full-palette theme: VS Code ──────────────────────────────────────── */
-[data-theme-variant="vscode"],
-.dark[data-theme-variant="vscode"] {
- --background: #1e1e1e;
- --foreground: #d4d4d4;
- --card: #252526;
- --card-foreground: #d4d4d4;
- --popover: #2d2d30;
- --popover-foreground: #d4d4d4;
- --primary: #569cd6;
- --primary-foreground: #1e1e1e;
- --secondary: #2d2d30;
- --secondary-foreground: #cccccc;
- --muted: #333333;
- --muted-foreground: #808080;
- --accent: #264f78;
- --accent-foreground: #d4d4d4;
- --destructive: #f44747;
- --border: #3e3e42;
- --input: #3c3c3c;
- --ring: #569cd6;
- --sidebar: #252526;
- --sidebar-foreground: #bbbbbb;
- --sidebar-primary: #569cd6;
- --sidebar-primary-foreground: #1e1e1e;
- --sidebar-accent: #37373d;
- --sidebar-accent-foreground: #ffffff;
- --sidebar-border: #3e3e42;
- --sidebar-ring: #569cd6;
- --card-shadow: 0 1px 0 rgba(255, 255, 255, 0.04) inset, 0 8px 24px rgba(0, 0, 0, 0.4);
- --card-shadow-hover: 0 1px 0 rgba(255, 255, 255, 0.06) inset, 0 12px 32px rgba(0, 0, 0, 0.5);
- --glass-bg: rgba(37, 37, 38, 0.92);
- --surface: #252526;
- --surface-elevated: #2d2d30;
- --surface-subtle: #333333;
- --border-subtle: #3e3e42;
-}
-
-/* ── Full-palette theme: Valorant ────────────────────────────────────── */
-[data-theme-variant="valorant"] {
- --background: oklch(0.97 0.02 12.78);
- --foreground: oklch(0.24 0.07 17.81);
- --card: oklch(0.98 0.01 17.28);
- --card-foreground: oklch(0.26 0.07 19);
- --popover: oklch(0.98 0.01 17.28);
- --popover-foreground: oklch(0.26 0.07 19);
- --primary: oklch(0.67 0.22 21.34);
- --primary-foreground: oklch(0.99 0 359.99);
- --secondary: oklch(0.95 0.02 11.28);
- --secondary-foreground: oklch(0.24 0.07 17.81);
- --muted: oklch(0.98 0.01 17.28);
- --muted-foreground: oklch(0.55 0.08 19);
- --accent: oklch(0.99 0 359.99);
- --accent-foreground: oklch(0.43 0.13 20.62);
- --destructive: oklch(0.8 0.17 73.27);
- --border: oklch(0.91 0.05 11.4);
- --input: oklch(0.9 0.05 12.59);
- --ring: oklch(0.92 0.04 12.39);
- --sidebar: oklch(0.97 0.02 12.78);
- --sidebar-foreground: oklch(0.26 0.07 19);
- --sidebar-primary: oklch(0.67 0.22 21.34);
- --sidebar-primary-foreground: oklch(0.99 0 359.99);
- --sidebar-accent: oklch(0.98 0.01 17.28);
- --sidebar-accent-foreground: oklch(0.43 0.13 20.62);
- --sidebar-border: oklch(0.91 0.05 11.4);
- --sidebar-ring: oklch(0.92 0.04 12.39);
- --card-shadow:
- 0px 0px 3px 0px oklch(0.3 0.0891 19.6 / 0.08), 0px 2px 4px -1px oklch(0.3 0.0891 19.6 / 0.08);
- --glass-bg: rgba(253, 248, 247, 0.82);
- --surface: oklch(0.97 0.02 12.78);
- --surface-elevated: oklch(0.98 0.01 17.28);
- --surface-subtle: oklch(0.95 0.02 11.28);
- --border-subtle: oklch(0.91 0.05 11.4);
-}
-
-.dark[data-theme-variant="valorant"] {
- --background: oklch(0.16 0.03 17.48);
- --foreground: oklch(0.99 0 359.99);
- --card: oklch(0.21 0.05 19.26);
- --card-foreground: oklch(0.98 0 0);
- --popover: oklch(0.26 0.07 19);
- --popover-foreground: oklch(0.99 0 359.99);
- --primary: oklch(0.67 0.22 21.34);
- --primary-foreground: oklch(0.99 0 359.99);
- --secondary: oklch(0.3 0.0891 19.6);
- --secondary-foreground: oklch(0.95 0.02 11.28);
- --muted: oklch(0.26 0.07 19);
- --muted-foreground: oklch(0.72 0.04 18);
- --accent: oklch(0.43 0.13 20.62);
- --accent-foreground: oklch(0.99 0 359.99);
- --destructive: oklch(0.8 0.17 73.27);
- --border: oklch(0.31 0.09 19.8);
- --input: oklch(0.39 0.12 20.37);
- --ring: oklch(0.5 0.16 20.89);
- --sidebar: oklch(0.26 0.07 19);
- --sidebar-foreground: oklch(0.99 0 359.99);
- --sidebar-primary: oklch(0.67 0.22 21.34);
- --sidebar-primary-foreground: oklch(0.99 0 359.99);
- --sidebar-accent: oklch(0.43 0.13 20.62);
- --sidebar-accent-foreground: oklch(0.99 0 359.99);
- --sidebar-border: oklch(0.39 0.12 20.37);
- --sidebar-ring: oklch(0.5 0.16 20.89);
- --card-shadow: 0 1px 3px 0px oklch(0 0 0 / 0.1), 0 2px 4px -1px oklch(0 0 0 / 0.1);
- --glass-bg: rgba(33, 14, 12, 0.82);
- --surface: oklch(0.21 0.05 19.26);
- --surface-elevated: oklch(0.26 0.07 19);
- --surface-subtle: oklch(0.3 0.0891 19.6);
- --border-subtle: oklch(0.31 0.09 19.8);
-}
-
-/* ── Full-palette theme: Ghibli Studio ──────────────────────────────── */
-[data-theme-variant="ghibli"] {
- --background: oklch(0.91 0.05 82.69);
- --foreground: oklch(0.41 0.08 79.04);
- --card: oklch(0.92 0.04 83.86);
- --card-foreground: oklch(0.41 0.08 73.75);
- --popover: oklch(0.92 0.04 83.86);
- --popover-foreground: oklch(0.41 0.08 73.75);
- --primary: oklch(0.71 0.1 111.99);
- --primary-foreground: oklch(0.98 0.01 3.71);
- --secondary: oklch(0.88 0.05 83.41);
- --secondary-foreground: oklch(0.51 0.08 79.21);
- --muted: oklch(0.86 0.06 83.48);
- --muted-foreground: oklch(0.51 0.08 74.26);
- --accent: oklch(0.86 0.05 84.5);
- --accent-foreground: oklch(0.26 0.02 358.42);
- --destructive: oklch(0.63 0.24 29.21);
- --border: oklch(0.74 0.06 79.81);
- --input: oklch(0.74 0.06 79.81);
- --ring: oklch(0.51 0.08 74.26);
- --sidebar: oklch(0.87 0.06 83.96);
- --sidebar-foreground: oklch(0.41 0.08 79.04);
- --sidebar-primary: oklch(0.26 0.02 358.42);
- --sidebar-primary-foreground: oklch(0.98 0.01 3.71);
- --sidebar-accent: oklch(0.83 0.06 84.46);
- --sidebar-accent-foreground: oklch(0.26 0.02 358.42);
- --sidebar-border: oklch(0.91 0 0.43);
- --sidebar-ring: oklch(0.71 0 0.37);
- --card-shadow: 0 1px 3px 0px oklch(0 0 0 / 0.08), 0 2px 4px -1px oklch(0 0 0 / 0.08);
- --glass-bg: rgba(244, 237, 216, 0.8);
- --surface: oklch(0.91 0.05 82.69);
- --surface-elevated: oklch(0.92 0.04 83.86);
- --surface-subtle: oklch(0.88 0.05 83.41);
- --border-subtle: oklch(0.74 0.06 79.81);
-}
-
-.dark[data-theme-variant="ghibli"] {
- --background: oklch(0.2 0.01 48.35);
- --foreground: oklch(0.88 0.05 79.26);
- --card: oklch(0.25 0.01 56.14);
- --card-foreground: oklch(0.88 0.05 79.26);
- --popover: oklch(0.25 0.01 56.14);
- --popover-foreground: oklch(0.88 0.05 79.26);
- --primary: oklch(0.64 0.05 115.39);
- --primary-foreground: oklch(0.98 0.01 3.71);
- --secondary: oklch(0.33 0.02 60.7);
- --secondary-foreground: oklch(0.88 0.05 83.41);
- --muted: oklch(0.27 0.01 39.35);
- --muted-foreground: oklch(0.74 0.06 79.81);
- --accent: oklch(0.33 0.02 60.7);
- --accent-foreground: oklch(0.86 0.05 84.5);
- --destructive: oklch(0.63 0.24 29.21);
- --border: oklch(0.33 0.02 60.7);
- --input: oklch(0.33 0.02 60.7);
- --ring: oklch(0.64 0.05 115.39);
- --sidebar: oklch(0.23 0.01 56.09);
- --sidebar-foreground: oklch(0.88 0.05 79.26);
- --sidebar-primary: oklch(0.64 0.05 115.39);
- --sidebar-primary-foreground: oklch(0.98 0.01 3.71);
- --sidebar-accent: oklch(0.33 0.02 60.7);
- --sidebar-accent-foreground: oklch(0.86 0.05 84.5);
- --sidebar-border: oklch(0.33 0.02 60.7);
- --sidebar-ring: oklch(0.64 0.05 115.39);
- --card-shadow: 0 1px 3px 0px oklch(0 0 0 / 0.2), 0 2px 4px -1px oklch(0 0 0 / 0.2);
- --glass-bg: rgba(33, 25, 14, 0.82);
- --surface: oklch(0.25 0.01 56.14);
- --surface-elevated: oklch(0.27 0.01 39.35);
- --surface-subtle: oklch(0.33 0.02 60.7);
- --border-subtle: oklch(0.33 0.02 60.7);
-}
-
@layer base {
* {
@apply border-border outline-ring/50;
diff --git a/src/components/layout/CommandPalette.tsx b/src/components/layout/CommandPalette.tsx
index 50453a05..187845b5 100644
--- a/src/components/layout/CommandPalette.tsx
+++ b/src/components/layout/CommandPalette.tsx
@@ -21,7 +21,6 @@ import * as React from "react";
import { useNavigate, useLocation } from "react-router-dom";
import {
Settings,
- Database,
Table as TableIcon,
Terminal,
Home,
@@ -41,6 +40,8 @@ import { useDatabases, useTables } from "@/features/project/hooks/useDbQueries";
import { useProjects } from "@/features/project/hooks/useProjectQueries";
import { useTheme } from "@/components/providers/ThemeProvider";
import { cn } from "@/lib/utils";
+import { SettingsDialog } from "@/features/settings/components";
+import { DatabaseLogo } from "@/lib/databases";
// ─── Types ────────────────────────────────────────────────────────────────────
@@ -51,6 +52,7 @@ interface RecentItem {
type: ItemType;
label: string;
path?: string;
+ dbType?: string;
timestamp: number;
}
@@ -173,6 +175,7 @@ function HighlightMatch({ text, query }: { text: string; query: string }) {
export function CommandPalette() {
const [open, setOpen] = React.useState(false);
+ const [settingsOpen, setSettingsOpen] = React.useState(false);
const [search, setSearch] = React.useState("");
const navigate = useNavigate();
const location = useLocation();
@@ -253,12 +256,16 @@ export function CommandPalette() {
const isSearching = search.trim().length > 0;
const showRecent = !isSearching && recentItems.length > 0;
- const contextLabel = React.useMemo(() => {
+ const activeDatabase = React.useMemo(() => {
if (!dbId) return null;
- const db = databases.find((d) => d.id === dbId);
- return db ? db.name : dbId;
+ return databases.find((d) => d.id === dbId);
}, [dbId, databases]);
+ const contextLabel = React.useMemo(() => {
+ if (!dbId) return null;
+ return activeDatabase ? activeDatabase.name : dbId;
+ }, [dbId, activeDatabase]);
+
// ── Render ──────────────────────────────────────────────────────────────────
return (
@@ -291,11 +298,11 @@ export function CommandPalette() {
{contextLabel && !isSearching && (
-
+
{contextLabel}
)}
@@ -332,25 +339,33 @@ export function CommandPalette() {
/>
}
>
- {recentItems.map((item) => (
- {
+ const itemDbType = item.dbType || databases.find((d) => d.id === item.id)?.type;
+ return (
+
+ ) : undefined
+ }
+ icon={
+ item.type === "project"
? FolderOpen
- : Home
- }
- label={item.label}
- type={item.type}
- query={search}
- onSelect={() => {
- if (item.path) runCommand(() => navigate(item.path!), item);
- }}
- />
- ))}
+ : item.type === "page"
+ ? Home
+ : undefined
+ }
+ label={item.label}
+ type={item.type}
+ query={search}
+ onSelect={() => {
+ if (item.path) runCommand(() => navigate(item.path!), item);
+ }}
+ />
+ );
+ })}
)}
@@ -363,14 +378,23 @@ export function CommandPalette() {
label={page.label}
type="page"
query={search}
- onSelect={() =>
- runCommand(() => navigate(page.path), {
- id: page.path,
- type: "page",
- label: page.label,
- path: page.path,
- })
- }
+ onSelect={() => {
+ if (page.path === "/settings") {
+ runCommand(() => setSettingsOpen(true), {
+ id: page.path,
+ type: "page",
+ label: page.label,
+ path: page.path,
+ });
+ } else {
+ runCommand(() => navigate(page.path), {
+ id: page.path,
+ type: "page",
+ label: page.label,
+ path: page.path,
+ });
+ }
+ }}
/>
))}
@@ -381,17 +405,18 @@ export function CommandPalette() {
{databases.map((db) => (
}
label={db.name}
type="connection"
query={search}
- meta={`${db.type} · ${db.host}`}
+ meta={`${db.type} · ${db.host || (db as any).database || ""}`}
onSelect={() =>
runCommand(() => navigate(`/${db.id}`), {
id: db.id,
type: "connection",
label: db.name,
path: `/${db.id}`,
+ dbType: db.type,
})
}
/>
@@ -517,6 +542,7 @@ export function CommandPalette() {
+
);
}
@@ -561,7 +587,8 @@ function GroupHeading({
// ─── CommandItem ──────────────────────────────────────────────────────────────
interface CommandItemProps {
- icon: React.ElementType;
+ icon?: React.ElementType;
+ iconNode?: React.ReactNode;
label: string;
type: ItemType;
query: string;
@@ -577,6 +604,7 @@ interface CommandItemProps {
function CommandItem({
icon: Icon,
+ iconNode,
label,
type,
query,
@@ -600,10 +628,12 @@ function CommandItem({
-
+ {iconNode ? iconNode : Icon ? : null}
{/* Label — IBM Plex Sans 450 (between regular and medium) */}
diff --git a/src/components/layout/TitleBar.tsx b/src/components/layout/TitleBar.tsx
index 79261285..bd73b983 100644
--- a/src/components/layout/TitleBar.tsx
+++ b/src/components/layout/TitleBar.tsx
@@ -1,8 +1,15 @@
import { Maximize2, Minus, X } from "lucide-react";
import { getCurrentWindow } from "@tauri-apps/api/window";
+import { useLocation, Link } from "react-router-dom";
import BridgeStatus from "./BridgeStatus";
+import { useDatabase } from "@/features/project/hooks/useDbQueries";
+import { DatabaseLogo } from "@/lib/databases";
const TitleBar = () => {
+ const location = useLocation();
+ const dbId = location.pathname !== "/" ? location.pathname.split("/")[1] : undefined;
+ const { data: database } = useDatabase(dbId);
+
const handleMinimize = async () => {
try {
const appWindow = getCurrentWindow();
@@ -44,7 +51,21 @@ const TitleBar = () => {
{/* App Title - Left */}
-
RelWave
+
+ RelWave
+
+ {database && (
+
+
/
+
+
+ {database.name}
+
+
+ )}
{/* Bridge Status - Center */}
diff --git a/src/components/layout/VerticalIconBar.tsx b/src/components/layout/VerticalIconBar.tsx
index 30f2b106..84ac2aed 100644
--- a/src/components/layout/VerticalIconBar.tsx
+++ b/src/components/layout/VerticalIconBar.tsx
@@ -1,3 +1,4 @@
+import { useState } from "react";
import {
Activity,
Home,
@@ -9,10 +10,12 @@ import {
Terminal,
History,
Sparkles,
+ Settings,
} from "lucide-react";
import { Link, useLocation } from "react-router-dom";
import { Button } from "@/components/ui/button";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
+import { SettingsDialog } from "@/features/settings/components";
export type PanelType =
| "data"
@@ -36,7 +39,33 @@ const globalNavigationItems = [{ icon: Home, label: "Dashboard", path: "/" }];
function supportsMonitoring(databaseType?: string) {
const type = databaseType?.toLowerCase();
- return type === "postgres" || type === "postgresql" || type === "mysql" || type === "mariadb";
+ return (
+ type === "postgres" ||
+ type === "postgresql" ||
+ type === "mysql" ||
+ type === "mariadb" ||
+ type === "clickhouse"
+ );
+}
+
+export function getDatabasePanelItems(databaseType?: string): Array<{
+ icon: any;
+ label: string;
+ panel: PanelType;
+}> {
+ return [
+ { icon: Layers, label: "Data View", panel: "data" },
+ { icon: Terminal, label: "SQL Workspace", panel: "sql-workspace" },
+ { icon: Sparkles, label: "AI Workspace", panel: "ai-workspace" },
+ { icon: Search, label: "Query Builder", panel: "query-builder" },
+ { icon: GitBranch, label: "Schema Explorer", panel: "schema-explorer" },
+ { icon: Database, label: "ER Diagram", panel: "er-diagram" },
+ ...(supportsMonitoring(databaseType)
+ ? [{ icon: Activity, label: "Monitoring", panel: "monitoring" as PanelType }]
+ : []),
+ { icon: History, label: "Migrations", panel: "migrations" },
+ { icon: GitCommitHorizontal, label: "Git Status", panel: "git-status" },
+ ];
}
export default function VerticalIconBar({
@@ -46,6 +75,7 @@ export default function VerticalIconBar({
onPanelChange,
}: VerticalIconBarProps) {
const location = useLocation();
+ const [settingsOpen, setSettingsOpen] = useState(false);
const isGlobalActive = (path: string) => {
if (path === "/") {
@@ -55,21 +85,7 @@ export default function VerticalIconBar({
};
// Database-specific panel items (only shown when dbId is provided)
- const databasePanelItems: Array<{ icon: any; label: string; panel: PanelType }> = dbId
- ? [
- { icon: Layers, label: "Data View", panel: "data" },
- { icon: Terminal, label: "SQL Workspace", panel: "sql-workspace" },
- { icon: Sparkles, label: "AI Workspace", panel: "ai-workspace" },
- { icon: Search, label: "Query Builder", panel: "query-builder" },
- { icon: GitBranch, label: "Schema Explorer", panel: "schema-explorer" },
- { icon: Database, label: "ER Diagram", panel: "er-diagram" },
- ...(supportsMonitoring(databaseType)
- ? [{ icon: Activity, label: "Monitoring", panel: "monitoring" as PanelType }]
- : []),
- { icon: History, label: "Migrations", panel: "migrations" },
- { icon: GitCommitHorizontal, label: "Git Status", panel: "git-status" },
- ]
- : [];
+ const databasePanelItems = dbId ? getDatabasePanelItems(databaseType) : [];
return (
@@ -112,9 +128,10 @@ export default function VerticalIconBar({
<>
- {databasePanelItems.map((item) => {
+ {databasePanelItems.map((item, index) => {
const Icon = item.icon;
const active = activePanel === item.panel;
+ const shortcut = index < 9 ? `Alt+${index + 1}` : undefined;
return (
@@ -135,8 +152,13 @@ export default function VerticalIconBar({
-
- {item.label}
+
+ {item.label}
+ {shortcut && (
+
+ {shortcut}
+
+ )}
);
@@ -144,6 +166,27 @@ export default function VerticalIconBar({
>
)}
+
+ {/* Bottom Actions */}
+
+
+
+ setSettingsOpen(true)}
+ className="w-10 h-10 rounded-lg text-sidebar-foreground/62 hover:bg-sidebar-accent hover:text-sidebar-accent-foreground transition-all duration-150"
+ >
+
+
+
+
+ Settings
+
+
+
+
+
);
}
diff --git a/src/components/shared/DataTable.tsx b/src/components/shared/DataTable.tsx
index fec24a20..9be2e3e6 100644
--- a/src/components/shared/DataTable.tsx
+++ b/src/components/shared/DataTable.tsx
@@ -1,3 +1,4 @@
+import { useState } from "react";
import {
Table,
TableBody,
@@ -7,26 +8,80 @@ import {
TableRow,
} from "@/components/ui/table";
import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area";
-import { Database, Pencil, Trash2 } from "lucide-react";
+import {
+ Database,
+ Pencil,
+ Trash2,
+ ArrowUp,
+ ArrowDown,
+ ArrowUpDown,
+ Copy,
+ Check,
+} from "lucide-react";
import { Button } from "@/components/ui/button";
import { formatTimestamp } from "@/lib/utils";
+import { toast } from "sonner";
+
+export interface DataTableColumnDef {
+ name: string;
+ type?: string;
+}
interface DataTableProps {
data: Array>;
+ columns?: Array;
maxHeight?: string;
showRowNumbers?: boolean;
onEditRow?: (row: Record) => void;
onDeleteRow?: (row: Record) => void;
+ emptyMessage?: string;
+ sortColumn?: string;
+ sortDirection?: "asc" | "desc";
+ onSort?: (column: string) => void;
}
export const DataTable = ({
data,
+ columns: propColumns,
maxHeight = "100%",
showRowNumbers = true,
onEditRow,
onDeleteRow,
+ emptyMessage,
+ sortColumn,
+ sortDirection,
+ onSort,
}: DataTableProps) => {
- if (!data || data.length === 0) {
+ const [copiedCellKey, setCopiedCellKey] = useState(null);
+
+ // Normalize columns from either props or first row of data
+ const columns: DataTableColumnDef[] = (() => {
+ if (propColumns && propColumns.length > 0) {
+ return propColumns.map((col) => (typeof col === "string" ? { name: col } : col));
+ }
+ if (data && data.length > 0) {
+ return Object.keys(data[0]).map((name) => ({ name }));
+ }
+ return [];
+ })();
+
+ const isEmpty = !data || data.length === 0;
+
+ const handleCellCopy = (value: any, key: string, e: React.MouseEvent) => {
+ e.stopPropagation();
+ if (value === null || value === undefined) return;
+ const str = typeof value === "object" ? JSON.stringify(value, null, 2) : String(value);
+ navigator.clipboard.writeText(str);
+ setCopiedCellKey(key);
+ const preview = str.length > 25 ? `${str.slice(0, 25)}...` : str;
+ toast.success(`Copied: ${preview}`, { duration: 1500 });
+ setTimeout(() => {
+ setCopiedCellKey((curr) => (curr === key ? null : curr));
+ }, 1500);
+ };
+
+ // If there is no data AND no columns defined, show generic placeholder
+ if (isEmpty && columns.length === 0) {
return (
@@ -34,93 +89,165 @@ export const DataTable = ({
No results found
- This table is empty or the query returned no data
+ {emptyMessage || "This table is empty or the query returned no data"}
);
}
- const columns = Object.keys(data[0]);
+ const columnNames = columns.map((col) => col.name);
const hasRowActions = onEditRow || onDeleteRow;
+ const colSpan = columnNames.length + (showRowNumbers ? 1 : 0) + (hasRowActions ? 1 : 0);
const tableContent = (
{showRowNumbers && (
-
+
#
)}
- {columns.map((column) => (
-
- {column}
-
- ))}
+ {columns.map((column) => {
+ const isSorted = sortColumn === column.name;
+ return (
+
+ onSort?.(column.name)}
+ >
+
{column.name}
+ {column.type && (
+
+ {column.type}
+
+ )}
+ {onSort && (
+
+ {isSorted ? (
+ sortDirection === "asc" ? (
+
+ ) : (
+
+ )
+ ) : (
+
+ )}
+
+ )}
+
+
+ );
+ })}
{hasRowActions && (
-
+
Actions
)}
- {data.map((row, index) => (
-
- {showRowNumbers && (
-
- {index + 1}
-
- )}
- {columns.map((column) => (
-
- {row[column] !== null && row[column] !== undefined ? (
- formatCellValue(row[column], column)
- ) : (
- null
- )}
-
- ))}
- {hasRowActions && (
-
-
- {onEditRow && (
-
onEditRow(row)}
- title="Edit row"
- >
-
-
- )}
- {onDeleteRow && (
-
onDeleteRow(row)}
- title="Delete row"
- >
-
-
- )}
-
-
- )}
+ {isEmpty ? (
+
+
+
+
+ Table is empty
+
+ 0 rows • Defined with {columns.length} columns ({columnNames.join(", ")})
+
+
+
- ))}
+ ) : (
+ data.map((row, index) => (
+
+ {showRowNumbers && (
+
+ {index + 1}
+
+ )}
+ {columnNames.map((column) => {
+ const cellKey = `${index}-${column}`;
+ const val = row[column];
+ const isCopied = copiedCellKey === cellKey;
+ const hasValue = val !== null && val !== undefined;
+
+ return (
+
+
+
{formatCellValue(val, column)}
+ {hasValue && (
+
handleCellCopy(val, cellKey, e)}
+ className="opacity-0 group-hover/cell:opacity-100 p-1 rounded hover:bg-muted/80 text-muted-foreground hover:text-foreground transition-all shrink-0 ml-1"
+ title="Copy cell value"
+ >
+ {isCopied ? (
+
+ ) : (
+
+ )}
+
+ )}
+
+
+ );
+ })}
+ {hasRowActions && (
+
+
+ {onEditRow && (
+
onEditRow(row)}
+ title="Edit row"
+ >
+
+
+ )}
+ {onDeleteRow && (
+
onDeleteRow(row)}
+ title="Delete row"
+ >
+
+
+ )}
+
+
+ )}
+
+ ))
+ )}
);
@@ -149,16 +276,20 @@ export const DataTable = ({
// Helper function to format cell values with improved styling
function formatCellValue(value: any, columnName?: string): React.ReactNode {
if (value === null || value === undefined) {
- return null ;
+ return (
+
+ NULL
+
+ );
}
if (typeof value === "boolean") {
return (
{value.toString()}
@@ -174,21 +305,21 @@ function formatCellValue(value: any, columnName?: string): React.ReactNode {
if (value > 1e9 && value < 1e10) {
// Seconds
return (
-
+
{formatTimestamp(new Date(value * 1000).toISOString())}
);
} else if (value > 1e12 && value < 1e13) {
// Milliseconds
return (
-
+
{formatTimestamp(new Date(value).toISOString())}
);
}
}
return (
-
+
{value.toLocaleString()}
);
@@ -196,7 +327,7 @@ function formatCellValue(value: any, columnName?: string): React.ReactNode {
if (value instanceof Date) {
return (
-
+
{formatTimestamp(value.toISOString())}
);
@@ -206,22 +337,32 @@ function formatCellValue(value: any, columnName?: string): React.ReactNode {
try {
const jsonStr = JSON.stringify(value);
return (
-
+
{jsonStr.slice(0, 80)}
{jsonStr.length > 80 ? "…" : ""}
);
} catch {
- return [Object] ;
+ return [Object] ;
}
}
const strValue = String(value);
+ if (strValue === "") {
+ return (
+
+ <empty>
+
+ );
+ }
+
// Check if it looks like a date string
if (typeof strValue === "string" && /^\d{4}-\d{2}-\d{2}/.test(strValue)) {
return (
- {formatTimestamp(strValue)}
+
+ {formatTimestamp(strValue)}
+
);
}
@@ -231,14 +372,14 @@ function formatCellValue(value: any, columnName?: string): React.ReactNode {
// 10 digits (seconds) or 13 digits (ms)
if (/^\d{10}$/.test(strValue)) {
return (
-
+
{formatTimestamp(new Date(Number(strValue) * 1000).toISOString())}
);
}
if (/^\d{13}$/.test(strValue)) {
return (
-
+
{formatTimestamp(new Date(Number(strValue)).toISOString())}
);
@@ -247,17 +388,17 @@ function formatCellValue(value: any, columnName?: string): React.ReactNode {
// Check if it looks like an ID or UUID
if (/^[a-f0-9-]{36}$/i.test(strValue)) {
- return {strValue} ;
+ return {strValue} ;
}
if (strValue.length > 80) {
return (
-
+
{strValue.slice(0, 80)}
- …
+ …
);
}
- return {strValue} ;
+ return {strValue} ;
}
diff --git a/src/components/shared/ShortcutsHelp.tsx b/src/components/shared/ShortcutsHelp.tsx
index fa642d0c..317c5117 100644
--- a/src/components/shared/ShortcutsHelp.tsx
+++ b/src/components/shared/ShortcutsHelp.tsx
@@ -30,7 +30,7 @@ const shortcutGroups: ShortcutGroup[] = [
{
title: "Database Detail",
shortcuts: [
- { keys: ["Ctrl", "Enter"], label: "Execute SQL Query" },
+ { keys: ["Ctrl", "Enter"], label: "Execute Query / Selection" },
{ keys: ["Ctrl", "S"], label: "Save Query / Changes" },
{ keys: ["Ctrl", "F"], label: "Find in Data / SQL" },
],
@@ -38,9 +38,10 @@ const shortcutGroups: ShortcutGroup[] = [
{
title: "Navigation",
shortcuts: [
- { keys: ["Alt", "1"], label: "Go to Dashboard" },
- { keys: ["Alt", "2"], label: "Go to Projects" },
- { keys: ["Alt", "3"], label: "Go to Settings" },
+ { keys: ["Alt", "1"], label: "Data View" },
+ { keys: ["Alt", "2"], label: "SQL Workspace" },
+ { keys: ["Alt", "3"], label: "AI Workspace" },
+ { keys: ["Alt", "4..9"], label: "Panels (Query, Schema, ER...)" },
],
},
];
diff --git a/src/features/ai/hooks/useAISettings.ts b/src/features/ai/hooks/useAISettings.ts
index bf8c8b02..abbf870c 100644
--- a/src/features/ai/hooks/useAISettings.ts
+++ b/src/features/ai/hooks/useAISettings.ts
@@ -1,30 +1,39 @@
-import { loadAISettings, type AISettings } from "@/services/bridge/ai";
-import { useEffect, useState } from "react";
+import { loadAISettings, getCachedAISettings, type AISettings } from "@/services/bridge/ai";
+import { useEffect, useState, useCallback } from "react";
const DEFAULT: AISettings = { defaultProvider: "openai" };
/**
- * Reads AISettings from the bridge (ai-settings.json on disk).
- * Returns a stable object that is refreshed whenever the settings dialog saves.
- *
- * Because saving goes through the bridge and NOT localStorage, the old
- * StorageEvent trick no longer applies. Instead, callers that need fresh
- * settings after a save should re-mount or call `reload()`.
+ * Reads AISettings from the bridge (ai-settings.json on disk) with instant memory cache.
*/
export function useAISettings(): { settings: AISettings; isLoading: boolean; reload: () => void } {
- const [settings, setSettings] = useState(DEFAULT);
- const [isLoading, setIsLoading] = useState(true);
+ const cached = getCachedAISettings();
+ const [settings, setSettings] = useState(cached ?? DEFAULT);
+ const [isLoading, setIsLoading] = useState(!cached);
const [tick, setTick] = useState(0);
useEffect(() => {
- setIsLoading(true);
+ let active = true;
+ if (!cached) {
+ setIsLoading(true);
+ }
loadAISettings().then((loaded) => {
- setSettings(loaded);
- setIsLoading(false);
+ if (active) {
+ setSettings(loaded);
+ setIsLoading(false);
+ }
});
- }, [tick]);
+ return () => {
+ active = false;
+ };
+ }, [tick, cached]);
- const reload = () => setTick((t) => t + 1);
+ const reload = useCallback(() => {
+ loadAISettings({ forceRefresh: true }).then((loaded) => {
+ setSettings(loaded);
+ });
+ setTick((t) => t + 1);
+ }, []);
return { settings, isLoading, reload };
}
diff --git a/src/features/database/components/ContentViewerPanel.tsx b/src/features/database/components/ContentViewerPanel.tsx
index fb6d0d04..0b7c68e1 100644
--- a/src/features/database/components/ContentViewerPanel.tsx
+++ b/src/features/database/components/ContentViewerPanel.tsx
@@ -1,12 +1,15 @@
-import { RefreshCw, Plus, TrendingUp, Search, X, Loader2, Table } from "lucide-react";
+import { useState, useMemo } from "react";
+import { RefreshCw, Plus, TrendingUp, Search, X, Loader2, Table, Copy } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { DataTable } from "@/components/shared/DataTable";
import { TableRow } from "@/features/database/types";
+import { toast } from "sonner";
interface ContentViewerPanelProps {
selectedTable: string | null;
tableData: TableRow[];
+ tableColumns?: Array<{ name: string; type?: string } | string>;
totalRows: number;
currentPage: number;
pageSize: number;
@@ -28,6 +31,7 @@ interface ContentViewerPanelProps {
export default function ContentViewerPanel({
selectedTable,
tableData,
+ tableColumns,
totalRows,
currentPage,
pageSize,
@@ -45,8 +49,47 @@ export default function ContentViewerPanel({
isSearching,
searchResultCount,
}: ContentViewerPanelProps) {
+ const [sortColumn, setSortColumn] = useState();
+ const [sortDirection, setSortDirection] = useState<"asc" | "desc">("asc");
+
const totalPages = Math.ceil(totalRows / pageSize);
+ const handleSort = (column: string) => {
+ if (sortColumn === column) {
+ if (sortDirection === "asc") {
+ setSortDirection("desc");
+ } else {
+ setSortColumn(undefined);
+ setSortDirection("asc");
+ }
+ } else {
+ setSortColumn(column);
+ setSortDirection("asc");
+ }
+ };
+
+ const sortedData = useMemo(() => {
+ if (!sortColumn || !tableData || tableData.length === 0) return tableData;
+ return [...tableData].sort((a, b) => {
+ const valA = a[sortColumn];
+ const valB = b[sortColumn];
+ if (valA === valB) return 0;
+ if (valA === null || valA === undefined) return 1;
+ if (valB === null || valB === undefined) return -1;
+ if (typeof valA === "number" && typeof valB === "number") {
+ return sortDirection === "asc" ? valA - valB : valB - valA;
+ }
+ const cmp = String(valA).localeCompare(String(valB));
+ return sortDirection === "asc" ? cmp : -cmp;
+ });
+ }, [tableData, sortColumn, sortDirection]);
+
+ const handleCopyTableJSON = () => {
+ if (!tableData || tableData.length === 0) return;
+ navigator.clipboard.writeText(JSON.stringify(sortedData, null, 2));
+ toast.success(`Copied ${sortedData.length} rows as JSON`, { duration: 1500 });
+ };
+
if (!selectedTable) {
return (
@@ -67,9 +110,28 @@ export default function ContentViewerPanel({
{selectedTable}
-
{totalRows.toLocaleString()} rows
+
+ {totalRows.toLocaleString()} rows
+ {sortColumn && (
+
+ (sorted by {sortColumn} {sortDirection})
+
+ )}
+
+ {tableData && tableData.length > 0 && (
+
+
+ Copy JSON
+
+ )}
{onInsert && (
@@ -82,7 +144,13 @@ export default function ContentViewerPanel({
Chart
)}
-
+
@@ -99,7 +167,7 @@ export default function ContentViewerPanel({
value={searchTerm || ""}
onChange={(e) => onSearchChange(e.target.value)}
onKeyDown={(e) => e.key === "Enter" && onSearch?.()}
- className="pl-8 pr-8 h-8 text-xs bg-background/70"
+ className="pl-8 pr-8 h-8 text-xs bg-background/70 font-mono"
/>
{searchTerm && (
{typeof searchResultCount === "number" && searchTerm && (
- {searchResultCount} found
+
+ {searchResultCount} found
+
)}
)}
@@ -132,15 +202,19 @@ export default function ContentViewerPanel({
- ) : tableData.length === 0 ? (
+ ) : tableData.length === 0 && (!tableColumns || tableColumns.length === 0) ? (
) : (
diff --git a/src/features/database/components/DataViewPanel.tsx b/src/features/database/components/DataViewPanel.tsx
index 573f2232..3aebdcd0 100644
--- a/src/features/database/components/DataViewPanel.tsx
+++ b/src/features/database/components/DataViewPanel.tsx
@@ -33,6 +33,7 @@ interface DataViewPanelProps {
// Data
tableData: any[];
+ tableColumns?: Array<{ name: string; type?: string } | string>;
totalRows: number;
currentPage: number;
pageSize: number;
@@ -80,6 +81,7 @@ export const DataViewPanel = ({
selectedSchema,
setSelectedSchema,
tableData,
+ tableColumns,
totalRows,
currentPage,
pageSize,
@@ -222,6 +224,7 @@ export const DataViewPanel = ({
void;
handleTableSelect: (tableName: string, schemaName: string) => Promise;
- handleExecuteQuery: () => Promise;
+ handleExecuteQuery: (sqlOverride?: string) => Promise;
handleCancelQuery: () => Promise;
fetchTables: () => Promise;
handlePageChange: (page: number) => Promise;
@@ -105,6 +114,15 @@ export function useDatabaseDetails({
// Cache invalidation
const { invalidateTable, invalidateDatabase } = useInvalidateCache();
+ // Full schema for column metadata fallback
+ const { data: fullSchema } = useFullSchema(dbId);
+ const selectedTableColumns = useMemo(() => {
+ if (!fullSchema?.schemas || !selectedTable) return [];
+ const schema = fullSchema.schemas.find((s) => s.name === selectedTable.schema);
+ const table = schema?.tables?.find((t) => t.name === selectedTable.name);
+ return table?.columns || [];
+ }, [fullSchema, selectedTable]);
+
// Derived state from table data query
const tableData = tableDataResult?.rows || [];
const rowCount = tableData.length;
@@ -118,9 +136,13 @@ export function useDatabaseDetails({
const [queryResults, setQueryResults] = useState([]);
const [queryRowCount, setQueryRowCount] = useState(0);
const [queryError, setQueryError] = useState(null);
+ const [executionTime, setExecutionTime] = useState(null);
const [hasExecutedQuery, setHasExecutedQuery] = useState(false);
const [error, _setError] = useState(null);
+ const querySessionIdRef = useRef(null);
+ const lastExecutedSqlRef = useRef("");
+
// Combined loading state
const loading = loadingTables || isLoadingTableData;
@@ -138,6 +160,7 @@ export function useDatabaseDetails({
setQueryResults([]);
setQueryRowCount(0);
setQueryError(null);
+ setExecutionTime(null);
// React Query will automatically fetch the data
},
[dbId, selectedTable, pageSize],
@@ -174,10 +197,11 @@ export function useDatabaseDetails({
}, [dbId, selectedTable, invalidateTable]);
const handleCancelQuery = useCallback(async () => {
- if (!querySessionId) return;
+ const activeSession = querySessionIdRef.current || querySessionId;
+ if (!activeSession) return;
try {
- const cancelled = await sessionService.cancelSession(querySessionId);
+ const cancelled = await sessionService.cancelSession(activeSession);
if (cancelled) {
toast.info("Cancelling query...", { description: "Stopping query execution" });
}
@@ -187,56 +211,67 @@ export function useDatabaseDetails({
}
}, [querySessionId]);
- const handleExecuteQuery = useCallback(async () => {
- if (!dbId || !query.trim()) {
- toast.error("Invalid query", { description: "Please enter a SQL query to execute" });
- return;
- }
-
- try {
- if (querySessionId) {
- toast.warning("Query already running", { description: "Cancelling previous query first." });
- await handleCancelQuery();
+ const handleExecuteQuery = useCallback(
+ async (sqlOverride?: string) => {
+ const sqlToRun = (sqlOverride !== undefined ? sqlOverride : query).trim();
+ if (!dbId || !sqlToRun) {
+ toast.error("Invalid query", { description: "Please enter a SQL query to execute" });
+ return;
}
- setQueryResults([]);
- setQueryRowCount(0);
- setQueryProgress(null);
- setQueryError(null);
- setHasExecutedQuery(true);
- setIsExecuting(true);
- analyticsService.trackQueryExecuted();
-
- const sessionId = await sessionService.createSession();
- setQuerySessionId(sessionId);
-
- toast.info("Executing query...", { description: "Query started, receiving results..." });
-
- await queryService.runQuery({
- sessionId,
- dbId,
- sql: query,
- batchSize: 1000,
- });
- } catch (err: any) {
- console.error("Error executing query:", err);
- setIsExecuting(false);
- setQuerySessionId(null);
- setQueryProgress(null);
- toast.error("Query execution failed", { description: err.message });
- }
- }, [dbId, query, querySessionId, handleCancelQuery]);
+ try {
+ if (querySessionIdRef.current) {
+ toast.warning("Query already running", {
+ description: "Cancelling previous query first.",
+ });
+ await handleCancelQuery();
+ }
+
+ setQueryResults([]);
+ setQueryRowCount(0);
+ setQueryProgress(null);
+ setQueryError(null);
+ setExecutionTime(null);
+ setHasExecutedQuery(true);
+ setIsExecuting(true);
+ analyticsService.trackQueryExecuted();
+
+ const sessionId = await sessionService.createSession();
+ querySessionIdRef.current = sessionId;
+ lastExecutedSqlRef.current = sqlToRun;
+ setQuerySessionId(sessionId);
+
+ toast.info("Executing query...", { description: "Query started, receiving results..." });
+
+ await queryService.runQuery({
+ sessionId,
+ dbId,
+ sql: sqlToRun,
+ batchSize: 1000,
+ });
+ } catch (err: any) {
+ console.error("Error executing query:", err);
+ querySessionIdRef.current = null;
+ setIsExecuting(false);
+ setQuerySessionId(null);
+ setQueryProgress(null);
+ setQueryError(err.message || String(err));
+ toast.error("Query execution failed", { description: err.message });
+ }
+ },
+ [dbId, query, handleCancelQuery],
+ );
// Setup query result listeners
useEffect(() => {
const handleResult = (event: CustomEvent) => {
- if (event.detail.sessionId !== querySessionId) return;
+ if (event.detail.sessionId !== querySessionIdRef.current) return;
setQueryResults((prev) => [...prev, ...event.detail.rows]);
setQueryRowCount((prev) => prev + event.detail.rows.length);
};
const handleProgress = (event: CustomEvent) => {
- if (event.detail.sessionId !== querySessionId) return;
+ if (event.detail.sessionId !== querySessionIdRef.current) return;
setQueryProgress({
rows: event.detail.rowsSoFar,
elapsed: Math.round(event.detail.elapsedMs / 1000),
@@ -244,13 +279,14 @@ export function useDatabaseDetails({
};
const handleDone = (event: CustomEvent) => {
- if (event.detail.sessionId !== querySessionId) return;
+ if (event.detail.sessionId !== querySessionIdRef.current) return;
+ querySessionIdRef.current = null;
setIsExecuting(false);
setQuerySessionId(null);
setQueryProgress(null);
- const isDDL = query
+ const isDDL = lastExecutedSqlRef.current
.trim()
.toUpperCase()
.match(/^(CREATE|DROP|ALTER|TRUNCATE)/);
@@ -259,11 +295,14 @@ export function useDatabaseDetails({
}
const { rows, timeMs, status } = event.detail;
+ const elapsedSec = timeMs !== undefined ? Number((timeMs / 1000).toFixed(2)) : null;
+ setExecutionTime(elapsedSec);
+
const statusType = status === "success" ? "success" : "warning";
const message =
status === "success"
- ? `Retrieved ${rows.toLocaleString()} rows in ${(timeMs / 1000).toFixed(2)}s`
- : `Stopped after retrieving ${rows.toLocaleString()} rows.`;
+ ? `Retrieved ${(rows || 0).toLocaleString()} rows in ${(timeMs / 1000).toFixed(2)}s`
+ : `Stopped after retrieving ${(rows || 0).toLocaleString()} rows.`;
toast[statusType](statusType === "success" ? "Query Complete" : "Query Cancelled", {
description: message,
@@ -271,8 +310,9 @@ export function useDatabaseDetails({
};
const handleError = (event: CustomEvent) => {
- if (event.detail.sessionId !== querySessionId) return;
+ if (event.detail.sessionId !== querySessionIdRef.current) return;
+ querySessionIdRef.current = null;
setIsExecuting(false);
setQuerySessionId(null);
setQueryProgress(null);
@@ -300,7 +340,7 @@ export function useDatabaseDetails({
window.removeEventListener(listener.name, listener.handler as EventListener);
});
};
- }, [querySessionId]);
+ }, [dbId, invalidateDatabase]);
// Auto-select first table when tables are loaded
useEffect(() => {
@@ -325,6 +365,7 @@ export function useDatabaseDetails({
tables,
selectedTable,
tableData: showQueryResults ? queryResults : tableData,
+ tableColumns: selectedTableColumns,
queryResults,
hasExecutedQuery,
rowCount: showQueryResults ? queryRowCount : rowCount,
@@ -332,6 +373,7 @@ export function useDatabaseDetails({
query,
queryProgress,
queryError,
+ executionTime,
isExecuting,
isLoadingData: isFetchingTableData,
loading,
diff --git a/src/features/database/hooks/useMigrationsPanel.ts b/src/features/database/hooks/useMigrationsPanel.ts
index 869103b4..daa2b1aa 100644
--- a/src/features/database/hooks/useMigrationsPanel.ts
+++ b/src/features/database/hooks/useMigrationsPanel.ts
@@ -1,8 +1,8 @@
import { MigrationsData } from "@/features/database/types";
-import { useQueryClient } from "@tanstack/react-query";
import { toast } from "sonner";
import { migrationService } from "@/services/bridge/migration";
import { useState } from "react";
+import { useInvalidateCache } from "@/features/project/hooks/useDbQueries";
interface MigrationsPanelProps {
migrations: MigrationsData;
@@ -12,7 +12,7 @@ interface MigrationsPanelProps {
export function useMigrationsPanel({ migrations, baselined, dbId }: MigrationsPanelProps) {
const { local, applied } = migrations;
- const queryClient = useQueryClient();
+ const { invalidateDatabase } = useInvalidateCache();
const [selectedMigration, setSelectedMigration] = useState<{
version: string;
name: string;
@@ -24,12 +24,7 @@ export function useMigrationsPanel({ migrations, baselined, dbId }: MigrationsPa
const handleRefresh = async () => {
setIsRefreshing(true);
try {
- await Promise.all([
- queryClient.invalidateQueries({ queryKey: ["migrations", dbId] }),
- queryClient.invalidateQueries({ queryKey: ["tables", dbId] }),
- queryClient.invalidateQueries({ queryKey: ["schema", dbId] }),
- queryClient.invalidateQueries({ queryKey: ["schemaNames", dbId] }),
- ]);
+ await invalidateDatabase(dbId);
toast.success("Refreshed successfully");
} finally {
setIsRefreshing(false);
@@ -62,10 +57,7 @@ export function useMigrationsPanel({ migrations, baselined, dbId }: MigrationsPa
toast.success("Migration applied successfully", {
description: `Applied migration: ${name}`,
});
- // Invalidate migrations query to refresh
- queryClient.invalidateQueries({ queryKey: ["migrations", dbId] });
- queryClient.invalidateQueries({ queryKey: ["tables", dbId] });
- queryClient.invalidateQueries({ queryKey: ["schema", dbId] });
+ await invalidateDatabase(dbId);
} catch (error: any) {
toast.error("Failed to apply migration", {
description: error.message,
@@ -79,9 +71,7 @@ export function useMigrationsPanel({ migrations, baselined, dbId }: MigrationsPa
toast.success("Migration rolled back successfully", {
description: `Rolled back migration: ${name}`,
});
- queryClient.invalidateQueries({ queryKey: ["migrations", dbId] });
- queryClient.invalidateQueries({ queryKey: ["tables", dbId] });
- queryClient.invalidateQueries({ queryKey: ["schema", dbId] });
+ await invalidateDatabase(dbId);
} catch (error: any) {
toast.error("Failed to rollback migration", {
description: error.message,
@@ -95,7 +85,7 @@ export function useMigrationsPanel({ migrations, baselined, dbId }: MigrationsPa
toast.success("Migration deleted successfully", {
description: `Deleted migration: ${name}`,
});
- queryClient.invalidateQueries({ queryKey: ["migrations", dbId] });
+ await invalidateDatabase(dbId);
} catch (error: any) {
toast.error("Failed to delete migration", {
description: error.message,
diff --git a/src/features/database/types.ts b/src/features/database/types.ts
index 97d78860..092a1065 100644
--- a/src/features/database/types.ts
+++ b/src/features/database/types.ts
@@ -1,4 +1,4 @@
-export type DatabaseType = "postgresql" | "mysql" | "sqlite" | "duckdb";
+export type DatabaseType = "postgresql" | "mysql" | "sqlite" | "duckdb" | "clickhouse";
export interface SSHConfig {
host: string;
@@ -11,7 +11,7 @@ export interface SSHConfig {
}
export interface DiscoveredDatabase {
- type: "postgresql" | "mysql" | "mariadb" | "sqlite" | "duckdb";
+ type: "postgresql" | "mysql" | "mariadb" | "sqlite" | "duckdb" | "clickhouse";
host: string;
port: number;
source: "local" | "docker";
@@ -37,6 +37,7 @@ export interface DatabaseConnection {
lastAccessedAt?: string;
credentialId?: string;
ssh?: SSHConfig;
+ url?: string;
}
export interface AddDatabaseParams {
@@ -52,6 +53,7 @@ export interface AddDatabaseParams {
ssl?: boolean;
sslmode?: string;
ssh?: SSHConfig;
+ url?: string;
}
export interface UpdateDatabaseParams {
@@ -65,6 +67,7 @@ export interface UpdateDatabaseParams {
notes?: string;
tags?: string[];
ssh?: SSHConfig;
+ url?: string;
}
export interface ConnectionTestResult {
@@ -202,7 +205,7 @@ export interface MonitoringActiveQuery {
}
export interface MonitoringSnapshot {
- databaseType: "postgres" | "mysql" | "mariadb" | "duckdb";
+ databaseType: "postgres" | "mysql" | "mariadb" | "duckdb" | "clickhouse";
sampledAt: string;
health: {
ok: boolean;
diff --git a/src/features/home/components/AddConnectionDialog.tsx b/src/features/home/components/AddConnectionDialog.tsx
index 34164e2d..5b554b36 100644
--- a/src/features/home/components/AddConnectionDialog.tsx
+++ b/src/features/home/components/AddConnectionDialog.tsx
@@ -10,6 +10,7 @@ import {
Key,
FileKey,
Settings,
+ ArrowLeft,
} from "lucide-react";
import { open as openDialog } from "@tauri-apps/plugin-dialog";
import { Button } from "@/components/ui/button";
@@ -32,9 +33,16 @@ import {
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Checkbox } from "@/components/ui/checkbox";
import { Switch } from "@/components/ui/switch";
-import { parseConnectionUrl } from "@/lib/parseConnectionUrl";
+import {
+ parseConnectionUrl,
+ cleanClickHouseUrl,
+ parseClickHouseUrl,
+} from "@/lib/parseConnectionUrl";
+import { databases, getDatabaseMeta, DatabaseConfig } from "@/lib/databases";
import { AddConnectionDialogProps, INITIAL_FORM_DATA, ConnectionFormData } from "../types";
+type DialogStep = "select" | "configure";
+
export function AddConnectionDialog({
open,
onOpenChange,
@@ -43,6 +51,7 @@ export function AddConnectionDialog({
initialData,
isDiscoveredMode,
}: AddConnectionDialogProps) {
+ const [step, setStep] = useState("select");
const [useUrl, setUseUrl] = useState(true);
const [connectionUrl, setConnectionUrl] = useState("");
const [formData, setFormData] = useState(INITIAL_FORM_DATA);
@@ -50,28 +59,93 @@ export function AddConnectionDialog({
// Apply initial data when dialog opens with prefilled values
useEffect(() => {
if (open) {
- if (initialData) {
- setFormData((prev) => ({ ...prev, ...INITIAL_FORM_DATA, ...initialData }));
- if (isDiscoveredMode) {
+ if (initialData && initialData.type) {
+ const reconstructedUrl =
+ initialData.url ||
+ (initialData.type === "clickhouse" && initialData.host
+ ? `${initialData.ssl ? "https" : "http"}://${initialData.host}:${
+ initialData.port || (initialData.ssl ? "8443" : "8123")
+ }`
+ : "");
+
+ setFormData((prev) => ({
+ ...prev,
+ ...INITIAL_FORM_DATA,
+ ...initialData,
+ url: reconstructedUrl || initialData.url || "",
+ }));
+
+ if (
+ isDiscoveredMode ||
+ initialData.type === "clickhouse" ||
+ initialData.type === "sqlite" ||
+ initialData.type === "duckdb"
+ ) {
setUseUrl(false);
}
+ setStep("configure");
} else {
- // Reset to empty form when opening without initial data
+ // Reset to initial state when opening fresh
setFormData(INITIAL_FORM_DATA);
setConnectionUrl("");
setUseUrl(true);
+ setStep("select");
}
}
- }, [open, initialData]);
+ }, [open, initialData, isDiscoveredMode]);
const handleInputChange = (field: string, value: any) => {
setFormData((prev) => ({ ...prev, [field]: value }));
};
+ const handleSelectDatabase = (db: DatabaseConfig) => {
+ const isFileBased = db.isFileBased || db.type === "sqlite" || db.type === "duckdb";
+ const isCh = db.type === "clickhouse";
+
+ setFormData((prev) => ({
+ ...prev,
+ type: db.type,
+ name: prev.name || `${db.name} Local`,
+ host: isFileBased ? "" : prev.host || (isCh ? "localhost" : "localhost"),
+ port: isFileBased ? "" : prev.port || db.defaultPort || "",
+ user: isFileBased ? "" : prev.user || db.defaultUser || "",
+ password: isFileBased ? "" : prev.password || "",
+ database: isFileBased ? "" : prev.database || db.defaultDatabase || "",
+ ssl: isCh ? false : prev.ssl,
+ sslmode: "",
+ url: isCh && !prev.url ? `http://localhost:${db.defaultPort || "8123"}` : prev.url,
+ useSsh: false,
+ }));
+
+ if (isFileBased || isCh) {
+ setUseUrl(false);
+ } else {
+ setUseUrl(true);
+ }
+
+ setStep("configure");
+ };
+
const handleUrlChange = (url: string) => {
setConnectionUrl(url);
const parsed = parseConnectionUrl(url);
if (parsed) {
+ if (parsed.type === "clickhouse") {
+ setUseUrl(false);
+ setFormData((prev) => ({
+ ...prev,
+ type: "clickhouse",
+ url: cleanClickHouseUrl(url),
+ host: parsed.host,
+ port: parsed.port,
+ user: parsed.user || "default",
+ password: parsed.password || prev.password,
+ database: parsed.database || "default",
+ ssl: parsed.ssl,
+ sslmode: parsed.sslmode,
+ }));
+ return;
+ }
setFormData((prev) => ({
...prev,
type: parsed.type,
@@ -87,6 +161,26 @@ export function AddConnectionDialog({
};
const handleSubmit = () => {
+ if (formData.type === "clickhouse") {
+ const parsed = parseClickHouseUrl(formData.url);
+ const cleanedUrl = cleanClickHouseUrl(formData.url);
+ onSubmit(
+ {
+ ...formData,
+ url: cleanedUrl,
+ host: parsed.host || formData.host || "localhost",
+ port: parsed.port || formData.port || (parsed.ssl ? "8443" : "8123"),
+ ssl: parsed.ssl ?? formData.ssl,
+ sslmode: (parsed.ssl ?? formData.ssl) ? "require" : "disable",
+ user: formData.user || parsed.user || "default",
+ password: formData.password || parsed.password || "",
+ database: formData.database || parsed.database || "default",
+ },
+ false,
+ cleanedUrl,
+ );
+ return;
+ }
onSubmit(formData, useUrl, connectionUrl);
};
@@ -95,19 +189,22 @@ export function AddConnectionDialog({
setFormData(INITIAL_FORM_DATA);
setConnectionUrl("");
setUseUrl(true);
+ setStep("select");
}
onOpenChange(newOpen);
};
const isFileBasedDB = formData.type === "sqlite" || formData.type === "duckdb";
+ const isClickHouse = formData.type === "clickhouse";
const showSslOption = formData.type === "postgresql" || formData.type === "mariadb";
+ const selectedDbMeta = getDatabaseMeta(formData.type);
return (
{
- if (e.key === "Enter") {
+ if (e.key === "Enter" && step === "configure") {
const target = e.target as HTMLElement;
if (target.tagName !== "BUTTON" && target.tagName !== "A") {
e.preventDefault();
@@ -116,371 +213,601 @@ export function AddConnectionDialog({
}
}}
>
-
-
-
- {initialData ? "Edit Connection" : "New Connection"}
-
-
- Connect to PostgreSQL, MySQL, MariaDB, SQLite, or DuckDB
-
-
-
-
- {!isFileBasedDB && !isDiscoveredMode && (
-
setUseUrl(v === "url")}>
-
-
-
- URL
-
-
-
- Parameters
-
-
-
- )}
-
- {useUrl && !isFileBasedDB && (
-
- Connection URL
- handleUrlChange(e.target.value)}
- className="font-mono text-xs h-9"
- />
-
- )}
-
-
- Name
- handleInputChange("name", e.target.value)}
- className="h-9 text-sm"
- />
-
-
- {!useUrl && (
-
-
Type
-
{
- handleInputChange("type", val);
- if (val === "sqlite" || val === "duckdb") {
- // Clear network-related fields when switching to SQLite or DuckDB
- setUseUrl(false);
- setConnectionUrl("");
- setFormData((prev) => ({
- ...prev,
- type: val,
- host: "",
- port: "",
- user: "",
- password: "",
- ssl: false,
- sslmode: "",
- // Clear all SSH fields so stale credentials don't pollute the SQLite payload
- useSsh: false,
- sshHost: "",
- sshPort: "22",
- sshUser: "",
- sshPassword: "",
- sshPrivateKeyPath: "",
- sshPassphrase: "",
- }));
- }
- }}
- >
-
-
-
-
- PostgreSQL
- MySQL
- MariaDB
- SQLite
- DuckDB
-
-
+ {step === "select" ? (
+ <>
+
+
+
+ Select Database
+
+
+ Choose a database engine to configure your connection.
+
+
+
+
+ {databases.map((db) => (
+
handleSelectDatabase(db)}
+ className="group relative flex flex-col items-center justify-between p-4 rounded-xl border border-border/60 bg-card hover:bg-accent/40 hover:border-primary/50 transition-all duration-150 text-center hover:shadow-sm cursor-pointer focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
+ >
+
+
+ {db.category}
+
+
+
+
+ {db.icon}
+
+
+
+
+ {db.name}
+
+
+ {db.description}
+
+
+
+ ))}
- )}
- {!useUrl &&
- (isFileBasedDB ? (
-
-
Database File
-
-
handleInputChange("database", e.target.value)}
- readOnly={isDiscoveredMode}
- className={`h-9 text-sm font-mono flex-1 ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : ""}`}
- />
+
+ handleOpenChange(false)}>
+ Cancel
+
+
+ >
+ ) : (
+ <>
+
+ {!isDiscoveredMode && !initialData && (
+
{
- const selected = await openDialog({
- title:
- formData.type === "duckdb"
- ? "Select DuckDB Database"
- : "Select SQLite Database",
- filters: [
- {
- name: formData.type === "duckdb" ? "DuckDB" : "SQLite",
- extensions:
- formData.type === "duckdb"
- ? ["db", "duckdb", "ddb"]
- : ["db", "sqlite", "sqlite3", "s3db"],
- },
- ],
- });
- if (selected) handleInputChange("database", selected as string);
- }}
+ onClick={() => setStep("select")}
+ className="h-7 px-2 -ml-2 text-xs text-muted-foreground hover:text-foreground gap-1.5"
>
-
+
+ All Databases
+ )}
+
+
+
+ {selectedDbMeta?.icon || }
+
+
+ {initialData
+ ? `Edit ${selectedDbMeta?.name || "Connection"}`
+ : `Connect to ${selectedDbMeta?.name || "Database"}`}
+
+
+
+ {selectedDbMeta?.description || "Enter connection credentials and details."}
+
+
+
+
+ {/* Tabs for URL vs Parameters (PostgreSQL, MySQL, MariaDB, ClickHouse) */}
+ {!isFileBasedDB && !isDiscoveredMode && (
+
{
+ const isUrlMode = v === "url";
+ setUseUrl(isUrlMode);
+ if (isClickHouse && !isUrlMode && !formData.url) {
+ setFormData((prev) => ({
+ ...prev,
+ url: `${prev.ssl ? "https" : "http"}://${prev.host || "localhost"}:${
+ prev.port || (prev.ssl ? "8443" : "8123")
+ }`,
+ }));
+ }
+ }}
+ >
+
+
+
+ URL
+
+
+
+ Parameters
+
+
+
+ )}
+
+ {/* Connection URL Mode */}
+ {useUrl && !isFileBasedDB && !isClickHouse && (
+
+ Connection URL
+ handleUrlChange(e.target.value)}
+ className="font-mono text-xs h-9"
+ />
+
+ )}
+
+ {/* Common Connection Name */}
+
+ Connection Name
+ handleInputChange("name", e.target.value)}
+ className="h-9 text-sm"
+ />
- ) : (
- <>
-
-
- Host
- handleInputChange("host", e.target.value)}
- readOnly={isDiscoveredMode}
- className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : ""}`}
- />
-
-
-
Port
+
+ {/* File-based DB (SQLite or DuckDB) */}
+ {isFileBasedDB ? (
+
+
Database File Path
+
handleInputChange("port", e.target.value)}
+ value={formData.database}
+ onChange={(e) => handleInputChange("database", e.target.value)}
readOnly={isDiscoveredMode}
- className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : ""}`}
+ className={`h-9 text-sm font-mono flex-1 ${
+ isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : ""
+ }`}
/>
+ {
+ const selected = await openDialog({
+ title: `Select ${selectedDbMeta?.name || "Database"} File`,
+ filters: [
+ {
+ name: selectedDbMeta?.name || "Database",
+ extensions: selectedDbMeta?.fileExtensions || [
+ "db",
+ "sqlite",
+ "duckdb",
+ ],
+ },
+ ],
+ });
+ if (selected) handleInputChange("database", selected as string);
+ }}
+ >
+
+
+
+ {formData.type === "duckdb"
+ ? "Select a local DuckDB file (.duckdb, .db, .ddb). Direct fast in-process analytical execution."
+ : "Select a local SQLite file (.db, .sqlite, .sqlite3). Direct zero-configuration database."}
+
+ ) : isClickHouse ? (
+ /* ClickHouse Specific Form */
+ <>
+ {useUrl ? (
+
+ Instance URL
+ {
+ const val = e.target.value;
+ handleInputChange("url", val);
+ const parsed = parseClickHouseUrl(val);
+ setFormData((prev) => ({
+ ...prev,
+ url: val,
+ host: parsed.host || prev.host,
+ port: parsed.port || prev.port,
+ ssl: parsed.ssl,
+ user: prev.user || parsed.user || "default",
+ database: prev.database || parsed.database || "default",
+ }));
+ }}
+ readOnly={isDiscoveredMode}
+ className={`h-9 text-sm font-mono ${
+ isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : ""
+ }`}
+ />
+
+ ) : (
+
+ )}
-
-
-
Username
-
handleInputChange("user", e.target.value)}
- readOnly={isDiscoveredMode}
- className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : ""}`}
- />
+
+
- Password
+ Database
handleInputChange("password", e.target.value)}
+ placeholder="default"
+ value={formData.database}
+ onChange={(e) => handleInputChange("database", e.target.value)}
readOnly={isDiscoveredMode}
- className={`h-9 text-sm ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : ""}`}
+ className={`h-9 text-sm font-mono ${
+ isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : ""
+ }`}
/>
-
-
- Database
- handleInputChange("database", e.target.value)}
- readOnly={isDiscoveredMode}
- className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : ""}`}
- />
-
+ {!isDiscoveredMode && !useUrl && (
+
+ {
+ const isSsl = Boolean(checked);
+ setFormData((prev) => ({
+ ...prev,
+ ssl: isSsl,
+ port: isSsl
+ ? prev.port === "8123"
+ ? "8443"
+ : prev.port
+ : prev.port === "8443"
+ ? "8123"
+ : prev.port,
+ url: `${isSsl ? "https" : "http"}://${prev.host || "localhost"}:${
+ isSsl ? (prev.port === "8123" ? "8443" : prev.port) : prev.port
+ }`,
+ }));
+ }}
+ />
+
+ Enable SSL / TLS (HTTPS port 8443)
+
+
+ )}
+ >
+ ) : !useUrl ? (
+ /* Parameters Mode for PostgreSQL, MySQL, MariaDB */
+ <>
+
- {showSslOption && !isDiscoveredMode && (
-
-
- setFormData((prev) => ({ ...prev, ssl: checked as boolean }))
- }
+
+
+
+ Database
+ handleInputChange("database", e.target.value)}
+ readOnly={isDiscoveredMode}
+ className={`h-9 text-sm font-mono ${
+ isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : ""
+ }`}
/>
-
- Enable SSL
-
- )}
- {/* SSH Tunnel Section */}
- {!isDiscoveredMode && (
-
-
-
-
-
- SSH Tunnel
-
-
-
handleInputChange("useSsh", checked)}
+ {showSslOption && !isDiscoveredMode && (
+
+
+ setFormData((prev) => ({ ...prev, ssl: checked as boolean }))
+ }
/>
+
+ Enable SSL
+
+ )}
- {formData.useSsh && (
-
-
-
-
-
- SSH Username
+ {/* SSH Tunnel Section */}
+ {!isDiscoveredMode && selectedDbMeta?.supportsSsh && (
+
+
+
+
+
+ SSH Tunnel
- handleInputChange("sshUser", e.target.value)}
- className="h-8 text-xs font-mono"
- />
+
handleInputChange("useSsh", checked)}
+ />
+
-
- Auth Method
-
- handleInputChange("sshAuthMethod", val)
- }
- >
-
-
-
-
- Password
- Private Key
-
-
-
+ {formData.useSsh && (
+
+
- {formData.sshAuthMethod === "password" ? (
- SSH Password
+ SSH Username
handleInputChange("sshPassword", e.target.value)}
- className="h-8 text-xs"
+ placeholder="ubuntu"
+ value={formData.sshUser}
+ onChange={(e) => handleInputChange("sshUser", e.target.value)}
+ className="h-8 text-xs font-mono"
/>
- ) : (
-
-
-
- Private Key Path
-
-
-
- handleInputChange("sshPrivateKeyPath", e.target.value)
- }
- className="h-8 text-xs font-mono flex-1"
- />
- {
- const selected = await openDialog({
- title: "Select Private Key",
- });
- if (selected)
- handleInputChange("sshPrivateKeyPath", selected as string);
- }}
- >
-
-
-
-
+
+
+ Auth Method
+
+ handleInputChange("sshAuthMethod", val)
+ }
+ >
+
+
+
+
+ Password
+ Private Key
+
+
+
+
+ {formData.sshAuthMethod === "password" ? (
- Passphrase (Optional)
+ SSH Password
handleInputChange("sshPassphrase", e.target.value)}
+ value={formData.sshPassword}
+ onChange={(e) => handleInputChange("sshPassword", e.target.value)}
className="h-8 text-xs"
/>
-
- )}
-
- )}
-
+ ) : (
+
+
+
+ Private Key Path
+
+
+
+ handleInputChange("sshPrivateKeyPath", e.target.value)
+ }
+ className="h-8 text-xs font-mono flex-1"
+ />
+ {
+ const selected = await openDialog({
+ title: "Select Private Key",
+ });
+ if (selected)
+ handleInputChange("sshPrivateKeyPath", selected as string);
+ }}
+ >
+
+
+
+
+
+
+ Passphrase (Optional)
+
+
+ handleInputChange("sshPassphrase", e.target.value)
+ }
+ className="h-8 text-xs"
+ />
+
+
+ )}
+
+ )}
+
+ )}
+ >
+ ) : null}
+
+
+
+
+ {!isDiscoveredMode && !initialData && (
+
setStep("select")}
+ disabled={isLoading}
+ className="text-xs gap-1.5 text-muted-foreground hover:text-foreground"
+ >
+
+ Back
+
)}
- >
- ))}
-
-
-
- handleOpenChange(false)}
- size="sm"
- disabled={isLoading}
- >
- Cancel
-
-
- {isLoading ? "Connecting..." : initialData ? "Save Changes" : "Connect"}
-
-
+
+
+ handleOpenChange(false)}
+ size="sm"
+ disabled={isLoading}
+ >
+ Cancel
+
+
+ {isLoading ? "Connecting..." : initialData ? "Save Changes" : "Connect"}
+
+
+
+ >
+ )}
);
diff --git a/src/features/home/components/ConnectionList.tsx b/src/features/home/components/ConnectionList.tsx
index ae9242f9..ea057904 100644
--- a/src/features/home/components/ConnectionList.tsx
+++ b/src/features/home/components/ConnectionList.tsx
@@ -404,14 +404,14 @@ export function ConnectionList({
Connections
-
+
setNewGroupOpen(true)}
>
@@ -422,11 +422,17 @@ export function ConnectionList({
+
{onImportClick && (
-
+
@@ -436,9 +442,42 @@ export function ConnectionList({
)}
-
-
-
+
+
+
+
+
+
+
+
+
+ Add Connection
+
+
+
+
+
+
+
+ setSettingsOpen(true)}
+ >
+
+
+
+
+ Settings
+
+
+
@@ -603,7 +642,7 @@ export function ConnectionList({
{/* Quick Stats Footer */}
-
+
@@ -625,16 +664,6 @@ export function ConnectionList({
-
- setSettingsOpen(true)}
- className="h-8 w-8 p-0 text-muted-foreground hover:text-foreground"
- >
-
-
-
{/* Dialogs */}
diff --git a/src/features/home/components/DatabasePreview.tsx b/src/features/home/components/DatabasePreview.tsx
index a3fe4e2a..1591c5d0 100644
--- a/src/features/home/components/DatabasePreview.tsx
+++ b/src/features/home/components/DatabasePreview.tsx
@@ -19,6 +19,7 @@ import {
import { cn } from "@/lib/utils";
import { DatabasePreviewProps } from "../types";
import { DatabaseOverviewPanel } from "./DatabaseOverviewPanel";
+import { DatabaseLogo } from "@/lib/databases";
import {
useProjectByDatabaseId,
useProjectSchema,
@@ -34,6 +35,7 @@ const DB_COLORS: Record
= {
mariadb: { bg: "bg-teal-500/10", text: "text-teal-500" },
sqlite: { bg: "bg-cyan-500/10", text: "text-cyan-500" },
duckdb: { bg: "bg-yellow-500/10", text: "text-yellow-600" },
+ clickhouse: { bg: "bg-amber-500/10", text: "text-amber-500" },
};
function getDbColors(type: string) {
@@ -87,11 +89,11 @@ export function DatabasePreview({
-
+
diff --git a/src/features/home/components/DiscoveredDatabasesCard.tsx b/src/features/home/components/DiscoveredDatabasesCard.tsx
index 095f9267..c6882c06 100644
--- a/src/features/home/components/DiscoveredDatabasesCard.tsx
+++ b/src/features/home/components/DiscoveredDatabasesCard.tsx
@@ -37,6 +37,11 @@ const DB_TYPE_COLORS = {
text: "text-yellow-600",
border: "border-yellow-500/20",
},
+ clickhouse: {
+ bg: "bg-amber-500/10",
+ text: "text-amber-500",
+ border: "border-amber-500/20",
+ },
};
export function DiscoveredDatabasesCard({
diff --git a/src/features/home/components/WelcomeView.tsx b/src/features/home/components/WelcomeView.tsx
index 42d02d01..1589963f 100644
--- a/src/features/home/components/WelcomeView.tsx
+++ b/src/features/home/components/WelcomeView.tsx
@@ -17,6 +17,7 @@ import { useDbStats, useTables } from "@/features/project/hooks/useDbQueries";
import { bytesToMBString } from "@/lib/bytesToMB";
import { DatabaseConnection } from "@/features/database/types";
import { useCountUp } from "../hooks/useCountUp";
+import { DatabaseLogo } from "@/lib/databases";
const DB_COLORS: Record
= {
postgresql: { bg: "bg-blue-500/10", text: "text-blue-500" },
@@ -24,6 +25,7 @@ const DB_COLORS: Record = {
mariadb: { bg: "bg-teal-500/10", text: "text-teal-500" },
sqlite: { bg: "bg-cyan-500/10", text: "text-cyan-500" },
duckdb: { bg: "bg-yellow-500/10", text: "text-yellow-600" },
+ clickhouse: { bg: "bg-amber-500/10", text: "text-amber-500" },
};
function getDbColors(type: string) {
@@ -36,7 +38,7 @@ function ConnectionSizeItem({ db }: { db: DatabaseConnection }) {
return (
-
+
{db.name}
@@ -59,7 +61,7 @@ function ConnectionTablesList({ db }: { db: DatabaseConnection }) {
@@ -290,11 +292,11 @@ export function WelcomeView({
>
-
+
{db.name}
diff --git a/src/features/home/hooks/useIndexPage.ts b/src/features/home/hooks/useIndexPage.ts
index 1995f8c9..2335a441 100644
--- a/src/features/home/hooks/useIndexPage.ts
+++ b/src/features/home/hooks/useIndexPage.ts
@@ -9,6 +9,7 @@ import { projectKeys, useProjects } from "@/features/project/hooks/useProjectQue
import {
ConnectionFormData,
DUCKDB_REQUIRED_FIELDS,
+ CLICKHOUSE_REQUIRED_FIELDS,
REQUIRED_FIELDS,
SQLITE_REQUIRED_FIELDS,
} from "@/features/home/types";
@@ -107,11 +108,20 @@ export const useIndexPage = (bridgeReady: boolean) => {
const handleAddDatabase = async (formData: ConnectionFormData) => {
const isSQLite = formData.type === "sqlite";
const isDuckDB = formData.type === "duckdb";
+ const isClickHouse = formData.type === "clickhouse";
+
+ if (isClickHouse) {
+ if (!formData.user) formData.user = "default";
+ if (!formData.database) formData.database = "default";
+ }
+
const requiredFields = isSQLite
? SQLITE_REQUIRED_FIELDS
: isDuckDB
? DUCKDB_REQUIRED_FIELDS
- : REQUIRED_FIELDS;
+ : isClickHouse
+ ? CLICKHOUSE_REQUIRED_FIELDS
+ : REQUIRED_FIELDS;
const missing = requiredFields.filter((field) => !formData[field as keyof typeof formData]);
if (missing.length) {
@@ -156,10 +166,13 @@ export const useIndexPage = (bridgeReady: boolean) => {
} = formData;
const isFileBasedDB = isSQLite || isDuckDB;
+ const port = isFileBasedDB
+ ? 0
+ : parseInt(formData.port) || (isClickHouse ? (formData.ssl ? 8443 : 8123) : 5432);
const payload: any = {
...rest,
- port: isFileBasedDB ? 0 : parseInt(formData.port),
+ port,
sslmode: isFileBasedDB
? "disable"
: formData.ssl
diff --git a/src/features/home/types.ts b/src/features/home/types.ts
index 8e2a67d6..1682dd4f 100644
--- a/src/features/home/types.ts
+++ b/src/features/home/types.ts
@@ -76,6 +76,7 @@ export interface ConnectionFormData {
database: string;
sslmode: string;
ssl: boolean;
+ url?: string;
useSsh: boolean;
sshHost: string;
sshPort: string;
@@ -96,6 +97,7 @@ export const INITIAL_FORM_DATA: ConnectionFormData = {
database: "",
sslmode: "",
ssl: false,
+ url: "",
useSsh: false,
sshHost: "",
sshPort: "22",
@@ -109,3 +111,4 @@ export const INITIAL_FORM_DATA: ConnectionFormData = {
export const REQUIRED_FIELDS = ["name", "type", "host", "port", "user", "database"];
export const SQLITE_REQUIRED_FIELDS = ["name", "type", "database"];
export const DUCKDB_REQUIRED_FIELDS = ["name", "type", "database"];
+export const CLICKHOUSE_REQUIRED_FIELDS = ["name", "type", "url"];
diff --git a/src/features/monitoring/components/MonitoringDashboard.tsx b/src/features/monitoring/components/MonitoringDashboard.tsx
index 7b774d96..2efed4fb 100644
--- a/src/features/monitoring/components/MonitoringDashboard.tsx
+++ b/src/features/monitoring/components/MonitoringDashboard.tsx
@@ -77,9 +77,15 @@ export function MonitoringDashboard({
/>
diff --git a/src/features/monitoring/components/MonitoringPanel.tsx b/src/features/monitoring/components/MonitoringPanel.tsx
index 714a6561..b51bff18 100644
--- a/src/features/monitoring/components/MonitoringPanel.tsx
+++ b/src/features/monitoring/components/MonitoringPanel.tsx
@@ -23,7 +23,13 @@ type ThroughputPoint = {
function isMonitoringSupported(databaseType?: string) {
const type = databaseType?.toLowerCase();
- return type === "postgres" || type === "postgresql" || type === "mysql" || type === "mariadb";
+ return (
+ type === "postgres" ||
+ type === "postgresql" ||
+ type === "mysql" ||
+ type === "mariadb" ||
+ type === "clickhouse"
+ );
}
export function MonitoringPanel({ dbId, databaseName, databaseType }: MonitoringPanelProps) {
diff --git a/src/features/project/components/ImportProjectDialog.tsx b/src/features/project/components/ImportProjectDialog.tsx
index f0324a2d..368365eb 100644
--- a/src/features/project/components/ImportProjectDialog.tsx
+++ b/src/features/project/components/ImportProjectDialog.tsx
@@ -425,6 +425,7 @@ export function ImportProjectDialog({
MariaDB
SQLite
DuckDB
+
ClickHouse
diff --git a/src/features/project/hooks/useDbQueries.ts b/src/features/project/hooks/useDbQueries.ts
index 869fe84b..a1b1a3e2 100644
--- a/src/features/project/hooks/useDbQueries.ts
+++ b/src/features/project/hooks/useDbQueries.ts
@@ -354,11 +354,17 @@ export function useInvalidateCache() {
return {
invalidateDatabase: (dbId: string) => {
- queryClient.invalidateQueries({ queryKey: ["tables", dbId] });
- queryClient.invalidateQueries({ queryKey: queryKeys.stats(dbId) });
- queryClient.invalidateQueries({ queryKey: queryKeys.schemas(dbId) });
- queryClient.invalidateQueries({ queryKey: ["fullSchema", dbId] }); // Also invalidate fullSchema
- queryClient.invalidateQueries({ queryKey: ["migrations", dbId] }); // Also invalidate migrations
+ return Promise.all([
+ queryClient.invalidateQueries({ queryKey: ["tables", dbId] }),
+ queryClient.invalidateQueries({ queryKey: queryKeys.stats(dbId) }),
+ queryClient.invalidateQueries({ queryKey: queryKeys.schemas(dbId) }),
+ queryClient.invalidateQueries({ queryKey: ["schemaNames", dbId] }),
+ queryClient.invalidateQueries({ queryKey: ["schema", dbId] }),
+ queryClient.invalidateQueries({ queryKey: ["fullSchema", dbId] }), // Also invalidate fullSchema
+ queryClient.invalidateQueries({ queryKey: ["migrations", dbId] }), // Also invalidate migrations
+ queryClient.invalidateQueries({ queryKey: ["tableData", dbId] }),
+ queryClient.invalidateQueries({ queryKey: ["projects"] }),
+ ]);
},
/** Invalidate table-specific caches */
diff --git a/src/features/schema-explorer/components/AlterTableDialog.tsx b/src/features/schema-explorer/components/AlterTableDialog.tsx
index 03f98aa3..4e608e23 100644
--- a/src/features/schema-explorer/components/AlterTableDialog.tsx
+++ b/src/features/schema-explorer/components/AlterTableDialog.tsx
@@ -12,7 +12,9 @@ import { Label } from "@/components/ui/label";
import {
Select,
SelectContent,
+ SelectGroup,
SelectItem,
+ SelectLabel,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
@@ -20,15 +22,14 @@ import { Loader2, Settings, Plus, Trash2 } from "lucide-react";
import { Checkbox } from "@/components/ui/checkbox";
import { AlterTableOperation, CreateTableColumn } from "@/features/database/types";
import { useAlterTableDialog } from "../hooks/useAlterTableDialog";
-
-const DATA_TYPES = [
- { value: "INT", label: "Integer" },
- { value: "BIGINT", label: "Big Integer" },
- { value: "TEXT", label: "Text" },
- { value: "BOOLEAN", label: "Boolean" },
- { value: "TIMESTAMP", label: "Timestamp" },
- { value: "JSON", label: "JSON" },
-];
+import { useDatabase } from "@/features/project/hooks/useDbQueries";
+import {
+ getDatabaseAcceptedDatatypes,
+ getAllAcceptedDatatypes,
+ getDefaultColumnType,
+ getDatabaseMeta,
+ DatabaseLogo,
+} from "@/lib/databases";
const OPERATION_TYPES = [
{ value: "ADD_COLUMN", label: "Add Column" },
@@ -48,6 +49,7 @@ interface AlterTableDialogProps {
schemaName: string;
tableName: string;
availableColumns: string[];
+ databaseType?: string;
onSuccess?: () => void;
}
@@ -58,8 +60,13 @@ export default function AlterTableDialog({
schemaName,
tableName,
availableColumns,
+ databaseType,
onSuccess,
}: AlterTableDialogProps) {
+ const { data: database } = useDatabase(dbId);
+ const resolvedDbType = databaseType || database?.type;
+ const dbMeta = getDatabaseMeta(resolvedDbType);
+
const {
handleClose,
handleSubmit,
@@ -73,6 +80,7 @@ export default function AlterTableDialog({
dbId,
schemaName,
tableName,
+ databaseType: resolvedDbType,
onSuccess,
open,
availableColumns,
@@ -83,7 +91,13 @@ export default function AlterTableDialog({
- Alter Table: {tableName}
+ Alter Table: {tableName}
+ {dbMeta && (
+
+
+ {dbMeta.name}
+
+ )}
Modify the structure of table {tableName} {" "}
@@ -124,6 +138,7 @@ export default function AlterTableDialog({
operation={op}
index={index}
availableColumns={availableColumns}
+ databaseType={resolvedDbType}
onUpdate={(newOp) => updateOperation(index, newOp)}
onRemove={() => removeOperation(index)}
/>
@@ -162,6 +177,7 @@ interface OperationRowProps {
operation: AlterTableOperation;
index: number;
availableColumns: string[];
+ databaseType?: string;
onUpdate: (operation: AlterTableOperation) => void;
onRemove: () => void;
}
@@ -170,9 +186,12 @@ function OperationRow({
operation,
index,
availableColumns,
+ databaseType,
onUpdate,
onRemove,
}: OperationRowProps) {
+ const defaultType = getDefaultColumnType(databaseType);
+
const updateType = (newType: string) => {
// Create a new operation based on type
let newOp: AlterTableOperation;
@@ -183,7 +202,7 @@ function OperationRow({
type: "ADD_COLUMN",
column: {
name: "",
- type: "TEXT",
+ type: defaultType,
not_nullable: false,
is_primary_key: false,
default_value: "",
@@ -197,13 +216,13 @@ function OperationRow({
newOp = { type: "RENAME_COLUMN", from: "", to: "" };
break;
case "ALTER_TYPE":
- newOp = { type: "ALTER_TYPE", column_name: "", new_type: "TEXT" };
+ newOp = { type: "ALTER_TYPE", column_name: "", new_type: defaultType };
break;
case "SET_NOT_NULL":
- newOp = { type: "SET_NOT_NULL", column_name: "", new_type: "TEXT" };
+ newOp = { type: "SET_NOT_NULL", column_name: "", new_type: defaultType };
break;
case "DROP_NOT_NULL":
- newOp = { type: "DROP_NOT_NULL", column_name: "", new_type: "TEXT" };
+ newOp = { type: "DROP_NOT_NULL", column_name: "", new_type: defaultType };
break;
case "SET_DEFAULT":
newOp = { type: "SET_DEFAULT", column_name: "", default_value: "" };
@@ -255,6 +274,7 @@ function OperationRow({
{operation.type === "ADD_COLUMN" && (
onUpdate({ ...operation, column })}
/>
)}
@@ -310,21 +330,11 @@ function OperationRow({
/>
New Type
- onUpdate({ ...operation, new_type })}
- >
-
-
-
-
- {DATA_TYPES.map((type) => (
-
- {type.label}
-
- ))}
-
-
+ databaseType={databaseType}
+ onChange={(new_type) => onUpdate({ ...operation, new_type })}
+ />
)}
@@ -339,21 +349,11 @@ function OperationRow({
/>
Column Type
-
onUpdate({ ...operation, new_type })}
- >
-
-
-
-
- {DATA_TYPES.map((type) => (
-
- {type.label}
-
- ))}
-
-
+ databaseType={databaseType}
+ onChange={(new_type) => onUpdate({ ...operation, new_type })}
+ />
MySQL requires type for NULL constraint changes
@@ -394,12 +394,59 @@ function OperationRow({
}
// Helper Components
+function DataTypeSelect({
+ value,
+ onChange,
+ databaseType,
+}: {
+ value?: string;
+ onChange: (value: string) => void;
+ databaseType?: string;
+}) {
+ const acceptedDatatypes = getDatabaseAcceptedDatatypes(databaseType);
+ const allKnownTypes = getAllAcceptedDatatypes(databaseType);
+
+ return (
+
+
+
+
+
+ {acceptedDatatypes.map((group) => (
+
+
+ {group.category}
+
+ {group.types.map((type) => (
+
+ {type}
+
+ ))}
+
+ ))}
+ {value && !allKnownTypes.some((t) => t.toLowerCase() === value.toLowerCase()) && (
+
+
+ Custom
+
+
+ {value}
+
+
+ )}
+
+
+ );
+}
+
function AddColumnFields({
column,
onChange,
+ databaseType,
}: {
column: CreateTableColumn;
onChange: (column: CreateTableColumn) => void;
+ databaseType?: string;
}) {
return (
@@ -415,18 +462,11 @@ function AddColumnFields({
Type
- onChange({ ...column, type })}>
-
-
-
-
- {DATA_TYPES.map((type) => (
-
- {type.label}
-
- ))}
-
-
+ onChange({ ...column, type })}
+ />
diff --git a/src/features/schema-explorer/components/CreateTableDialog.tsx b/src/features/schema-explorer/components/CreateTableDialog.tsx
index 6673674c..69af568f 100644
--- a/src/features/schema-explorer/components/CreateTableDialog.tsx
+++ b/src/features/schema-explorer/components/CreateTableDialog.tsx
@@ -11,12 +11,15 @@ import { Loader2, Table } from "lucide-react";
import TableDesignerForm from "./TableDesignerForm";
import AddIndexesDialog from "./AddIndexesDialog";
import { useCreateTableDialog } from "../hooks/useCreateTableDialog";
+import { useDatabase } from "@/features/project/hooks/useDbQueries";
+import { DatabaseLogo, getDatabaseMeta } from "@/lib/databases";
interface CreateTableDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
dbId: string;
schemaName: string;
+ databaseType?: string;
onSuccess?: () => void;
}
@@ -25,8 +28,13 @@ export default function CreateTableDialog({
onOpenChange,
dbId,
schemaName,
+ databaseType,
onSuccess,
}: CreateTableDialogProps) {
+ const { data: database } = useDatabase(dbId);
+ const resolvedDbType = databaseType || database?.type;
+ const dbMeta = getDatabaseMeta(resolvedDbType);
+
const {
handleClose,
handleIndexesSuccess,
@@ -46,6 +54,7 @@ export default function CreateTableDialog({
onOpenChange,
dbId,
schemaName,
+ databaseType: resolvedDbType,
onSuccess,
open,
});
@@ -57,7 +66,13 @@ export default function CreateTableDialog({
- Create New Table
+ Create New Table
+ {dbMeta && (
+
+
+ {dbMeta.name}
+
+ )}
Create a new table in schema{" "}
@@ -74,6 +89,7 @@ export default function CreateTableDialog({
onForeignKeysChange={setForeignKeys}
currentSchema={schemaName}
availableTables={availableTables}
+ databaseType={resolvedDbType}
/>
diff --git a/src/features/schema-explorer/components/TableDesignerForm.tsx b/src/features/schema-explorer/components/TableDesignerForm.tsx
index 194d305c..d890b113 100644
--- a/src/features/schema-explorer/components/TableDesignerForm.tsx
+++ b/src/features/schema-explorer/components/TableDesignerForm.tsx
@@ -4,25 +4,23 @@ import { Label } from "@/components/ui/label";
import {
Select,
SelectContent,
+ SelectGroup,
SelectItem,
+ SelectLabel,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Checkbox } from "@/components/ui/checkbox";
-import { Trash2, Plus, Link2 } from "lucide-react";
+import { Trash2, Plus, Link2, Info } from "lucide-react";
import { CreateTableColumn, ForeignKeyConstraint } from "@/features/database/types";
+import {
+ getDatabaseAcceptedDatatypes,
+ getDatabaseForeignKeySupport,
+ getAllAcceptedDatatypes,
+} from "@/lib/databases";
import ForeignKeyRow from "./ForeignKeyRow";
import useTableDesignerForm from "../hooks/useTableDesignerForm";
-const DATA_TYPES = [
- { value: "INT", label: "Integer" },
- { value: "BIGINT", label: "Big Integer" },
- { value: "TEXT", label: "Text" },
- { value: "BOOLEAN", label: "Boolean" },
- { value: "TIMESTAMP", label: "Timestamp" },
- { value: "JSON", label: "JSON" },
-];
-
interface TableDesignerFormProps {
tableName: string;
onTableNameChange: (name: string) => void;
@@ -32,6 +30,7 @@ interface TableDesignerFormProps {
onForeignKeysChange: (foreignKeys: ForeignKeyConstraint[]) => void;
currentSchema: string;
availableTables: Array<{ schema: string; name: string }>;
+ databaseType?: string;
}
export default function TableDesignerForm({
@@ -43,7 +42,12 @@ export default function TableDesignerForm({
onForeignKeysChange,
currentSchema,
availableTables,
+ databaseType,
}: TableDesignerFormProps) {
+ const acceptedDatatypes = getDatabaseAcceptedDatatypes(databaseType);
+ const foreignKeySupport = getDatabaseForeignKeySupport(databaseType);
+ const allKnownTypes = getAllAcceptedDatatypes(databaseType);
+
const {
addColumn,
addForeignKey,
@@ -60,6 +64,7 @@ export default function TableDesignerForm({
foreignKeys,
currentSchema,
availableTables,
+ databaseType,
});
return (
@@ -147,14 +152,29 @@ export default function TableDesignerForm({
value={column.type}
onValueChange={(value) => updateColumn(index, "type", value)}
>
-
-
+
+
-
- {DATA_TYPES.map((type) => (
-
- {type.label}
-
+
+ {column.type &&
+ !allKnownTypes.some(
+ (t) => t.toLowerCase() === column.type.toLowerCase(),
+ ) && (
+
+ {column.type} (custom)
+
+ )}
+ {acceptedDatatypes.map((group) => (
+
+
+ {group.category}
+
+ {group.types.map((type) => (
+
+ {type}
+
+ ))}
+
))}
@@ -222,40 +242,59 @@ export default function TableDesignerForm({
Foreign Keys
-
-
- Add Foreign Key
-
+ {foreignKeySupport.supported && (
+
+
+ Add Foreign Key
+
+ )}
- {foreignKeys.length === 0 ? (
-
-
- No foreign keys defined. Add columns first, then define relationships.
-
+ {!foreignKeySupport.supported ? (
+
+
+
+
+ Foreign Key Constraints Not Supported
+
+
{foreignKeySupport.notes}
+
) : (
-
- {foreignKeys.map((fk, index) => (
- col.name).filter(Boolean)}
- availableTables={availableTables}
- currentSchema={currentSchema}
- />
- ))}
-
+ <>
+ {foreignKeySupport.notes && (
+
{foreignKeySupport.notes}
+ )}
+ {foreignKeys.length === 0 ? (
+
+
+ No foreign keys defined. Add columns first, then define relationships.
+
+
+ ) : (
+
+ {foreignKeys.map((fk, index) => (
+ col.name).filter(Boolean)}
+ availableTables={availableTables}
+ currentSchema={currentSchema}
+ />
+ ))}
+
+ )}
+ >
)}
diff --git a/src/features/schema-explorer/hooks/useAlterTableDialog.ts b/src/features/schema-explorer/hooks/useAlterTableDialog.ts
index 11c3f91d..8ea9c7f2 100644
--- a/src/features/schema-explorer/hooks/useAlterTableDialog.ts
+++ b/src/features/schema-explorer/hooks/useAlterTableDialog.ts
@@ -3,6 +3,7 @@ import { migrationService } from "@/services/bridge/migration";
import { useState, useEffect } from "react";
import { toast } from "sonner";
import { useInvalidateCache } from "@/features/project/hooks/useDbQueries";
+import { getDefaultColumnType } from "@/lib/databases";
interface AlterTableDialogProps {
open: boolean;
@@ -11,6 +12,7 @@ interface AlterTableDialogProps {
schemaName: string;
tableName: string;
availableColumns: string[];
+ databaseType?: string;
onSuccess?: () => void;
}
@@ -20,6 +22,7 @@ export function useAlterTableDialog({
dbId,
schemaName,
tableName,
+ databaseType,
onSuccess,
}: AlterTableDialogProps) {
const [operations, setOperations] = useState
([]);
@@ -44,12 +47,13 @@ export function useAlterTableDialog({
};
const addOperation = () => {
+ const defaultType = getDefaultColumnType(databaseType);
// Default to ADD_COLUMN
const newOp: AlterTableOperation = {
type: "ADD_COLUMN",
column: {
name: "",
- type: "TEXT",
+ type: defaultType,
not_nullable: false,
is_primary_key: false,
default_value: "",
@@ -83,12 +87,24 @@ export function useAlterTableDialog({
toast.error(`Operation ${i + 1}: Column name is required`);
return;
}
+ if (!op.column.type?.trim()) {
+ toast.error(`Operation ${i + 1}: Column data type is required`);
+ return;
+ }
+ } else if (op.type === "ALTER_TYPE") {
+ if (!op.column_name?.trim()) {
+ toast.error(`Operation ${i + 1}: Column name is required`);
+ return;
+ }
+ if (!op.new_type?.trim()) {
+ toast.error(`Operation ${i + 1}: New column type is required`);
+ return;
+ }
} else if (
op.type === "DROP_COLUMN" ||
op.type === "SET_NOT_NULL" ||
op.type === "DROP_NOT_NULL" ||
- op.type === "DROP_DEFAULT" ||
- op.type === "ALTER_TYPE"
+ op.type === "DROP_DEFAULT"
) {
if (!op.column_name?.trim()) {
toast.error(`Operation ${i + 1}: Column name is required`);
diff --git a/src/features/schema-explorer/hooks/useCreateTableDialog.ts b/src/features/schema-explorer/hooks/useCreateTableDialog.ts
index 9545244d..da8973b9 100644
--- a/src/features/schema-explorer/hooks/useCreateTableDialog.ts
+++ b/src/features/schema-explorer/hooks/useCreateTableDialog.ts
@@ -4,12 +4,14 @@ import { migrationService } from "@/services/bridge/migration";
import { useEffect, useState } from "react";
import { toast } from "sonner";
import { useInvalidateCache } from "@/features/project/hooks/useDbQueries";
+import { getDatabaseForeignKeySupport, getDatabaseMeta } from "@/lib/databases";
interface CreateTableDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
dbId: string;
schemaName: string;
+ databaseType?: string;
onSuccess?: () => void;
}
@@ -17,6 +19,7 @@ export function useCreateTableDialog({
onOpenChange,
dbId,
schemaName,
+ databaseType,
onSuccess,
open,
}: CreateTableDialogProps) {
@@ -92,6 +95,13 @@ export function useCreateTableDialog({
return false;
}
+ // Check for empty column types
+ const emptyTypes = columns.filter((col) => !col.type?.trim());
+ if (emptyTypes.length > 0) {
+ toast.error("All columns must have a data type specified");
+ return false;
+ }
+
// Check for duplicate column names
const names = columns.map((col) => col.name.toLowerCase());
const duplicates = names.filter((name, index) => names.indexOf(name) !== index);
@@ -100,6 +110,18 @@ export function useCreateTableDialog({
return false;
}
+ // Validate foreign key support
+ if (foreignKeys.length > 0) {
+ const fkSupport = getDatabaseForeignKeySupport(databaseType);
+ if (!fkSupport.supported) {
+ const dbName = getDatabaseMeta(databaseType)?.name || "This database";
+ toast.error(`${dbName} does not support foreign key constraints`, {
+ description: fkSupport.notes,
+ });
+ return false;
+ }
+ }
+
// Validate foreign keys
for (let i = 0; i < foreignKeys.length; i++) {
const fk = foreignKeys[i];
diff --git a/src/features/schema-explorer/hooks/useTableDesignerForm.ts b/src/features/schema-explorer/hooks/useTableDesignerForm.ts
index e5aace51..ec86608d 100644
--- a/src/features/schema-explorer/hooks/useTableDesignerForm.ts
+++ b/src/features/schema-explorer/hooks/useTableDesignerForm.ts
@@ -1,4 +1,5 @@
import { CreateTableColumn, ForeignKeyConstraint } from "@/features/database/types";
+import { getDefaultColumnType } from "@/lib/databases";
interface TableDesignerFormProps {
tableName: string;
@@ -9,6 +10,7 @@ interface TableDesignerFormProps {
onForeignKeysChange: (foreignKeys: ForeignKeyConstraint[]) => void;
currentSchema: string;
availableTables: Array<{ schema: string; name: string }>;
+ databaseType?: string;
}
export default function useTableDesignerForm({
@@ -18,11 +20,13 @@ export default function useTableDesignerForm({
foreignKeys,
onForeignKeysChange,
currentSchema,
+ databaseType,
}: TableDesignerFormProps) {
const addColumn = () => {
+ const defaultType = getDefaultColumnType(databaseType);
const newColumn: CreateTableColumn = {
name: "",
- type: "TEXT",
+ type: defaultType,
not_nullable: false,
is_primary_key: false,
default_value: "",
diff --git a/src/features/settings/components/AISettings.tsx b/src/features/settings/components/AISettings.tsx
index 3b85fa02..3ccd482c 100644
--- a/src/features/settings/components/AISettings.tsx
+++ b/src/features/settings/components/AISettings.tsx
@@ -1,10 +1,12 @@
-import { useState, useEffect } from "react";
+import { useState, useEffect, useMemo, useCallback, memo } from "react";
import {
Bot,
Eye,
EyeOff,
CheckCircle2,
XCircle,
+ AlertCircle,
+ AlertTriangle,
Loader2,
ChevronDown,
Zap,
@@ -29,6 +31,7 @@ import {
type AISettings as AISettingsData,
loadAISettings,
saveAISettings,
+ getCachedAISettings,
aiService,
} from "@/services/bridge/ai";
@@ -63,9 +66,11 @@ const MODEL_OPTIONS: Partial> = {
{ value: "gemini-2.5-pro-preview-06-05", label: "Gemini 2.5 Pro", tier: "powerful" },
],
groq: [
+ { value: "openai/gpt-oss-20b", label: "GPT-OSS 20B", tier: "fast" },
+ { value: "qwen/qwen3.8-27b", label: "Qwen 3.8 27B", tier: "balanced" },
+ { value: "openai/gpt-oss-120b", label: "GPT-OSS 120B", tier: "powerful" },
{ value: "llama-3.1-8b-instant", label: "Llama 3.1 8B", tier: "fast" },
{ value: "llama-3.3-70b-versatile", label: "Llama 3.3 70B", tier: "balanced" },
- { value: "moonshotai/kimi-k2-instruct", label: "Kimi K2", tier: "powerful" },
],
mistral: [
{ value: "mistral-small-latest", label: "Mistral Small", tier: "fast" },
@@ -133,7 +138,7 @@ const PROVIDERS: ProviderMeta[] = [
{
name: "groq",
label: "Groq",
- defaultModelLabel: "Llama 3.3 70B",
+ defaultModelLabel: "GPT-OSS 20B",
requiresKey: true,
keyField: "groqApiKey",
keyPlaceholder: "gsk_…",
@@ -188,7 +193,7 @@ const TIER_CONFIG: Record
);
-}
+});
// ── Model selector ────────────────────────────────────────────────────────
-function ModelSelector({
+const ModelSelector = memo(function ModelSelector({
provider,
value,
onChange,
@@ -283,13 +288,13 @@ function ModelSelector({
);
-}
+});
// ── Connection status ─────────────────────────────────────────────────────
-type ConnectionStatus = "idle" | "testing" | "ok" | "error";
+type ConnectionStatus = "idle" | "testing" | "ok" | "warning" | "error";
-function StatusBadge({ status, message }: { status: ConnectionStatus; message?: string }) {
+const StatusBadge = memo(function StatusBadge({ status }: { status: ConnectionStatus }) {
if (status === "idle") return null;
return (
{status === "testing" &&
}
{status === "ok" &&
}
+ {status === "warning" &&
}
{status === "error" &&
}
- {status === "testing" ? "Testing…" : status === "ok" ? "Connected" : (message ?? "Failed")}
+ {status === "testing"
+ ? "Testing…"
+ : status === "ok"
+ ? "Connected"
+ : status === "warning"
+ ? "Notice"
+ : "Connection Failed"}
);
-}
+});
// ── Password field ────────────────────────────────────────────────────────
-function SecretInput({
+const SecretInput = memo(function SecretInput({
value,
onChange,
placeholder,
@@ -345,69 +358,108 @@ function SecretInput({
);
-}
+});
// ── Main component ────────────────────────────────────────────────────────
export default function AISettings() {
- const [settings, setSettings] = useState({ defaultProvider: "ollama" });
- const [isLoading, setIsLoading] = useState(true);
+ const cached = getCachedAISettings();
+ const [settings, setSettings] = useState(
+ () => cached ?? { defaultProvider: "ollama" },
+ );
+ const [isLoading, setIsLoading] = useState(() => !cached);
const [dirty, setDirty] = useState(false);
const [status, setStatus] = useState("idle");
const [statusMessage, setStatusMessage] = useState();
- // Load from bridge (ai-settings.json) on mount
+ // Load from bridge (ai-settings.json) on mount or background refresh
useEffect(() => {
+ let isMounted = true;
loadAISettings().then((loaded) => {
- setSettings(loaded);
- setIsLoading(false);
+ if (isMounted) {
+ setSettings((prev) => (dirty ? prev : loaded));
+ setIsLoading(false);
+ }
});
- }, []);
-
- const activeProvider = PROVIDERS.find((p) => p.name === settings.defaultProvider) ?? PROVIDERS[0];
+ return () => {
+ isMounted = false;
+ };
+ }, [dirty]);
+
+ const activeProvider = useMemo(
+ () => PROVIDERS.find((p) => p.name === settings.defaultProvider) ?? PROVIDERS[0],
+ [settings.defaultProvider],
+ );
- const update = (patch: Partial) => {
+ const update = useCallback((patch: Partial) => {
setSettings((prev) => ({ ...prev, ...patch }));
setDirty(true);
setStatus("idle");
- };
+ setStatusMessage(undefined);
+ }, []);
- const handleSave = async () => {
+ const handleSave = useCallback(async () => {
await saveAISettings(settings);
setDirty(false);
setStatus("idle");
- };
+ setStatusMessage(undefined);
+ }, [settings]);
- const handleTest = async () => {
+ const handleTest = useCallback(async () => {
await saveAISettings(settings);
setDirty(false);
setStatus("testing");
setStatusMessage(undefined);
- const result = await aiService.testConnection(settings);
- if (result.connected) {
- setStatus("ok");
- } else {
+ try {
+ const result = await aiService.testConnection(settings);
+ if (result.connected) {
+ if (result.warning) {
+ setStatus("warning");
+ setStatusMessage(result.warning);
+ } else {
+ setStatus("ok");
+ setStatusMessage(undefined);
+ }
+ } else {
+ setStatus("error");
+ setStatusMessage(result.message);
+ }
+ } catch (err) {
setStatus("error");
- setStatusMessage(result.message);
+ setStatusMessage(err instanceof Error ? err.message : String(err));
}
- };
+ }, [settings]);
// Get the currently selected model label for the provider description
const activeModelField = MODEL_FIELD[activeProvider.name];
const activeModelValue = activeModelField
? (settings[activeModelField] as string | undefined)
: undefined;
- const activeModelOptions = MODEL_OPTIONS[activeProvider.name] ?? [];
- const activeModelInfo =
- activeModelOptions.find((o) => o.value === activeModelValue) ?? activeModelOptions[0];
+ const activeModelOptions = useMemo(
+ () => MODEL_OPTIONS[activeProvider.name] ?? [],
+ [activeProvider.name],
+ );
+ const activeModelInfo = useMemo(
+ () => activeModelOptions.find((o) => o.value === activeModelValue) ?? activeModelOptions[0],
+ [activeModelOptions, activeModelValue],
+ );
// Which providers already have keys saved (excluding ollama which needs no key)
- const configuredProviders = PROVIDERS.filter((p) => {
- if (!p.requiresKey || !p.keyField) return false;
- return !!(settings[p.keyField] as string | undefined)?.trim();
- });
+ const configuredProviders = useMemo(
+ () =>
+ PROVIDERS.filter((p) => {
+ if (!p.requiresKey || !p.keyField) return false;
+ return !!(settings[p.keyField] as string | undefined)?.trim();
+ }),
+ [settings],
+ );
const configuredCount = configuredProviders.length;
+ const otherProviders = useMemo(
+ () => PROVIDERS.filter((p) => p.name !== settings.defaultProvider && p.requiresKey),
+ [settings.defaultProvider],
+ );
+
if (isLoading) {
return (
@@ -592,7 +644,7 @@ export default function AISettings() {
{/* Actions */}
+
+ {/* Warning / Notice banner */}
+ {status === "warning" && statusMessage && (
+
+
+
+
Notice
+
+ {statusMessage}
+
+
+
+ )}
+
+ {/* Error banner */}
+ {status === "error" && statusMessage && (
+
+
+
+
Connection Test Failed
+
+ {statusMessage}
+
+
+
+ )}
{/* Configure other providers (collapsed) */}
@@ -618,53 +696,51 @@ export default function AISettings() {
Configure other providers
- {PROVIDERS.filter((p) => p.name !== settings.defaultProvider && p.requiresKey).map(
- (provider) => {
- const modelField = MODEL_FIELD[provider.name];
- const modelOptions = MODEL_OPTIONS[provider.name] ?? [];
- const selectedModel = modelField
- ? (settings[modelField] as string | undefined)
- : undefined;
- const selectedModelInfo =
- modelOptions.find((o) => o.value === selectedModel) ?? modelOptions[0];
- return (
-
-
-
{
+ const modelField = MODEL_FIELD[provider.name];
+ const modelOptions = MODEL_OPTIONS[provider.name] ?? [];
+ const selectedModel = modelField
+ ? (settings[modelField] as string | undefined)
+ : undefined;
+ const selectedModelInfo =
+ modelOptions.find((o) => o.value === selectedModel) ?? modelOptions[0];
+ return (
+
+
-
update({ [provider.keyField!]: v })}
- placeholder={provider.keyPlaceholder}
- />
- {modelField && modelOptions.length > 0 && (
- update({ [modelField]: model })}
- />
+ Get key
+
)}
- );
- },
- )}
+ update({ [provider.keyField!]: v })}
+ placeholder={provider.keyPlaceholder}
+ />
+ {modelField && modelOptions.length > 0 && (
+ update({ [modelField]: model })}
+ />
+ )}
+
+ );
+ })}
diff --git a/src/features/settings/components/ColorVariant.tsx b/src/features/settings/components/ColorVariant.tsx
index b434d1e7..78738997 100644
--- a/src/features/settings/components/ColorVariant.tsx
+++ b/src/features/settings/components/ColorVariant.tsx
@@ -1,63 +1,40 @@
+import { Check } from "lucide-react";
import { useThemeVariant } from "@/features/settings/hooks/useThemeVariant";
import { themeVariants, ThemeVariant } from "@/lib/themes";
import { cn } from "@/lib/utils";
const ACCENT_THEMES: ThemeVariant[] = ["blue", "slate", "green", "purple", "orange", "rose"];
-const FULL_THEMES: ThemeVariant[] = ["cyberpunk", "vscode", "valorant", "ghibli"];
export default function ColorVariant() {
const { variant, setVariant } = useThemeVariant();
return (
-
-
-
Accent color
-
- {ACCENT_THEMES.map((key) => {
- const config = themeVariants[key];
- const isActive = variant === key;
- return (
- setVariant(key)}
- className={cn(
- "w-7 h-7 rounded-full transition-all flex items-center justify-center",
- isActive
- ? "ring-2 ring-white ring-offset-2 ring-offset-background"
- : "hover:scale-110",
- )}
- style={{ backgroundColor: config.primary }}
- title={config.name}
- aria-label={`Select ${config.name} accent`}
- />
- );
- })}
-
-
-
- {/* Keeping Full Themes as they are custom themes and valuable */}
-
-
Full themes
-
- {FULL_THEMES.map((key) => {
- const config = themeVariants[key];
- const isActive = variant === key;
- return (
- setVariant(key)}
- className={cn(
- "px-3 py-1.5 text-xs font-medium rounded-md transition-all border",
- isActive
- ? "bg-primary/20 text-primary border-primary"
- : "bg-transparent text-muted-foreground border-border/20 hover:border-border/40 hover:text-foreground",
- )}
- >
- {config.name}
-
- );
- })}
-
+
+ Accent color
+
+ {ACCENT_THEMES.map((key) => {
+ const config = themeVariants[key];
+ const isActive = variant === key;
+ return (
+ setVariant(key)}
+ className={cn(
+ "w-7 h-7 rounded-full transition-all flex items-center justify-center cursor-pointer shadow-sm",
+ isActive
+ ? "ring-2 ring-primary ring-offset-2 ring-offset-background scale-105"
+ : "hover:scale-110 opacity-85 hover:opacity-100",
+ )}
+ style={{ backgroundColor: config.primary }}
+ title={config.name}
+ aria-label={`Select ${config.name} accent`}
+ >
+ {isActive && (
+
+ )}
+
+ );
+ })}
);
diff --git a/src/features/settings/components/SettingsDialog.tsx b/src/features/settings/components/SettingsDialog.tsx
index afe5b556..91460360 100644
--- a/src/features/settings/components/SettingsDialog.tsx
+++ b/src/features/settings/components/SettingsDialog.tsx
@@ -1,4 +1,4 @@
-import { useState } from "react";
+import { useState, useEffect } from "react";
import {
AISettings,
CheckForUpdates,
@@ -11,6 +11,7 @@ import {
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog";
+import { loadAISettings } from "@/services/bridge/ai";
type TabId = "general" | "ai" | "analytics" | "shortcuts" | "about";
@@ -35,6 +36,13 @@ export interface SettingsDialogProps {
export function SettingsDialog({ open, onOpenChange }: SettingsDialogProps) {
const [activeTab, setActiveTab] = useState
("general");
+ // Pre-warm AI settings as soon as settings dialog opens so AI tab is instant
+ useEffect(() => {
+ if (open) {
+ loadAISettings().catch(() => {});
+ }
+ }, [open]);
+
return (
diff --git a/src/features/settings/hooks/useThemeVariant.ts b/src/features/settings/hooks/useThemeVariant.ts
index be78d172..0994f67c 100644
--- a/src/features/settings/hooks/useThemeVariant.ts
+++ b/src/features/settings/hooks/useThemeVariant.ts
@@ -1,140 +1,20 @@
import { useEffect, useState } from "react";
-import { ThemeVariant, ThemePalette, defaultVariant, themeVariants } from "@/lib/themes";
+import { ThemeVariant, defaultVariant, themeVariants } from "@/lib/themes";
const STORAGE_KEY = "relwave-theme-variant";
-/** CSS variable names that map 1-to-1 to ThemePalette keys */
-const PALETTE_CSS_VARS: Array<[keyof ThemePalette, string]> = [
- ["background", "--background"],
- ["foreground", "--foreground"],
- ["card", "--card"],
- ["cardForeground", "--card-foreground"],
- ["popover", "--popover"],
- ["popoverForeground", "--popover-foreground"],
- ["primary", "--primary"],
- ["primaryForeground", "--primary-foreground"],
- ["secondary", "--secondary"],
- ["secondaryForeground", "--secondary-foreground"],
- ["muted", "--muted"],
- ["mutedForeground", "--muted-foreground"],
- ["accent", "--accent"],
- ["accentForeground", "--accent-foreground"],
- ["destructive", "--destructive"],
- ["border", "--border"],
- ["input", "--input"],
- ["ring", "--ring"],
- ["sidebar", "--sidebar"],
- ["sidebarForeground", "--sidebar-foreground"],
- ["sidebarPrimary", "--sidebar-primary"],
- ["sidebarPrimaryForeground", "--sidebar-primary-foreground"],
- ["sidebarAccent", "--sidebar-accent"],
- ["sidebarAccentForeground", "--sidebar-accent-foreground"],
- ["sidebarBorder", "--sidebar-border"],
- ["sidebarRing", "--sidebar-ring"],
- ["cardShadow", "--card-shadow"],
- ["glassBg", "--glass-bg"],
- ["surface", "--surface"],
- ["surfaceElevated", "--surface-elevated"],
- ["surfaceSubtle", "--surface-subtle"],
- ["borderSubtle", "--border-subtle"],
-];
-
-/** All CSS var names that a full-palette theme can set (used for cleanup when switching away) */
-const ALL_PALETTE_CSS_VARS = PALETTE_CSS_VARS.map(([, cssVar]) => cssVar);
-
-function isDarkMode(): boolean {
- return document.documentElement.classList.contains("dark");
-}
-
-function applyPalette(root: HTMLElement, palette: ThemePalette) {
- for (const [key, cssVar] of PALETTE_CSS_VARS) {
- const value = palette[key];
- if (value !== undefined) {
- root.style.setProperty(cssVar, value as string);
- }
- }
- // Sidebar primary/ring: use explicit palette overrides when present,
- // otherwise fall back to the main primary/ring values.
- if (!palette.sidebarPrimary && palette.primary) {
- root.style.setProperty("--sidebar-primary", palette.primary);
- }
- if (!palette.sidebarRing) {
- root.style.setProperty("--sidebar-ring", palette.ring ?? palette.primary);
- }
-}
-
-function clearPalette(root: HTMLElement) {
- for (const cssVar of ALL_PALETTE_CSS_VARS) {
- root.style.removeProperty(cssVar);
- }
- root.style.removeProperty("--sidebar-primary");
- root.style.removeProperty("--sidebar-ring");
-}
-
-/**
- * Resolve which palette to apply for a given variant, respecting dark mode.
- * Priority: lightPalette/darkPalette (mode-aware) → palette (single always-on) → null
- */
-function resolvePalette(variant: ThemeVariant): ThemePalette | null {
- const config = themeVariants[variant];
- if (!config.fullPalette) return null;
-
- const dark = isDarkMode();
-
- // Mode-aware themes (e.g. Valorant)
- if (config.lightPalette && config.darkPalette) {
- return dark ? config.darkPalette : config.lightPalette;
- }
-
- // Single-mode themes (e.g. Cyberpunk, VS Code — always dark)
- if (config.palette) {
- return config.palette;
- }
-
- return null;
-}
-
export function useThemeVariant() {
const [variant, setVariantState] = useState(() => {
const stored = localStorage.getItem(STORAGE_KEY);
- return (stored as ThemeVariant) || defaultVariant;
+ if (stored && stored in themeVariants) {
+ return stored as ThemeVariant;
+ }
+ return defaultVariant;
});
useEffect(() => {
const root = document.documentElement;
-
- const applyTheme = () => {
- // Always update the data attribute so CSS [data-theme-variant] selectors still work
- root.setAttribute("data-theme-variant", variant);
-
- const palette = resolvePalette(variant);
- if (palette) {
- applyPalette(root, palette);
- } else {
- // Accent-only theme: remove any full-palette inline overrides so the
- // base :root / .dark stylesheet variables resume control.
- clearPalette(root);
- }
- };
-
- // Apply immediately
- applyTheme();
-
- // For mode-aware full-palette themes, watch for .dark class toggling on
- // so we can swap lightPalette ↔ darkPalette without a page reload.
- const config = themeVariants[variant];
- if (config.fullPalette && config.lightPalette && config.darkPalette) {
- const observer = new MutationObserver((mutations) => {
- for (const mutation of mutations) {
- if (mutation.type === "attributes" && mutation.attributeName === "class") {
- applyTheme();
- break;
- }
- }
- });
- observer.observe(root, { attributes: true, attributeFilter: ["class"] });
- return () => observer.disconnect();
- }
+ root.setAttribute("data-theme-variant", variant);
}, [variant]);
const setVariant = (newVariant: ThemeVariant) => {
diff --git a/src/features/workspace/components/QueryTabBar.tsx b/src/features/workspace/components/QueryTabBar.tsx
index 71282675..59982816 100644
--- a/src/features/workspace/components/QueryTabBar.tsx
+++ b/src/features/workspace/components/QueryTabBar.tsx
@@ -1,3 +1,4 @@
+import { useState } from "react";
import { Plus, X, AlertCircle, CheckCircle2, Loader2, FileCode } from "lucide-react";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
import { cn } from "@/lib/utils";
@@ -9,6 +10,7 @@ interface QueryTabBarProps {
onTabSelect: (tabId: string) => void;
onTabClose: (tabId: string, e: React.MouseEvent) => void;
onNewTab: () => void;
+ onTabRename?: (tabId: string, newName: string) => void;
}
export function QueryTabBar({
@@ -17,22 +19,61 @@ export function QueryTabBar({
onTabSelect,
onTabClose,
onNewTab,
+ onTabRename,
}: QueryTabBarProps) {
+ const [editingTabId, setEditingTabId] = useState(null);
+ const [editingName, setEditingName] = useState("");
+
+ const handleStartRename = (tab: QueryTab, e: React.MouseEvent) => {
+ e.stopPropagation();
+ setEditingTabId(tab.id);
+ setEditingName(tab.name);
+ };
+
+ const handleSaveRename = (tabId: string) => {
+ if (editingName.trim() && onTabRename) {
+ onTabRename(tabId, editingName.trim());
+ }
+ setEditingTabId(null);
+ };
+
return (
-
+
{tabs.map((tab) => (
onTabSelect(tab.id)}
className={cn(
- "h-full px-3 flex items-center gap-2 border-r border-border/30 min-w-[120px] max-w-[180px] group transition-colors",
+ "h-full px-3 flex items-center gap-2 border-r border-border/30 min-w-[120px] max-w-[200px] group transition-colors",
activeTabId === tab.id
? "bg-background text-foreground"
: "text-muted-foreground hover:text-foreground hover:bg-muted/30",
)}
>
- {tab.name}
+ {editingTabId === tab.id ? (
+ setEditingName(e.target.value)}
+ onBlur={() => handleSaveRename(tab.id)}
+ onKeyDown={(e) => {
+ if (e.key === "Enter") handleSaveRename(tab.id);
+ if (e.key === "Escape") setEditingTabId(null);
+ }}
+ autoFocus
+ className="text-xs bg-background border border-primary/50 rounded px-1 py-0.5 w-24 text-foreground outline-none font-mono"
+ onClick={(e) => e.stopPropagation()}
+ />
+ ) : (
+ handleStartRename(tab, e)}
+ title="Double-click to rename"
+ >
+ {tab.name}
+
+ )}
{tab.status === "running" && (
)}
@@ -44,6 +85,7 @@ export function QueryTabBar({
onTabClose(tab.id, e)}
className="opacity-0 group-hover:opacity-100 hover:bg-muted rounded p-0.5 shrink-0"
+ title="Close tab"
>
diff --git a/src/features/workspace/components/ResultsPanel.tsx b/src/features/workspace/components/ResultsPanel.tsx
index af9b8996..444bf713 100644
--- a/src/features/workspace/components/ResultsPanel.tsx
+++ b/src/features/workspace/components/ResultsPanel.tsx
@@ -1,7 +1,16 @@
-import { Database, AlertCircle, Loader2, Trash2 } from "lucide-react";
+import {
+ Database,
+ AlertCircle,
+ Loader2,
+ Trash2,
+ Copy,
+ FileSpreadsheet,
+ FileJson,
+} from "lucide-react";
import { Button } from "@/components/ui/button";
import { DataTable } from "@/components/shared/DataTable";
import { QueryTab } from "../types";
+import { toast } from "sonner";
interface ResultsPanelProps {
activeTab: QueryTab | undefined;
@@ -10,6 +19,38 @@ interface ResultsPanelProps {
}
export function ResultsPanel({ activeTab, queryProgress, onClearResults }: ResultsPanelProps) {
+ const handleCopyCSV = () => {
+ if (!activeTab?.results || activeTab.results.length === 0) return;
+ const headers = Object.keys(activeTab.results[0]);
+ const csvRows = [
+ headers.join(","),
+ ...activeTab.results.map((r) =>
+ headers
+ .map((h) => {
+ const val = r[h];
+ if (val === null || val === undefined) return "";
+ const str = String(val).replace(/"/g, '""');
+ return `"${str}"`;
+ })
+ .join(","),
+ ),
+ ];
+ navigator.clipboard.writeText(csvRows.join("\n"));
+ toast.success("Results copied as CSV", { duration: 1500 });
+ };
+
+ const handleCopyJSON = () => {
+ if (!activeTab?.results || activeTab.results.length === 0) return;
+ navigator.clipboard.writeText(JSON.stringify(activeTab.results, null, 2));
+ toast.success("Results copied as JSON", { duration: 1500 });
+ };
+
+ const handleCopyError = () => {
+ if (!activeTab?.error) return;
+ navigator.clipboard.writeText(activeTab.error);
+ toast.success("Error message copied", { duration: 1500 });
+ };
+
return (
{/* Results Header */}
@@ -17,7 +58,7 @@ export function ResultsPanel({ activeTab, queryProgress, onClearResults }: Resul
Results
{activeTab?.status === "success" && (
-
+
{activeTab.rowCount.toLocaleString()} rows
{activeTab.executionTime && (
• {activeTab.executionTime}s
@@ -26,21 +67,49 @@ export function ResultsPanel({ activeTab, queryProgress, onClearResults }: Resul
)}
{activeTab?.results && activeTab.results.length > 0 && (
-
-
- Clear
-
+
+
+
+ CSV
+
+
+
+ JSON
+
+
+
+ Clear
+
+
)}
{/* Results Content */}
{activeTab?.status === "running" ? (
-
+
Executing query...
{queryProgress && (
-
+
{queryProgress.rows} rows • {queryProgress.elapsed}s
)}
@@ -48,8 +117,19 @@ export function ResultsPanel({ activeTab, queryProgress, onClearResults }: Resul
) : activeTab?.error ? (
-
Query Failed
-
{activeTab.error}
+
Query Failed
+
+ {activeTab.error}
+
+
+
+ Copy Error
+
) : activeTab?.results && activeTab.results.length > 0 ? (
diff --git a/src/features/workspace/components/SQLWorkspacePanel.tsx b/src/features/workspace/components/SQLWorkspacePanel.tsx
index 2eeecd7c..389c58aa 100644
--- a/src/features/workspace/components/SQLWorkspacePanel.tsx
+++ b/src/features/workspace/components/SQLWorkspacePanel.tsx
@@ -1,4 +1,4 @@
-import { useState, useEffect, useCallback } from "react";
+import { useState, useEffect, useCallback, useRef } from "react";
import { useBridgeQuery } from "@/services/bridge/useBridgeQuery";
import { useDatabaseDetails } from "@/features/database/hooks/useDatabaseDetails";
import { WorkspaceHeader } from "./WorkspaceHeader";
@@ -17,25 +17,54 @@ interface SQLWorkspacePanelProps {
const SQLWorkspacePanel = ({ dbId }: SQLWorkspacePanelProps) => {
const { data: bridgeReady } = useBridgeQuery();
+ const storageKey = `relwave_sql_tabs_${dbId}`;
// Sidebar state
const [sidebarCollapsed, setSidebarCollapsed] = useState(false);
const [sidebarTab, setSidebarTab] = useState<"tables" | "history">("tables");
- // Query tabs state
- const [tabs, setTabs] = useState
([
- {
- id: "1",
- name: "Query 1",
- query: "-- Write your SQL query here\nSELECT * FROM ",
- results: [],
- rowCount: 0,
- error: null,
- executionTime: null,
- status: "idle",
- },
- ]);
- const [activeTabId, setActiveTabId] = useState("1");
+ // Selection tracking in active editor
+ const [selectedText, setSelectedText] = useState("");
+
+ // Elapsed execution time stopwatch
+ const [elapsedMs, setElapsedMs] = useState(0);
+
+ // Query tabs state with localStorage persistence
+ const [tabs, setTabs] = useState(() => {
+ try {
+ const saved = localStorage.getItem(`relwave_sql_tabs_${dbId}`);
+ if (saved) {
+ const parsed = JSON.parse(saved);
+ if (Array.isArray(parsed) && parsed.length > 0) {
+ return parsed.map((t: any) => ({
+ id: t.id || "1",
+ name: t.name || "Query 1",
+ query: t.query || "-- Write your SQL query here\nSELECT * FROM ",
+ results: [],
+ rowCount: 0,
+ error: null,
+ executionTime: null,
+ status: "idle",
+ }));
+ }
+ }
+ } catch {}
+ return [
+ {
+ id: "1",
+ name: "Query 1",
+ query: "-- Write your SQL query here\nSELECT * FROM ",
+ results: [],
+ rowCount: 0,
+ error: null,
+ executionTime: null,
+ status: "idle",
+ },
+ ];
+ });
+ const [activeTabId, setActiveTabId] = useState(tabs[0]?.id || "1");
+ const executingTabIdRef = useRef(activeTabId);
+ const wasExecutingRef = useRef(false);
// Query history
const [queryHistory, setQueryHistory] = useState([]);
@@ -44,27 +73,50 @@ const SQLWorkspacePanel = ({ dbId }: SQLWorkspacePanelProps) => {
const {
databaseName,
+ databaseType,
tables,
queryProgress,
queryError,
+ executionTime,
isExecuting,
setQuery,
handleExecuteQuery,
handleCancelQuery,
queryResults,
- hasExecutedQuery,
rowCount,
} = useDatabaseDetails({
dbId,
bridgeReady: bridgeReady ?? false,
});
+ // Persist query tabs to localStorage
+ useEffect(() => {
+ try {
+ const toSave = tabs.map(({ id, name, query }) => ({ id, name, query }));
+ localStorage.setItem(storageKey, JSON.stringify(toSave));
+ } catch {}
+ }, [tabs, storageKey]);
+
+ // Live stopwatch when executing
+ useEffect(() => {
+ if (!isExecuting) {
+ setElapsedMs(0);
+ return;
+ }
+ const start = Date.now();
+ const interval = setInterval(() => {
+ setElapsedMs(Date.now() - start);
+ }, 100);
+ return () => clearInterval(interval);
+ }, [isExecuting]);
+
// Sync with active tab
const activeTab = tabs.find((t) => t.id === activeTabId);
useEffect(() => {
if (activeTab) {
setQuery(activeTab.query);
+ setSelectedText("");
}
}, [activeTabId]);
@@ -79,34 +131,71 @@ const SQLWorkspacePanel = ({ dbId }: SQLWorkspacePanelProps) => {
[activeTabId, setQuery],
);
- // Update tab results when query completes
+ const handleRenameTab = useCallback((tabId: string, newName: string) => {
+ setTabs((prev) => prev.map((t) => (t.id === tabId ? { ...t, name: newName } : t)));
+ }, []);
+
+ const handleRunActiveQuery = useCallback(() => {
+ const queryToExecute = selectedText.trim() ? selectedText : activeTab?.query;
+ if (!queryToExecute?.trim() || isExecuting) return;
+ const targetId = activeTabId;
+ executingTabIdRef.current = targetId;
+ wasExecutingRef.current = true;
+ setTabs((prev) =>
+ prev.map((tab) => (tab.id === targetId ? { ...tab, status: "running", error: null } : tab)),
+ );
+ handleExecuteQuery(queryToExecute);
+ }, [activeTab?.query, selectedText, isExecuting, activeTabId, handleExecuteQuery]);
+
+ // Keyboard shortcut Ctrl+Enter / Cmd+Enter to execute active query or selection
useEffect(() => {
- if (hasExecutedQuery && !isExecuting) {
+ const handleKeyDown = (e: KeyboardEvent) => {
+ if ((e.ctrlKey || e.metaKey) && e.key === "Enter") {
+ e.preventDefault();
+ handleRunActiveQuery();
+ }
+ };
+ window.addEventListener("keydown", handleKeyDown);
+ return () => window.removeEventListener("keydown", handleKeyDown);
+ }, [handleRunActiveQuery]);
+
+ // Update tab results when query transitions from running to finished
+ useEffect(() => {
+ if (isExecuting) {
+ wasExecutingRef.current = true;
+ return;
+ }
+
+ if (wasExecutingRef.current && !isExecuting) {
+ wasExecutingRef.current = false;
+ const targetTabId = executingTabIdRef.current || activeTabId;
+
+ let executedQuery = "";
setTabs((prev) =>
- prev.map((tab) =>
- tab.id === activeTabId
- ? {
- ...tab,
- results: queryResults,
- rowCount: rowCount,
- error: queryError,
- status: queryError ? "error" : "success",
- executionTime: queryProgress?.elapsed || null,
- }
- : tab,
- ),
+ prev.map((tab) => {
+ if (tab.id === targetTabId) {
+ executedQuery = tab.query;
+ return {
+ ...tab,
+ results: queryResults,
+ rowCount: rowCount,
+ error: queryError,
+ status: queryError ? "error" : "success",
+ executionTime: executionTime,
+ };
+ }
+ return tab;
+ }),
);
- // Add to history
- if (activeTab?.query.trim() && !queryError) {
+ if (executedQuery.trim() && !queryError) {
setQueryHistory((prev) => {
- // Avoid duplicating the exact same query consecutively
- if (prev.length > 0 && prev[0].query === activeTab.query) {
+ if (prev.length > 0 && prev[0].query === executedQuery) {
return prev;
}
return [
{
- query: activeTab.query,
+ query: executedQuery,
timestamp: new Date(),
rowCount: rowCount,
success: true,
@@ -116,16 +205,7 @@ const SQLWorkspacePanel = ({ dbId }: SQLWorkspacePanelProps) => {
});
}
}
- }, [isExecuting, hasExecutedQuery, queryResults, rowCount, queryError]);
-
- // Update tab status when executing
- useEffect(() => {
- if (isExecuting) {
- setTabs((prev) =>
- prev.map((tab) => (tab.id === activeTabId ? { ...tab, status: "running" } : tab)),
- );
- }
- }, [isExecuting, activeTabId]);
+ }, [isExecuting, queryResults, rowCount, queryError, executionTime, activeTabId]);
const addNewTab = useCallback(() => {
const newId = Date.now().toString();
@@ -162,10 +242,21 @@ const SQLWorkspacePanel = ({ dbId }: SQLWorkspacePanelProps) => {
const insertTableQuery = useCallback(
(tableName: string, schema: string) => {
- const newQuery = `SELECT * FROM "${schema}"."${tableName}" LIMIT 100;`;
+ let newQuery = "";
+ if (databaseType === "sqlite") {
+ newQuery = `SELECT * FROM "${tableName}" LIMIT 100;`;
+ } else if (
+ databaseType === "mysql" ||
+ databaseType === "mariadb" ||
+ databaseType === "clickhouse"
+ ) {
+ newQuery = `SELECT * FROM \`${schema}\`.\`${tableName}\` LIMIT 100;`;
+ } else {
+ newQuery = `SELECT * FROM "${schema}"."${tableName}" LIMIT 100;`;
+ }
updateActiveTabQuery(newQuery);
},
- [updateActiveTabQuery],
+ [databaseType, updateActiveTabQuery],
);
const loadFromHistory = useCallback(
@@ -209,9 +300,11 @@ const SQLWorkspacePanel = ({ dbId }: SQLWorkspacePanelProps) => {
databaseName={databaseName || "Database"}
isExecuting={isExecuting}
queryProgress={queryProgress}
- canExecute={!!activeTab?.query.trim()}
- activeQuery={activeTab?.query}
- onExecute={handleExecuteQuery}
+ canExecute={!!activeTab?.query.trim() || !!selectedText.trim()}
+ activeQuery={selectedText.trim() ? selectedText : activeTab?.query}
+ hasSelection={!!selectedText.trim()}
+ elapsedMs={elapsedMs}
+ onExecute={handleRunActiveQuery}
onCancel={handleCancelQuery}
onNLQueryClick={() => setIsNLDialogOpen(true)}
/>
@@ -237,6 +330,7 @@ const SQLWorkspacePanel = ({ dbId }: SQLWorkspacePanelProps) => {
onTabSelect={setActiveTabId}
onTabClose={closeTab}
onNewTab={addNewTab}
+ onTabRename={handleRenameTab}
/>
{/* Split View: Editor + Results */}
@@ -246,6 +340,11 @@ const SQLWorkspacePanel = ({ dbId }: SQLWorkspacePanelProps) => {
void;
+ onSelectionChange?: (selectedText: string) => void;
}
// Light theme customization
@@ -58,6 +60,8 @@ const SqlEditor: FC = ({
placeholder = "-- Enter your SQL query here\nSELECT * FROM users;",
minHeight = "200px",
dialect = "postgresql",
+ onExecute,
+ onSelectionChange,
}) => {
const { theme } = useTheme();
const isDark =
@@ -80,6 +84,22 @@ const SqlEditor: FC = ({
sql({ dialect: sqlDialect }),
EditorView.lineWrapping,
EditorView.editable.of(!disabled),
+ ...(onExecute
+ ? [
+ keymap.of([
+ {
+ key: "Mod-Enter",
+ run: () => {
+ if (!disabled) {
+ onExecute();
+ return true;
+ }
+ return false;
+ },
+ },
+ ]),
+ ]
+ : []),
isDark ? darkThemeExtension : lightTheme,
];
@@ -88,6 +108,13 @@ const SqlEditor: FC = ({
{
+ if (onSelectionChange && (viewUpdate.selectionSet || viewUpdate.docChanged)) {
+ const { from, to } = viewUpdate.state.selection.main;
+ const selected = from !== to ? viewUpdate.state.sliceDoc(from, to) : "";
+ onSelectionChange(selected);
+ }
+ }}
extensions={extensions}
theme={isDark ? oneDark : "light"}
placeholder={placeholder}
diff --git a/src/features/workspace/components/WorkspaceHeader.tsx b/src/features/workspace/components/WorkspaceHeader.tsx
index da27bf3f..50a032bc 100644
--- a/src/features/workspace/components/WorkspaceHeader.tsx
+++ b/src/features/workspace/components/WorkspaceHeader.tsx
@@ -9,6 +9,8 @@ interface WorkspaceHeaderProps {
queryProgress: { rows: number; elapsed: number } | null;
canExecute: boolean;
activeQuery?: string;
+ hasSelection?: boolean;
+ elapsedMs?: number;
onExecute: () => void;
onCancel: () => void;
onNLQueryClick: () => void;
@@ -21,6 +23,8 @@ export function WorkspaceHeader({
queryProgress,
canExecute,
activeQuery,
+ hasSelection = false,
+ elapsedMs = 0,
onExecute,
onCancel,
onNLQueryClick,
@@ -37,11 +41,11 @@ export function WorkspaceHeader({
{/* Execution status */}
{isExecuting && (
-
+
{queryProgress?.rows || 0} rows
•
- {queryProgress?.elapsed || 0}s
+ {(elapsedMs / 1000).toFixed(1)}s
)}
@@ -66,14 +70,28 @@ export function WorkspaceHeader({
{/* Run/Stop buttons */}
{isExecuting ? (
-
+
- Stop
+ Stop ({(elapsedMs / 1000).toFixed(1)}s)
) : (
-
-
- Run Query
+
+
+ {hasSelection ? "Run Selection" : "Run Query"}
+
+ Ctrl+↵
+
)}
diff --git a/src/lib/databases.tsx b/src/lib/databases.tsx
new file mode 100644
index 00000000..81c1e704
--- /dev/null
+++ b/src/lib/databases.tsx
@@ -0,0 +1,377 @@
+import type { ReactNode } from "react";
+import type { DatabaseType } from "@/features/database/types";
+import { cn } from "@/lib/utils";
+import { Database } from "lucide-react";
+
+export type SupportedDatabaseType = DatabaseType | "mariadb";
+
+export interface DatatypeCategory {
+ category: string;
+ types: string[];
+}
+
+export interface ForeignKeySupport {
+ supported: boolean;
+ notes: string;
+}
+
+export interface DatabaseConfig {
+ id: SupportedDatabaseType;
+ type: SupportedDatabaseType;
+ name: string;
+ icon: ReactNode;
+ deviconClass?: string;
+ category: "Relational" | "Embedded" | "OLAP";
+ description: string;
+ defaultPort?: string;
+ defaultUser?: string;
+ defaultDatabase?: string;
+ placeholderUrl?: string;
+ isFileBased?: boolean;
+ fileExtensions?: string[];
+ supportsSsl?: boolean;
+ supportsSsh?: boolean;
+ foreignKeySupport: ForeignKeySupport;
+ acceptedDatatypes: DatatypeCategory[];
+}
+
+export const databases: DatabaseConfig[] = [
+ {
+ id: "postgresql",
+ type: "postgresql",
+ name: "PostgreSQL",
+ category: "Relational",
+ description: "Powerful open-source object-relational SQL database",
+ defaultPort: "5432",
+ defaultUser: "postgres",
+ defaultDatabase: "postgres",
+ placeholderUrl: "postgresql://postgres:password@localhost:5432/mydb",
+ supportsSsl: true,
+ supportsSsh: true,
+ deviconClass: "devicon-postgresql-plain colored",
+ foreignKeySupport: {
+ supported: true,
+ notes:
+ "Robust support including ON DELETE CASCADE, ON UPDATE, and deferred constraint checks.",
+ },
+ acceptedDatatypes: [
+ { category: "Integers", types: ["INT", "BIGINT", "SMALLINT", "SERIAL", "BIGSERIAL"] },
+ {
+ category: "Decimals and Floats",
+ types: ["NUMERIC", "DOUBLE PRECISION", "REAL", "DECIMAL"],
+ },
+ { category: "Text Strings", types: ["VARCHAR", "TEXT", "CHAR"] },
+ {
+ category: "Date and Time",
+ types: ["DATE", "TIMESTAMP", "TIMESTAMPTZ", "INTERVAL", "TIME"],
+ },
+ { category: "Boolean Flags", types: ["BOOLEAN"] },
+ { category: "Complex and Arrays", types: ["JSON", "JSONB", "UUID", "BYTEA", "ARRAY"] },
+ ],
+ icon: (
+
+
+
+
+
+
+ ),
+ },
+ {
+ id: "mysql",
+ type: "mysql",
+ name: "MySQL",
+ category: "Relational",
+ description: "High-performance open-source relational database",
+ defaultPort: "3306",
+ defaultUser: "root",
+ defaultDatabase: "mysql",
+ placeholderUrl: "mysql://root:password@localhost:3306/mydb",
+ supportsSsl: false,
+ supportsSsh: true,
+ deviconClass: "devicon-mysql-original colored",
+ foreignKeySupport: {
+ supported: true,
+ notes: "Enforced natively when using the default InnoDB transactional storage engine.",
+ },
+ acceptedDatatypes: [
+ { category: "Integers", types: ["INT", "BIGINT", "TINYINT", "SMALLINT", "MEDIUMINT"] },
+ { category: "Decimals and Floats", types: ["DECIMAL", "DOUBLE", "FLOAT"] },
+ { category: "Text Strings", types: ["VARCHAR", "TEXT", "CHAR", "MEDIUMTEXT", "LONGTEXT"] },
+ { category: "Date and Time", types: ["DATE", "DATETIME", "TIMESTAMP", "TIME"] },
+ { category: "Boolean Flags", types: ["BOOLEAN", "TINYINT(1)"] },
+ { category: "Complex and Arrays", types: ["JSON"] },
+ ],
+ icon: (
+
+
+
+ ),
+ },
+ {
+ id: "mariadb",
+ type: "mariadb",
+ name: "MariaDB",
+ category: "Relational",
+ description: "Fast, community-driven relational database fork of MySQL",
+ defaultPort: "3306",
+ defaultUser: "root",
+ defaultDatabase: "mariadb",
+ placeholderUrl: "mariadb://root:password@localhost:3306/mydb",
+ supportsSsl: true,
+ supportsSsh: true,
+ deviconClass: "devicon-mariadb-original colored",
+ foreignKeySupport: {
+ supported: true,
+ notes: "Enforced natively when using the default InnoDB transactional storage engine.",
+ },
+ acceptedDatatypes: [
+ { category: "Integers", types: ["INT", "BIGINT", "TINYINT", "SMALLINT", "MEDIUMINT"] },
+ { category: "Decimals and Floats", types: ["DECIMAL", "DOUBLE", "FLOAT"] },
+ { category: "Text Strings", types: ["VARCHAR", "TEXT", "CHAR", "MEDIUMTEXT", "LONGTEXT"] },
+ { category: "Date and Time", types: ["DATE", "DATETIME", "TIMESTAMP", "TIME"] },
+ { category: "Boolean Flags", types: ["BOOLEAN", "TINYINT(1)"] },
+ { category: "Complex and Arrays", types: ["JSON"] },
+ ],
+ icon: (
+
+
+
+ ),
+ },
+ {
+ id: "sqlite",
+ type: "sqlite",
+ name: "SQLite",
+ category: "Embedded",
+ description: "Lightweight, zero-config file-based embedded SQL database",
+ isFileBased: true,
+ fileExtensions: ["db", "sqlite", "sqlite3", "s3db"],
+ deviconClass: "devicon-sqlite-plain colored",
+ foreignKeySupport: {
+ supported: true,
+ notes:
+ "Syntax is supported but disabled by default. Must run 'PRAGMA foreign_keys = ON;' per session.",
+ },
+ acceptedDatatypes: [
+ { category: "Integers", types: ["INTEGER", "INT", "BIGINT", "SMALLINT", "TINYINT"] },
+ { category: "Decimals and Floats", types: ["REAL", "NUMERIC", "FLOAT", "DOUBLE"] },
+ { category: "Text Strings", types: ["TEXT", "VARCHAR", "CHAR", "CLOB"] },
+ { category: "Date and Time", types: ["DATETIME", "DATE", "TIMESTAMP"] },
+ { category: "Boolean Flags", types: ["BOOLEAN"] },
+ { category: "Complex and Arrays", types: ["BLOB", "JSON"] },
+ ],
+ icon: (
+
+
+
+
+
+
+
+
+
+
+
+
+ ),
+ },
+ {
+ id: "duckdb",
+ type: "duckdb",
+ name: "DuckDB",
+ category: "Embedded",
+ description: "Fast in-process columnar analytical SQL database file",
+ isFileBased: true,
+ fileExtensions: ["db", "duckdb", "ddb"],
+ foreignKeySupport: {
+ supported: true,
+ notes:
+ "Enforced during INSERT statements for data validation, but lacks cascading delete/update mechanisms.",
+ },
+ acceptedDatatypes: [
+ {
+ category: "Integers",
+ types: ["INT", "BIGINT", "HUGEINT", "SMALLINT", "TINYINT", "UTINYINT", "UINT", "UBIGINT"],
+ },
+ { category: "Decimals and Floats", types: ["DECIMAL", "DOUBLE", "FLOAT"] },
+ { category: "Text Strings", types: ["VARCHAR", "TEXT"] },
+ { category: "Date and Time", types: ["DATE", "TIMESTAMP", "TIME", "INTERVAL"] },
+ { category: "Boolean Flags", types: ["BOOLEAN"] },
+ { category: "Complex and Arrays", types: ["ARRAY", "LIST", "STRUCT", "MAP", "JSON", "UUID"] },
+ ],
+ icon: (
+
+
+
+
+ ),
+ },
+ {
+ id: "clickhouse",
+ type: "clickhouse",
+ name: "ClickHouse",
+ category: "OLAP",
+ description: "Ultra-fast columnar database for real-time analytics",
+ defaultPort: "8123",
+ defaultUser: "default",
+ defaultDatabase: "default",
+ placeholderUrl: "http://default:password@localhost:8123/default",
+ supportsSsl: true,
+ supportsSsh: false,
+ foreignKeySupport: {
+ supported: false,
+ notes:
+ "Analytical column-store. Does not support relational constraints; structural relations rely entirely on JOINs.",
+ },
+ acceptedDatatypes: [
+ {
+ category: "Integers",
+ types: ["Int8", "Int16", "Int32", "Int64", "UInt8", "UInt16", "UInt32", "UInt64"],
+ },
+ { category: "Decimals and Floats", types: ["Decimal", "Float32", "Float64"] },
+ { category: "Text Strings", types: ["String", "FixedString"] },
+ { category: "Date and Time", types: ["Date", "Date32", "DateTime", "DateTime64"] },
+ { category: "Boolean Flags", types: ["Bool"] },
+ { category: "Complex and Arrays", types: ["Array(T)", "Map(K, V)", "Tuple", "JSON", "UUID"] },
+ ],
+ icon: (
+
+
+
+
+ ),
+ },
+];
+
+export const SUPPORTED_DATABASES = databases;
+
+export const STANDARD_SQL_DATATYPES: DatatypeCategory[] = [
+ { category: "Integers", types: ["INT", "INTEGER", "BIGINT", "SMALLINT"] },
+ { category: "Decimals and Floats", types: ["DECIMAL", "FLOAT", "NUMERIC", "REAL"] },
+ { category: "Text Strings", types: ["VARCHAR", "CHAR", "TEXT"] },
+ { category: "Date and Time", types: ["DATE", "TIMESTAMP", "TIME"] },
+ { category: "Boolean Flags", types: ["BOOLEAN"] },
+ { category: "Complex and Arrays", types: ["ARRAY", "JSON"] },
+];
+
+export function getDatabaseMeta(typeOrName?: string | null): DatabaseConfig | undefined {
+ if (!typeOrName) return undefined;
+ const normalized = typeOrName.toLowerCase().trim();
+ return databases.find(
+ (db) => db.id === normalized || db.type === normalized || db.name.toLowerCase() === normalized,
+ );
+}
+
+export function getDatabaseForeignKeySupport(typeOrName?: string | null): ForeignKeySupport {
+ const meta = getDatabaseMeta(typeOrName);
+ return (
+ meta?.foreignKeySupport ?? {
+ supported: true,
+ notes: "Fully supported natively as part of standard relational constraints.",
+ }
+ );
+}
+
+export function getDatabaseAcceptedDatatypes(typeOrName?: string | null): DatatypeCategory[] {
+ const meta = getDatabaseMeta(typeOrName);
+ const rawCategories = meta?.acceptedDatatypes ?? STANDARD_SQL_DATATYPES;
+ const seen = new Set
();
+
+ return rawCategories
+ .map((group) => ({
+ category: group.category,
+ types: group.types.filter((type) => {
+ const lower = type.toLowerCase();
+ if (seen.has(lower)) return false;
+ seen.add(lower);
+ return true;
+ }),
+ }))
+ .filter((group) => group.types.length > 0);
+}
+
+export function getAllAcceptedDatatypes(typeOrName?: string | null): string[] {
+ const categories = getDatabaseAcceptedDatatypes(typeOrName);
+ return categories.flatMap((cat) => cat.types);
+}
+
+export function getDefaultColumnType(typeOrName?: string | null): string {
+ const normalized = typeOrName?.toLowerCase().trim();
+ if (normalized === "clickhouse") return "String";
+ if (normalized === "sqlite") return "INTEGER";
+ return "VARCHAR";
+}
+
+export function getDatabaseIcon(typeOrName?: string | null): ReactNode | null {
+ return getDatabaseMeta(typeOrName)?.icon ?? null;
+}
+
+export function DatabaseLogo({
+ type,
+ className = "w-5 h-5",
+ useDevicon = false,
+}: {
+ type?: string | null;
+ className?: string;
+ useDevicon?: boolean;
+}) {
+ const meta = getDatabaseMeta(type);
+
+ if (!meta) {
+ return ;
+ }
+
+ if (useDevicon && meta.deviconClass) {
+ return ;
+ }
+
+ return (
+ svg]:w-full [&>svg]:h-full",
+ className,
+ )}
+ >
+ {meta.icon}
+
+ );
+}
diff --git a/src/lib/parseConnectionUrl.ts b/src/lib/parseConnectionUrl.ts
index d8498a94..54d951e5 100644
--- a/src/lib/parseConnectionUrl.ts
+++ b/src/lib/parseConnectionUrl.ts
@@ -41,6 +41,83 @@ function encodeSqlitePathSegments(database: string): string {
.join("/");
}
+export function cleanClickHouseUrl(raw?: string): string {
+ let url = (raw || "").trim();
+ if (!url) return "";
+
+ // Strip markdown link brackets e.g. [https://...](https://...)
+ const mdMatch = url.match(/\[(.*?)\]\((.*?)\)/);
+ if (mdMatch) {
+ url = mdMatch[2] || mdMatch[1];
+ }
+
+ // Strip enclosing quotes
+ url = url.replace(/^['"`]+|['"`]+$/g, "").trim();
+
+ // If starts with clickhouses:// -> https://
+ if (url.startsWith("clickhouses://")) {
+ url = "https://" + url.slice("clickhouses://".length);
+ } else if (url.startsWith("clickhouse://")) {
+ const rest = url.slice("clickhouse://".length);
+ const isSsl =
+ rest.includes(":8443") || rest.includes(":443") || rest.includes("clickhouse.cloud");
+ url = (isSsl ? "https://" : "http://") + rest;
+ }
+
+ // If no scheme was provided, determine http vs https
+ if (!url.startsWith("http://") && !url.startsWith("https://")) {
+ const isSsl = url.includes(":8443") || url.includes(":443") || url.includes("clickhouse.cloud");
+ url = (isSsl ? "https://" : "http://") + url;
+ }
+
+ // Strip trailing slashes
+ url = url.replace(/\/+$/, "");
+
+ return url;
+}
+
+export function parseClickHouseUrl(raw?: string): {
+ url: string;
+ host: string;
+ port: string;
+ ssl: boolean;
+ database: string;
+ user: string;
+ password?: string;
+} {
+ const cleanUrl = cleanClickHouseUrl(raw);
+ try {
+ const u = new URL(cleanUrl);
+ const ssl = u.protocol === "https:";
+ const defaultPort = ssl ? "8443" : "8123";
+ const port = u.port || defaultPort;
+ const database = decodeURIComponent(u.pathname.replace(/^\//, "")) || "default";
+ const user = decodeURIComponent(u.username) || "default";
+ const password = decodeURIComponent(u.password) || undefined;
+
+ return {
+ url: cleanUrl,
+ host: u.hostname,
+ port,
+ ssl,
+ database,
+ user,
+ password,
+ };
+ } catch {
+ const isSsl = cleanUrl.includes(":8443") || cleanUrl.startsWith("https:");
+ const port = isSsl ? "8443" : "8123";
+ return {
+ url: cleanUrl,
+ host: cleanUrl,
+ port,
+ ssl: isSsl,
+ database: "default",
+ user: "default",
+ };
+ }
+}
+
export function parseConnectionUrl(url: string): ParsedConnectionUrl | null {
try {
// Trim whitespace
@@ -50,10 +127,11 @@ export function parseConnectionUrl(url: string): ParsedConnectionUrl | null {
if (!url) return null;
// Parse the URL
- const parsed = new URL(url);
+ const parsed = new URL(cleanClickHouseUrl(url));
// Determine database type from protocol
let type = "";
+ let isSslProtocol = false;
const protocol = parsed.protocol.replace(":", "").toLowerCase();
if (protocol === "postgres" || protocol === "postgresql") {
@@ -64,6 +142,18 @@ export function parseConnectionUrl(url: string): ParsedConnectionUrl | null {
type = "sqlite";
} else if (protocol === "duckdb") {
type = "duckdb";
+ } else if (protocol === "clickhouse" || protocol === "clickhouses") {
+ type = "clickhouse";
+ if (protocol === "clickhouses") {
+ isSslProtocol = true;
+ }
+ } else if (
+ parsed.hostname.includes("clickhouse") ||
+ parsed.port === "8443" ||
+ parsed.port === "8123"
+ ) {
+ type = "clickhouse";
+ isSslProtocol = protocol === "https";
} else {
return null; // Unsupported protocol
}
@@ -85,16 +175,26 @@ export function parseConnectionUrl(url: string): ParsedConnectionUrl | null {
// Extract components
const host = parsed.hostname || "localhost";
- const port = parsed.port || (type === "postgresql" ? "5432" : "3306");
- const user = decodeURIComponent(parsed.username || "");
+ const defaultPort =
+ type === "postgresql"
+ ? "5432"
+ : type === "clickhouse"
+ ? isSslProtocol
+ ? "8443"
+ : "8123"
+ : "3306";
+ const port = parsed.port || defaultPort;
+ const user = decodeURIComponent(parsed.username || (type === "clickhouse" ? "default" : ""));
const password = decodeURIComponent(parsed.password || "");
// Database name is the pathname without leading slash
- const database = decodeURIComponent(parsed.pathname.replace(/^\//, "") || "");
+ const database = decodeURIComponent(
+ parsed.pathname.replace(/^\//, "") || (type === "clickhouse" ? "default" : ""),
+ );
// Parse SSL parameters from query string
- const sslmode = parsed.searchParams.get("sslmode") || "";
- const ssl = sslmode !== "" && sslmode !== "disable";
+ const sslmode = parsed.searchParams.get("sslmode") || (isSslProtocol ? "require" : "");
+ const ssl = isSslProtocol || (sslmode !== "" && sslmode !== "disable");
return {
type,
@@ -127,14 +227,23 @@ export function buildConnectionUrl(params: {
if (params.type === "sqlite" || params.type === "duckdb") {
return `${params.type}:///${encodeSqlitePathSegments(params.database)}`;
}
- const protocol = params.type === "mysql" ? "mysql" : "postgres";
+ const isClickHouseSsl =
+ params.type === "clickhouse" && (params.sslmode === "require" || params.port === "8443");
+ const protocol =
+ params.type === "mysql"
+ ? "mysql"
+ : params.type === "clickhouse"
+ ? isClickHouseSsl
+ ? "clickhouses"
+ : "clickhouse"
+ : "postgres";
const auth = params.password
? `${encodeURIComponent(params.user)}:${encodeURIComponent(params.password)}`
: encodeURIComponent(params.user);
let url = `${protocol}://${auth}@${params.host}:${params.port}/${encodeURIComponent(params.database)}`;
- if (params.sslmode && params.sslmode !== "disable") {
+ if (params.sslmode && params.sslmode !== "disable" && !isClickHouseSsl) {
url += `?sslmode=${params.sslmode}`;
}
diff --git a/src/lib/themes.ts b/src/lib/themes.ts
index 9bf5943b..b804f66e 100644
--- a/src/lib/themes.ts
+++ b/src/lib/themes.ts
@@ -1,79 +1,14 @@
-export type ThemeVariant =
- | "blue"
- | "slate"
- | "green"
- | "purple"
- | "orange"
- | "rose"
- | "cyberpunk"
- | "vscode"
- | "valorant"
- | "ghibli";
+export type ThemeVariant = "blue" | "slate" | "green" | "purple" | "orange" | "rose";
export interface ThemeVariantConfig {
name: string;
- description?: string;
/** Swatch color shown in the picker */
primary: string;
primaryForeground: string;
ring: string;
- /** If true, this theme overrides the full palette (not just the accent) */
- fullPalette?: boolean;
- /**
- * Single palette applied regardless of light/dark mode.
- * Used by themes that are always dark (Cyberpunk, VS Code).
- */
- palette?: ThemePalette;
- /**
- * Light-mode palette. When set alongside darkPalette, the hook will
- * automatically switch between them as the user toggles light/dark mode.
- */
- lightPalette?: ThemePalette;
- /** Dark-mode palette. Used together with lightPalette. */
- darkPalette?: ThemePalette;
- /** Optional gradient/icon style for the picker card */
- previewGradient?: string;
-}
-
-export interface ThemePalette {
- background: string;
- foreground: string;
- card: string;
- cardForeground: string;
- popover: string;
- popoverForeground: string;
- primary: string;
- primaryForeground: string;
- secondary: string;
- secondaryForeground: string;
- muted: string;
- mutedForeground: string;
- accent: string;
- accentForeground: string;
- destructive: string;
- border: string;
- input: string;
- ring: string;
- sidebar: string;
- sidebarForeground: string;
- /** Overrides --sidebar-primary. Falls back to `primary` when omitted. */
- sidebarPrimary?: string;
- sidebarPrimaryForeground?: string;
- sidebarAccent: string;
- sidebarAccentForeground: string;
- sidebarBorder: string;
- /** Overrides --sidebar-ring. Falls back to `ring` when omitted. */
- sidebarRing?: string;
- cardShadow?: string;
- glassBg?: string;
- surface?: string;
- surfaceElevated?: string;
- surfaceSubtle?: string;
- borderSubtle?: string;
}
export const themeVariants: Record = {
- // ── Standard accent-only themes ──────────────────────────────────────────
blue: {
name: "Blue",
primary: "oklch(0.7 0.15 250)",
@@ -110,268 +45,6 @@ export const themeVariants: Record = {
primaryForeground: "oklch(0.98 0 0)",
ring: "oklch(0.7 0.22 10)",
},
-
- // ── Full-palette themes ───────────────────────────────────────────────────
-
- /**
- * Cyberpunk
- * Electric neon-cyan on a near-black background with magenta accents.
- * Always dark — ignores the system light/dark toggle.
- */
- cyberpunk: {
- name: "Cyberpunk",
- description: "Neon cyan & magenta on deep dark",
- primary: "#00f5ff",
- primaryForeground: "#000",
- ring: "#00f5ff",
- fullPalette: true,
- previewGradient: "linear-gradient(135deg, #00f5ff 0%, #ff00c8 100%)",
- palette: {
- background: "#070b14",
- foreground: "#e2f0ff",
- card: "#0d1526",
- cardForeground: "#e2f0ff",
- popover: "#0d1526",
- popoverForeground: "#e2f0ff",
- primary: "#00f5ff",
- primaryForeground: "#030c12",
- secondary: "#0f1e35",
- secondaryForeground: "#a8cfef",
- muted: "#0d1a2e",
- mutedForeground: "#6a8faf",
- accent: "#ff00c8",
- accentForeground: "#fff",
- destructive: "#ff3366",
- border: "#0e2540",
- input: "#0a1a2e",
- ring: "#00f5ff",
- sidebar: "#060d1c",
- sidebarForeground: "#cde8ff",
- sidebarAccent: "#0f2240",
- sidebarAccentForeground: "#00f5ff",
- sidebarBorder: "rgba(0,245,255,0.08)",
- cardShadow: "0 0 0 1px rgba(0,245,255,0.06), 0 8px 32px rgba(0,0,0,0.6)",
- glassBg: "rgba(7,11,20,0.85)",
- surface: "#0a1220",
- surfaceElevated: "#0d1526",
- surfaceSubtle: "#0f1d32",
- borderSubtle: "rgba(0,245,255,0.07)",
- },
- },
-
- /**
- * VS Code
- * Faithful recreation of VS Code's default Dark+ palette.
- * Deep navy backgrounds, cool gray text, blue accent.
- */
- vscode: {
- name: "VS Code",
- description: "Inspired by VS Code Dark+",
- primary: "#569cd6",
- primaryForeground: "#1e1e1e",
- ring: "#569cd6",
- fullPalette: true,
- previewGradient: "linear-gradient(135deg, #1e1e1e 0%, #264f78 50%, #569cd6 100%)",
- palette: {
- background: "#1e1e1e",
- foreground: "#d4d4d4",
- card: "#252526",
- cardForeground: "#d4d4d4",
- popover: "#2d2d30",
- popoverForeground: "#d4d4d4",
- primary: "#569cd6",
- primaryForeground: "#1e1e1e",
- secondary: "#2d2d30",
- secondaryForeground: "#cccccc",
- muted: "#333333",
- mutedForeground: "#808080",
- accent: "#264f78",
- accentForeground: "#d4d4d4",
- destructive: "#f44747",
- border: "#3e3e42",
- input: "#3c3c3c",
- ring: "#569cd6",
- sidebar: "#252526",
- sidebarForeground: "#bbbbbb",
- sidebarAccent: "#37373d",
- sidebarAccentForeground: "#ffffff",
- sidebarBorder: "#3e3e42",
- cardShadow: "0 1px 0 rgba(255,255,255,0.04) inset, 0 8px 24px rgba(0,0,0,0.4)",
- glassBg: "rgba(37,37,38,0.92)",
- surface: "#252526",
- surfaceElevated: "#2d2d30",
- surfaceSubtle: "#333333",
- borderSubtle: "#3e3e42",
- },
- },
-
- /**
- * Valorant
- * Inspired by Riot Games' Valorant UI — vivid red-orange primary on warm
- * cream/rose backgrounds in light mode; deep dark reddish tones in dark mode.
- * Supports both light and dark modes, switching automatically with the toggle.
- */
- valorant: {
- name: "Valorant",
- description: "Riot red on warm cream & dark",
- primary: "oklch(0.67 0.22 21.34)",
- primaryForeground: "oklch(0.99 0 360)",
- ring: "oklch(0.92 0.04 12.39)",
- fullPalette: true,
- previewGradient:
- "linear-gradient(135deg, oklch(0.97 0.02 12.78) 0%, oklch(0.67 0.22 21.34) 50%, oklch(0.16 0.03 17.48) 100%)",
- // Light palette — warm cream/rose base with Valorant red accent
- lightPalette: {
- background: "oklch(0.97 0.02 12.78)",
- foreground: "oklch(0.24 0.07 17.81)",
- card: "oklch(0.98 0.01 17.28)",
- cardForeground: "oklch(0.26 0.07 19)",
- popover: "oklch(0.98 0.01 17.28)",
- popoverForeground: "oklch(0.26 0.07 19)",
- primary: "oklch(0.67 0.22 21.34)",
- primaryForeground: "oklch(0.99 0.00 359.99)",
- secondary: "oklch(0.95 0.02 11.28)",
- secondaryForeground: "oklch(0.24 0.07 17.81)",
- muted: "oklch(0.98 0.01 17.28)",
- mutedForeground: "oklch(0.55 0.08 19)",
- accent: "oklch(0.99 0.00 359.99)",
- accentForeground: "oklch(0.43 0.13 20.62)",
- destructive: "oklch(0.80 0.17 73.27)",
- border: "oklch(0.91 0.05 11.40)",
- input: "oklch(0.90 0.05 12.59)",
- ring: "oklch(0.92 0.04 12.39)",
- sidebar: "oklch(0.97 0.02 12.78)",
- sidebarForeground: "oklch(0.26 0.07 19)",
- sidebarAccent: "oklch(0.98 0.01 17.28)",
- sidebarAccentForeground: "oklch(0.43 0.13 20.62)",
- sidebarBorder: "oklch(0.91 0.05 11.40)",
- cardShadow:
- "0px 0px 3px 0px oklch(0.3 0.0891 19.6 / 0.08), 0px 2px 4px -1px oklch(0.3 0.0891 19.6 / 0.08)",
- glassBg: "rgba(253, 248, 247, 0.82)",
- surface: "oklch(0.97 0.02 12.78)",
- surfaceElevated: "oklch(0.98 0.01 17.28)",
- surfaceSubtle: "oklch(0.95 0.02 11.28)",
- borderSubtle: "oklch(0.91 0.05 11.40)",
- },
- // Dark palette — deep near-black reddish backgrounds, same red accent
- darkPalette: {
- background: "oklch(0.16 0.03 17.48)",
- foreground: "oklch(0.99 0.00 359.99)",
- card: "oklch(0.21 0.05 19.26)",
- cardForeground: "oklch(0.98 0 0)",
- popover: "oklch(0.26 0.07 19)",
- popoverForeground: "oklch(0.99 0.00 359.99)",
- primary: "oklch(0.67 0.22 21.34)",
- primaryForeground: "oklch(0.99 0.00 359.99)",
- secondary: "oklch(0.3 0.0891 19.6)",
- secondaryForeground: "oklch(0.95 0.02 11.28)",
- muted: "oklch(0.26 0.07 19)",
- mutedForeground: "oklch(0.72 0.04 18)",
- accent: "oklch(0.43 0.13 20.62)",
- accentForeground: "oklch(0.99 0.00 359.99)",
- destructive: "oklch(0.80 0.17 73.27)",
- border: "oklch(0.31 0.09 19.80)",
- input: "oklch(0.39 0.12 20.37)",
- ring: "oklch(0.50 0.16 20.89)",
- sidebar: "oklch(0.26 0.07 19)",
- sidebarForeground: "oklch(0.99 0.00 359.99)",
- sidebarAccent: "oklch(0.43 0.13 20.62)",
- sidebarAccentForeground: "oklch(0.99 0.00 359.99)",
- sidebarBorder: "oklch(0.39 0.12 20.37)",
- cardShadow: "0 1px 3px 0px oklch(0.00 0 0 / 0.10), 0 2px 4px -1px oklch(0.00 0 0 / 0.10)",
- glassBg: "rgba(33, 14, 12, 0.82)",
- surface: "oklch(0.21 0.05 19.26)",
- surfaceElevated: "oklch(0.26 0.07 19)",
- surfaceSubtle: "oklch(0.3 0.0891 19.6)",
- borderSubtle: "oklch(0.31 0.09 19.80)",
- },
- },
-
- /**
- * Ghibli Studio
- * Inspired by Studio Ghibli's warm, earthy colour palette — golden sandy
- * backgrounds with muted olive-green accents in light mode; deep dark warm
- * browns with sage green highlights in dark mode.
- * Supports both light and dark modes.
- */
- ghibli: {
- name: "Ghibli",
- description: "Earthy sage & golden warm tones",
- primary: "oklch(0.71 0.10 111.99)",
- primaryForeground: "oklch(0.98 0.01 3.71)",
- ring: "oklch(0.51 0.08 74.26)",
- fullPalette: true,
- previewGradient:
- "linear-gradient(135deg, oklch(0.91 0.05 82.69) 0%, oklch(0.71 0.10 111.99) 50%, oklch(0.20 0.01 48.35) 100%)",
- lightPalette: {
- background: "oklch(0.91 0.05 82.69)",
- foreground: "oklch(0.41 0.08 79.04)",
- card: "oklch(0.92 0.04 83.86)",
- cardForeground: "oklch(0.41 0.08 73.75)",
- popover: "oklch(0.92 0.04 83.86)",
- popoverForeground: "oklch(0.41 0.08 73.75)",
- primary: "oklch(0.71 0.10 111.99)",
- primaryForeground: "oklch(0.98 0.01 3.71)",
- secondary: "oklch(0.88 0.05 83.41)",
- secondaryForeground: "oklch(0.51 0.08 79.21)",
- muted: "oklch(0.86 0.06 83.48)",
- mutedForeground: "oklch(0.51 0.08 74.26)",
- accent: "oklch(0.86 0.05 84.50)",
- accentForeground: "oklch(0.26 0.02 358.42)",
- destructive: "oklch(0.63 0.24 29.21)",
- border: "oklch(0.74 0.06 79.81)",
- input: "oklch(0.74 0.06 79.81)",
- ring: "oklch(0.51 0.08 74.26)",
- sidebar: "oklch(0.87 0.06 83.96)",
- sidebarForeground: "oklch(0.41 0.08 79.04)",
- sidebarPrimary: "oklch(0.26 0.02 358.42)",
- sidebarPrimaryForeground: "oklch(0.98 0.01 3.71)",
- sidebarAccent: "oklch(0.83 0.06 84.46)",
- sidebarAccentForeground: "oklch(0.26 0.02 358.42)",
- sidebarBorder: "oklch(0.91 0.00 0.43)",
- sidebarRing: "oklch(0.71 0.00 0.37)",
- cardShadow: "0 1px 3px 0px oklch(0.00 0 0 / 0.08), 0 2px 4px -1px oklch(0.00 0 0 / 0.08)",
- glassBg: "rgba(244, 237, 216, 0.80)",
- surface: "oklch(0.91 0.05 82.69)",
- surfaceElevated: "oklch(0.92 0.04 83.86)",
- surfaceSubtle: "oklch(0.88 0.05 83.41)",
- borderSubtle: "oklch(0.74 0.06 79.81)",
- },
- darkPalette: {
- background: "oklch(0.20 0.01 48.35)",
- foreground: "oklch(0.88 0.05 79.26)",
- card: "oklch(0.25 0.01 56.14)",
- cardForeground: "oklch(0.88 0.05 79.26)",
- popover: "oklch(0.25 0.01 56.14)",
- popoverForeground: "oklch(0.88 0.05 79.26)",
- primary: "oklch(0.64 0.05 115.39)",
- primaryForeground: "oklch(0.98 0.01 3.71)",
- secondary: "oklch(0.33 0.02 60.70)",
- secondaryForeground: "oklch(0.88 0.05 83.41)",
- muted: "oklch(0.27 0.01 39.35)",
- mutedForeground: "oklch(0.74 0.06 79.81)",
- accent: "oklch(0.33 0.02 60.70)",
- accentForeground: "oklch(0.86 0.05 84.50)",
- destructive: "oklch(0.63 0.24 29.21)",
- border: "oklch(0.33 0.02 60.70)",
- input: "oklch(0.33 0.02 60.70)",
- ring: "oklch(0.64 0.05 115.39)",
- sidebar: "oklch(0.23 0.01 56.09)",
- sidebarForeground: "oklch(0.88 0.05 79.26)",
- sidebarPrimary: "oklch(0.64 0.05 115.39)",
- sidebarPrimaryForeground: "oklch(0.98 0.01 3.71)",
- sidebarAccent: "oklch(0.33 0.02 60.70)",
- sidebarAccentForeground: "oklch(0.86 0.05 84.50)",
- sidebarBorder: "oklch(0.33 0.02 60.70)",
- sidebarRing: "oklch(0.64 0.05 115.39)",
- cardShadow: "0 1px 3px 0px oklch(0.00 0 0 / 0.20), 0 2px 4px -1px oklch(0.00 0 0 / 0.20)",
- glassBg: "rgba(33, 25, 14, 0.82)",
- surface: "oklch(0.25 0.01 56.14)",
- surfaceElevated: "oklch(0.27 0.01 39.35)",
- surfaceSubtle: "oklch(0.33 0.02 60.70)",
- borderSubtle: "oklch(0.33 0.02 60.70)",
- },
- },
};
export const defaultVariant: ThemeVariant = "blue";
diff --git a/src/lib/utils.ts b/src/lib/utils.ts
index 356c3a31..ed6b2c4a 100644
--- a/src/lib/utils.ts
+++ b/src/lib/utils.ts
@@ -49,6 +49,7 @@ export function formatDbType(databaseType?: string) {
if (databaseType === "mariadb") return "MariaDB";
if (databaseType === "sqlite") return "SQLite";
if (databaseType === "duckdb") return "DuckDB";
+ if (databaseType === "clickhouse") return "ClickHouse";
return databaseType;
}
export function formatDuration(seconds: number) {
diff --git a/src/main.tsx b/src/main.tsx
index 502c74b8..e4b0830d 100644
--- a/src/main.tsx
+++ b/src/main.tsx
@@ -14,6 +14,7 @@ import { UpdateNotification } from "./components/shared/UpdateNotification";
import { WhatsNewDialog } from "./components/shared/WhatsNewDialog";
import TitleBar from "./components/layout/TitleBar";
import { CommandPalette } from "./components/layout/CommandPalette";
+import { themeVariants, defaultVariant } from "@/lib/themes";
const queryClient = new QueryClient();
@@ -26,10 +27,11 @@ function ThemeVariantInitializer() {
useEffect(() => {
// Initialize theme variant from localStorage on mount
const savedVariant = localStorage.getItem("relwave-theme-variant");
- if (savedVariant) {
+ if (savedVariant && savedVariant in themeVariants) {
document.documentElement.setAttribute("data-theme-variant", savedVariant);
} else {
- document.documentElement.setAttribute("data-theme-variant", "blue");
+ document.documentElement.setAttribute("data-theme-variant", defaultVariant);
+ localStorage.setItem("relwave-theme-variant", defaultVariant);
}
}, []);
return null;
@@ -96,13 +98,13 @@ function AppRoot() {
-
-
+
diff --git a/src/pages/DatabaseDetails.tsx b/src/pages/DatabaseDetails.tsx
index a2b2a5bc..86afc2e5 100644
--- a/src/pages/DatabaseDetails.tsx
+++ b/src/pages/DatabaseDetails.tsx
@@ -10,7 +10,7 @@ import { useProjectSync } from "@/features/project/hooks/useProjectSync";
import { useProjectDir } from "@/features/project/hooks/useProjectQueries";
import { useRowOperations } from "@/features/database/hooks/useRowOperations";
import BridgeLoader from "@/components/feedback/BridgeLoader";
-import VerticalIconBar from "@/components/layout/VerticalIconBar";
+import VerticalIconBar, { getDatabasePanelItems } from "@/components/layout/VerticalIconBar";
import SlideOutPanel from "@/components/layout/SlideOutPanel";
import ConfirmDialog from "@/components/shared/ConfirmDialog";
@@ -31,7 +31,7 @@ import AIWorkspacePanel from "@/features/database/components/AIWorkspacePanel";
import { ShortcutsHelp } from "@/components/shared/ShortcutsHelp";
import { ShortcutsTrigger } from "@/components/shared/ShortcutsTrigger";
import { MigrationSyncDialog } from "@/features/project/components/MigrationSyncDialog";
-import { useState } from "react";
+import { useState, useEffect } from "react";
const DatabaseDetail = () => {
const { id: dbId } = useParams<{ id: string }>();
@@ -58,6 +58,7 @@ const DatabaseDetail = () => {
tables,
selectedTable,
tableData,
+ tableColumns,
totalRows,
currentPage,
pageSize,
@@ -100,6 +101,37 @@ const DatabaseDetail = () => {
const { data: projectDir } = useProjectDir(projectId);
const [migrationSyncDismissed, setMigrationSyncDismissed] = useState(false);
+ // Keyboard shortcut listener: Alt+1..9 switches active database panel
+ useEffect(() => {
+ const handleKeyDown = (e: KeyboardEvent) => {
+ // Don't intercept if user is typing in a form input or textarea
+ const target = e.target as HTMLElement | null;
+ if (target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement) {
+ return;
+ }
+
+ // Don't switch if a modal dialog is currently open
+ if (document.querySelector('[role="dialog"]')) {
+ return;
+ }
+
+ if (e.altKey && !e.ctrlKey && !e.metaKey && !e.shiftKey) {
+ const num = parseInt(e.key, 10);
+ if (!isNaN(num) && num >= 1 && num <= 9) {
+ const items = getDatabasePanelItems(databaseType);
+ const targetItem = items[num - 1];
+ if (targetItem) {
+ e.preventDefault();
+ setActivePanel(targetItem.panel);
+ }
+ }
+ }
+ };
+
+ window.addEventListener("keydown", handleKeyDown);
+ return () => window.removeEventListener("keydown", handleKeyDown);
+ }, [databaseType, setActivePanel]);
+
// ---- Guards ----
if (bridgeLoading || bridgeReady === undefined) return ;
if (error)
@@ -153,6 +185,7 @@ const DatabaseDetail = () => {
selectedSchema={selectedSchema}
setSelectedSchema={setSelectedSchema}
tableData={tableData}
+ tableColumns={tableColumns}
totalRows={totalRows}
currentPage={currentPage}
pageSize={pageSize}
diff --git a/src/services/bridge/ai.ts b/src/services/bridge/ai.ts
index 4e2cdead..46c6337b 100644
--- a/src/services/bridge/ai.ts
+++ b/src/services/bridge/ai.ts
@@ -117,40 +117,74 @@ const LS_LEGACY_KEY = "relwave:ai-settings";
const DEFAULT_SETTINGS: AISettings = { defaultProvider: "ollama" };
+let cachedAISettings: AISettings | null = null;
+let pendingLoadPromise: Promise | null = null;
+
+/**
+ * Synchronous accessor for currently in-memory cached AI settings.
+ * Returns null if settings haven't been loaded yet.
+ */
+export function getCachedAISettings(): AISettings | null {
+ return cachedAISettings;
+}
+
/**
- * Load AI settings from the bridge (reads ai-settings.json on disk).
+ * Load AI settings from memory cache or the bridge (reads ai-settings.json on disk).
* Falls back to empty defaults if the file doesn't exist yet.
* Also performs a one-time migration of any settings previously saved in localStorage.
+ *
+ * Concurrent calls are deduplicated into a single in-flight promise.
*/
-export async function loadAISettings(): Promise {
- try {
- const result = await bridgeRequest("ai.loadSettings", {});
- const fromFile = (result?.data ?? {}) as Partial;
-
- // If nothing is on disk yet, check localStorage for a legacy migration
- if (!fromFile.defaultProvider) {
- const migrated = migrateFromLocalStorage();
- if (migrated) {
- // Persist the migrated settings to disk right away
- await saveAISettings(migrated);
- return migrated;
+export async function loadAISettings(options?: { forceRefresh?: boolean }): Promise {
+ if (cachedAISettings && !options?.forceRefresh) {
+ return cachedAISettings;
+ }
+
+ if (pendingLoadPromise && !options?.forceRefresh) {
+ return pendingLoadPromise;
+ }
+
+ pendingLoadPromise = (async () => {
+ try {
+ const result = await bridgeRequest("ai.loadSettings", {}, 3000);
+ const fromFile = (result?.data ?? {}) as Partial;
+
+ // If nothing is on disk yet, check localStorage for a legacy migration
+ if (!fromFile.defaultProvider) {
+ const migrated = migrateFromLocalStorage();
+ if (migrated) {
+ cachedAISettings = migrated;
+ saveAISettings(migrated).catch(() => {});
+ return migrated;
+ }
+ cachedAISettings = { ...DEFAULT_SETTINGS };
+ return cachedAISettings;
+ }
+
+ cachedAISettings = { ...DEFAULT_SETTINGS, ...fromFile };
+ return cachedAISettings;
+ } catch {
+ // Bridge unavailable or timed out — return cached, migrated, or default settings
+ if (!cachedAISettings) {
+ cachedAISettings = migrateFromLocalStorage() ?? { ...DEFAULT_SETTINGS };
}
- return { ...DEFAULT_SETTINGS };
+ return cachedAISettings;
+ } finally {
+ pendingLoadPromise = null;
}
+ })();
- return { ...DEFAULT_SETTINGS, ...fromFile };
- } catch {
- // Bridge unavailable (e.g. during Vite standalone dev) — degrade gracefully
- return migrateFromLocalStorage() ?? { ...DEFAULT_SETTINGS };
- }
+ return pendingLoadPromise;
}
/**
* Save AI settings to disk via the bridge (writes ai-settings.json).
+ * Updates in-memory cache immediately (optimistic update).
*/
export async function saveAISettings(settings: AISettings): Promise {
+ cachedAISettings = { ...settings };
try {
- await bridgeRequest("ai.saveSettings", { settings });
+ await bridgeRequest("ai.saveSettings", { settings }, 5000);
} catch {
// Fallback: keep a copy in localStorage so settings aren’t totally lost
localStorage.setItem(LS_LEGACY_KEY, JSON.stringify(settings));
@@ -182,12 +216,24 @@ class AIService {
/**
* Test whether the configured provider is reachable.
*/
- async testConnection(settings: AISettings): Promise<{ connected: boolean; message?: string }> {
+ async testConnection(settings: AISettings): Promise<{
+ connected: boolean;
+ warning?: string;
+ code?: string;
+ message?: string;
+ }> {
try {
- await bridgeRequest("ai.testConnection", { settings });
+ const result = await bridgeRequest("ai.testConnection", { settings });
+ if (result?.data?.connected !== undefined) {
+ return result.data;
+ }
return { connected: true };
} catch (err: any) {
- return { connected: false, message: err?.message ?? String(err) };
+ return {
+ connected: false,
+ code: err?.code ?? "UNKNOWN",
+ message: err?.message ?? String(err),
+ };
}
}
diff --git a/src/services/bridge/bridgeClient.ts b/src/services/bridge/bridgeClient.ts
index 1c2ec6e0..78af8eb3 100644
--- a/src/services/bridge/bridgeClient.ts
+++ b/src/services/bridge/bridgeClient.ts
@@ -346,6 +346,11 @@ function getTimeoutForMethod(method: string): number {
// Ping should be fast
if (method === "ping" || method === HEALTH_PING_METHOD) return 5000; // 5 seconds
+ // Local settings operations should be fast
+ if (method === "ai.loadSettings") return 3000; // 3 seconds
+ if (method === "ai.saveSettings") return 5000; // 5 seconds
+ if (method === "ai.testConnection") return 15000; // 15 seconds
+
// Schema operations can be very slow on large databases
if (method === "db.getSchema") return 180000; // 3 minutes
diff --git a/src/services/bridge/database.ts b/src/services/bridge/database.ts
index ef43d65c..f75088be 100644
--- a/src/services/bridge/database.ts
+++ b/src/services/bridge/database.ts
@@ -66,14 +66,20 @@ class DatabaseService {
try {
// Validate required fields
const isFileBasedDB = params.type === "sqlite" || params.type === "duckdb";
+ const isClickHouse = params.type === "clickhouse";
const required = isFileBasedDB
? ["name", "type", "database"]
- : ["name", "type", "host", "port", "user", "database"];
+ : isClickHouse
+ ? ["name", "type"]
+ : ["name", "type", "host", "port", "user", "database"];
for (const field of required) {
if (!params[field as keyof AddDatabaseParams]) {
throw new Error(`Missing required field: ${field}`);
}
}
+ if (isClickHouse && !params.url && !params.host) {
+ throw new Error("Missing required field: url");
+ }
const result = await bridgeRequest("db.add", params);
if (!result?.ok) {
throw new Error("Failed to add database");
@@ -303,9 +309,9 @@ class DatabaseService {
tableName,
});
return (
- result?.primaryKeys[0].column_name ||
- result?.primaryKeys[0].COLUMN_NAME ||
- result.primaryKeys[0] ||
+ result?.primaryKeys?.[0]?.column_name ||
+ result?.primaryKeys?.[0]?.COLUMN_NAME ||
+ (typeof result?.primaryKeys?.[0] === "string" ? result.primaryKeys[0] : "") ||
""
);
} catch (error: any) {