import type { Schema } from './schema'; export class Document { constructor(public readonly id: string) {} fields = new Map(); public insert(field: F, value: string | string[]) { const values = this.fields.get(field) ?? []; if (Array.isArray(value)) { values.push(...value); } else { values.push(value); } this.fields.set(field, values); } get(field: F): string[] | string | undefined { const values = this.fields.get(field); if (values === undefined) { return undefined; } else if (values.length === 1) { return values[0]; } else { return values; } } static from( id: string, map: | Partial> | Map ): Document { const doc = new Document(id); if (map instanceof Map) { for (const [key, value] of map) { doc.insert(key, value); } } else { for (const key in map) { doc.insert(key, map[key] as string | string[]); } } return doc; } }