@prisma/orm-sqlite 8.0.0-rc.9-dev.8 → 8.0.0-rc.9-dev.10

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.
@@ -8,12 +8,12 @@ This skill covers the **runtime entry point** — `db.ts` — and how to compose
8
8
  ## When to Use
9
9
 
10
10
  - User is wiring up `db.ts` for the first time (post-init).
11
- - User wants to add middleware (telemetry, lints, budgets, custom).
11
+ - User wants to add middleware (lints, budgets, cache, custom).
12
12
  - User wants per-environment config (dev vs prod, multi-region).
13
13
  - User wants to switch between the Postgres, SQLite, and Mongo façades.
14
14
  - User wants to wrap operations in `db.transaction(...)` (Postgres and SQLite).
15
15
  - User is running a one-off script (`tsx my-script.ts`, Node CLI, CI task) and the process won't exit after queries finish, or they need script teardown (`db.close()`, `await using`).
16
- - User mentions: *db.ts, postgres(), mongo(), middleware, telemetry, lints, budgets, DATABASE_URL, .env, connection pool, poolOptions, dev vs prod, transactions, read replicas, multi-database, script won't exit, hangs, db.close, db.end, close connection, pool.end, await using*.
16
+ - User mentions: *db.ts, postgres(), mongo(), middleware, lints, budgets, cache, query log, slow query, DATABASE_URL, .env, connection pool, poolOptions, dev vs prod, transactions, read replicas, multi-database, script won't exit, hangs, db.close, db.end, close connection, pool.end, await using*.
17
17
 
18
18
  ## When Not to Use
19
19
 
@@ -29,8 +29,8 @@ This skill covers the **runtime entry point** — `db.ts` — and how to compose
29
29
  - **`db.ts` is the runtime entry point.** Imports the runtime factory from the `@internal/<target>` façade (`@internal/postgres/runtime`, `@internal/sqlite/runtime`, or `@internal/mongo/runtime`), the contract artefacts (`contract.json` + the `Contract` type from `contract.d.ts`), and any middleware. Exports a `db` value the rest of your app imports.
30
30
  - **The façade's runtime factory is the only surface user-authored `db.ts` imports from.** Each factory is a *default* export. For Postgres: `import postgres from '@internal/postgres/runtime'`; SQLite: `import sqlite from '@internal/sqlite/runtime'`; Mongo: `import mongo from '@internal/mongo/runtime'`. The factory signature is `<Target><Contract>(options)` — a single type parameter (the `Contract` type from `contract.d.ts`), and one options object.
31
31
  - **Lazy connect.** The factory does not connect to the database synchronously. Static query surfaces (`db.sql`, `db.orm`) are available immediately; the driver / pool is instantiated on the first call that needs a runtime (or when you explicitly call `await db.connect({ url })`). This is why `db.ts` can be imported in modules that load before the env is ready.
32
- - **Middleware composes in order.** The first middleware in the `middleware: [...]` array runs *outermost* — it sees the operation first on the way in and last on the way out. Telemetry first means budget / lint failures show up inside telemetry spans.
33
- - **`prisma.config.ts` vs `.env`.** The config (`defineConfig({ contract, db, extensions, migrations })`) is for static project shape: contract path, installed extensions, migrations directory, default connection string. `.env` is for per-environment values (`DATABASE_URL`, secrets). The config reads `.env` automatically via `dotenv/config`. Hardcoding `DATABASE_URL` in the config file leaks credentials and bypasses per-env overrides.
32
+ - **Middleware composes in order.** The first middleware in the `middleware: [...]` array runs *outermost* — its `beforeQuery` sees the operation first, and `interceptQuery` hooks are consulted in registration order with the first non-`undefined` result winning. Register a cache first so it gets first claim on a hit. Every middleware's `afterQuery` still fires on a cache hit, with `result.source: 'middleware'`.
33
+ - **`prisma.config.ts` vs `.env`.** The config (`definePrismaConfig({ orm: ormConfig({ contract, db, extensions, migrations }) })` — see `references/contract.md`) is for static project shape: contract path, installed extensions, migrations directory, default connection string. `.env` is for per-environment values (`DATABASE_URL`, secrets). Nothing reads `.env` on its own: the scaffolded `prisma.config.ts` starts with `import 'dotenv/config'`, and that import is what loads `.env` into `process.env` before the config (and the CLI running it) reads `process.env['DATABASE_URL']`. Keep the import; a config without it sees no `.env` values. Hardcoding `DATABASE_URL` in the config file leaks credentials and bypasses per-env overrides.
34
34
  - **Build-system / dev-server integration is a separate skill.** `vite dev` auto-emit lives in `references/build.md`. The runtime side (this skill) reads `contract.json` / `contract.d.ts` regardless of how they got onto disk, so the two skills compose cleanly.
35
35
 
36
36
  ## Workflow — Basic `db.ts`
@@ -47,11 +47,11 @@ import contractJson from './contract.json' with { type: 'json' };
47
47
 
48
48
  export const db = postgres<Contract>({
49
49
  contractJson,
50
- url: process.env['DATABASE_URL'],
50
+ url: process.env['DATABASE_URL']!,
51
51
  });
