import { Link } from "./Link"; import { readMyData } from "../utils/fileStorage"; export abstract class LinkManager { protected abstract fileName: string; protected abstract tableName: string; toLink(id: string): Link { return new Link(this.tableName, id); } async resolve(link: Link): Promise { if (link.table !== this.tableName) { throw new Error( `Expected link to ${this.tableName}, got ${link.table}` ); } const items = await readMyData(this.fileName); return items.find((item: any) => item.id === link.id) || null; } async resolveMany(links: Link[]): Promise { if (!links || links.length === 0) return []; const results = await Promise.all( links.map((link) => this.resolve(link)) ); return results.filter((item): item is NonNullable => item !== null) as T[]; } }