152 lines
4.6 KiB
TypeScript
152 lines
4.6 KiB
TypeScript
|
|
/**
|
||
|
|
* In-memory `Map`-backed repositories.
|
||
|
|
*
|
||
|
|
* Implements the MVP storage layer. The repository interfaces match the
|
||
|
|
* shape that ADR-0005's eventual persistence implementation will satisfy,
|
||
|
|
* so swapping `createInMemoryRepos()` for a SQLite/Postgres factory later
|
||
|
|
* is a localised change.
|
||
|
|
*
|
||
|
|
* All mutating methods return the *stored* object so callers can pick up
|
||
|
|
* server-assigned fields (none in MVP, but the contract anticipates it).
|
||
|
|
*/
|
||
|
|
|
||
|
|
import type { Annotation } from "@shared/annotation";
|
||
|
|
import type { Document, DocumentRepresentation } from "@shared/document";
|
||
|
|
import type { EvidenceItem } from "@shared/evidence";
|
||
|
|
import type {
|
||
|
|
AnnotationId,
|
||
|
|
DocumentId,
|
||
|
|
EvidenceItemId,
|
||
|
|
RepresentationId,
|
||
|
|
} from "@shared/ids";
|
||
|
|
|
||
|
|
export interface DocumentRepository {
|
||
|
|
create(document: Document): Document;
|
||
|
|
get(id: DocumentId): Document | null;
|
||
|
|
list(): readonly Document[];
|
||
|
|
update(document: Document): Document;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface RepresentationRepository {
|
||
|
|
create(representation: DocumentRepresentation): DocumentRepresentation;
|
||
|
|
get(id: RepresentationId): DocumentRepresentation | null;
|
||
|
|
listByDocument(documentId: DocumentId): readonly DocumentRepresentation[];
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface AnnotationRepository {
|
||
|
|
create(annotation: Annotation): Annotation;
|
||
|
|
get(id: AnnotationId): Annotation | null;
|
||
|
|
listByDocument(documentId: DocumentId): readonly Annotation[];
|
||
|
|
update(annotation: Annotation): Annotation;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface EvidenceItemRepository {
|
||
|
|
create(item: EvidenceItem): EvidenceItem;
|
||
|
|
get(id: EvidenceItemId): EvidenceItem | null;
|
||
|
|
listByDocument(
|
||
|
|
documentId: DocumentId,
|
||
|
|
annotationLookup: (id: AnnotationId) => Annotation | null,
|
||
|
|
): readonly EvidenceItem[];
|
||
|
|
update(item: EvidenceItem): EvidenceItem;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface InMemoryRepos {
|
||
|
|
readonly documents: DocumentRepository;
|
||
|
|
readonly representations: RepresentationRepository;
|
||
|
|
readonly annotations: AnnotationRepository;
|
||
|
|
readonly evidenceItems: EvidenceItemRepository;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function createInMemoryRepos(): InMemoryRepos {
|
||
|
|
const documents = new Map<DocumentId, Document>();
|
||
|
|
const representations = new Map<RepresentationId, DocumentRepresentation>();
|
||
|
|
const annotations = new Map<AnnotationId, Annotation>();
|
||
|
|
const evidenceItems = new Map<EvidenceItemId, EvidenceItem>();
|
||
|
|
|
||
|
|
return {
|
||
|
|
documents: {
|
||
|
|
create(document) {
|
||
|
|
documents.set(document.id, document);
|
||
|
|
return document;
|
||
|
|
},
|
||
|
|
get(id) {
|
||
|
|
return documents.get(id) ?? null;
|
||
|
|
},
|
||
|
|
list() {
|
||
|
|
return [...documents.values()];
|
||
|
|
},
|
||
|
|
update(document) {
|
||
|
|
if (!documents.has(document.id)) {
|
||
|
|
throw new Error(`DocumentRepository.update: unknown id ${document.id}`);
|
||
|
|
}
|
||
|
|
documents.set(document.id, document);
|
||
|
|
return document;
|
||
|
|
},
|
||
|
|
},
|
||
|
|
representations: {
|
||
|
|
create(representation) {
|
||
|
|
representations.set(representation.id, representation);
|
||
|
|
return representation;
|
||
|
|
},
|
||
|
|
get(id) {
|
||
|
|
return representations.get(id) ?? null;
|
||
|
|
},
|
||
|
|
listByDocument(documentId) {
|
||
|
|
const out: DocumentRepresentation[] = [];
|
||
|
|
for (const rep of representations.values()) {
|
||
|
|
if (rep.documentId === documentId) out.push(rep);
|
||
|
|
}
|
||
|
|
return out;
|
||
|
|
},
|
||
|
|
},
|
||
|
|
annotations: {
|
||
|
|
create(annotation) {
|
||
|
|
annotations.set(annotation.id, annotation);
|
||
|
|
return annotation;
|
||
|
|
},
|
||
|
|
get(id) {
|
||
|
|
return annotations.get(id) ?? null;
|
||
|
|
},
|
||
|
|
listByDocument(documentId) {
|
||
|
|
const out: Annotation[] = [];
|
||
|
|
for (const ann of annotations.values()) {
|
||
|
|
if (ann.documentId === documentId) out.push(ann);
|
||
|
|
}
|
||
|
|
return out;
|
||
|
|
},
|
||
|
|
update(annotation) {
|
||
|
|
if (!annotations.has(annotation.id)) {
|
||
|
|
throw new Error(`AnnotationRepository.update: unknown id ${annotation.id}`);
|
||
|
|
}
|
||
|
|
annotations.set(annotation.id, annotation);
|
||
|
|
return annotation;
|
||
|
|
},
|
||
|
|
},
|
||
|
|
evidenceItems: {
|
||
|
|
create(item) {
|
||
|
|
evidenceItems.set(item.id, item);
|
||
|
|
return item;
|
||
|
|
},
|
||
|
|
get(id) {
|
||
|
|
return evidenceItems.get(id) ?? null;
|
||
|
|
},
|
||
|
|
listByDocument(documentId, annotationLookup) {
|
||
|
|
const out: EvidenceItem[] = [];
|
||
|
|
for (const item of evidenceItems.values()) {
|
||
|
|
if (item.annotationIds.some((aid) => annotationLookup(aid)?.documentId === documentId)) {
|
||
|
|
out.push(item);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return out;
|
||
|
|
},
|
||
|
|
update(item) {
|
||
|
|
if (!evidenceItems.has(item.id)) {
|
||
|
|
throw new Error(`EvidenceItemRepository.update: unknown id ${item.id}`);
|
||
|
|
}
|
||
|
|
evidenceItems.set(item.id, item);
|
||
|
|
return item;
|
||
|
|
},
|
||
|
|
},
|
||
|
|
};
|
||
|
|
}
|