52
52
  ```
53
53
 
54
- (`init` currently scaffolds at `prisma/db.ts` instead — see TML-2532 in `references/quickstart.md`. The canonical path is `src/prisma/db.ts`; the rest of `src/` imports from `./prisma/db` or `../prisma/db` depending on depth.)
54
+ (The rest of `src/` imports from `./prisma/db` or `../prisma/db` depending on depth.)
55
55
 
56
56
  Three things to know:
57
57
 
@@ -59,7 +59,7 @@ Three things to know:
59
59
  - **`with { type: 'json' }` is required.** Node's ESM JSON-import-attribute spec. Without it, the import errors.
60
60
  - **`url` is optional at construct time.** If `DATABASE_URL` is not set when `db.ts` loads, the factory still returns a client; you can call `await db.connect({ url })` later. The factory throws lazily — only when a runtime is actually needed.
61
61
 
62
- The Mongo façade has the same construction shape — `import mongo from '@internal/mongo/runtime'` — and the same `db.connect(...)` / `db.close()` lifecycle methods. **The Mongo façade does not expose `db.transaction(...)`.** See *What Prisma 8 doesn't do yet* for the workaround. **The ORM surface differs in one place: keys.** On Mongo, `db.orm` is keyed by the collection's storage name (from `@@map(...)`, or the lowercased model name if no `@@map` is set), not by the PSL model name — so `model User { … @@map("users") }` is reached at `db.orm.users`, not `db.orm.User`. The SQL builder lane (`db.sql.<table>`) doesn't exist on Mongo at all (`db.sql` is `undefined`). See `references/queries.md` § *MongoDB ORM addressing* for the full rule and a rewrite recipe for SQL-target examples.
62
+ The Mongo façade has the same construction shape — `import mongo from '@internal/mongo/runtime'` — and the same `db.connect(...)` / `db.close()` lifecycle methods. **The Mongo façade does not expose `db.transaction(...)`.** See *What Prisma 8 doesn't do yet* for the workaround. **The ORM surface differs in one place: keys.** On Mongo, `db.orm` is keyed by the collection's storage name (from `@@map(...)`, or the lowercased model name if no `@@map` is set), not by the PSL model name — so `model User { … @@map("users") }` is reached at `db.orm.users`, not `db.orm.public.User`. The SQL builder lane (`db.sql.<ns>.<table>`) doesn't exist on Mongo at all (`db.sql` is `undefined`). See `references/queries.md` § *MongoDB ORM addressing* for the full rule and a rewrite recipe for SQL-target examples.
63
63
 
64
64
  ## Workflow — Running as a script (teardown)
65
65
 
@@ -71,8 +71,8 @@ The concept: short scripts that connect, query, then expect the process to exit
71
71
  // src/scripts/hello.ts
72
72
  import { db } from '../prisma/db';
73
73
 
74
- const created = await db.orm.User.create({ email: 'alice@example.com', name: 'Alice' });
75
- const read = await db.orm.User.first();
74
+ const created = await db.orm.public.User.create({ email: 'alice@example.com', name: 'Alice' });
75
+ const read = await db.orm.public.User.first();
76
76
  console.log({ created, read });
77
77
 
78
78
  await db.close();
@@ -88,7 +88,7 @@ import contractJson from '../prisma/contract.json' with { type: 'json' };
88
88
 
89
89
  await using db = postgres<Contract>({ contractJson, url: process.env.DATABASE_URL! });
90
90
 
91
- const user = await db.orm.User.first();
91
+ const user = await db.orm.public.User.first();
92
92
  console.log(user);
93
93
  // db.close() runs automatically when the script module exits.
94
94
  ```
@@ -101,7 +101,7 @@ This is the most important rule in this section. `await using db = postgres(...)
101
101
  // DO NOT do this — closes the pool after every request.
102
102
  app.get('/users', async (req, res) => {
103
103
  await using db = postgres<Contract>({ contractJson, url: process.env.DATABASE_URL! });
104
- const users = await db.orm.User.all();
104
+ const users = await db.orm.public.User.all();
105
105
  res.json(users);
106
106
  });
107
107
  ```
