Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions apps/tauri/src/api/connection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
import { invoke } from "@tauri-apps/api/core";

export type SchemaObjectKind = "table" | "view";

export type SchemaObject = {
name: string;
kind: SchemaObjectKind;
schema?: string | null;
};

export type ColumnMeta = {
name: string;
data_type: string;
is_nullable: boolean;
is_primary_key: boolean;
primary_key_position?: number | null;
default_value?: string | null;
};

export type TableSchema = {
table_name: string;
schema?: string | null;
columns: ColumnMeta[];
primary_keys: string[];
};

export async function dbConnectSqlite(path: string): Promise<string> {
return invoke<string>("db_connect_sqlite", { path });
}
Expand All @@ -12,13 +36,29 @@ export async function dbIsConnected(id: string): Promise<boolean> {
return invoke<boolean>("db_is_connected", { id });
}

export async function dbListSchemas(id: string): Promise<string[]> {
return invoke<string[]>("db_list_schemas", { id });
}

export async function dbListTables(id: string): Promise<SchemaObject[]> {
return invoke<SchemaObject[]>("db_list_tables", { id });
}

export async function dbListColumns(
id: string,
table: string,
): Promise<TableSchema> {
return invoke<TableSchema>("db_list_columns", { id, table });
}

export function formatConnectionError(err: unknown): string {
if (typeof err === "string") return err;
if (err && typeof err === "object") {
const e = err as Record<string, unknown>;
if (typeof e.kind === "string") {
const parts = [e.kind, e.driver, e.message, e.id]
.filter((v): v is string => typeof v === "string" && v.length > 0);
const parts = [e.kind, e.driver, e.message, e.id].filter(
(v): v is string => typeof v === "string" && v.length > 0,
);
return parts.join(": ");
}
if (typeof e.message === "string") return e.message;
Expand Down
106 changes: 106 additions & 0 deletions apps/tauri/src/stores/objectTree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { defineStore } from "pinia";
import { ref, watch } from "vue";
import {
dbListColumns,
dbListTables,
formatConnectionError,
type ColumnMeta,
type SchemaObject,
} from "../api/connection";
import { useConnectionsStore } from "./connections";
import { useShellStatusStore } from "./shellStatus";

export type TreeTableNode = SchemaObject & {
expanded: boolean;
loadingColumns: boolean;
columns: ColumnMeta[] | null;
error: string | null;
};

/** SQLite object tree for the active connection. */
export const useObjectTreeStore = defineStore("objectTree", () => {
const connectionId = ref<string | null>(null);
const objects = ref<TreeTableNode[]>([]);
const loading = ref(false);
const error = ref<string | null>(null);
const filter = ref("");

async function loadFor(id: string | null) {
connectionId.value = id;
objects.value = [];
error.value = null;
filter.value = "";
if (!id) return;

loading.value = true;
try {
const list = await dbListTables(id);
objects.value = list.map((o) => ({
...o,
expanded: false,
loadingColumns: false,
columns: null,
error: null,
}));
} catch (err) {
error.value = formatConnectionError(err);
} finally {
loading.value = false;
}
}

async function toggleTable(name: string) {
const id = connectionId.value;
const node = objects.value.find((o) => o.name === name);
if (!id || !node) return;

if (node.expanded) {
node.expanded = false;
return;
}

node.expanded = true;
if (node.columns) return;

node.loadingColumns = true;
node.error = null;
try {
const schema = await dbListColumns(id, name);
node.columns = schema.columns;
} catch (err) {
node.error = formatConnectionError(err);
const status = useShellStatusStore();
status.setQueryMetrics({
message: node.error,
busy: false,
});
} finally {
node.loadingColumns = false;
}
}

function refresh() {
return loadFor(connectionId.value);
}

// Keep tree in sync with active connection.
const connections = useConnectionsStore();
watch(
() => connections.activeId,
(id) => {
void loadFor(id);
},
{ immediate: true },
);

return {
connectionId,
objects,
loading,
error,
filter,
loadFor,
toggleTable,
refresh,
};
});
2 changes: 2 additions & 0 deletions apps/tauri/src/ui/ConnectionsPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useShellStore } from "../stores/shell";
import { useConnectionsStore } from "../stores/connections";
import { useQueryaMotion } from "../motion";
import SplitHandle from "./SplitHandle.vue";
import SqliteObjectTree from "./SqliteObjectTree.vue";

const emit = defineEmits<{
openSqlite: [];
Expand Down Expand Up @@ -115,6 +116,7 @@ onUnmounted(() => {
Open a SQLite file to start the vertical slice.
</p>
</template>
<SqliteObjectTree />
<div class="actions">
<button
type="button"
Expand Down
Loading
Loading