# API Reference (/docs/api)

Complete reference for all Tauri backend commands.

This page documents the complete list of remote procedure calls (RPC) available to the frontend via Tauri's `invoke` system.

---

## Connection Commands [#connection-commands]

### `add_connection` [#add_connection]

Register a new database connection.

<Callout type='info' title='Stable' />

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `name` | `String` | Display name |
| `database_info` | `DatabaseInfo` | Connection details |
| `color` | `number?` | UI color hue |

**Returns:** `Promise<ConnectionInfo>`

```typescript
await invoke('add_connection', { name, database_info, color })
```

---

### `update_connection` [#update_connection]

Update an existing connection configuration.

<Callout type='warn' title='Side Effect'>
	May disconnect active session.
</Callout>

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `conn_id` | `Uuid` | Connection ID |
| `name` | `String` | New name |
| `database_info` | `DatabaseInfo` | New details |
| `color` | `number?` | New color |

**Returns:** `Promise<ConnectionInfo>`

```typescript
await invoke('update_connection', { conn_id, name, database_info, color })
```

---

### `update_connection_color` [#update_connection_color]

Update only the color of a connection.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `connection_id` | `Uuid` | Connection ID |
| `color` | `number?` | New color hue |

**Returns:** `Promise<void>`

```typescript
await invoke('update_connection_color', { connection_id, color })
```

---

### `connect_to_database` [#connect_to_database]

Establish an active connection.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `connection_id` | `Uuid` | Connection ID |

**Returns:** `Promise<boolean>`

```typescript
await invoke('connect_to_database', { connection_id })
```

---

### `disconnect_from_database` [#disconnect_from_database]

Close the active connection.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `connection_id` | `Uuid` | Connection ID |

**Returns:** `Promise<void>`

```typescript
await invoke('disconnect_from_database', { connection_id })
```

---

### `get_connections` [#get_connections]

List all configured connections.

**Returns:** `Promise<ConnectionInfo[]>`

```typescript
const conns = await invoke('get_connections')
```

---

### `remove_connection` [#remove_connection]

Delete a connection configuration.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `connection_id` | `Uuid` | Connection ID |

**Returns:** `Promise<void>`

```typescript
await invoke('remove_connection', { connection_id })
```

---

### `test_connection` [#test_connection]

Test connectivity without saving.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `database_info` | `DatabaseInfo` | Details to test |

**Returns:** `Promise<boolean>`

```typescript
await invoke('test_connection', { database_info })
```

---

### `initialize_connections` [#initialize_connections]

Loads and initializes connections from storage on startup.

**Returns:** `Promise<void>`

```typescript
await invoke('initialize_connections')
```

---

### `get_recent_connections` [#get_recent_connections]

Get recently used connections.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `limit` | `number?` | Max results |

**Returns:** `Promise<ConnectionInfo[]>`

```typescript
await invoke('get_recent_connections', { limit: 5 })
```

---

### `get_connection_history` [#get_connection_history]

Get historical connection attempts.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `db_type_filter` | `String?` | Filter by DB type |
| `success_filter` | `boolean?` | Filter by success |
| `limit` | `number?` | Max results |

**Returns:** `Promise<ConnectionHistoryEntry[]>`

```typescript
await invoke('get_connection_history', { limit: 20 })
```

---

## Query Commands [#query-commands]

### `start_query` [#start_query]

Execute a SQL query. Returns partial results immediately if possible or Query IDs.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `connection_id` | `Uuid` | Active connection |
| `query` | `String` | SQL string |

**Returns:** `Promise<number[]>` (Query IDs)

```typescript
await invoke('start_query', { connection_id, query })
```

---

### `fetch_query` [#fetch_query]

Get info about a query execution.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `query_id` | `number` | Query ID |

**Returns:** `Promise<StatementInfo>`

```typescript
await invoke('fetch_query', { query_id })
```

---

### `fetch_page` [#fetch_page]

Fetch a specific page of results.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `query_id` | `number` | Query ID |
| `page_index` | `number` | Page number |

**Returns:** `Promise<Record<string, any>[] | null>`

```typescript
await invoke('fetch_page', { query_id, page_index: 0 })
```

---

### `get_query_status` [#get_query_status]

Check execution status.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `query_id` | `number` | Query ID |

**Returns:** `Promise<QueryStatus>`

```typescript
await invoke('get_query_status', { query_id })
```

---

### `get_page_count` [#get_page_count]

Get total pages for a query.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `query_id` | `number` | Query ID |

