UNPKG

@furystack/repository

Version:

Repository implementation for FuryStack

71 lines 3.24 kB
import type { Constructable, StoreToken, WithOptionalId } from '@furystack/core'; import type { Token } from '@furystack/inject'; import type { DataSetSettings } from './data-set-setting.js'; import { DataSet } from './data-set.js'; /** * A DI token that resolves to a {@link DataSet} and carries its model and * primary key metadata (mirrored from the backing {@link StoreToken}). * * Consumers can `injector.get(token)` to obtain the dataset, or read the * `model` / `primaryKey` fields to drive reflection-style tooling such as * entity sync and OpenAPI generation. */ export type DataSetToken<T, TPrimaryKey extends keyof T, TWritableData = WithOptionalId<T, TPrimaryKey>> = Token<DataSet<T, TPrimaryKey, TWritableData>, 'singleton'> & { readonly model: Constructable<T>; readonly primaryKey: TPrimaryKey; }; /** * Settings accepted by {@link defineDataSet}. A subset of * {@link DataSetSettings}: the physical store is supplied by the store token * and must not be passed here. */ export type DefineDataSetSettings<T, TPrimaryKey extends keyof T, TWritableData = WithOptionalId<T, TPrimaryKey>> = Omit<DataSetSettings<T, TPrimaryKey, TWritableData>, 'physicalStore'>; /** * Options accepted by {@link defineDataSet}. * * `settings` is wrapped in `NoInfer` so the `store` token alone fixes the * generic parameters. Without it, an inline callback inside `settings` * (e.g. a `modifyOnAdd` arrow) triggers bidirectional inference where * TypeScript widens `TPrimaryKey` back to `keyof T` to satisfy the * callback's contextual typing. */ export type DefineDataSetOptions<T, TPrimaryKey extends keyof T, TWritableData = WithOptionalId<T, TPrimaryKey>> = { /** * Debug-only identifier. Token identity is established by the returned * {@link DataSetToken} object reference, not this string. */ name: string; /** Backing store; its `model` and `primaryKey` propagate to the returned token. */ store: StoreToken<T, TPrimaryKey>; settings?: NoInfer<DefineDataSetSettings<T, TPrimaryKey, TWritableData>>; }; /** * Defines a singleton {@link DataSet} token backed by a {@link StoreToken}. * The token mirrors the store's `model` and `primaryKey` so reflection * tools (entity sync, OpenAPI generators, test harnesses) can discover the * dataset's shape from the token alone. Disposal of the owning injector * tears down the dataset's event subscriptions. * * @example * ```ts * const UserStore = defineStore({ * name: 'my-app/UserStore', * model: User, * primaryKey: 'username', * factory: () => new InMemoryStore({ model: User, primaryKey: 'username' }), * }) * * export const UserDataSet = defineDataSet({ * name: 'my-app/UserDataSet', * store: UserStore, * settings: { * authorizeAdd: async ({ injector, entity }) => ({ isAllowed: true }), * }, * }) * * const ds = injector.get(UserDataSet) * await ds.add(injector, { username: 'alice', roles: [] }) * ``` */ export declare const defineDataSet: <T, const TPrimaryKey extends keyof T, TWritableData = WithOptionalId<T, TPrimaryKey>>(options: DefineDataSetOptions<T, TPrimaryKey, TWritableData>) => DataSetToken<T, TPrimaryKey, TWritableData>; //# sourceMappingURL=define-data-set.d.ts.map