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)

117 lines (115 loc) 3.06 kB
import "../chunks/chunk-U5RRZUYZ.js"; // src/core/Graph.ts import { Schema } from "./Schema.js"; import { Type } from "./Type.js"; import { createSelection } from "./pages/CreateSelection.js"; import { Cursor } from "./pages/Cursor.js"; import { Realm } from "./pages/Realm.js"; import { seralizeLocation, serializeSelection } from "./pages/Serialize.js"; var GraphRealm = class _GraphRealm { constructor(config, resolve, origin = {}) { this.config = config; this.resolve = resolve; this.origin = origin; this.targets = Schema.targets(config.schema); } targets; in(location) { return new _GraphRealm(this.config, this.resolve, { ...this.origin, location }); } locale(locale) { return new _GraphRealm(this.config, this.resolve, { ...this.origin, locale }); } async maybeGet(select) { if (select instanceof Cursor.Find) select = select.first(); if (Type.isType(select)) select = select().first(); const selection = createSelection(select); serializeSelection(this.targets, selection); return this.resolve({ selection, location: seralizeLocation(this.config, this.origin.location), locale: this.origin.locale }); } async get(select) { const result = this.maybeGet(select); if (result === null) throw new Error("Not found"); return result; } async find(select) { const selection = createSelection(select); serializeSelection(this.targets, selection); return this.resolve({ selection, location: seralizeLocation(this.config, this.origin.location), locale: this.origin.locale }); } async count(cursor) { const selection = createSelection(cursor.count()); serializeSelection(this.targets, selection); return this.resolve({ selection, location: seralizeLocation(this.config, this.origin.location), locale: this.origin.locale }); } }; var Graph = class { constructor(config, resolve) { this.config = config; this.resolve = resolve; this.drafts = new GraphRealm(this.config, (params) => { return this.resolve({ ...params, realm: Realm.Draft }); }); this.archived = new GraphRealm(config, (params) => { return resolve({ ...params, realm: Realm.Archived }); }); this.published = new GraphRealm(config, (params) => { return resolve({ ...params, realm: Realm.Published }); }); this.preferDraft = new GraphRealm(config, (params) => { return resolve({ ...params, realm: Realm.PreferDraft }); }); this.preferPublished = new GraphRealm(config, (params) => { return resolve({ ...params, realm: Realm.PreferPublished }); }); this.all = new GraphRealm(config, (params) => { return resolve({ ...params, realm: Realm.All }); }); } drafts; archived; published; preferPublished; preferDraft; all; }; export { Graph, GraphRealm };