**Returns:** `Promise<number>`

```typescript
await invoke('get_page_count', { query_id })
```

---

### `get_columns` [#get_columns]

Get column definitions for a query result.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `query_id` | `number` | Query ID |

**Returns:** `Promise<ColumnDef[] | null>`

```typescript
await invoke('get_columns', { query_id })
```

---

### `save_query_to_history` [#save_query_to_history]

Manually record a query execution.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `connection_id` | `String` | Connection ID |
| `query` | `String` | SQL |
| `duration_ms` | `number?` | Execution time |
| `status` | `String` | Result status |
| `row_count` | `number` | Rows affected |
| `error_message` | `String?` | Error details |

**Returns:** `Promise<void>`

```typescript
await invoke('save_query_to_history', { ... });
```

---

### `get_query_history` [#get_query_history]

Get history for a specific connection.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `connection_id` | `String` | Connection ID |
| `limit` | `number?` | Max items |

**Returns:** `Promise<QueryHistoryEntry[]>`

```typescript
await invoke('get_query_history', { connection_id })
```

---

### `get_recent_queries` [#get_recent_queries]

Get global query history.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `connection_id` | `String?` | Filter by connection |
| `limit` | `number?` | Max items |
| `status_filter` | `String?` | Filter by status |

**Returns:** `Promise<QueryHistoryEntry[]>`

```typescript
await invoke('get_recent_queries', { limit: 50 })
```

---

## Script Commands [#script-commands]

### `save_script` [#save_script]

Save a SQL script.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `name` | `String` | Script name |
| `content` | `String` | SQL content |
| `connection_id` | `Uuid?` | Linked connection |
| `description` | `String?` | Metadata |

**Returns:** `Promise<number>` (Script ID)

```typescript
const id = await invoke('save_script', { name, content })
```

---

### `update_script` [#update_script]

Update an existing script.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `id` | `number` | Script ID |
| `name` | `String` | New name |
| `content` | `String` | New content |
| `connection_id` | `Uuid?` | Linked connection |
| `description` | `String?` | Description |

**Returns:** `Promise<void>`

```typescript
await invoke('update_script', { id, name, content })
```

---

### `get_scripts` [#get_scripts]

List saved scripts.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `connection_id` | `Uuid?` | Filter by connection |

**Returns:** `Promise<SavedQuery[]>`

```typescript
await invoke('get_scripts')
```

---

### `delete_script` [#delete_script]

Delete a saved script.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `id` | `number` | Script ID |

**Returns:** `Promise<void>`

```typescript
await invoke('delete_script', { id })
```

---

## Setting / Session Commands [#setting--session-commands]

### `save_session_state` [#save_session_state]

Persist UI session state (tabs, etc.).

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `session_data` | `String` | JSON string |

**Returns:** `Promise<void>`

```typescript
await invoke('save_session_state', { session_data })
```

---

### `get_session_state` [#get_session_state]

Retrieve persisted session state.

**Returns:** `Promise<String | null>`

```typescript
await invoke('get_session_state')
```

---

### `get_setting` [#get_setting]

Get a user preference.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `key` | `String` | Setting key |

**Returns:** `Promise<String | null>`

```typescript
await invoke('get_setting', { key: 'theme' })
```

---

### `set_setting` [#set_setting]

Set a user preference.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `key` | `String` | Setting key |
| `value` | `String` | Value |

**Returns:** `Promise<void>`

```typescript
await invoke('set_setting', { key: 'theme', value: 'dark' })
```

---

## Mutation Commands [#mutation-commands]

### `insert_row` [#insert_row]

Insert a record.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `connection_id` | `Uuid` | Connection ID |
| `table_name` | `String` | Table |
| `schema_name` | `String?` | Schema |
| `row_data` | `Object` | Data |

**Returns:** `Promise<MutationResult>`

```typescript
await invoke('insert_row', { connection_id, table_name, row_data })
```

---

### `update_cell` [#update_cell]

Update a single cell value.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `connection_id` | `Uuid` | Connection ID |
| `table_name` | `String` | Table |
| `schema_name` | `String?` | Schema |
| `primary_key_column` | `String` | PK Column |
| `primary_key_value` | `any` | PK Value |
| `column_name` | `String` | Target Column |
| `new_value` | `any` | New Value |

**Returns:** `Promise<MutationResult>`

```typescript
await invoke('update_cell', { ... });
```

---

### `delete_rows` [#delete_rows]

