# amalshaji/dbcooper architecture

> Architecture diagram of the GitHub repository amalshaji/dbcooper, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/amalshaji/dbcooper
- Repository: https://github.com/amalshaji/dbcooper

## Overview

#### Purpose and Runtime
DBcooper is a native desktop database client for PostgreSQL, SQLite, Redis, and ClickHouse. It runs a React/TypeScript renderer in a Tauri v2 shell, with Rust owning privileged operations, local persistence, database connectivity, SSH tunneling, and AI-provider integration. Primary entry points are `src/main.tsx`, `src/App.tsx`, `src-tauri/src/main.rs`, and `src-tauri/src/lib.rs`.

#### Desktop Boundary and Command Surface
The renderer invokes Tauri commands rather than accessing databases or local secrets directly. Rust command modules expose connection lifecycle, schema/data operations, query execution, settings, pools, PostgreSQL-specific operations, and AI actions:
- `src-tauri/src/commands/mod.rs`
- `src-tauri/src/commands/database.rs`
- `src-tauri/src/commands/queries.rs`
- `src-tauri/src/commands/ai.rs`

The frontend-side invocation boundary is centralized in `src/lib/tauri.ts`.

#### Connection and Local State Management
Saved connections, settings, SSH tunnel configuration, database type metadata, query history, and AI-provider settings are persisted in an application-local SQLite database. The schema evolves through ordered SQL migrations:
- `src-tauri/migrations/001_initial.sql`
- `src-tauri/migrations/003_ssh_tunnel.sql`
- `src-tauri/migrations/006_ai_provider.sql`

Rust models and persistence access are organized under `src-tauri/src/db/models.rs` and `src-tauri/src/db/mod.rs`. This local store is distinct from user-connected databases.

#### Multi-Database Adapter Layer
A common database layer dispatches operations to engine-specific adapters for PostgreSQL, SQLite, Redis, and ClickHouse. Query generation/execution has a further per-engine split, allowing differences in SQL dialects and Redis semantics to remain behind Rust interfaces:
- `src-tauri/src/database/mod.rs`
- `src-tauri/src/database/postgres.rs`
- `src-tauri/src/database/redis.rs`
- `src-tauri/src/database/queries/mod.rs`

Connection pools are managed centrally in `src-tauri/src/database/pool_manager.rs`; SSH access is handled separately by `src-tauri/src/ssh_tunnel.rs`, forming the network boundary before database connections are opened.

#### Renderer Application and Database Workspace
The React app is organized around connections, connection-detail workspaces, settings, and tabs. `src/pages/Connections.tsx` is the connection-management view, while `src/pages/ConnectionDetails.tsx` hosts the active database exploration/query workflow. `src/App.tsx` coordinates application-level routing/layout.

Within a connected workspace, object/schema exploration feeds table data views, filtering, row editing/insertion, Redis-key views, and query results:
- `src/components/connection-details/ObjectExplorer.tsx`
- `src/components/DataTable.tsx`
- `src/components/SqlEditor.tsx`
- `src/components/QueryResultSheet.tsx`

#### Query Safety, Editing, and Result Flow
Users select database objects, inspect schema, compose SQL, and execute through Tauri commands. Frontend logic supports SQL parsing, safety checks, filtering, and type-aware field inputs before mutations are sent to Rust:
- `src/lib/sqlSafety.ts`
- `src/lib/sqlParser.ts`
- `src/components/RowEditSheet.tsx`
- `src/components/connection-details/TableFilterBar.tsx`

The backend executes against the selected adapter/pool and returns results for renderer presentation. This keeps credentials, networking, and raw database drivers outside the browser-like renderer.

#### AI SQL Generation and Delivery
AI generation combines user instructions, current editor SQL, and selected table/column metadata. The frontend manages draft/session/selection state and previews generated SQL before execution:
- `src/hooks/useAIGeneration.ts`
- `src/lib/aiGenerationSession.ts`
- `src/components/SqlAIPreview.tsx`

Rust constructs prompts and routes requests to either OpenAI-compatible endpoints or local CLI-backed providers (Claude Code, Codex CLI, opencode):
- `src-tauri/src/ai/prompts.rs`
- `src-tauri/src/ai/providers/openai.rs`
- `src-tauri/src/ai/providers/harness.rs`

Production builds target macOS Apple Silicon via Tauri configuration in `src-tauri/tauri.conf.json`; GitHub Actions publish signed updater artifacts through `.github/workflows/publish.yml`.

