/** * Resolves the cache service used for relation caching via a precedence chain * (per-model `relationsCacheService` option → process-wide default → none) and * exposes the package-level default registration setter. * * @module @nhtio/lucid-resourceful/private/utils/cache_service_resolver */ import type { LucidModel } from '@adonisjs/lucid/types/model'; import type { CacheProvider, RawCommonOptions, Duration } from 'bentocache/types'; /** * A cache TTL/duration as understood by the underlying cache service. * * Aliased to BentoCache's own `Duration` (`number | string | null`) so the * relation cache never drifts from the option types it forwards to. * * @public */ export type CacheDuration = Duration; /** * Per-relation (and model-level) cache entry options. * * The subset of BentoCache's `RawCommonOptions` that the relation cache forwards * to `set` (and may merge for `getOrSet`). Sourced directly from BentoCache via * `Pick`, so each option's type is the cache implementation's own — there is no * hand-rolled duplicate to keep in sync. * * @public */ export type RelationCacheEntryOptions = Partial>; /** * The cache service the relation cache talks to. * * The narrow slice of BentoCache's `CacheProvider` the relation cache actually * uses (`get`/`set`/`getOrSet`/`delete`/`deleteByTag`), taken via `Pick` so the * method signatures track BentoCache exactly. A `BentoCache` instance (e.g. * `app.container.make('cache')`) satisfies this directly, while the narrow surface * keeps test doubles small and documents precisely what the package depends on. * * @public */ export type CacheServiceLike = Pick; /** * Resolver supplied by the host that returns the cache service for a model, or * `null` to opt that model out of relation caching. * * @public */ export type RelationsCacheServiceResolver = () => CacheServiceLike | null | Promise; /** * Registers a related-model identity name as a relation-cache TARGET. Idempotent. * * @param name - The related model's canonical identity name (`$resourcefulName` * or class name). * @public */ export declare const registerRelationCacheTarget: (name: string) => void; /** * Reports whether a model identity name is a registered relation-cache TARGET, * i.e. some model caches a 1:1 relation pointing at this model. * * @param name - The model's canonical identity name. * @returns `true` when the name is a registered cache target. * @public */ export declare const isRelationCacheTarget: (name: string) => boolean; /** * Clears the registered relation-cache target set. Test-only hook: the set is * module-scoped and accumulates across a process, which can mask regressions * between tests. Hosts must NOT call this at runtime. * * @public */ export declare const resetRelationCacheTargets: () => void; /** * Registers (or clears) the process-wide default relation cache resolver. * * The host application calls this once at boot. Passing `null` clears the * default (used by tests). Models without their own `relationsCacheService` * fall back to this resolver via the precedence chain. * * @param resolver - The default resolver, or `null` to clear it. * @public */ export declare const setDefaultRelationsCacheService: (resolver: RelationsCacheServiceResolver | null) => void; /** * Reads the currently-registered default resolver (used internally and by the * write-through eviction hook). Exposed for completeness; prefer * `resolveRelationsCacheService`. * * @returns The default resolver, or `null` when none is registered. * @internal */ export declare const getDefaultRelationsCacheService: () => RelationsCacheServiceResolver | null; /** * Resolves the cache service for a model via the precedence chain: * per-model resolver → process-wide default → `null` (uncached). * * Returns `null` only when the selected resolver itself returns `null` (an * intentional opt-out). If the selected resolver throws or fails to acquire the * cache, the error propagates — callers on the read path must fail loud, while * the write-through hook guards this call to stay best-effort (ADR-007). * Resolved instances are memoized per model; a premature `null` is never cached. * * @param model - The Lucid model whose cache service should be resolved. * @param perModelResolver - The model's own `relationsCacheService` option, if any. * @returns The resolved cache service, or `null` when caching is opted out. * @public */ export declare const resolveRelationsCacheService: (model: LucidModel, perModelResolver?: RelationsCacheServiceResolver) => Promise;