// In-memory Repository — a Map, nothing more. The point is to settle the record shape and the // lineage queries with zero persistence friction before IndexedDB / atproto. Same interface, so // those drop in later without touching callers. import type { Repository, StoredRecord } from "./Repository"; import type { OrganismRecord } from "./record"; export class MemoryRepository implements Repository { private store = new Map(); private seq = 0; async save(record: OrganismRecord): Promise { const id = `org-${(++this.seq).toString(36)}-${Date.now().toString(36)}`; this.store.set(id, record); return id; } async get(id: string): Promise { return this.store.get(id); } async delete(id: string): Promise { this.store.delete(id); } async list(): Promise { return [...this.store.entries()].map(([id, record]) => ({ id, record })); } async children(id: string): Promise { return [...this.store.entries()] .filter(([, record]) => record.parents.includes(id)) .map(([cid, record]) => ({ id: cid, record })); } async ancestry(id: string): Promise { // BFS up the DAG; dedupe (a node can be reached via two parents), nearest-first. const out: StoredRecord[] = []; const seen = new Set(); let frontier = this.store.get(id)?.parents ?? []; while (frontier.length) { const next: string[] = []; for (const pid of frontier) { if (seen.has(pid)) continue; seen.add(pid); const record = this.store.get(pid); if (!record) continue; out.push({ id: pid, record }); next.push(...record.parents); } frontier = next; } return out; } }