import { ItemService } from '../catalog/index.js'; import { DcuplModel, ModelParser } from '../model-parser/index.js'; import { DataContainer, DcuplInitOptions, ModelDefinition, ListItem, RawItem, ViewDefinition, ChangeDetector, DcuplGlobalItemOptions, CallbackFunction, DcuplQueryGroup, DcuplQuery, DcuplGlobalQueryOptions, DataOptions, DcuplGlobalManyItemOptions, AnalyticsController, CustomOperatorFn, QueryManager, PivotResponse, DcuplGlobalPivotOptions, DcuplGlobalAggregateOptions, Aggregation, DcuplGlobalSuggestionOptions, Suggestion, DcuplGlobalGroupByOptions, GroupByResponse, DcuplGlobalFacetOptions, FilterItemEntry, ListMetadata, DcuplGlobalMetadataOptions, ScriptController, EndpointScriptRequest, QualityAnalyzer } from '@dcupl/common'; import { ListsController } from './dcupl-list-controller.js'; import { QualityController } from '../quality/quality.controller.js'; /** * Source metadata for a DataContainer carried via side channel from the loader. * Not part of the public DataContainer type — kept internal so SDK users * authoring containers programmatically don't need to populate these fields. */ export type ContainerSourceMetadata = { sourceColumns: string[]; coercionFailureSamples: Record; /** * Where the container data came from. For loader-fetched resources this is * the resource URL; for inline `model.data` no metadata is registered and * callers should fall back to `'inline'`. Surfaced in error `meta.origin`. */ origin?: string; }; /** * Options for update() method */ export type DcuplUpdateOptions = { /** * Override auto-update behavior for this specific update. * - true: Force auto-update for all lists (even those configured as false) * - false: Disable auto-update for all lists (even those configured as true) * - undefined: Use list-level and global configuration * * Highest priority - overrides both list-level and global settings. * * Auto-update gates re-deriving a list's item set (a scoped list's query * re-runs). Affected lists that do not auto-update still have their * query/result caches dropped — lazily, recomputed on next read — so no * list ever serves pre-update results (#239/#245). * @default undefined */ autoUpdateLists?: boolean; }; export declare class DcuplApi { protected cdRef: ChangeDetector; protected analyticsController: AnalyticsController; protected containerSourceMetadata: WeakMap; protected scriptController: ScriptController; protected qualityController: QualityController; private log; modelParser: ModelParser; globalItemService: ItemService; protected queryManager: QueryManager>; private _loaders; private rootQueryBuilder; private _defaultInitOptions; initOptions: DcuplInitOptions; lists: ListsController; private busy; private initialized; private updateQueue; constructor(options?: DcuplInitOptions); /** True after seal() / an init() with performance.retainStagedData: false (#332). */ get sealed(): boolean; /** * Drops the staged raw rows for good (#332): frees their memory, and from now on every data or * model write throws DcuplSealedError while init()/update() resolve false. Reads are unaffected. * Refuses (throws DcuplSealedError, changes nothing) before the first completed init(), while an * apply is in flight, while a loader resource is still pending, or while staged rows or model * changes are not applied yet (call `update()` first): sealing then would lose data that was * never applied. */ seal(): void; /** A loader resource that registered its placeholder container and has not resolved yet. */ private hasPendingLoaderResources; /** * Rows or model changes (definition set/updated/removed, model dropped) staged since the last * apply. The parser clears its change tracker and definition changes when an init()/update() * completes and re-records what was staged while it ran, so this is false right after one. */ private hasUnappliedChanges; /** Why the staged log cannot be dropped now because it holds data that was never applied; undefined if it can. */ private sealBlocker; /** * First statement of every staging verb: a call on a sealed instance names itself in the error and * changes nothing, where the parser's own guard would fire after e.g. `models.set` had already * dropped the registered definition (#332). */ private rejectWhenSealed; models: { /** * A copy of the model's definition (#314): changing it does not change this instance. Inline * `data` passed to `set`/`update` is not part of it (it is loaded like `data.set`). */ get: (modelKey: ModelNames) => ModelDefinition | undefined; /** * Registers `model` as the whole definition for its key: a definition registered earlier under * the same key (by `set` or `update`) is replaced, so get → change → `set` edits a model and an * attribute left out is removed. Takes effect on the next `init()` or `update()`; after init, * that `update()` rebuilds all data like `init()`, and existing lists serve the new definition * (#408). The instance keeps a copy, so later changes to `model` have no effect (#314). For a * partial change, use `update`. */ set: (model: ModelDefinition) => void; /** * Merges `model` into the definition registered under its key (or registers it): scalar fields * of `model` win, `properties` and `references` are appended, and an attribute declared twice * keeps its first declaration (to change an attribute, use `set`). Takes effect on the next * `init()` or `update()`; after init, that `update()` rebuilds all data like `init()` (#408). * Stores a copy (#314). */ update: (model: ModelDefinition) => void; /** * Unregisters the model: its definition, its data (rows passed to `data.*` or inline) and its * lists are gone at once, also when an `init()`/`update()` is running, and the next `init()` does * not bring it back. Its errors leave `quality.errors.summary()` at once and the * `$DcuplErrorTracking*` models on the next `init()`/`update()`. That run rebuilds all data like * `init()` and resolves references to the model as to a missing model. A later `set` registers * it again, without the old data (#420). */ remove: (modelKey: ModelNames) => void; keys: () => string[]; }; quality: { errors: { /** * Exact error totals per (model, attribute, errorType) group, including * how many records were retained under `maxErrorsPerGroup` and whether * the group was truncated. Also queryable as `$DcuplErrorTrackingSummary`. */ summary: () => QualityAnalyzer.ErrorGroupSummary[]; }; }; views: { /** A copy of the view's definition (#314): changing it does not change this instance. */ get: (viewKey: string) => ViewDefinition | undefined; keys: (options?: { modelKey: string; }) => string[]; /** * Registers `view` as the whole definition for its key, replacing an earlier one. The instance * keeps a copy, so later changes to `view` have no effect (#314). */ set: (view: ViewDefinition) => void; /** Unregisters the view. Takes effect at once; no `update()` is needed (#420). */ remove: (viewKey: string) => void; }; data: { update: (data: RawItem[], options: DataOptions) => void; upsert: (data: RawItem[], options: DataOptions) => void; set: (data: RawItem[], options: DataOptions) => void; remove: (data: RawItem[], options: DataOptions) => void; /** * Stages the removal of every row of a model. Like the other data verbs * it is applied by the next `update()`/`init()`, is folded in call order * with mutations staged after it, and invalidates the affected lists * through the normal update path. Equivalent to `data.set([], { model })`. * Used to clear the built model directly, which the next apply undid by * replaying the data log (#257). */ reset: (options: { model: ModelNames; }) => void; apply: (container: DataContainer) => void; }; loaders: { add: (loader: any, key?: string) => void; remove: (key: string) => void; get: (key: string) => any; }; scripts: { set: (key: string, script: string) => void; remove: (key: string) => void; get: (key: string) => string | undefined; keys: () => string[]; execute: (key: string, options?: any) => any; executeAsync: (key: string, options?: any) => any; /** * Runs `key` in `endpoint` mode (#311): the script receives `{ dcupl, request, options }` where * `dcupl` is the read-only facade of this instance, and its return value — awaited when it is a * Promise — becomes the result. `request` is passed by reference (the host clones it). * Rejects with `ScriptNotFoundError` for an unknown key — check `error.name === 'ScriptNotFound'`, * not `instanceof`, since `@dcupl/core` bundles its own copy of `@dcupl/common` — and refuses * like `execute` when scripting is disabled. Timeouts are the host's job: synchronous script * code cannot be interrupted here. */ executeEndpoint: (key: string, request: EndpointScriptRequest, options?: Record) => Promise; }; private _readOnlyFacade?; /** * What an `endpoint` script sees as `dcupl` (#311): the read paths of this instance as closures * over the real methods, frozen at every level, one object per instance. `data`, `init`, * `update`, `lists`, `scripts.set` and `query.registerCustomOperator` are absent, so * `dcupl.data.update(...)` is a `TypeError` inside the script. * * Every result is a deep copy whatever `performance.clone` says: with cloning off (the app * runner's setting) the real query methods hand out live items, so a script could otherwise * edit instance state through a result. Same `clone` as `@trigger({ cloneResult: true })`, * forced on. `models.get`/`views.get` are passed through: they return copies already (#314). */ private getReadOnlyFacade; fn: { pivot: (options: DcuplGlobalPivotOptions) => PivotResponse; aggregate: (options: DcuplGlobalAggregateOptions) => Aggregation; suggest: (options: DcuplGlobalSuggestionOptions) => Suggestion[]; groupBy: (options: DcuplGlobalGroupByOptions) => GroupByResponse; facets: (options: DcuplGlobalFacetOptions) => FilterItemEntry[]; metadata: (options: DcuplGlobalMetadataOptions) => ListMetadata; }; private _pivot; private _aggregate; private _suggest; private _groupBy; private _facets; private _metadata; protected registerContainer(container: DataContainer): void; protected updateRegisteredContainer(container: DataContainer, sourceMetadata?: ContainerSourceMetadata): void; protected removeRegisteredContainer(container: DataContainer): void; getContainerSourceMetadata(container: DataContainer): ContainerSourceMetadata | undefined; /** * Registers source metadata for a container. Used internally by the model * parser to carry per-resource loader metadata onto a merged container * (dcupl#197). Not part of the public SDK contract. * @internal */ setContainerSourceMetadata(container: DataContainer, metadata: ContainerSourceMetadata): void; /** * The hidden list that serves `fn.*` and `query.execute` for a model. It is reused across * calls, so every call starts from a reset query and applies `options.query` as the whole * (root) query: a query without `groupType` is evaluated as `'and'`, the same as in * `query.execute` and `lists.create({ query })`, and the same call always returns the same * result (#370). Only `groupType` and `queries` are taken from `options.query`. */ private getFnList; /** * Snapshots the pending changes as one `model → changed attribute keys` map * before the parser clears the tracker. Every effectively changed model is a * key: a full reset (data.set) records no attribute changes and a deletion * records its attribute changes only later, inside the parser, so both * expand to all of the model's property and reference keys — which is what * the parser itself treats as changed for them. Returns null when nothing * is pending (a plain full rebuild). */ private snapshotChangedAttributes; /** * Adds the models whose definition `models.set`/`models.update` changed since the last init to * the changed attributes, with every attribute of the rebuilt model, so their lists are * refreshed and the update event names them (#408). */ /** * Adds the models that gained or lost items a referrer auto-created in the update, with their * `key`: their item set changed though no change of theirs was staged, so their lists refresh and * the update event names them (#599). The items' other attributes are new, not changed: no * attribute of another model derives from an item without referrers. */ private withAutoCreationChanges; private withRedefinedModels; /** * Gets all models that are affected by changes to the specified models/attributes. * Includes the changed models themselves, models that (transitively) reference * them (their lists can observe the change through deep queries), and models * reached through the attribute-level cascade (resolved/derived attributes * live on models the reference graph does not point at) (#241). */ private getAffectedModels; /** * Update lists based on auto-update configuration and affected models. * * @param changeInfo - Information about changed/affected models (null for full updates) * @param autoUpdateOverride - Per-update auto-update override */ private updateFnLists; /** * Determines if a list is affected by changes to specific models: its own * model is affected, or one of the models its cached results were read * through is. The model graph has no edges for resolved and derived * references (they would make it cyclic), so a `project` list filtered on * `memberships.user.daysInactive` is not downstream of `user` there — the * list itself knows which models its dotted paths resolve (#284). */ private isListAffectedByChanges; /** * Determines if auto-update should be enabled for a specific list. * Priority: per-update > list-level > global * * @param list - The list to check * @param perUpdateOverride - Optional per-update override from update() call * @returns true if the list should auto-update, false otherwise */ private shouldAutoUpdateList; query: { one: (options: DcuplGlobalItemOptions) => ItemType | null; many: (options: DcuplGlobalManyItemOptions) => ItemType[]; execute: (options: DcuplGlobalQueryOptions) => ItemType[]; generate: (modelKey: string, query: DcuplQueryGroup | DcuplQuery | DcuplQuery[] | DcuplQueryGroup[]) => DcuplGlobalQueryOptions; registerCustomOperator: (operator: string, fn: CustomOperatorFn) => void; }; on(cb: CallbackFunction): () => void; init(): Promise; update(options?: DcuplUpdateOptions): Promise; destroy(): void; private getItem; private _getManyItems; private executeQuery; private addLoader; private removeLoader; /** * UPDATE QUEUE * * Every init()/update() apply runs through here, strictly one at a time and * in call order. `busy` stays set until the queue is drained, so a call that * arrives while queued applies are still running waits for its turn instead * of running alongside them. Each queued entry settles its own caller's * promise — resolve or reject — and never rejects itself, so a failing apply * neither strands the applies queued behind it nor surfaces as an unhandled * rejection. When idle, the apply starts synchronously, as before. */ private performUpdate; private processQueue; }