Init
This commit is contained in:
commit
0ad405c26e
78 changed files with 9127 additions and 0 deletions
53
src/lib/storage/db.ts
Normal file
53
src/lib/storage/db.ts
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
// IndexedDB persistence (offline-first local working copy).
|
||||
|
||||
import { openDB, type DBSchema, type IDBPDatabase } from 'idb';
|
||||
import type { CanvasData } from '$lib/model/types';
|
||||
|
||||
export interface FileRecord {
|
||||
/** Repo-relative path, e.g. "school/math/lecture2.pdf" or "school/math" for a folder. */
|
||||
path: string;
|
||||
kind: 'canvas' | 'folder';
|
||||
updatedAt: number;
|
||||
/** Git blob sha of the version last pulled/pushed. */
|
||||
remoteSha?: string;
|
||||
/** Local changes not yet pushed. */
|
||||
dirty: boolean;
|
||||
/** Tombstone: deleted locally, deletion not yet pushed. */
|
||||
deleted?: boolean;
|
||||
}
|
||||
|
||||
export interface SearchRecord {
|
||||
path: string;
|
||||
pages: { id: string; text: string; origin?: string }[];
|
||||
}
|
||||
|
||||
interface Schema extends DBSchema {
|
||||
files: { key: string; value: FileRecord };
|
||||
docs: { key: string; value: CanvasData };
|
||||
sources: { key: string; value: Uint8Array };
|
||||
search: { key: string; value: SearchRecord };
|
||||
kv: { key: string; value: unknown };
|
||||
}
|
||||
|
||||
let dbp: Promise<IDBPDatabase<Schema>> | null = null;
|
||||
|
||||
export function db() {
|
||||
dbp ??= openDB<Schema>('papure', 1, {
|
||||
upgrade(d) {
|
||||
d.createObjectStore('files', { keyPath: 'path' });
|
||||
d.createObjectStore('docs');
|
||||
d.createObjectStore('sources');
|
||||
d.createObjectStore('search', { keyPath: 'path' });
|
||||
d.createObjectStore('kv');
|
||||
}
|
||||
});
|
||||
return dbp;
|
||||
}
|
||||
|
||||
export async function kvGet<T>(key: string): Promise<T | undefined> {
|
||||
return (await (await db()).get('kv', key)) as T | undefined;
|
||||
}
|
||||
|
||||
export async function kvSet(key: string, value: unknown) {
|
||||
await (await db()).put('kv', value, key);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue