import { BetterDrizzleError } from "better-drizzle"; //#region src/plugins/cache/types.d.ts /** * A duration in seconds, or a string such as `'30s'`, `'5m'`, `'1h'`, `'1d'`. */ type CacheDuration = number | `${number}s` | `${number}m` | `${number}h` | `${number}d`; /** One value written by the plugin. `ttl` is in whole seconds. */ type CacheStoreEntry = { key: string; ttl: number; value: string; }; /** * Storage used by the cache plugin. `get` returns one value per key, in * order, with `null` for missing keys. */ interface CacheStore { delete(keys: readonly string[]): Promise; get(keys: readonly string[]): Promise>; set(entries: readonly CacheStoreEntry[]): Promise; } /** Converts results to strings and back. */ type CacheSerializer = { deserialize(value: string): unknown; serialize(value: unknown): string; }; /** Per-call cache settings for reads. */ type CacheReadOptions = { /** Run after hooks on a hit. Defaults to the plugin's `afterHooksOnHit`. */ afterHooks?: boolean; /** Custom key. Namespace, version, and dependencies still apply. */ key?: string; /** TTL for `null` results of `findFirst`, `findOne`, and `findUnique`. */ negativeTtl?: CacheDuration; /** Skip the cached value, run the query, and store the fresh result. */ refresh?: boolean; /** Tags that `$cache.invalidate({ tags })` and write hints can target. */ tags?: readonly string[]; ttl?: CacheDuration; /** Extra value hashed into the key. */ vary?: unknown; }; /** * The `cache` argument accepted by reads. `true` enables caching with * defaults, `false` disables it, and a string sets a custom key. */ type CacheReadArg = boolean | string | CacheReadOptions; /** Models and tags to invalidate after a write succeeds or commits. */ type CacheInvalidateHint = { models?: readonly string[]; tags?: readonly string[]; }; /** The `cache` argument accepted by writes and raw SQL calls. */ type CacheWriteArg = { invalidate?: CacheInvalidateHint; }; /** Targets for `$cache.invalidate()`. `keys` are custom read keys. */ type CacheInvalidateTarget = CacheInvalidateHint & { keys?: readonly string[]; }; /** Per-model defaults. `true` enables caching for every read of the model. */ type CacheModelOptions = boolean | { enabled?: boolean; negativeTtl?: CacheDuration; tags?: readonly string[]; ttl?: CacheDuration; }; /** Values available to the `vary` option. */ type CacheVaryContext = { meta: unknown; model: string; operation: string; state: Readonly>; transactionContext: Record | undefined; }; type CacheOptions = { /** Run after hooks on a hit. Defaults to `true`. */ afterHooksOnHit?: boolean; /** Cache every read unless a model or call disables it. Defaults to `false`. */ enabled?: boolean; /** Largest serialized entry in bytes. Defaults to 1 MiB. */ maxSize?: number; models?: Readonly>; /** Key prefix. Defaults to `'better-drizzle'`. */ namespace?: string; /** TTL for `null` single-row results. Defaults to `min(ttl, 30s)`. */ negativeTtl?: CacheDuration; /** * Called when the store fails or a value cannot be cached. Reads fall * back to the database and writes still succeed. */ onError?: (error: BetterDrizzleError) => void; serializer?: CacheSerializer; store: CacheStore; /** Default entry TTL. */ ttl: CacheDuration; /** Values hashed into every key, e.g. the current tenant. */ vary?: (context: CacheVaryContext) => unknown; /** Bump to drop every entry written by another version. Defaults to `1`. */ version?: number | string; /** * Lifetime of dependency versions. Entry TTLs cannot exceed it. * Defaults to `'7d'`. */ versionTtl?: CacheDuration; }; type CacheClient = { /** Invalidates every entry written under the current namespace and version. */ clear(): Promise; /** Invalidates entries that depend on the given models, tags, or keys. */ invalidate(target: CacheInvalidateTarget): Promise; }; type CacheClientExtension = { $cache: CacheClient; }; type ReadArgs = { cache?: CacheReadArg; }; type WriteArgs = { cache?: CacheWriteArg; }; type CacheOperationArgs = { count: ReadArgs; create: WriteArgs; createMany: WriteArgs; cursor: ReadArgs; delete: WriteArgs; deleteMany: WriteArgs; exists: ReadArgs; findFirst: ReadArgs; findMany: ReadArgs; findOne: ReadArgs; findUnique: ReadArgs; paginate: ReadArgs; update: WriteArgs; updateEach: WriteArgs; updateMany: WriteArgs; upsert: WriteArgs; upsertMany: WriteArgs; }; //#endregion export { CacheInvalidateTarget as a, CacheOptions as c, CacheSerializer as d, CacheStore as f, CacheWriteArg as h, CacheInvalidateHint as i, CacheReadArg as l, CacheVaryContext as m, CacheClientExtension as n, CacheModelOptions as o, CacheStoreEntry as p, CacheDuration as r, CacheOperationArgs as s, CacheClient as t, CacheReadOptions as u };