import { DataStoreToken } from './DataStoreToken'; import { RetentionOptions } from './Compartments'; import { DataCompartment } from './DataCompartment'; import { ILookup } from './Lookup'; import { ICompartmentStorage } from './ICompartmentStorage'; /** * Represents a data source used to resolve an entity by key. */ export interface IEntityResolver { /** * An idempotent function used to load the entity. * @param key The key of the entity to load */ resolve: (key: Key) => Promise; } /** * Adapts a function to the IEntityResolver interface. */ export declare class ConfiguredEntityResolver implements IEntityResolver { private readonly resolver; constructor(resolver: (key: Key) => Promise); /** * An idempotent function used to load the entity. * @param key The key of the entity to load */ resolve(key: Key): Promise; } /** * Provides strongly-typed configuration options for an individual entity compartment. */ interface IRepositoryCompartmentOptions { /** * Optional configuration for controlling how frequently the entity is reloaded. * If no configuration is provided, the entity will be retained in-memory until it is manually updated. */ retention?: RetentionOptions; } /** * Provides strongly-typed configuration options for an individual entity compartment. */ export interface RepositoryCompartmentOptions extends IRepositoryCompartmentOptions { /** * The data source used to fill a compartment. */ resolver: IEntityResolver; } /** * Represents a set of entity compartments. */ export interface IRepository extends ICompartmentStorage { /** * Clears the cached value for the specified key/id. * @param key The key of the compartment to clear. * @param id The id of the entity to clear. */ clear(key: keyof Registry, id: Key): void; /** * Clears all cached values for the specified key. * @param key The key of the compartment to clear. */ clearAll(key: keyof Registry): void; /** * Gets a data compartment that is configured to retrieve the specific entity. * @param key The key of the compartment to retrieve. * @param id The id of the entity to retrieve. */ get(key: keyof Registry, id: Key): DataCompartment; /** * Modifies the underlying value of the specified key/id. * @param key The key of the compartment * @param id The id of the entity * @param modifier The function used to modify the current value. */ modify(key: keyof Registry, id: Key, modifier: (currentValue: Model) => Promise): Promise; /** * Resets the repository by clearing all caches. */ reset(): void; /** * The token used to identify the repository. */ token: DataStoreToken; } /** * Represents a set of entity compartments. */ export declare class Repository implements IRepository { readonly token: DataStoreToken; readonly managedTokens: DataStoreToken[]; private readonly lookups; private readonly compartmentLookups; constructor(token: DataStoreToken, managedTokens: DataStoreToken[], lookups: { [key: string | number]: ILookup>; }); /** * Clears the cached value for the specified key/id. * @param key The key of the compartment to clear. * @param id The id of the entity to clear. */ clear(key: keyof Registry, id: Key): void; /** * Clears all the cached values for the specified key. * @param key The key of the compartment to clear. */ clearAll(key: keyof Registry): void; /** * Gets a data compartment that is configured to retrieve the specific entity. * @param key The key of the compartment to retrieve. * @param id The id of the entity to retrieve. */ get(key: keyof Registry, id: Key): DataCompartment; /** * Modifies the underlying value of the specified key/id. * @param key The key of the compartment * @param id The id of the entity * @param modifier The function used to modify the current value. */ modify(key: keyof Registry, id: Key, modifier: (currentValue: Model) => Promise): Promise; /** * Resets the repository by clearing all caches. */ reset(): void; private findLookup; } /** * Creates a repository to hold compartments for each item in the specified registry. * @param registry The registry used to configure the various entity options * @example * ```ts * import { RepositoryCompartmentOptions } from '@aesop-fables/scrinium'; * * // In this example, we'll be looking up videos by id * interface Video { * id: string; * title: string; * url: string; * } * * // Now we define the interface that's used to configure each entity * interface VideoRegistry { * videos: RepositoryCompartmentOptions; * } * * const repository = createRepository({ * videos: { * resolver: new ConfiguredEntityResolver((id) => axios.get(`/videos/${id}`)), * }, * }); * * // This returns a `DataCompartment`. * const videoCompartment = repository.get('videos', '1'); * ``` */ export declare function createRepository>(token: DataStoreToken, registry: Registry): IRepository; export {};