Delete rows by PK.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `connection_id` | `Uuid` | Connection ID |
| `table_name` | `String` | Table |
| `schema_name` | `String?` | Schema |
| `primary_key_column` | `String` | PK Column |
| `primary_key_values` | `any[]` | PK Values |

**Returns:** `Promise<MutationResult>`

```typescript
await invoke('delete_rows', { ... });
```

---

### `export_table` [#export_table]

Export table data.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `connection_id` | `Uuid` | Connection ID |
| `table_name` | `String` | Table |
| `schema_name` | `String?` | Schema |
| `format` | `ExportFormat` | Enum (CSV, JSON, SQL) |
| `limit` | `number?` | Max rows |

**Returns:** `Promise<String>` (Data or Path)

```typescript
await invoke('export_table', { ... });
```

---

### `soft_delete_rows` [#soft_delete_rows]

Perform a soft delete (update flag).

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `connection_id` | `Uuid` | Connection ID |
| `table_name` | `String` | Table |
| `schema_name` | `String?` | Schema |
| `primary_key_column` | `String` | PK Column |
| `primary_key_values` | `any[]` | PK Values |
| `soft_delete_column` | `String?` | Column to update |

**Returns:** `Promise<SoftDeleteResult>`

```typescript
await invoke('soft_delete_rows', { ... });
```

---

### `undo_soft_delete` [#undo_soft_delete]

Restore soft-deleted rows.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `connection_id` | `Uuid` | Connection ID |
| `table_name` | `String` | Table |
| `schema_name` | `String?` | Schema |
| `primary_key_column` | `String` | PK Column |
| `primary_key_values` | `any[]` | PK Values |
| `soft_delete_column` | `String` | Column to restore |

**Returns:** `Promise<MutationResult>`

```typescript
await invoke('undo_soft_delete', { ... });
```

---

---

### `seed_table` [#seed_table]

Populate a table with mock data for testing. All seedable columns (non-PK) will be filled with realistic data based on their type and name.

<Callout type='warn' title='Experimental'>
	May fail on complex constraints.
</Callout>

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `connection_id` | `Uuid` | Connection ID |
| `table_name` | `String` | Target Table |
| `schema_name` | `String?` | Target Schema |
| `count` | `number` | Rows to insert |

**Returns:** `Promise<SeedResult>`

```typescript
await invoke('seed_table', {
	connection_id,
	table_name: 'users',
	count: 100
})
```

---

### `truncate_table` [#truncate_table]

Truncate a table.

<Callout type='warn'>Irreversible</Callout>

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `connection_id` | `Uuid` | Connection ID |
| `table_name` | `String` | Table |
| `schema_name` | `String?` | Schema |
| `cascade` | `boolean?` | CASCADE option |

**Returns:** `Promise<TruncateResult>`

```typescript
await invoke('truncate_table', { ... });
```

---

### `truncate_database` [#truncate_database]

Truncate ALL tables.

<Callout type='error'>EXTREMELY DANGEROUS</Callout>

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `connection_id` | `Uuid` | Connection ID |
| `schema_name` | `String?` | Target schema (optional) |
| `confirm` | `boolean` | Safety flag |

**Returns:** `Promise<TruncateResult>`

```typescript
await invoke('truncate_database', { confirm: true, ... });
```

---

### `dump_database` [#dump_database]

Create a database dump.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `connection_id` | `Uuid` | Connection ID |
| `output_path` | `String` | Target file path |

**Returns:** `Promise<DumpResult>`

```typescript
await invoke('dump_database', { output_path: '/tmp/dump.sql' })
```

---

### `execute_batch` [#execute_batch]

Execute multiple raw SQL statements.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `connection_id` | `Uuid` | Connection ID |
| `statements` | `String[]` | SQL statements |

**Returns:** `Promise<MutationResult>`

```typescript
await invoke('execute_batch', { statements: [...] });
```

---

## Metadata Commands [#metadata-commands]

### `get_database_schema` [#get_database_schema]

Get full schema tree.

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `connection_id` | `Uuid` | Connection ID |

**Returns:** `Promise<DatabaseSchema>`

```typescript
await invoke('get_database_schema', { connection_id })
```

---

### `get_database_metadata` [#get_database_metadata]

Get simple metadata (version, size).

**Inputs:**
| Name | Type | Description |
| :--- | :--- | :--- |
| `connection_id` | `Uuid` | Connection ID |

**Returns:** `Promise<DatabaseMetadata>`

```typescript
await invoke('get_database_metadata', { connection_id })
```
