File size: 933 Bytes
d76f93d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { Link } from "./Link";
import { readMyData } from "../utils/fileStorage";

export abstract class LinkManager<T> {
   protected abstract fileName: string;
   protected abstract tableName: string;

   toLink(id: string): Link {
      return new Link(this.tableName, id);
   }

   async resolve(link: Link): Promise<T | null> {
      if (link.table !== this.tableName) {
         throw new Error(
            `Expected link to ${this.tableName}, got ${link.table}`
         );
      }

      const items = await readMyData<T>(this.fileName);
      return items.find((item: any) => item.id === link.id) || null;
   }

   async resolveMany(links: Link[]): Promise<T[]> {
      if (!links || links.length === 0) return [];
      
      const results = await Promise.all(
         links.map((link) => this.resolve(link))
      );

      return results.filter((item): item is NonNullable<typeof item> => item !== null) as T[];
   }
}