UNPKG

@furystack/repository

Version:

Repository implementation for FuryStack

62 lines 2.91 kB
import type { CreateResult, FilterType, FindOptions, PartialResult, WithOptionalId } from '@furystack/core'; import type { Injector } from '@furystack/inject'; import { EventHub, type ListenerErrorPayload } from '@furystack/utils'; import type { DataSetSettings } from './data-set-setting.js'; /** * Authorization-enforcing wrapper around a {@link PhysicalStore}. The * recommended write gateway for application code — `furystack/no-direct-store-token` * enforces this. Each mutation runs the relevant `authorize*` and * `modify*` callbacks from {@link DataSetSettings}, persists, then emits * `onEntityAdded` / `onEntityUpdated` / `onEntityRemoved` (consumed by * entity sync, audit logs). * * Mutating methods take an `injector` parameter to surface caller identity * to the authorizers. For server-side / background work without an HTTP * request, wrap the injector with `useSystemIdentityContext` from * `@furystack/core`. * * @example * ```ts * await usingAsync( * useSystemIdentityContext({ injector, username: 'background-job' }), * async (systemInjector) => { * const dataSet = getDataSetFor(systemInjector, UserDataSet) * await dataSet.add(systemInjector, { username: 'alice', roles: [] }) * }, * ) * ``` */ export declare class DataSet<T, TPrimaryKey extends keyof T, TWritableData = WithOptionalId<T, TPrimaryKey>> extends EventHub<{ onEntityAdded: { injector: Injector; entity: T; }; onEntityUpdated: { injector: Injector; id: T[TPrimaryKey]; change: Partial<T>; }; onEntityRemoved: { injector: Injector; key: T[TPrimaryKey]; }; onListenerError: ListenerErrorPayload; }> implements Disposable { readonly settings: DataSetSettings<T, TPrimaryKey, TWritableData>; primaryKey: TPrimaryKey; add(injector: Injector, ...entities: TWritableData[]): Promise<CreateResult<T>>; update(injector: Injector, id: T[TPrimaryKey], change: Partial<T>): Promise<void>; count(injector: Injector, filter?: FilterType<T>): Promise<number>; find<TFields extends Array<keyof T>>(injector: Injector, filter: FindOptions<T, TFields>): Promise<Array<PartialResult<T, TFields>>>; get<TSelect extends Array<keyof T>>(injector: Injector, key: T[TPrimaryKey], select?: TSelect): Promise<PartialResult<T, TSelect> | undefined>; /** * Removes by primary key. Pre-load `authorizeRemove` and per-entity * `authorizeRemoveEntity` are all-or-nothing — any rejection aborts the * whole batch before any persist call. When `authorizeRemoveEntity` is * configured, missing keys are silently forwarded to the physical store * (no entity to authorize). */ remove(injector: Injector, ...keys: Array<T[TPrimaryKey]>): Promise<void>; constructor(settings: DataSetSettings<T, TPrimaryKey, TWritableData>); } //# sourceMappingURL=data-set.d.ts.map