diff --git a/website/src/website/switch/sync.ts b/website/src/website/switch/sync.ts new file mode 100644 index 00000000..4e2b648c --- /dev/null +++ b/website/src/website/switch/sync.ts @@ -0,0 +1,37 @@ +import { getAll, getAllByIndex, type RepoRecord, type BranchRecord, type CommitRecord, type IssueRecord } from './db'; +import { supabase } from './supabase'; + +export interface ExportBundle { + repo: RepoRecord; + branches: BranchRecord[]; + commits: CommitRecord[]; + issues: IssueRecord[]; + exportedAt: number; +} + +export async function exportRepoBundle(repoId: string): Promise { + const repos = await getAll('repos'); + const repo = repos.find(r=>r.id===repoId)!; + const branches = await getAllByIndex('branches','by_repo', repoId); + const commits = await getAllByIndex('commits','by_repo', repoId); + const issues = await getAllByIndex('issues','by_repo', repoId); + return { repo, branches, commits, issues, exportedAt: Date.now() }; +} + +export async function pushRepoToSupabase(repoId: string): Promise { + if (!supabase) throw new Error('Supabase not configured'); + const bundle = await exportRepoBundle(repoId); + const path = `repos/${repoId}.json`; + const data = new Blob([JSON.stringify(bundle)], { type: 'application/json' }); + const { error } = await supabase.storage.from('switch').upload(path, data, { upsert: true }); + if (error) throw error; +} + +export async function pullRepoFromSupabase(repoId: string): Promise { + if (!supabase) throw new Error('Supabase not configured'); + const path = `repos/${repoId}.json`; + const { data, error } = await supabase.storage.from('switch').download(path); + if (error) throw error; + const text = await data.text(); + return JSON.parse(text) as ExportBundle; +}