Skip to content
File

Blob: src/client/stores/db/workspace-directory.ts

typescript52 lines
1import type { Workspace, WorkspaceMembershipSummary } from "@/shared/types";
2import { db, type MemberWorkspaceRow } from "./bland-db";
3 
4async function replaceAll(workspaces: WorkspaceMembershipSummary[]): Promise<void> {
5 const rows: MemberWorkspaceRow[] = workspaces.map((ws, rank) => ({ ...ws, rank }));
6 await db.transaction("rw", db.memberWorkspaces, async () => {
7 await db.memberWorkspaces.clear();
8 if (rows.length > 0) {
9 await db.memberWorkspaces.bulkPut(rows);
10 }
11 });
12}
13 
14async function upsert(workspace: WorkspaceMembershipSummary): Promise<void> {
15 await db.transaction("rw", db.memberWorkspaces, async () => {
16 const existing = await db.memberWorkspaces.get(workspace.id);
17 if (existing) {
18 await db.memberWorkspaces.put({ ...workspace, rank: existing.rank });
19 return;
20 }
21 const tail = await db.memberWorkspaces.orderBy("rank").last();
22 const rank = tail ? tail.rank + 1 : 0;
23 await db.memberWorkspaces.put({ ...workspace, rank });
24 });
25}
26 
27async function patch(workspaceId: string, updates: Partial<Workspace>): Promise<void> {
28 await db.transaction("rw", db.memberWorkspaces, async () => {
29 const existing = await db.memberWorkspaces.get(workspaceId);
30 if (!existing) return;
31 // Preserve role + rank; only the plain Workspace shape is patched.
32 await db.memberWorkspaces.put({
33 ...existing,
34 ...updates,
35 id: existing.id,
36 role: existing.role,
37 rank: existing.rank,
38 });
39 });
40}
41 
42async function remove(workspaceId: string): Promise<void> {
43 await db.memberWorkspaces.delete(workspaceId);
44}
45 
46export const directoryCommands = {
47 replaceAll,
48 upsert,
49 patch,
50 remove,
51};