@@ -116,7 +116,7 @@ export const db = postgres<Contract>({ contractJson, url: process.env.DATABASE_U
116
116
  import { db } from '../prisma/db';
117
117
 
118
118
  app.get('/users', async (req, res) => {
119
- const users = await db.orm.User.all();
119
+ const users = await db.orm.public.User.all();
120
120
  res.json(users);
121
121
  });
122
122
  ```
@@ -127,45 +127,46 @@ Servers (HTTP handlers, workers in a request loop) **do not call `db.close()`**
127
127
 
128
128
  - **`close()` is idempotent.** Calling it twice is a no-op.
129
129
  - **`close()` is terminal.** There is no reconnect on a closed `db` — construct a new client if you need another connection. After close, `db.runtime()`, `db.connect(...)`, `db.transaction(...)`, and `db.prepare(...)` reject with `Error('<target> client is closed')` (e.g. `'Postgres client is closed'`, `'SQLite client is closed'`, `'Mongo client is closed'`).
130
- - **`close()` does not abort in-flight queries.** `await` outstanding work before calling `close()`. Async iterators from `db.runtime().execute(plan)` and `PreparedStatement` handles held after `close()` fail on their next call.
130
+ - **`close()` does not abort in-flight queries.** `await` outstanding work before calling `close()`. Async iterators from `db.runtime().query(plan)` and `PreparedStatement` handles held after `close()` fail on their next call.
131
131
  - **Ownership.** `close()` releases only what the façade constructed (`pg.Pool` from `{ url }`, `MongoClient` from `{ url }` / `{ uri, dbName }`, SQLite handle from `{ path }`). If you supplied your own `pg.Pool` / `pg.Client` (Postgres `pg:` option), `mongodb.MongoClient` (Mongo `mongoClient:` option), or a pre-built `binding`, `db.close()` does **not** touch those — you own their lifecycle.
132
132
 
133
133
  **`db.end()` does not exist.** The universal `node-postgres` name is `pool.end()` on a `pg.Pool`; the Prisma 8 runtime client is not a `pg.Pool`. The right call is `await db.close()`.
134
134
 
135
- ## Workflow — Telemetry middleware
135
+ ## Workflow — Custom middleware (query log, slow-query warning)
136
136
 
137
- The concept: telemetry middleware sees every operation and emits a structured event for each (start, success, error). Pair the events with your observability stack's collector.
137
+ The concept: a middleware is a plain object with `name`, `familyId: 'sql'`, and any of the hooks `beforeQuery`, `interceptQuery`, `afterQuery`. There is no separate telemetry package — observe queries with `afterQuery`, which fires once per execution after the rows are consumed, with `result.latencyMs`, `result.rowCount`, and `result.source` (`'driver'` or `'middleware'` for a cache hit). The `SqlMiddleware` type comes from `@prisma/orm-postgres/family-runtime`. `examples/prisma-8-demo/src/prisma/slow-query-warning.ts` is the canonical example:
138
138
 
139
139
  ```typescript
140
- import postgres from '@internal/postgres/runtime';
141
- import { createTelemetryMiddleware } from '@internal/middleware-telemetry';
142
- import type { Contract } from './contract.d';
143
- import contractJson from './contract.json' with { type: 'json' };
144
-
145
- export const db = postgres<Contract>({
146
- contractJson,
147
- url: process.env['DATABASE_URL'],
148
- middleware: [
149
- createTelemetryMiddleware({
150
- onEvent: (event) => {
151
- // forward to your collector, log, etc.
152
- },
153
- }),
154
- ],
155
- });
140
+ import type { SqlMiddleware } from '@prisma/orm-postgres/family-runtime';
141
+
142
+ export function slowQueryWarning(options?: { readonly thresholdMs?: number }): SqlMiddleware {
143
+ const thresholdMs = options?.thresholdMs ?? 250;
144
+ return {
145
+ name: 'slow-query-warning',
146
+ familyId: 'sql',
147
+ async afterQuery(plan, result, ctx) {
148
+ if (result.latencyMs <= thresholdMs) return;
149
+ ctx.log.warn({
150
+ code: 'APP.SLOW_QUERY',
151
+ message: `Query took ${result.latencyMs}ms (threshold: ${thresholdMs}ms)`,
152
+ details: { sql: plan.sql, rowCount: result.rowCount, source: result.source },
153
+ });
154
+ },
155
+ };
156
+ }
156
157
  ```
157
158
 
158
- `createTelemetryMiddleware` is shipped as a separate user-installable package (`@internal/middleware-telemetry`), not as a `/middleware` subpath of the postgres façade. Install it directly. Run `pnpm ls @internal/middleware-telemetry` to confirm it's on the lockfile.
159
+ A "log every query" middleware is the same shape without the threshold. The runtime also exposes its last telemetry event through `db.runtime().telemetry()`.
159
160
 
160
161
  ## Workflow — Lints and budgets middleware
161
162
 
162
163
  The concept: lints catch authoring mistakes that survive type-check (e.g. `DELETE` without a `WHERE`, `SELECT` without a `LIMIT` on a large table); budgets enforce row-count and latency ceilings at runtime. Both surface findings through the structured-error envelope so an agent can branch on the code.
163
164
 
164
- These ship in the underlying SQL runtime package (`@internal/sql-runtime`) and are *not* yet re-exported from the postgres façade — see *What Prisma 8 doesn't do yet*. The example apps under `examples/prisma-8-demo/src/prisma/db.ts` show the canonical import.
165
+ Both are exported from the façade's `family-runtime` subpath — `@prisma/orm-postgres/family-runtime` (and `@prisma/orm-sqlite/family-runtime`, `@prisma/orm-mongo/family-runtime`). `examples/prisma-8-demo/src/prisma/db.ts` shows the canonical import.
165
166
 
166
167
  ```typescript
167
168
  import postgres from '@internal/postgres/runtime';
168
- import { budgets, lints } from '@internal/sql-runtime';
169
+ import { budgets, lints } from '@prisma/orm-postgres/family-runtime';
169
170
  import type { Contract } from './contract.d';
170
171
  import contractJson from './contract.json' with { type: 'json' };
171
172
 
@@ -193,19 +194,40 @@ export const db = postgres<Contract>({
193
194
  });
194
195
  ```
195
196
 
196
- For the full option surface, read the source: `packages/2-sql/5-runtime/src/middleware/lints.ts` and `.../budgets.ts`. The `severities` keys (`selectStar`, `noLimit`, `deleteWithoutWhere`, `updateWithoutWhere`, `readOnlyMutation` for lints; `rowCount`, `latency` for budgets) are the source of truth; do not extrapolate to a key that ripgrep can't find.
197
+ For the full option surface, read the source: `packages/2-sql/5-runtime/src/middleware/lints.ts` and `.../budgets.ts`. The `severities` keys (`selectStar`, `noLimit`, `deleteWithoutWhere`, `updateWithoutWhere`, `readOnlyMutation` for lints; `rowCount`, `latency` for budgets) are the source of truth; do not extrapolate to a key that ripgrep can't find. `lints()` with no argument uses the default severities.
198
+
199
+ ## Workflow — Cache middleware
200
+
201
+ The concept: `@prisma/orm-extension-middleware-cache` ships an opt-in read cache built on the `interceptQuery` hook. On a hit the driver is never called; on a miss the rows are buffered and committed to the store when the query completes. Caching is strictly opt-in per query: only a plan annotated with `cacheAnnotation({ ttl })` is ever cached. Cache keys default to the runtime's content hash of the plan (`key` overrides), queries inside a transaction or pinned connection bypass the cache, and the default store is an in-memory LRU with TTL (`CacheStore` is the interface for a Redis-style backend).
202
+
203
+ ```typescript
204
+ import { cacheAnnotation, createCacheMiddleware } from '@prisma/orm-extension-middleware-cache';
205
+
206
+ export const db = postgres<Contract>({
207
+ contractJson,
208
+ url: process.env['DATABASE_URL']!,
209
+ middleware: [createCacheMiddleware({ maxEntries: 1_000 }), lints(), budgets({ maxRows: 10_000 })],
210
+ });
211
+
212
+ // Cached for 60s; an identical plan within the TTL is served without a driver call.
213
+ const user = await db.orm.public.User.first({ id: 1 }, (meta) => meta.annotate(cacheAnnotation({ ttl: 60_000 })));
214
+ // Un-annotated queries always hit the database.
215
+ ```
216
+
217
+ **The cache key carries no identity.** The default key is the runtime's content hash of the plan — contract hash, SQL text, and bound parameters — so two callers issuing the same statement share one entry regardless of who they are. Never annotate a read whose rows depend on the caller (per-user, per-tenant, or RLS-filtered data) on the plain `postgres()` façade unless the identity is part of the key: `cacheAnnotation({ ttl, key: `user:${userId}:profile` })`, or a `where` clause that binds the identity as a parameter (the parameter is in the hash). Queries that run on a pinned connection or inside a transaction bypass the cache entirely (`ctx.scope !== 'runtime'`), which is why a Supabase `RoleBoundDb` read — executed on a connection with the role bound via `set_config` — is never served from cache; the plain façade has no such protection.
197
218
 
198
219
  ## Workflow — Compose multiple middleware
199
220
 
200
221
  ```typescript
201
222
  middleware: [
202
- createTelemetryMiddleware({ onEvent }), // outermost — sees all sub-failures as inner errors
223
+ createCacheMiddleware({ maxEntries: 1_000 }), // first — gets first claim on an interceptQuery hit
203
224
  lints({ severities: { noLimit: 'error' } }),
204
- budgets({ maxLatencyMs: 5_000 }), // innermost — runs closest to the driver
225
+ budgets({ maxLatencyMs: 5_000 }),
226
+ slowQueryWarning({ thresholdMs: 250 }), // afterQuery fires for cache hits too (source: 'middleware')
205
227
  ],
206
228
  ```
207
229
 
208
- Order matters: outermost wraps. Telemetry first means budget / lint failures are captured as spans (the agent can correlate the lint code with the operation in the same trace).
230
+ Order matters: `beforeQuery` runs in registration order for every middleware before any `interceptQuery` is consulted, so lints and budgets still check a query the cache then answers; `afterQuery` fires for all of them either way.
209
231
 
210
232
  ## Workflow — Configure the connection
211
233
 
@@ -241,11 +263,11 @@ const isProd = process.env['NODE_ENV'] === 'production';
241
263
 
242
264
  export const db = postgres<Contract>({
243
265
  contractJson,
244
- url: process.env['DATABASE_URL'],
266
+ url: process.env['DATABASE_URL']!,
245
267
  middleware: isProd
246
- ? [createTelemetryMiddleware({ onEvent })]
268
+ ? [slowQueryWarning({ thresholdMs: 250 })]
247
269
  : [
248
- createTelemetryMiddleware({ onEvent }),
270
+ slowQueryWarning({ thresholdMs: 250 }),
249
271
  lints({ severities: { noLimit: 'error', deleteWithoutWhere: 'error' } }),
250
272
  ],
251
273
  });
@@ -259,17 +281,17 @@ The concept applies to **Postgres and SQLite**. `db.transaction(fn)` opens a tra
259
281
 
260
282
  ```typescript
261
283
  await db.transaction(async (tx) => {
262
- const user = await tx.orm.User.create({ email: 'alice@example.com' });
263
- await tx.orm.Post.create({ userId: user.id, title: 'hello' });
284
+ const user = await tx.orm.public.User.create({ email: 'alice@example.com' });
285
+ await tx.orm.public.Post.create({ userId: user.id, title: 'hello' });
264
286
  // If either call throws, both inserts roll back.
265
287
  });
266
288
  ```
267
289
 
268
- The callback returns whatever you return from it — the transaction wrapper passes it through. The `tx` object exposes `execute(plan)` for SQL-builder plans inside the transaction.
290
+ The callback returns whatever you return from it — the transaction wrapper passes it through. The `tx` object exposes `query(plan)` (rows) and `execute(plan)` (affected count) for SQL-builder plans inside the transaction.
269
291
 
270
292
  ## Workflow — Switch between Postgres, SQLite, and Mongo
271
293
 
272
- The concept: the façade selection is baked into `db.ts` (`@internal/postgres` or `@internal/mongo`) and `prisma.config.ts` (which `defineConfig` you import from). To switch a project's target, re-run `prisma orm init` in the same directory and pick the other target — the init flow detects the existing scaffold and prompts to reinit (non-interactive runs grant the consent with `--confirm <directory name>`). PN re-scaffolds `prisma.config.ts` and `db.ts` for the new façade. The contract source needs to be re-authored for the new target's idioms (Mongo expresses nested documents; Postgres expresses relations).
294
+ The concept: the façade selection is baked into `db.ts` (`@internal/postgres` or `@internal/mongo`) and `prisma.config.ts` (which target's `config` subpath `ormConfig` is imported from). To switch a project's target, re-run `prisma orm init` in the same directory and pick the other target — the init flow detects the existing scaffold and prompts to reinit (non-interactive runs grant the consent with `--confirm <directory name>`). PN re-scaffolds `prisma.config.ts` and `db.ts` for the new façade. The contract source needs to be re-authored for the new target's idioms (Mongo expresses nested documents; Postgres expresses relations).
273
295
 
274
296
  After the switch (Mongo):
275
297
 
@@ -293,7 +315,7 @@ import contractJson from './contract.json' with { type: 'json' };
293
315
  export const db = sqlite<Contract>({ contractJson, path: 'app.db' });
294
316
  ```
295
317
 
296
- `path` is optional at construct time (you can call `db.connect({ path })` later); omit it and the façade still returns a client. The SQLite façade exposes the same `db.sql`, `db.orm`, `db.transaction(...)`, `db.close()`, and `[Symbol.asyncDispose]` surfaces as Postgres. The Mongo façade shares `db.orm`, `db.close()`, and `[Symbol.asyncDispose]` but has no `db.sql` and no `db.transaction(...)`.
318
+ `path` is optional at construct time (you can call `db.connect({ path })` later); omit it and the façade still returns a client. The SQLite façade exposes the same `db.sql`, `db.orm`, `db.transaction(...)`, `db.close()`, and `[Symbol.asyncDispose]` surfaces as Postgres, with one addressing difference: SQLite has no schemas, so `db.sql` and `db.orm` are the unbound namespace itself (`db.orm.User`, `db.sql.user`) instead of Postgres's `db.orm.public.User` / `db.sql.public.user`. The Mongo façade shares `db.orm`, `db.close()`, and `[Symbol.asyncDispose]` but has no `db.sql` and no `db.transaction(...)`.
297
319
 
298
320
  The `db.sql` / `db.orm` surfaces stay the same in name; the operators each surface exposes are target-shaped (Mongo has no `JOIN`).
299
321
 
@@ -311,22 +333,22 @@ The runtime side (this skill) is the same regardless: `db.ts` reads `contract.js
311
333
  1. **Hardcoding `DATABASE_URL` in `prisma.config.ts`.** Leaks credentials; bypasses per-environment overrides. Use `.env`.
312
334
  2. **Omitting the `<Contract>` type parameter** in `postgres<Contract>(...)`. Without it, static surfaces collapse to a generic shape and you lose autocomplete for models. There is no second type parameter — the older two-param signature (`postgres<Contract, TypeMaps>`) is gone.
313
335
  3. **Forgetting `with { type: 'json' }` on the contract import.** Required by Node's ESM JSON-import-attribute spec.
314
- 4. **Middleware order matters.** Outermost wraps. Put telemetry first if you want it to capture inner-middleware errors.
315
- 5. **Importing middleware from a non-existent façade subpath.** `@internal/postgres/middleware` does *not* exist. Telemetry comes from `@internal/middleware-telemetry`; lints / budgets come from `@internal/sql-runtime` today (see *What Prisma 8 doesn't do yet*).
336
+ 4. **Middleware order matters.** Registration order is hook order; put the cache first so its `interceptQuery` is consulted first.
337
+ 5. **Importing middleware from a non-existent package or subpath.** There is no `@internal/postgres/middleware` subpath and no `@internal/middleware-telemetry` package. `lints` / `budgets` / `SqlMiddleware` come from `@prisma/orm-postgres/family-runtime`; the cache comes from `@prisma/orm-extension-middleware-cache`; a query log or telemetry hook is a custom `afterQuery` middleware (above).
316
338
  6. **Confabulating lint / budget option names.** Lints take `severities` (with the five keys above), not `requireWhere` / `maxRowsWithoutLimit`. Budgets use `maxLatencyMs` (not `maxDurationMs`) plus `maxRows` / `defaultTableRows` / `tableRows`. When in doubt, read the source.
317
339
  7. **Switching targets without re-emitting.** The contract artefacts are target-shaped; emit after the target change.
318
340
  8. **Script hangs after queries finish on Postgres.** The `pg.Pool` keeps Node's event loop alive. Solution: `await db.close()` before the script returns, or `await using db = postgres<Contract>(...)` at the top of a script module. Do not put `await using db = postgres(...)` inside a request handler — it's block-scoped and would close the pool after every request. The right server pattern is a module-level singleton in `db.ts` that lives for the process lifetime.
319
341
 
320
342
  ## What Prisma 8 doesn't do yet
321
343
 
322
- - **`@internal/postgres/middleware` subpath.** The postgres façade re-exports the runtime factory (`./runtime`), config (`./config`), contract-builder (`./contract-builder`), control (`./control`), family (`./family`), target (`./target`), and serverless (`./serverless`) — but not middleware. Today's workaround: import `lints` and `budgets` from `@internal/sql-runtime`, and `createTelemetryMiddleware` from `@internal/middleware-telemetry`. File additional gaps you hit via `references/feedback.md`.
344
+ - **A `/middleware` subpath or a telemetry package.** Neither exists. The middleware surface is `lints`, `budgets`, and the `SqlMiddleware` type on `@prisma/orm-postgres/family-runtime`, plus the separately installed `@prisma/orm-extension-middleware-cache`. Anything else (query log, tracing spans, metrics) is a custom `afterQuery` middleware you write. File additional gaps you hit via `references/feedback.md`.
323
345
  - **Multi-database routing / read replicas.** Prisma 8 doesn't ship a built-in primary/replica router or shard-aware client. Workaround: configure separate `db.ts` instances per data store and call the right one in your application code. If you need first-class multi-database routing, file a feature request via the `references/feedback.md` skill.
324
346
  - **Connection pooling as a first-class config field.** `poolOptions.connectionTimeoutMillis` and `poolOptions.idleTimeoutMillis` are wired through, but the rest of `pg.Pool`'s tuning surface (max connections, `allowExitOnIdle`, ssl options, …) is not exposed by name. Workaround: construct the `pg.Pool` yourself and pass it via `pg:`. If you need more pool fields surfaced on the façade, file a feature request via the `references/feedback.md` skill.
325
- - **Query logger middleware as a built-in.** Prisma 8 doesn't ship a "log every query" middleware. Workaround: write a small custom middleware that wraps each operation and logs; or use `createTelemetryMiddleware` and log inside the `onEvent` callback. If you need a built-in query log, file a feature request via the `references/feedback.md` skill.
347
+ - **Query logger middleware as a built-in.** Prisma 8 doesn't ship a "log every query" middleware. Workaround: a custom `afterQuery` middleware (see *Workflow — Custom middleware*). If you need a built-in query log, file a feature request via the `references/feedback.md` skill.
326
348
 
327
349
  ## Reference Files
328
350
 
329
- This skill is intentionally body-only; `prisma orm init --help`, the `defineConfig` factory in `packages/3-extensions/postgres/src/config/define-config.ts`, the `postgres()` factory in `packages/3-extensions/postgres/src/runtime/postgres.ts`, and the middleware sources in `packages/2-sql/5-runtime/src/middleware/{lints,budgets}.ts` are the authoritative surfaces for option-level detail. When in doubt, read the source.
351
+ This skill is intentionally body-only; `prisma orm init --help`, the target `defineConfig` (`ormConfig`) factory in `packages/3-extensions/postgres/src/config/define-config.ts`, the `postgres()` factory in `packages/3-extensions/postgres/src/runtime/postgres.ts`, the middleware sources in `packages/2-sql/5-runtime/src/middleware/{lints,budgets}.ts`, and `packages/3-extensions/middleware-cache/README.md` are the authoritative surfaces for option-level detail. When in doubt, read the source.
330
352
 
331
353
  ## Checklist
332
354
 
@@ -334,11 +356,11 @@ This skill is intentionally body-only; `prisma orm init --help`, the `defineConf
334
356
  - [ ] `with { type: 'json' }` on the contract JSON import.
335
357
  - [ ] `<Contract>` is the single type parameter on `postgres<Contract>(...)` (no second parameter).
336
358
  - [ ] `DATABASE_URL` lives in `.env`, not in `prisma.config.ts`.
337
- - [ ] Middleware ordered intentionally (telemetry outermost typically).
338
- - [ ] `lints` / `budgets` use the verified option keys (`severities`, `maxLatencyMs`, `maxRows`, `tableRows`).
359
+ - [ ] Middleware ordered intentionally (cache first when used).
360
+ - [ ] `lints` / `budgets` imported from `@prisma/orm-postgres/family-runtime` and using the verified option keys (`severities`, `maxLatencyMs`, `maxRows`, `tableRows`).
339
361
  - [ ] Per-env divergence (if any) gated by `NODE_ENV` or similar.
340
362
  - [ ] Did NOT hardcode credentials in any committed file.
341
- - [ ] Did NOT confabulate a `@internal/postgres/middleware` subpath, a `@internal/postgres-extension-audit` package, or a second type parameter on `postgres<...>`.
363
+ - [ ] Did NOT confabulate a `@internal/postgres/middleware` subpath, a `@internal/middleware-telemetry` package, a `@internal/postgres-extension-audit` package, or a second type parameter on `postgres<...>`.
342
364
  - [ ] Did NOT claim `db.transaction(...)` exists on the Mongo façade — only Postgres and SQLite expose it.
343
365
  - [ ] Did NOT confabulate read-replica / multi-DB / extra pool config — pointed at *What Prisma 8 doesn't do yet* and routed to `references/feedback.md`.
344
366
  - [ ] For build-system / dev-server prompts (Vite plugin, Next.js plugin, …) routed to `references/build.md`.
@@ -25,7 +25,7 @@ This skill covers using Prisma 8 against a **Supabase** project end-to-end: comp
25
25
 
26
26
  - **The pack is an `external` contract space.** `@internal/extension-supabase/pack` ships a complete, introspection-generated contract of everything Supabase owns — the `auth` and `storage` schemas, their native enum types, and the platform roles (`anon`, `authenticated`, `service_role`) — all with control policy `external`. Composed via `extensions`, it means: the migration planner **emits no DDL** for those objects (Supabase manages them), and `db verify` **confirms they exist** in the live database. Your own tables stay `managed` as usual.
27
27
  - **Roles come from the pack; you never declare them.** RLS `roles = [authenticated]` identifiers resolve against the composed contract. Pointing the runtime at a non-Supabase Postgres fails verify with a `not-found` issue naming the missing role — the common "wrong database" misconfiguration surfaces before queries run.
28
- - **The runtime is role-first.** `supabase()` returns a `SupabaseDb` with **no top-level query surface** — there is no `db.sql` / `db.orm` until you bind a role. `await db.asUser(jwt)` / `db.asAnon()` / `db.asServiceRole()` each return a `RoleBoundDb` exposing `.sql`, `.orm`, `.raw`, `.execute(plan)`, and `.transaction(fn)`. This is deliberate: in a Supabase app there is no meaningful "no role" execution context, and defaulting to the connection's login role is a silent-RLS-bypass footgun.
28
+ - **The runtime is role-first.** `supabase()` returns a `SupabaseDb` with **no top-level query surface** — there is no `db.sql` / `db.orm` until you bind a role. `await db.asUser(jwt)` / `db.asAnon()` / `db.asServiceRole()` each return a `RoleBoundDb` exposing `.sql`, `.orm`, `.raw`, `.query(plan)` (rows), `.execute(plan)` (affected count), and `.transaction(fn)`. This is deliberate: in a Supabase app there is no meaningful "no role" execution context, and defaulting to the connection's login role is a silent-RLS-bypass footgun.
29
29
  - **Role binding is below middleware and cannot leak.** Each role-bound query runs on a connection that had `set_config('role', …)` and `set_config('request.jwt.claims', …)` applied beneath the user-middleware chain, with `RESET ALL` on release. Postgres-side `auth.uid()` / `auth.jwt()` read those session vars — RLS enforcement is Postgres's job; the runtime's job is binding the context.
30
30
  - **RLS is enforced by policies *and* grants.** Policies filter *rows*; `GRANT` controls *table access*. Prisma 8 authors and migrates the policies; it does not author grants (see *What Prisma 8 doesn't do yet*). A role with policies but no `GRANT` gets a permission error, not filtered rows. On Supabase your `public` tables already carry the platform-role grants via default privileges — the grant that is actually missing out of the box is `service_role`'s on `auth.*` / `storage.*` (see *Workflow — Grants*).
31
31
  - **JWT validation is eager and configurable — current Supabase projects need `jwksUrl`.** `asUser(jwt)` verifies the token (via `jose`) *before* any connection is acquired: signature + expiry against `jwksUrl` (asymmetric signing keys — **the default on current Supabase projects**, which sign ES256) **xor** `jwtSecret` (the symmetric HS256 secret — legacy projects only). Both or neither → a structured error with code `SUPABASE.CONFIG_INVALID`. Bad tokens throw a structured error with code `SUPABASE.JWT_INVALID` and a typed `meta.reason` — including a mismatch between the token's algorithm and the configured key source (an ES256 token against a `jwtSecret` client names the problem and tells you to switch to `jwksUrl`). The Postgres role is derived from the token's `role` claim (defaults to `authenticated`). Note: `supabase status` still prints a `JWT_SECRET` even on projects that sign ES256 — its presence does not mean your project uses it.
@@ -33,32 +33,20 @@ This skill covers using Prisma 8 against a **Supabase** project end-to-end: comp
33
33
 
34
34
  ## Workflow — Wire the pack into the config
35
35
 
36
- The concept: the pack registers the Supabase contract space so your contract can reference it and the planner/verifier know what Supabase owns. The extension has no `/control` subpath yet, so it can't go through the target façade's `defineConfig({ extensions: [...] })` — it wires into the low-level config's `extensions` (see *What Prisma 8 doesn't do yet*). The low-level imports below are a **deliberate exception** to the façade-only import rule, forced by that gap; the block mirrors `examples/supabase/prisma.config.ts` verbatim — copy it rather than composing your own:
36
+ The concept: the pack registers the Supabase contract space so your contract can reference it and the planner/verifier know what Supabase owns. Its descriptor is the `pack` export and it goes into the ordinary `extensions` array of the target config, inside the standard envelope (see `references/contract.md` § *Key Concepts*). The block mirrors `examples/supabase/prisma.config.ts`:
37
37
 
38
38
  ```typescript
39
39
  // prisma.config.ts
40
- import postgresAdapter from '@internal/adapter-postgres/control';
41
- import { defineConfig } from '@internal/cli/config-types';
42
- import postgresDriver from '@internal/driver-postgres/control';
40
+ import { definePrismaConfig } from '@prisma/cli-engine';
43
41
  import supabasePack from '@internal/extension-supabase/pack';
44
- import sql from '@internal/family-sql/control';
45
- import { prismaContract } from '@internal/sql-contract-psl/provider';
46
- import postgres from '@internal/target-postgres/control';
47
- import postgresPackRef from '@internal/target-postgres/pack';
48
- import { postgresCreateNamespace } from '@internal/target-postgres/types';
49
-
50
- export default defineConfig({
51
- family: sql,
52
- target: postgres,
53
- adapter: postgresAdapter,
54
- driver: postgresDriver,
55
- extensions: [supabasePack],
56
- contract: prismaContract('./src/contract.prisma', {
57
- output: 'src/contract.json',
58
- target: postgresPackRef,
59
- createNamespace: postgresCreateNamespace,
42
+ import { defineConfig as ormConfig } from '@internal/postgres/config';
43
+
44
+ export default definePrismaConfig({
45
+ orm: ormConfig({
46
+ contract: './src/contract.prisma',
47
+ extensions: [supabasePack],
48
+ migrations: { dir: 'migrations' },
60
49
  }),
61
- migrations: { dir: 'migrations' },
62
50
  });
63
51
  ```
64
52
 
@@ -164,10 +152,10 @@ The concept: Supabase-internal tables are not part of your contract, so they are
164
152
  ```typescript
165
153
  const admin = db.asServiceRole();
166
154
 
167
- // SQL builder over the pack contract:
168
- const users = await admin.supabase
169
- .execute(admin.supabase.sql.auth.users.select('id', 'email').build())
170
- .toArray();
155
+ // SQL builder over the pack contract — rows come from `query`, not `execute`:
156
+ const users = await admin.supabase.query(
157
+ admin.supabase.sql.auth.users.select('id', 'email').build(),
158
+ );
171
159
 
172
160
  // ORM over the pack contract:
173
161
  const sessions = await admin.supabase.orm.auth.AuthSession.select('id', 'aal').all();
@@ -219,7 +207,6 @@ The concept: the runtime needs a **direct, session-capable** Postgres connection
219
207
 
220
208
  ## What Prisma 8 doesn't do yet
221
209
 
222
- - **No `/control` subpath on the extension** — it can't register through the target façade's `defineConfig({ extensions: [...] })`; wiring goes through the low-level config's `extensions` as shown above. File interest via `references/feedback.md`.
223
210
  - **`GRANT` authoring.** Table privileges are not contract elements; the one grant a Supabase app needs (the `service_role` `auth.*` pair for admin reads) is run once by hand (SQL editor / `psql`). If you want grants managed by the contract, file via `references/feedback.md`.
224
211
  - **Transactions spanning the app root and the `.supabase` admin root.** The two roots are separate contract-bound runtimes sharing one pool; a cross-root transaction is not supported.
225
212
  - **Triggers / functions as contract elements.** The classic "create a profile row on signup" `auth.users` trigger is authored as raw SQL against your database, not in the contract. `auth.uid()` etc. appear only inside opaque policy predicate strings.
@@ -233,7 +220,7 @@ The concept: the runtime needs a **direct, session-capable** Postgres connection
233
220
 
234
221
  ## Checklist
235
222
 
236
- - [ ] `extensions: [supabasePack]` in the low-level `defineConfig` (no `/control` subpath exists).
223
+ - [ ] `extensions: [supabasePack]` (the `pack` export) in `ormConfig({...})` inside `definePrismaConfig({ orm: ... })`.
237
224
  - [ ] Cross-space FK typed `supabase:auth.AuthUser` with explicit `fields` / `references` (+ `onDelete` if wanted).
238
225
  - [ ] Every policy target model carries `@@rls`; predicates quote camelCase columns and cast for `auth.uid()`.
239
226
  - [ ] `db.ts` uses `await supabase<Contract>({ contractJson, url, jwksUrl | jwtSecret })` — exactly one JWT key source; `jwksUrl` for current projects, `jwtSecret` only for legacy HS256.
@@ -1,8 +1,37 @@
1
1
  ---
2
2
  from: "8.0.0-rc.9"
3
3
  to: "8.0.0-rc.10"
4
- # Prisma 8 naming sweep: prose only, no entry required
5
4
  changes:
5
+ - id: schema-header-use-prisma-8
6
+ summary: |
7
+ The schema header that marks a Prisma 8 schema is now `// use prisma-8`. The language server
8
+ still serves the old header and its Format action rewrites it; new schemas and the
9
+ inferred-schema printer write the new form. Replace `// use prisma-next`
10
+ at the top of every `.prisma` file.
11
+ detection:
12
+ glob: "**/*.prisma"
13
+ contains:
14
+ - "// use prisma-next"
15
+ - id: env-vars-drop-next-infix
16
+ summary: |
17
+ The CLI environment variables lost their `NEXT_` infix: `PRISMA_NEXT_DISABLE_TELEMETRY`,
18
+ `PRISMA_NEXT_TELEMETRY_ENDPOINT`, `PRISMA_NEXT_DEBUG`, and the rest are now
19
+ `PRISMA_DISABLE_TELEMETRY`, `PRISMA_TELEMETRY_ENDPOINT`, `PRISMA_DEBUG`, and so on. The old
20
+ `PRISMA_NEXT_DISABLE_TELEMETRY` opt-out is still honoured; the others are not. Rename them
21
+ in shell profiles, `.env` files, and CI configuration. The per-user telemetry config also moved
22
+ from `~/.config/prisma-next/` to `~/.config/prisma-8/`, so the consent prompt runs once more.
23
+ detection:
24
+ glob: "**/*"
25
+ contains:
26
+ - "PRISMA_NEXT_"
27
+ - id: primer-file-prisma-8-md
28
+ summary: |
29
+ The quick-reference primer `init` writes at the project root is now `prisma-8.md`. Rename the
30
+ existing `prisma-next.md` and update any README or agent instruction that points at it.
31
+ detection:
32
+ glob: "**/prisma-next.md"
33
+ contains:
34
+ - "#"
6
35
  - id: to-one-relations-record-nullable
7
36
  summary: |
8
37
  Every `1:1` and `N:1` relation in `contract.json` now carries a `nullable` boolean. A
@@ -28,3 +57,15 @@ For every `contract.json` matched by `detection`, run the project's emit command
28
57
  ## `contract-dts-exports-models`
29
58
 
30
59
  After the emit, `import type { Models, models } from './prisma/contract'` (the project's contract path) gives `Models.<namespace>_<Model>` for every model, and `Scalars<M>` / `Shape<M, { '+': 'relation' }>` from `@prisma/orm-postgres/family-contract/types` (or the Mongo family package) derive the default row and a data structure with relations from it. Replace hand-written row types that duplicate a model's fields with these when convenient.
60
+
61
+ ## `schema-header-use-prisma-8`
62
+
63
+ For every `.prisma` file matched by `detection`, replace the first-line header `// use prisma-next` with `// use prisma-8`. Nothing else in the file changes.
64
+
65
+ ## `env-vars-drop-next-infix`
66
+
67
+ For every file matched by `detection`, replace the `PRISMA_NEXT_` prefix with `PRISMA_` on each environment variable name. Delete `~/.config/prisma-next/` (or `%APPDATA%\prisma-next\` on Windows); the CLI recreates its config under `prisma-8` and asks for telemetry consent once.
68
+
69
+ ## `primer-file-prisma-8-md`
70
+
71
+ Rename `prisma-next.md` at the project root to `prisma-8.md`, and update any link to it in the project README or agent instructions.
@@ -1,10 +1,30 @@
1
1
  ---
2
2
  from: "8.0.0-rc.9"
3
3
  to: "8.0.0-rc.10"
4
- # Prisma 8 naming sweep: prose only, no entry required
5
4
  # sql-orm-client doc-comment sweep: reviewed, no entry required
6
5
  # postgres shell dependency ownership: reviewed, no extension-author action required; bundled packages now declare the catalog Node/pg type dependencies that public shell manifests mirror
7
6
  changes:
7
+ - id: schema-header-use-prisma-8
8
+ summary: |
9
+ The schema header that marks a Prisma 8 schema is now `// use prisma-8`. The language server
10
+ still serves the old header and its Format action rewrites it; new schemas and the
11
+ inferred-schema printer write the new form. Replace `// use prisma-next`
12
+ at the top of every `.prisma` file the extension ships or tests against.
13
+ detection:
14
+ glob: "**/*.prisma"
15
+ contains:
16
+ - "// use prisma-next"
17
+ - id: env-vars-drop-next-infix
18
+ summary: |
19
+ The CLI environment variables lost their `NEXT_` infix: `PRISMA_NEXT_DISABLE_TELEMETRY`,
20
+ `PRISMA_NEXT_TELEMETRY_ENDPOINT`, `PRISMA_NEXT_DEBUG`, and the rest are now
21
+ `PRISMA_DISABLE_TELEMETRY`, `PRISMA_TELEMETRY_ENDPOINT`, `PRISMA_DEBUG`, and so on. The old
22
+ `PRISMA_NEXT_DISABLE_TELEMETRY` opt-out is still honoured; the others are not. Rename them
23
+ in the extension's test setup and CI configuration.
24
+ detection:
25
+ glob: "**/*"
26
+ contains:
27
+ - "PRISMA_NEXT_"
8
28
  - id: to-one-relations-record-nullable
9
29
  summary: |
10
30
  `ContractNonJunctionRelation`'s `'1:1'` and `'N:1'` members now require `nullable: boolean`,
@@ -33,3 +53,11 @@ For every TypeScript file matched by `detection`, find each object literal that
33
53
  ## `contract-space-re-emit-nullable`
34
54
 
35
55
  For every `contract.json` matched by `detection`, run the extension package's `build:contract-space` script (or its emit command) once after upgrading. The expected diff is one `"nullable"` boolean per to-one relation in `contract.json`, plus the `Models` namespace, `models` constant, and `RelationKeys` import in `contract.d.ts`.
56
+
57
+ ## `schema-header-use-prisma-8`
58
+
59
+ For every `.prisma` file matched by `detection`, replace the first-line header `// use prisma-next` with `// use prisma-8`. Nothing else in the file changes.
60
+
61
+ ## `env-vars-drop-next-infix`
62
+
63
+ For every file matched by `detection`, replace the `PRISMA_NEXT_` prefix with `PRISMA_` on each environment variable name.