UNPKG

alinea

Version:

[![npm](https://img.shields.io/npm/v/alinea.svg)](https://npmjs.org/package/alinea) [![install size](https://packagephobia.com/badge?p=alinea)](https://packagephobia.com/result?p=alinea)

68 lines (66 loc) 1.98 kB
import { require_dataloader } from "../../chunks/chunk-ICLGQ53P.js"; import { Query } from "../../chunks/chunk-4JLFL6LD.js"; import { __toESM } from "../../chunks/chunk-U5RRZUYZ.js"; // src/backend/resolver/LinkResolver.ts var import_dataloader = __toESM(require_dataloader(), 1); import { Entry } from "alinea/core"; import { createSelection } from "alinea/core/pages/CreateSelection"; import { serializeSelection } from "alinea/core/pages/Serialize"; import { ResolveContext } from "./ResolveContext.js"; var LinkResolver = class { constructor(resolver, store, realm) { this.resolver = resolver; this.store = store; this.realm = realm; } loaders = /* @__PURE__ */ new Map(); load(projection) { return new import_dataloader.default( async (ids) => { const selection = createSelection( Entry().where(Entry.entryId.isIn(ids)).select({ entryId: Entry.entryId, projection }) ); serializeSelection(this.resolver.targets, selection); const query = new Query( this.resolver.query( new ResolveContext({ realm: this.realm }), selection ) ); const entries = await this.store(query); const results = new Map( entries.map((entry) => [entry.entryId, entry.projection]) ); return ids.map((id) => results.get(id) || {}); } ); } resolveLinks(projection, entryIds) { if (this.loaders.has(projection)) return this.loaders.get(projection).loadMany(entryIds).then(skipErrors); const loader = this.load(projection); this.loaders.set(projection, loader); return loader.loadMany(entryIds).then(skipErrors); function skipErrors(results) { return results.map((result) => { if (result instanceof Error) { console.error(result); return void 0; } return result; }); } } }; export { LinkResolver };