@prisma-next/middleware-cache 0.9.0-dev.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs ADDED
@@ -0,0 +1,246 @@
1
+ import { defineAnnotation } from "@prisma-next/framework-components/runtime";
2
+ //#region src/cache-annotation.ts
3
+ /**
4
+ * Read-only annotation handle for the cache middleware.
5
+ *
6
+ * Declared with `applicableTo: ['read']`. Write terminals supply
7
+ * `K = 'write'` to the type-level `ValidAnnotations<'write', As>` gate
8
+ * (and the runtime `assertAnnotationsApplicable(annotations, 'write', ...)`
9
+ * check); the join `K extends Kinds` fails for this annotation, making
10
+ * "cache a mutation" structurally impossible without an `as any` cast
11
+ * bypass at both type *and* runtime levels.
12
+ *
13
+ * Stored under namespace `'cache'` in `plan.meta.annotations`. The cache
14
+ * middleware reads it via `cacheAnnotation.read(plan)`.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * import { cacheAnnotation } from '@prisma-next/middleware-cache';
19
+ *
20
+ * // ORM read terminal — accepts the read-only annotation via the meta callback.
21
+ * const user = await db.User.first(
22
+ * { id },
23
+ * (meta) => meta.annotate(cacheAnnotation({ ttl: 60_000 })),
24
+ * );
25
+ *
26
+ * // SQL DSL select builder — chainable.
27
+ * const plan = db.sql
28
+ * .from(tables.user)
29
+ * .annotate(cacheAnnotation({ ttl: 60_000 }))
30
+ * .select({ id: tables.user.columns.id })
31
+ * .build();
32
+ * ```
33
+ */
34
+ const cacheAnnotation = defineAnnotation()({
35
+ namespace: "cache",
36
+ applicableTo: ["read"]
37
+ });
38
+ //#endregion
39
+ //#region src/cache-store.ts
40
+ /**
41
+ * Default cache backend. An LRU with per-entry TTL, backed by a `Map`.
42
+ *
43
+ * Eviction policy:
44
+ *
45
+ * - On `set` of a fresh key whose insertion would push the live count
46
+ * above `maxEntries`, the least recently used entry is evicted.
47
+ * Setting an existing key updates the entry in place and refreshes its
48
+ * recency without changing the live count.
49
+ * - On `get` of an existing key, recency is bumped (so the entry is no
50
+ * longer the LRU candidate).
51
+ * - On `get` of an expired entry, the entry is removed from the map and
52
+ * `undefined` is returned. The slot becomes available for new writes
53
+ * without counting against `maxEntries`.
54
+ *
55
+ * `Map` insertion order is the LRU order: the first key is the LRU
56
+ * candidate; the last key is the most recently used. Bumping recency is
57
+ * a delete-then-set on the underlying map.
58
+ *
59
+ * The default store is **not** coherent across processes or replicas —
60
+ * each process holds its own Map. Users who need a shared cache supply
61
+ * their own `CacheStore` (Redis, Memcached, etc.).
62
+ */
63
+ function createInMemoryCacheStore(options) {
64
+ const maxEntries = options.maxEntries;
65
+ const clock = options.clock ?? Date.now;
66
+ const map = /* @__PURE__ */ new Map();
67
+ function get(key) {
68
+ const record = map.get(key);
69
+ if (record === void 0) return Promise.resolve(void 0);
70
+ if (clock() >= record.expiresAt) {
71
+ map.delete(key);
72
+ return Promise.resolve(void 0);
73
+ }
74
+ map.delete(key);
75
+ map.set(key, record);
76
+ return Promise.resolve(record.entry);
77
+ }
78
+ function set(key, entry, ttlMs) {
79
+ const expiresAt = clock() + ttlMs;
80
+ if (map.has(key)) map.delete(key);
81
+ map.set(key, {
82
+ entry,
83
+ expiresAt
84
+ });
85
+ while (map.size > maxEntries) {
86
+ const oldest = map.keys().next();
87
+ if (oldest.done) break;
88
+ map.delete(oldest.value);
89
+ }
90
+ return Promise.resolve();
91
+ }
92
+ return {
93
+ get,
94
+ set
95
+ };
96
+ }
97
+ //#endregion
98
+ //#region src/cache-middleware.ts
99
+ /**
100
+ * Default `maxEntries` for the built-in in-memory store. Bounded so a
101
+ * runaway producer cannot exhaust process memory; users who need
102
+ * different bounds supply a custom `CacheStore`.
103
+ */
104
+ const DEFAULT_MAX_ENTRIES = 1e3;
105
+ /**
106
+ * Reads the cache payload from the plan, if present and branded.
107
+ *
108
+ * Returns `undefined` when:
109
+ * - the plan has no `meta.annotations`, or
110
+ * - the `cache` namespace key is absent, or
111
+ * - the value under `cache` is not a branded `AnnotationValue` (the
112
+ * `cacheAnnotation.read` defensive check covers this).
113
+ */
114
+ function readCachePayload(plan) {
115
+ return cacheAnnotation.read(plan);
116
+ }
117
+ /**
118
+ * Computes the cache key for an execution.
119
+ *
120
+ * Two-tier resolution:
121
+ *
122
+ * 1. Per-query override: `cacheAnnotation({ key })` — the supplied
123
+ * string is used verbatim. Not rehashed; the user is responsible for
124
+ * keeping the string bounded and free of sensitive data.
125
+ * 2. Default: `ctx.contentHash(exec)` — the family runtime owns this and
126
+ * returns an opaque, bounded digest (SHA-512 in the SQL and Mongo
127
+ * runtimes today).
128
+ *
129
+ * The returned string is consumed directly as the `Map<string, …>` key
130
+ * by the underlying `CacheStore`; the cache middleware does not perform
131
+ * any further transformation.
132
+ */
133
+ async function resolveCacheKey(payload, exec, ctx) {
134
+ if (payload.key !== void 0) return payload.key;
135
+ return ctx.contentHash(exec);
136
+ }
137
+ /**
138
+ * Creates a family-agnostic caching middleware.
139
+ *
140
+ * The middleware uses three hooks:
141
+ *
142
+ * - `intercept` — on each execution, checks the cache. On a hit, returns
143
+ * the cached raw rows; the runtime skips `beforeExecute`, `runDriver`,
144
+ * and `onRow`, and yields the cached rows to the consumer (which, in
145
+ * the SQL runtime, sees them after the standard `decodeRow` pass —
146
+ * i.e. the cache stores wire-format values). On a miss, records a
147
+ * pending buffer keyed on the `exec` object identity and returns
148
+ * `undefined` (passthrough).
149
+ * - `onRow` — on the miss path, appends each row yielded by the driver
150
+ * to the pending buffer.
151
+ * - `afterExecute` — on the miss path, commits the buffer to the store
152
+ * if and only if `result.completed === true && result.source === 'driver'`.
153
+ * Failed executions and middleware-served executions never populate
154
+ * the cache. The pending buffer is cleared in all branches so a stale
155
+ * `WeakMap` entry cannot leak between executions sharing an `exec`.
156
+ *
157
+ * The middleware bypasses the cache entirely when:
158
+ * - the plan has no `cache` annotation, or
159
+ * - the annotation has `skip: true`, or
160
+ * - the annotation has no `ttl`, or
161
+ * - `ctx.scope !== 'runtime'` (connection / transaction scopes opt out).
162
+ *
163
+ * Returns a cross-family `RuntimeMiddleware` (no `familyId` /
164
+ * `targetId`). The package depends only on
165
+ * `@prisma-next/framework-components/runtime`; cache keys come from
166
+ * `ctx.contentHash(exec)`, populated by the family runtime, so SQL and
167
+ * Mongo runtimes both work out of the box.
168
+ *
169
+ * @example
170
+ * ```typescript
171
+ * import { createCacheMiddleware, cacheAnnotation } from '@prisma-next/middleware-cache';
172
+ *
173
+ * const db = postgres({
174
+ * contractJson,
175
+ * url: process.env['DATABASE_URL']!,
176
+ * middleware: [createCacheMiddleware({ maxEntries: 1000 })],
177
+ * });
178
+ *
179
+ * const user = await db.User.first(
180
+ * { id },
181
+ * (meta) => meta.annotate(cacheAnnotation({ ttl: 60_000 })),
182
+ * );
183
+ * ```
184
+ */
185
+ function createCacheMiddleware(options) {
186
+ const store = options?.store ?? createInMemoryCacheStore({ maxEntries: options?.maxEntries ?? DEFAULT_MAX_ENTRIES });
187
+ const clock = options?.clock ?? Date.now;
188
+ const pending = /* @__PURE__ */ new WeakMap();
189
+ async function intercept(exec, ctx) {
190
+ if (ctx.scope !== "runtime") return;
191
+ const payload = readCachePayload(exec);
192
+ if (payload === void 0) return;
193
+ if (payload.skip === true) return;
194
+ if (payload.ttl === void 0) return;
195
+ const key = await resolveCacheKey(payload, exec, ctx);
196
+ const hit = await store.get(key);
197
+ if (hit !== void 0) {
198
+ ctx.log.debug?.({
199
+ event: "middleware.cache.hit",
200
+ middleware: "cache",
201
+ key
202
+ });
203
+ return { rows: hit.rows };
204
+ }
205
+ pending.set(exec, {
206
+ key,
207
+ ttlMs: payload.ttl,
208
+ buffer: []
209
+ });
210
+ ctx.log.debug?.({
211
+ event: "middleware.cache.miss",
212
+ middleware: "cache",
213
+ key
214
+ });
215
+ }
216
+ async function onRow(row, exec, _ctx) {
217
+ const slot = pending.get(exec);
218
+ if (slot === void 0) return;
219
+ slot.buffer.push(row);
220
+ }
221
+ async function afterExecute(exec, result, ctx) {
222
+ const slot = pending.get(exec);
223
+ if (slot === void 0) return;
224
+ pending.delete(exec);
225
+ if (!result.completed || result.source !== "driver") return;
226
+ await store.set(slot.key, {
227
+ rows: slot.buffer,
228
+ storedAt: clock()
229
+ }, slot.ttlMs);
230
+ ctx.log.debug?.({
231
+ event: "middleware.cache.store",
232
+ middleware: "cache",
233
+ key: slot.key
234
+ });
235
+ }
236
+ return {
237
+ name: "cache",
238
+ intercept,
239
+ onRow,
240
+ afterExecute
241
+ };
242
+ }
243
+ //#endregion
244
+ export { cacheAnnotation, createCacheMiddleware, createInMemoryCacheStore };
245
+
246
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/cache-annotation.ts","../src/cache-store.ts","../src/cache-middleware.ts"],"sourcesContent":["import { defineAnnotation } from '@prisma-next/framework-components/runtime';\n\n/**\n * Payload accepted when calling the `cacheAnnotation` handle.\n *\n * - `ttl` — Time-to-live for the cached entry, in milliseconds. When\n * omitted, the cache middleware passes the query through untouched —\n * presence of the annotation alone is not sufficient to enable caching.\n * This makes the cache strictly opt-in per query.\n * - `skip` — When `true`, the cache middleware passes the query through\n * untouched even if a `ttl` is set. Useful for selectively bypassing\n * the cache on a per-call basis without removing the annotation\n * entirely (e.g. a \"force refresh\" knob in user code).\n * - `key` — Per-query override of the cache key. When supplied, replaces\n * the default `RuntimeMiddlewareContext.contentHash(exec)` digest.\n * The supplied string is stored as-is — the cache middleware does\n * **not** rehash it, so the caller is responsible for ensuring the\n * string is bounded in size and free of sensitive data they do not\n * want flowing into logs / Redis `KEYS` / persistence dumps.\n */\nexport interface CachePayload {\n readonly ttl?: number;\n readonly skip?: boolean;\n readonly key?: string;\n}\n\n/**\n * Read-only annotation handle for the cache middleware.\n *\n * Declared with `applicableTo: ['read']`. Write terminals supply\n * `K = 'write'` to the type-level `ValidAnnotations<'write', As>` gate\n * (and the runtime `assertAnnotationsApplicable(annotations, 'write', ...)`\n * check); the join `K extends Kinds` fails for this annotation, making\n * \"cache a mutation\" structurally impossible without an `as any` cast\n * bypass at both type *and* runtime levels.\n *\n * Stored under namespace `'cache'` in `plan.meta.annotations`. The cache\n * middleware reads it via `cacheAnnotation.read(plan)`.\n *\n * @example\n * ```typescript\n * import { cacheAnnotation } from '@prisma-next/middleware-cache';\n *\n * // ORM read terminal — accepts the read-only annotation via the meta callback.\n * const user = await db.User.first(\n * { id },\n * (meta) => meta.annotate(cacheAnnotation({ ttl: 60_000 })),\n * );\n *\n * // SQL DSL select builder — chainable.\n * const plan = db.sql\n * .from(tables.user)\n * .annotate(cacheAnnotation({ ttl: 60_000 }))\n * .select({ id: tables.user.columns.id })\n * .build();\n * ```\n */\nexport const cacheAnnotation = defineAnnotation<CachePayload>()({\n namespace: 'cache',\n applicableTo: ['read'],\n});\n","/**\n * A cached set of rows produced by a single execution.\n *\n * - `rows` are stored raw (undecoded). The SQL runtime's `decodeRow` pass\n * wraps the orchestrator output, so intercepted rows go through the\n * same codec decoding as driver rows on the way to the consumer. The\n * cache stores wire-format values; decoding happens once per consumer\n * read regardless of where the rows came from.\n * - `storedAt` is the clock value at the moment the entry was committed\n * to the store. It is informational metadata for callers (debugging,\n * telemetry) and is **not** used by the in-memory store itself for\n * expiry — TTL is driven by the store's own clock plus the `ttlMs`\n * passed to `set`. Custom stores may use it differently.\n */\nexport interface CachedEntry {\n readonly rows: readonly Record<string, unknown>[];\n readonly storedAt: number;\n}\n\n/**\n * Pluggable cache backend used by the cache middleware.\n *\n * The default implementation is an in-memory LRU with TTL produced by\n * `createInMemoryCacheStore`. Users can supply Redis, Memcached, or any\n * other backend by implementing this interface.\n *\n * The interface is intentionally minimal:\n *\n * - `get` returns the entry if it exists and has not expired, or\n * `undefined` otherwise. Implementations that gate on TTL should\n * treat an expired entry as absent (return `undefined`) and may\n * evict it as a side effect.\n * - `set` writes the entry under the key with an associated TTL in\n * milliseconds. Implementations may evict other entries to make\n * room (LRU, LFU, etc.) and may treat the operation as fire-and-\n * forget at scale; the cache middleware does not rely on `set`\n * completing before subsequent `get`s.\n *\n * Both methods are async to leave the door open for I/O-backed stores\n * (Redis, S3, etc.). The default in-memory store completes\n * synchronously and wraps the result in `Promise.resolve` for type\n * conformance.\n */\nexport interface CacheStore {\n get(key: string): Promise<CachedEntry | undefined>;\n set(key: string, entry: CachedEntry, ttlMs: number): Promise<void>;\n}\n\n/**\n * Options accepted by `createInMemoryCacheStore`.\n *\n * - `maxEntries` — hard cap on the number of live entries. Once the cap\n * is exceeded, the least recently used entry is evicted. Reads and\n * writes both count as \"uses\" for ordering purposes.\n * - `clock` — injectable time source for TTL math. Defaults to\n * `Date.now`. Tests inject a controlled clock to verify expiry without\n * real-time waits.\n */\nexport interface InMemoryCacheStoreOptions {\n readonly maxEntries: number;\n readonly clock?: () => number;\n}\n\ninterface StoredRecord {\n readonly entry: CachedEntry;\n readonly expiresAt: number;\n}\n\n/**\n * Default cache backend. An LRU with per-entry TTL, backed by a `Map`.\n *\n * Eviction policy:\n *\n * - On `set` of a fresh key whose insertion would push the live count\n * above `maxEntries`, the least recently used entry is evicted.\n * Setting an existing key updates the entry in place and refreshes its\n * recency without changing the live count.\n * - On `get` of an existing key, recency is bumped (so the entry is no\n * longer the LRU candidate).\n * - On `get` of an expired entry, the entry is removed from the map and\n * `undefined` is returned. The slot becomes available for new writes\n * without counting against `maxEntries`.\n *\n * `Map` insertion order is the LRU order: the first key is the LRU\n * candidate; the last key is the most recently used. Bumping recency is\n * a delete-then-set on the underlying map.\n *\n * The default store is **not** coherent across processes or replicas —\n * each process holds its own Map. Users who need a shared cache supply\n * their own `CacheStore` (Redis, Memcached, etc.).\n */\nexport function createInMemoryCacheStore(options: InMemoryCacheStoreOptions): CacheStore {\n const maxEntries = options.maxEntries;\n const clock = options.clock ?? Date.now;\n const map = new Map<string, StoredRecord>();\n\n function get(key: string): Promise<CachedEntry | undefined> {\n const record = map.get(key);\n if (record === undefined) {\n return Promise.resolve(undefined);\n }\n if (clock() >= record.expiresAt) {\n map.delete(key);\n return Promise.resolve(undefined);\n }\n // Bump recency: re-insert at the end of the iteration order.\n map.delete(key);\n map.set(key, record);\n return Promise.resolve(record.entry);\n }\n\n function set(key: string, entry: CachedEntry, ttlMs: number): Promise<void> {\n const expiresAt = clock() + ttlMs;\n // Re-set semantics: if the key is already present, deleting first\n // ensures the new value lands at the end of the iteration order\n // (most recently used) rather than retaining the old slot's\n // position. This matters for LRU correctness when the same key is\n // re-cached after a refresh.\n if (map.has(key)) {\n map.delete(key);\n }\n map.set(key, { entry, expiresAt });\n\n // Evict LRU entries until the live count is within bounds. The\n // iterator yields keys in insertion order; the first one is the\n // oldest (LRU).\n while (map.size > maxEntries) {\n const oldest = map.keys().next();\n if (oldest.done) {\n break;\n }\n map.delete(oldest.value);\n }\n\n return Promise.resolve();\n }\n\n return { get, set };\n}\n","import type {\n AfterExecuteResult,\n CrossFamilyMiddleware,\n ExecutionPlan,\n RuntimeMiddlewareContext,\n} from '@prisma-next/framework-components/runtime';\nimport { type CachePayload, cacheAnnotation } from './cache-annotation';\nimport { type CacheStore, createInMemoryCacheStore } from './cache-store';\n\n/**\n * Options accepted by `createCacheMiddleware`.\n *\n * - `store` — pluggable cache backend. Defaults to an in-process LRU\n * produced by `createInMemoryCacheStore`. Users supply Redis,\n * Memcached, or any other backend by implementing the `CacheStore`\n * interface.\n * - `maxEntries` — only consulted when `store` is omitted. Sets the\n * `maxEntries` cap on the default in-memory store. Defaults to 1000.\n * - `clock` — injectable time source for `storedAt` stamping on\n * committed entries. Defaults to `Date.now`. Tests inject a controlled\n * clock to make commit-time observable. Note: TTL math lives inside\n * the store, not the middleware — supplying a clock here only affects\n * the `storedAt` field on committed `CachedEntry` values.\n */\nexport interface CacheMiddlewareOptions {\n readonly store?: CacheStore;\n readonly maxEntries?: number;\n readonly clock?: () => number;\n}\n\n/**\n * Per-execution buffer correlated with the post-lowering `exec` object\n * via a private `WeakMap`. Each in-flight cache miss owns one of these.\n *\n * The plan-identity invariant required by this `WeakMap` correlation is\n * documented in the runtime subsystem doc and pinned by a regression\n * test: family runtimes produce a fresh, frozen `exec` per call (SQL\n * `executeAgainstQueryable` constructs `Object.freeze({...lowered, ...})`\n * on each invocation; Mongo lowers fresh per call). If a future plan-\n * memoization change ever recycles `exec` objects across calls, this\n * correlation would silently leak rows between concurrent executions\n * — which is exactly what the regression test catches.\n */\ninterface PendingMiss {\n readonly key: string;\n readonly ttlMs: number;\n readonly buffer: Record<string, unknown>[];\n}\n\n/**\n * Default `maxEntries` for the built-in in-memory store. Bounded so a\n * runaway producer cannot exhaust process memory; users who need\n * different bounds supply a custom `CacheStore`.\n */\nconst DEFAULT_MAX_ENTRIES = 1000;\n\n/**\n * Reads the cache payload from the plan, if present and branded.\n *\n * Returns `undefined` when:\n * - the plan has no `meta.annotations`, or\n * - the `cache` namespace key is absent, or\n * - the value under `cache` is not a branded `AnnotationValue` (the\n * `cacheAnnotation.read` defensive check covers this).\n */\nfunction readCachePayload(plan: ExecutionPlan): CachePayload | undefined {\n return cacheAnnotation.read(plan);\n}\n\n/**\n * Computes the cache key for an execution.\n *\n * Two-tier resolution:\n *\n * 1. Per-query override: `cacheAnnotation({ key })` — the supplied\n * string is used verbatim. Not rehashed; the user is responsible for\n * keeping the string bounded and free of sensitive data.\n * 2. Default: `ctx.contentHash(exec)` — the family runtime owns this and\n * returns an opaque, bounded digest (SHA-512 in the SQL and Mongo\n * runtimes today).\n *\n * The returned string is consumed directly as the `Map<string, …>` key\n * by the underlying `CacheStore`; the cache middleware does not perform\n * any further transformation.\n */\nasync function resolveCacheKey(\n payload: CachePayload,\n exec: ExecutionPlan,\n ctx: RuntimeMiddlewareContext,\n): Promise<string> {\n if (payload.key !== undefined) {\n return payload.key;\n }\n return ctx.contentHash(exec);\n}\n\n/**\n * Creates a family-agnostic caching middleware.\n *\n * The middleware uses three hooks:\n *\n * - `intercept` — on each execution, checks the cache. On a hit, returns\n * the cached raw rows; the runtime skips `beforeExecute`, `runDriver`,\n * and `onRow`, and yields the cached rows to the consumer (which, in\n * the SQL runtime, sees them after the standard `decodeRow` pass —\n * i.e. the cache stores wire-format values). On a miss, records a\n * pending buffer keyed on the `exec` object identity and returns\n * `undefined` (passthrough).\n * - `onRow` — on the miss path, appends each row yielded by the driver\n * to the pending buffer.\n * - `afterExecute` — on the miss path, commits the buffer to the store\n * if and only if `result.completed === true && result.source === 'driver'`.\n * Failed executions and middleware-served executions never populate\n * the cache. The pending buffer is cleared in all branches so a stale\n * `WeakMap` entry cannot leak between executions sharing an `exec`.\n *\n * The middleware bypasses the cache entirely when:\n * - the plan has no `cache` annotation, or\n * - the annotation has `skip: true`, or\n * - the annotation has no `ttl`, or\n * - `ctx.scope !== 'runtime'` (connection / transaction scopes opt out).\n *\n * Returns a cross-family `RuntimeMiddleware` (no `familyId` /\n * `targetId`). The package depends only on\n * `@prisma-next/framework-components/runtime`; cache keys come from\n * `ctx.contentHash(exec)`, populated by the family runtime, so SQL and\n * Mongo runtimes both work out of the box.\n *\n * @example\n * ```typescript\n * import { createCacheMiddleware, cacheAnnotation } from '@prisma-next/middleware-cache';\n *\n * const db = postgres({\n * contractJson,\n * url: process.env['DATABASE_URL']!,\n * middleware: [createCacheMiddleware({ maxEntries: 1000 })],\n * });\n *\n * const user = await db.User.first(\n * { id },\n * (meta) => meta.annotate(cacheAnnotation({ ttl: 60_000 })),\n * );\n * ```\n */\nexport function createCacheMiddleware(options?: CacheMiddlewareOptions): CrossFamilyMiddleware {\n const store =\n options?.store ??\n createInMemoryCacheStore({\n maxEntries: options?.maxEntries ?? DEFAULT_MAX_ENTRIES,\n });\n const clock = options?.clock ?? Date.now;\n\n // Per-execution scratch space, keyed on the post-lowering `exec`\n // object identity. WeakMap keeps cleanup automatic: if an execution is\n // dropped without `afterExecute` firing (e.g. an early throw before\n // `runWithMiddleware` even starts), the entry is GC'd alongside the\n // exec object.\n const pending = new WeakMap<object, PendingMiss>();\n\n async function intercept(\n exec: ExecutionPlan,\n ctx: RuntimeMiddlewareContext,\n ): Promise<{ readonly rows: Iterable<Record<string, unknown>> } | undefined> {\n if (ctx.scope !== 'runtime') {\n return undefined;\n }\n\n const payload = readCachePayload(exec);\n if (payload === undefined) {\n return undefined;\n }\n if (payload.skip === true) {\n return undefined;\n }\n if (payload.ttl === undefined) {\n return undefined;\n }\n\n const key = await resolveCacheKey(payload, exec, ctx);\n const hit = await store.get(key);\n if (hit !== undefined) {\n ctx.log.debug?.({ event: 'middleware.cache.hit', middleware: 'cache', key });\n // Hit path leaves no WeakMap entry — afterExecute's lookup will\n // return undefined and short-circuit.\n return { rows: hit.rows };\n }\n\n // Miss: record the pending buffer so onRow / afterExecute can\n // commit on success. The TTL is captured here so a later mutation\n // of the annotation (defensive) cannot change the commit window.\n pending.set(exec, { key, ttlMs: payload.ttl, buffer: [] });\n ctx.log.debug?.({ event: 'middleware.cache.miss', middleware: 'cache', key });\n return undefined;\n }\n\n async function onRow(\n row: Record<string, unknown>,\n exec: ExecutionPlan,\n _ctx: RuntimeMiddlewareContext,\n ): Promise<void> {\n const slot = pending.get(exec);\n if (slot === undefined) {\n return;\n }\n slot.buffer.push(row);\n }\n\n async function afterExecute(\n exec: ExecutionPlan,\n result: AfterExecuteResult,\n ctx: RuntimeMiddlewareContext,\n ): Promise<void> {\n const slot = pending.get(exec);\n if (slot === undefined) {\n return;\n }\n // Always release the WeakMap entry — the exec is single-use and\n // any state we leave behind is dead weight on the GC.\n pending.delete(exec);\n\n if (!result.completed || result.source !== 'driver') {\n return;\n }\n\n await store.set(slot.key, { rows: slot.buffer, storedAt: clock() }, slot.ttlMs);\n ctx.log.debug?.({ event: 'middleware.cache.store', middleware: 'cache', key: slot.key });\n }\n\n return {\n name: 'cache',\n intercept,\n onRow,\n afterExecute,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyDA,MAAa,kBAAkB,kBAAgC,CAAC;CAC9D,WAAW;CACX,cAAc,CAAC,OAAO;CACvB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;AC+BF,SAAgB,yBAAyB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",
4
+ "license": "Apache-2.0",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "description": "Family-agnostic caching middleware for Prisma Next runtimes",
8
+ "dependencies": {
9
+ "@prisma-next/framework-components": "0.9.0-dev.6"
10
+ },
11
+ "devDependencies": {
12
+ "@prisma-next/contract": "0.9.0-dev.6",
13
+ "@prisma-next/tsconfig": "0.9.0-dev.6",
14
+ "@prisma-next/tsdown": "0.9.0-dev.6",
15
+ "tsdown": "0.22.0",
16
+ "typescript": "5.9.3",
17
+ "vitest": "4.1.5"
18
+ },
19
+ "files": [
20
+ "dist",
21
+ "src"
22
+ ],
23
+ "exports": {
24
+ ".": "./dist/index.mjs",
25
+ "./package.json": "./package.json"
26
+ },
27
+ "types": "./dist/index.d.mts",
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://github.com/prisma/prisma-next.git",
31
+ "directory": "packages/3-extensions/middleware-cache"
32
+ },
33
+ "scripts": {
34
+ "build": "tsdown",
35
+ "test": "vitest run",
36
+ "test:coverage": "vitest run --coverage",
37
+ "typecheck": "tsc --project tsconfig.json --noEmit",
38
+ "lint": "biome check . --error-on-warnings",
39
+ "lint:fix": "biome check --write .",
40
+ "lint:fix:unsafe": "biome check --write --unsafe .",
41
+ "clean": "rm -rf dist dist-tsc dist-tsc-prod coverage .tmp-output"
42
+ }
43
+ }
@@ -0,0 +1,61 @@
1
+ import { defineAnnotation } from '@prisma-next/framework-components/runtime';
2
+
3
+ /**
4
+ * Payload accepted when calling the `cacheAnnotation` handle.
5
+ *
6
+ * - `ttl` — Time-to-live for the cached entry, in milliseconds. When
7
+ * omitted, the cache middleware passes the query through untouched —
8
+ * presence of the annotation alone is not sufficient to enable caching.
9
+ * This makes the cache strictly opt-in per query.
10
+ * - `skip` — When `true`, the cache middleware passes the query through
11
+ * untouched even if a `ttl` is set. Useful for selectively bypassing
12
+ * the cache on a per-call basis without removing the annotation
13
+ * entirely (e.g. a "force refresh" knob in user code).
14
+ * - `key` — Per-query override of the cache key. When supplied, replaces
15
+ * the default `RuntimeMiddlewareContext.contentHash(exec)` digest.
16
+ * The supplied string is stored as-is — the cache middleware does
17
+ * **not** rehash it, so the caller is responsible for ensuring the
18
+ * string is bounded in size and free of sensitive data they do not
19
+ * want flowing into logs / Redis `KEYS` / persistence dumps.
20
+ */
21
+ export interface CachePayload {
22
+ readonly ttl?: number;
23
+ readonly skip?: boolean;
24
+ readonly key?: string;
25
+ }
26
+
27
+ /**
28
+ * Read-only annotation handle for the cache middleware.
29
+ *
30
+ * Declared with `applicableTo: ['read']`. Write terminals supply
31
+ * `K = 'write'` to the type-level `ValidAnnotations<'write', As>` gate
32
+ * (and the runtime `assertAnnotationsApplicable(annotations, 'write', ...)`
33
+ * check); the join `K extends Kinds` fails for this annotation, making
34
+ * "cache a mutation" structurally impossible without an `as any` cast
35
+ * bypass at both type *and* runtime levels.
36
+ *
37
+ * Stored under namespace `'cache'` in `plan.meta.annotations`. The cache
38
+ * middleware reads it via `cacheAnnotation.read(plan)`.
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * import { cacheAnnotation } from '@prisma-next/middleware-cache';
43
+ *
44
+ * // ORM read terminal — accepts the read-only annotation via the meta callback.
45
+ * const user = await db.User.first(
46
+ * { id },
47
+ * (meta) => meta.annotate(cacheAnnotation({ ttl: 60_000 })),
48
+ * );
49
+ *
50
+ * // SQL DSL select builder — chainable.
51
+ * const plan = db.sql
52
+ * .from(tables.user)
53
+ * .annotate(cacheAnnotation({ ttl: 60_000 }))
54
+ * .select({ id: tables.user.columns.id })
55
+ * .build();
56
+ * ```
57
+ */
58
+ export const cacheAnnotation = defineAnnotation<CachePayload>()({
59
+ namespace: 'cache',
60
+ applicableTo: ['read'],
61
+ });
@@ -0,0 +1,235 @@
1
+ import type {
2
+ AfterExecuteResult,
3
+ CrossFamilyMiddleware,
4
+ ExecutionPlan,
5
+ RuntimeMiddlewareContext,
6
+ } from '@prisma-next/framework-components/runtime';
7
+ import { type CachePayload, cacheAnnotation } from './cache-annotation';
8
+ import { type CacheStore, createInMemoryCacheStore } from './cache-store';
9
+
10
+ /**
11
+ * Options accepted by `createCacheMiddleware`.
12
+ *
13
+ * - `store` — pluggable cache backend. Defaults to an in-process LRU
14
+ * produced by `createInMemoryCacheStore`. Users supply Redis,
15
+ * Memcached, or any other backend by implementing the `CacheStore`
16
+ * interface.
17
+ * - `maxEntries` — only consulted when `store` is omitted. Sets the
18
+ * `maxEntries` cap on the default in-memory store. Defaults to 1000.
19
+ * - `clock` — injectable time source for `storedAt` stamping on
20
+ * committed entries. Defaults to `Date.now`. Tests inject a controlled
21
+ * clock to make commit-time observable. Note: TTL math lives inside
22
+ * the store, not the middleware — supplying a clock here only affects
23
+ * the `storedAt` field on committed `CachedEntry` values.
24
+ */
25
+ export interface CacheMiddlewareOptions {
26
+ readonly store?: CacheStore;
27
+ readonly maxEntries?: number;
28
+ readonly clock?: () => number;
29
+ }
30
+
31
+ /**
32
+ * Per-execution buffer correlated with the post-lowering `exec` object
33
+ * via a private `WeakMap`. Each in-flight cache miss owns one of these.
34
+ *
35
+ * The plan-identity invariant required by this `WeakMap` correlation is
36
+ * documented in the runtime subsystem doc and pinned by a regression
37
+ * test: family runtimes produce a fresh, frozen `exec` per call (SQL
38
+ * `executeAgainstQueryable` constructs `Object.freeze({...lowered, ...})`
39
+ * on each invocation; Mongo lowers fresh per call). If a future plan-
40
+ * memoization change ever recycles `exec` objects across calls, this
41
+ * correlation would silently leak rows between concurrent executions
42
+ * — which is exactly what the regression test catches.
43
+ */
44
+ interface PendingMiss {
45
+ readonly key: string;
46
+ readonly ttlMs: number;
47
+ readonly buffer: Record<string, unknown>[];
48
+ }
49
+
50
+ /**
51
+ * Default `maxEntries` for the built-in in-memory store. Bounded so a
52
+ * runaway producer cannot exhaust process memory; users who need
53
+ * different bounds supply a custom `CacheStore`.
54
+ */
55
+ const DEFAULT_MAX_ENTRIES = 1000;
56
+
57
+ /**
58
+ * Reads the cache payload from the plan, if present and branded.
59
+ *
60
+ * Returns `undefined` when:
61
+ * - the plan has no `meta.annotations`, or
62
+ * - the `cache` namespace key is absent, or
63
+ * - the value under `cache` is not a branded `AnnotationValue` (the
64
+ * `cacheAnnotation.read` defensive check covers this).
65
+ */
66
+ function readCachePayload(plan: ExecutionPlan): CachePayload | undefined {
67
+ return cacheAnnotation.read(plan);
68
+ }
69
+
70
+ /**
71
+ * Computes the cache key for an execution.
72
+ *
73
+ * Two-tier resolution:
74
+ *
75
+ * 1. Per-query override: `cacheAnnotation({ key })` — the supplied
76
+ * string is used verbatim. Not rehashed; the user is responsible for
77
+ * keeping the string bounded and free of sensitive data.
78
+ * 2. Default: `ctx.contentHash(exec)` — the family runtime owns this and
79
+ * returns an opaque, bounded digest (SHA-512 in the SQL and Mongo
80
+ * runtimes today).
81
+ *
82
+ * The returned string is consumed directly as the `Map<string, …>` key
83
+ * by the underlying `CacheStore`; the cache middleware does not perform
84
+ * any further transformation.
85
+ */
86
+ async function resolveCacheKey(
87
+ payload: CachePayload,
88
+ exec: ExecutionPlan,
89
+ ctx: RuntimeMiddlewareContext,
90
+ ): Promise<string> {
91
+ if (payload.key !== undefined) {
92
+ return payload.key;
93
+ }
94
+ return ctx.contentHash(exec);
95
+ }
96
+
97
+ /**
98
+ * Creates a family-agnostic caching middleware.
99
+ *
100
+ * The middleware uses three hooks:
101
+ *
102
+ * - `intercept` — on each execution, checks the cache. On a hit, returns
103
+ * the cached raw rows; the runtime skips `beforeExecute`, `runDriver`,
104
+ * and `onRow`, and yields the cached rows to the consumer (which, in
105
+ * the SQL runtime, sees them after the standard `decodeRow` pass —
106
+ * i.e. the cache stores wire-format values). On a miss, records a
107
+ * pending buffer keyed on the `exec` object identity and returns
108
+ * `undefined` (passthrough).
109
+ * - `onRow` — on the miss path, appends each row yielded by the driver
110
+ * to the pending buffer.
111
+ * - `afterExecute` — on the miss path, commits the buffer to the store
112
+ * if and only if `result.completed === true && result.source === 'driver'`.
113
+ * Failed executions and middleware-served executions never populate
114
+ * the cache. The pending buffer is cleared in all branches so a stale
115
+ * `WeakMap` entry cannot leak between executions sharing an `exec`.
116
+ *
117
+ * The middleware bypasses the cache entirely when:
118
+ * - the plan has no `cache` annotation, or
119
+ * - the annotation has `skip: true`, or
120
+ * - the annotation has no `ttl`, or
121
+ * - `ctx.scope !== 'runtime'` (connection / transaction scopes opt out).
122
+ *
123
+ * Returns a cross-family `RuntimeMiddleware` (no `familyId` /
124
+ * `targetId`). The package depends only on
125
+ * `@prisma-next/framework-components/runtime`; cache keys come from
126
+ * `ctx.contentHash(exec)`, populated by the family runtime, so SQL and
127
+ * Mongo runtimes both work out of the box.
128
+ *
129
+ * @example
130
+ * ```typescript
131
+ * import { createCacheMiddleware, cacheAnnotation } from '@prisma-next/middleware-cache';
132
+ *
133
+ * const db = postgres({
134
+ * contractJson,
135
+ * url: process.env['DATABASE_URL']!,
136
+ * middleware: [createCacheMiddleware({ maxEntries: 1000 })],
137
+ * });
138
+ *
139
+ * const user = await db.User.first(
140
+ * { id },
141
+ * (meta) => meta.annotate(cacheAnnotation({ ttl: 60_000 })),
142
+ * );
143
+ * ```
144
+ */
145
+ export function createCacheMiddleware(options?: CacheMiddlewareOptions): CrossFamilyMiddleware {
146
+ const store =
147
+ options?.store ??
148
+ createInMemoryCacheStore({
149
+ maxEntries: options?.maxEntries ?? DEFAULT_MAX_ENTRIES,
150
+ });
151
+ const clock = options?.clock ?? Date.now;
152
+
153
+ // Per-execution scratch space, keyed on the post-lowering `exec`
154
+ // object identity. WeakMap keeps cleanup automatic: if an execution is
155
+ // dropped without `afterExecute` firing (e.g. an early throw before
156
+ // `runWithMiddleware` even starts), the entry is GC'd alongside the
157
+ // exec object.
158
+ const pending = new WeakMap<object, PendingMiss>();
159
+
160
+ async function intercept(
161
+ exec: ExecutionPlan,
162
+ ctx: RuntimeMiddlewareContext,
163
+ ): Promise<{ readonly rows: Iterable<Record<string, unknown>> } | undefined> {
164
+ if (ctx.scope !== 'runtime') {
165
+ return undefined;
166
+ }
167
+
168
+ const payload = readCachePayload(exec);
169
+ if (payload === undefined) {
170
+ return undefined;
171
+ }
172
+ if (payload.skip === true) {
173
+ return undefined;
174
+ }
175
+ if (payload.ttl === undefined) {
176
+ return undefined;
177
+ }
178
+
179
+ const key = await resolveCacheKey(payload, exec, ctx);
180
+ const hit = await store.get(key);
181
+ if (hit !== undefined) {
182
+ ctx.log.debug?.({ event: 'middleware.cache.hit', middleware: 'cache', key });
183
+ // Hit path leaves no WeakMap entry — afterExecute's lookup will
184
+ // return undefined and short-circuit.
185
+ return { rows: hit.rows };
186
+ }
187
+
188
+ // Miss: record the pending buffer so onRow / afterExecute can
189
+ // commit on success. The TTL is captured here so a later mutation
190
+ // of the annotation (defensive) cannot change the commit window.
191
+ pending.set(exec, { key, ttlMs: payload.ttl, buffer: [] });
192
+ ctx.log.debug?.({ event: 'middleware.cache.miss', middleware: 'cache', key });
193
+ return undefined;
194
+ }
195
+
196
+ async function onRow(
197
+ row: Record<string, unknown>,
198
+ exec: ExecutionPlan,
199
+ _ctx: RuntimeMiddlewareContext,
200
+ ): Promise<void> {
201
+ const slot = pending.get(exec);
202
+ if (slot === undefined) {
203
+ return;
204
+ }
205
+ slot.buffer.push(row);
206
+ }
207
+
208
+ async function afterExecute(
209
+ exec: ExecutionPlan,
210
+ result: AfterExecuteResult,
211
+ ctx: RuntimeMiddlewareContext,
212
+ ): Promise<void> {
213
+ const slot = pending.get(exec);
214
+ if (slot === undefined) {
215
+ return;
216
+ }
217
+ // Always release the WeakMap entry — the exec is single-use and
218
+ // any state we leave behind is dead weight on the GC.
219
+ pending.delete(exec);
220
+
221
+ if (!result.completed || result.source !== 'driver') {
222
+ return;
223
+ }
224
+
225
+ await store.set(slot.key, { rows: slot.buffer, storedAt: clock() }, slot.ttlMs);
226
+ ctx.log.debug?.({ event: 'middleware.cache.store', middleware: 'cache', key: slot.key });
227
+ }
228
+
229
+ return {
230
+ name: 'cache',
231
+ intercept,
232
+ onRow,
233
+ afterExecute,
234
+ };
235
+ }