File
Blob: src/worker/do/repo/repoState.ts
| 1 | // Typed schema for Repo Durable Object storage |
| 2 | // Provides a light wrapper to get strong typing on storage keys/values in tests and code. |
| 3 | |
| 4 | export type ObjKey = `obj:${string}`; |
| 5 | |
| 6 | export type Ref = { name: string; oid: string }; |
| 7 | export type Head = { target: string; oid?: string; unborn?: boolean }; |
| 8 | export type RepoLease = { |
| 9 | token: string; |
| 10 | createdAt: number; |
| 11 | expiresAt: number; |
| 12 | }; |
| 13 | |
| 14 | export type RepoStateSchema = { |
| 15 | refs: Ref[]; |
| 16 | head: Head; |
| 17 | refsVersion: number; |
| 18 | packsetVersion: number; |
| 19 | nextPackSeq: number; |
| 20 | receiveLease: RepoLease | undefined; |
| 21 | compactLease: RepoLease | undefined; |
| 22 | compactionWantedAt: number | undefined; |
| 23 | lastAccessMs: number; |
| 24 | } & Record<ObjKey, Uint8Array | ArrayBuffer>; |
| 25 | |
| 26 | export type TypedStorage<S> = { |
| 27 | get<K extends keyof S & string>(key: K): Promise<S[K] | undefined>; |
| 28 | get<K extends keyof S & string>(keys: K[]): Promise<Map<K, S[K] | undefined>>; |
| 29 | put<K extends keyof S & string>(key: K, value: S[K]): Promise<void>; |
| 30 | delete<K extends keyof S & string>(key: K): Promise<boolean | void>; |
| 31 | }; |
| 32 | |
| 33 | export function asTypedStorage<S>(storage: DurableObjectStorage): TypedStorage<S> { |
| 34 | async function get<K extends keyof S & string>(key: K): Promise<S[K] | undefined>; |
| 35 | async function get<K extends keyof S & string>(keys: K[]): Promise<Map<K, S[K] | undefined>>; |
| 36 | async function get(keyOrKeys: any): Promise<any> { |
| 37 | return storage.get(keyOrKeys as any); |
| 38 | } |
| 39 | const put = <K extends keyof S & string>(key: K, value: S[K]) => |
| 40 | storage.put(key as string, value); |
| 41 | const del = <K extends keyof S & string>(key: K) => storage.delete(key as string); |
| 42 | return { get: get as any, put: put as any, delete: del as any }; |
| 43 | } |
| 44 | |
| 45 | // Key helpers for template-literal key families |
| 46 | export function objKey(oid: string): ObjKey { |
| 47 | return `obj:${oid}` as ObjKey; |
| 48 | } |