Main entry points: [`src/pages/ConnectionDetails.tsx`](https://github.com/amalshaji/dbcooper/blob/main/src/pages/ConnectionDetails.tsx) (Connection workspace), [`src/lib/tauri.ts`](https://github.com/amalshaji/dbcooper/blob/main/src/lib/tauri.ts) (Tauri invocation bridge), [`src-tauri/src/commands/mod.rs`](https://github.com/amalshaji/dbcooper/blob/main/src-tauri/src/commands/mod.rs) (Database command surface), [`src/App.tsx`](https://github.com/amalshaji/dbcooper/blob/main/src/App.tsx) (App shell)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_renderer["React renderer"]
  node_react_entry["Renderer entry<br/>React bootstrap<br/>[main.tsx]"]
  node_app["App shell<br/>React layout/router<br/>[App.tsx]"]
  node_connections["Connections view<br/>connection management<br/>[Connections.tsx]"]
  node_workspace["Connection workspace<br/>database workflow"]
  node_explorer["Object explorer<br/>schema browser<br/>[ObjectExplorer.tsx]"]
  node_query_ui["SQL editor &amp; results<br/>query UI<br/>[SqlEditor.tsx]"]
  node_table_ui["Table data &amp; editing<br/>data UI<br/>[DataTable.tsx]"]
  node_query_guard["SQL safety &amp; parsing<br/>frontend validation<br/>[sqlSafety.ts]"]
  node_tauri_bridge{{"Tauri invocation bridge<br/>frontend command client<br/>[tauri.ts]"}}
end

subgraph group_rust["Tauri / Rust backend"]
  node_tauri_shell["Tauri application<br/>desktop shell<br/>[lib.rs]"]
  node_commands{{"Database command surface<br/>Tauri commands<br/>[mod.rs]"}}
  node_local_store[("Application-local SQLite<br/>persistent state<br/>[mod.rs]")]
  node_migrations["SQLite migrations<br/>schema evolution<br/>[001_initial.sql]"]
  node_pool_manager["Connection pool manager<br/>pool lifecycle<br/>[pool_manager.rs]"]
  node_ssh_tunnel["SSH tunnel<br/>network boundary<br/>[ssh_tunnel.rs]"]
  node_adapters["Database adapter layer<br/>engine dispatch<br/>[mod.rs]"]
  node_query_adapters["Per-engine query execution<br/>query dispatch<br/>[mod.rs]"]
end

subgraph group_ai["AI generation"]
  node_ai_ui["AI SQL session &amp; preview<br/>generation UI<br/>[useAIGeneration.ts]"]
  node_ai_commands{{"AI commands<br/>Tauri AI actions<br/>[ai.rs]"}}
  node_ai_prompts["Prompt builder<br/>AI context assembly<br/>[prompts.rs]"]
  node_ai_providers["AI provider routing<br/>OpenAI or CLI harness<br/>[harness.rs]"]
end

node_user_databases[("Connected databases")]
node_ai_services["AI endpoints &amp; local CLIs<br/>external providers"]

node_react_entry -->|"mounts"| node_app
node_app -->|"shows"| node_connections
node_app -->|"opens"| node_workspace
node_workspace -->|"browses objects"| node_explorer
node_workspace -->|"composes queries"| node_query_ui
node_workspace -->|"inspects and edits"| node_table_ui
node_query_ui -->|"validates SQL"| node_query_guard
node_connections -->|"manages connections"| node_tauri_bridge
node_workspace -->|"loads data and mutates"| node_tauri_bridge
node_tauri_bridge -->|"invokes"| node_commands
node_tauri_shell -->|"registers"| node_commands
node_commands -->|"reads and writes state"| node_local_store
node_migrations -->|"evolves schema"| node_local_store
node_commands -->|"acquires pools"| node_pool_manager
node_pool_manager -->|"provides connections"| node_adapters
node_adapters -->|"dispatches operations"| node_query_adapters
node_ssh_tunnel -->|"secures network path"| node_pool_manager
node_adapters -->|"connects and executes"| node_user_databases
node_ai_ui -->|"requests generation"| node_tauri_bridge
node_tauri_bridge -->|"invokes AI actions"| node_ai_commands
node_ai_commands -->|"builds contextual prompt"| node_ai_prompts
node_ai_prompts -->|"routes request"| node_ai_providers
node_ai_providers -->|"generates SQL"| node_ai_services
node_ai_ui -->|"previews generated SQL"| node_query_ui

click node_react_entry "https://github.com/amalshaji/dbcooper/blob/main/src/main.tsx"
click node_app "https://github.com/amalshaji/dbcooper/blob/main/src/App.tsx"
click node_connections "https://github.com/amalshaji/dbcooper/blob/main/src/pages/Connections.tsx"
click node_workspace "https://github.com/amalshaji/dbcooper/blob/main/src/pages/ConnectionDetails.tsx"
click node_explorer "https://github.com/amalshaji/dbcooper/blob/main/src/components/connection-details/ObjectExplorer.tsx"
click node_query_ui "https://github.com/amalshaji/dbcooper/blob/main/src/components/SqlEditor.tsx"
click node_table_ui "https://github.com/amalshaji/dbcooper/blob/main/src/components/DataTable.tsx"
click node_query_guard "https://github.com/amalshaji/dbcooper/blob/main/src/lib/sqlSafety.ts"
click node_tauri_bridge "https://github.com/amalshaji/dbcooper/blob/main/src/lib/tauri.ts"
click node_tauri_shell "https://github.com/amalshaji/dbcooper/blob/main/src-tauri/src/lib.rs"
click node_commands "https://github.com/amalshaji/dbcooper/blob/main/src-tauri/src/commands/mod.rs"
click node_local_store "https://github.com/amalshaji/dbcooper/blob/main/src-tauri/src/db/mod.rs"
click node_migrations "https://github.com/amalshaji/dbcooper/blob/main/src-tauri/migrations/001_initial.sql"
click node_pool_manager "https://github.com/amalshaji/dbcooper/blob/main/src-tauri/src/database/pool_manager.rs"
click node_ssh_tunnel "https://github.com/amalshaji/dbcooper/blob/main/src-tauri/src/ssh_tunnel.rs"
click node_adapters "https://github.com/amalshaji/dbcooper/blob/main/src-tauri/src/database/mod.rs"
click node_query_adapters "https://github.com/amalshaji/dbcooper/blob/main/src-tauri/src/database/queries/mod.rs"
click node_ai_ui "https://github.com/amalshaji/dbcooper/blob/main/src/hooks/useAIGeneration.ts"
click node_ai_commands "https://github.com/amalshaji/dbcooper/blob/main/src-tauri/src/commands/ai.rs"
click node_ai_prompts "https://github.com/amalshaji/dbcooper/blob/main/src-tauri/src/ai/prompts.rs"
click node_ai_providers "https://github.com/amalshaji/dbcooper/blob/main/src-tauri/src/ai/providers/harness.rs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_react_entry,node_app,node_connections,node_workspace,node_explorer,node_query_ui,node_table_ui,node_query_guard,node_tauri_bridge toneBlue
class node_tauri_shell,node_commands,node_local_store,node_migrations,node_pool_manager,node_ssh_tunnel,node_adapters,node_query_adapters toneAmber
class node_ai_ui,node_ai_commands,node_ai_prompts,node_ai_providers toneMint
class node_user_databases,node_ai_services toneNeutral
```

## Components

### React renderer

Browser-like UI with no direct database or secret access.

- [Renderer entry](https://github.com/amalshaji/dbcooper/blob/main/src/main.tsx): React bootstrap · `src/main.tsx`
- [App shell](https://github.com/amalshaji/dbcooper/blob/main/src/App.tsx): React layout/router · `src/App.tsx`
- [Connections view](https://github.com/amalshaji/dbcooper/blob/main/src/pages/Connections.tsx): connection management · `src/pages/Connections.tsx`
- [Connection workspace](https://github.com/amalshaji/dbcooper/blob/main/src/pages/ConnectionDetails.tsx): database workflow · Hosts exploration, editing, and query work for an active connection. · `src/pages/ConnectionDetails.tsx`
- [Object explorer](https://github.com/amalshaji/dbcooper/blob/main/src/components/connection-details/ObjectExplorer.tsx): schema browser · `src/components/connection-details/ObjectExplorer.tsx`
- [SQL editor & results](https://github.com/amalshaji/dbcooper/blob/main/src/components/SqlEditor.tsx): query UI · `src/components/SqlEditor.tsx`
- [Table data & editing](https://github.com/amalshaji/dbcooper/blob/main/src/components/DataTable.tsx): data UI · Includes filtering and row mutation inputs. · `src/components/DataTable.tsx`
- [SQL safety & parsing](https://github.com/amalshaji/dbcooper/blob/main/src/lib/sqlSafety.ts): frontend validation · `src/lib/sqlSafety.ts`
- [Tauri invocation bridge](https://github.com/amalshaji/dbcooper/blob/main/src/lib/tauri.ts): frontend command client · `src/lib/tauri.ts`

### Tauri / Rust backend

Privileged desktop operations and database runtime.

- [Tauri application](https://github.com/amalshaji/dbcooper/blob/main/src-tauri/src/lib.rs): desktop shell · `src-tauri/src/lib.rs`
- [Database command surface](https://github.com/amalshaji/dbcooper/blob/main/src-tauri/src/commands/mod.rs): Tauri commands · Connection lifecycle, schema/data, query, settings, and pool operations. · `src-tauri/src/commands/mod.rs`
- [Application-local SQLite](https://github.com/amalshaji/dbcooper/blob/main/src-tauri/src/db/mod.rs): persistent state · Stores saved connections, settings, history, tunnels, and AI settings. · `src-tauri/src/db/mod.rs`
- [SQLite migrations](https://github.com/amalshaji/dbcooper/blob/main/src-tauri/migrations/001_initial.sql): schema evolution · `src-tauri/migrations/001_initial.sql`
- [Connection pool manager](https://github.com/amalshaji/dbcooper/blob/main/src-tauri/src/database/pool_manager.rs): pool lifecycle · `src-tauri/src/database/pool_manager.rs`
- [SSH tunnel](https://github.com/amalshaji/dbcooper/blob/main/src-tauri/src/ssh_tunnel.rs): network boundary · `src-tauri/src/ssh_tunnel.rs`
- [Database adapter layer](https://github.com/amalshaji/dbcooper/blob/main/src-tauri/src/database/mod.rs): engine dispatch · Routes common operations to PostgreSQL, SQLite, Redis, and ClickHouse adapters. · `src-tauri/src/database/mod.rs`
- [Per-engine query execution](https://github.com/amalshaji/dbcooper/blob/main/src-tauri/src/database/queries/mod.rs): query dispatch · Contains dialect-specific SQL and Redis query semantics. · `src-tauri/src/database/queries/mod.rs`

### AI generation

SQL generation flow across renderer and Rust providers.

- [AI SQL session & preview](https://github.com/amalshaji/dbcooper/blob/main/src/hooks/useAIGeneration.ts): generation UI · Combines instruction, editor SQL, and selected schema before execution. · `src/hooks/useAIGeneration.ts`
- [AI commands](https://github.com/amalshaji/dbcooper/blob/main/src-tauri/src/commands/ai.rs): Tauri AI actions · `src-tauri/src/commands/ai.rs`
- [Prompt builder](https://github.com/amalshaji/dbcooper/blob/main/src-tauri/src/ai/prompts.rs): AI context assembly · `src-tauri/src/ai/prompts.rs`
- [AI provider routing](https://github.com/amalshaji/dbcooper/blob/main/src-tauri/src/ai/providers/harness.rs): OpenAI or CLI harness · Routes to OpenAI-compatible endpoints or local Claude Code, Codex, and opencode CLIs. · `src-tauri/src/ai/providers/harness.rs`

### Other components

- **Connected databases**: PostgreSQL / SQLite / Redis / ClickHouse · User-selected remote or local database targets.
- **AI endpoints & local CLIs**: external providers

## Connections

- Renderer entry → App shell: mounts
- App shell → Connections view: shows
- App shell → Connection workspace: opens
- Connection workspace → Object explorer: browses objects
- Connection workspace → SQL editor & results: composes queries
- Connection workspace → Table data & editing: inspects and edits
- SQL editor & results → SQL safety & parsing: validates SQL
- Connections view → Tauri invocation bridge: manages connections
- Connection workspace → Tauri invocation bridge: loads data and mutates
- Tauri invocation bridge → Database command surface: invokes
- Tauri application → Database command surface: registers
- Database command surface → Application-local SQLite: reads and writes state
- SQLite migrations → Application-local SQLite: evolves schema
- Database command surface → Connection pool manager: acquires pools
- Connection pool manager → Database adapter layer: provides connections
- Database adapter layer → Per-engine query execution: dispatches operations
- SSH tunnel → Connection pool manager: secures network path
- Database adapter layer → Connected databases: connects and executes
- AI SQL session & preview → Tauri invocation bridge: requests generation
- Tauri invocation bridge → AI commands: invokes AI actions
- AI commands → Prompt builder: builds contextual prompt
- Prompt builder → AI provider routing: routes request
- AI provider routing → AI endpoints & local CLIs: generates SQL
- AI SQL session & preview → SQL editor & results: previews generated SQL
