Commit 0fa64ac
authored
TML-2955: A client-safe static surface — @prisma-next/<target>/static (#888)
## At a glance
You can now use a contract's enums, query builder, and generated types
in a `'use client'` component — with **no database driver in the browser
bundle**:
```tsx
'use client';
import mongoStatic from '@prisma-next/mongo/static';
import contractJson from './contract.json' with { type: 'json' };
import type { Contract } from './contract';
const { enums } = mongoStatic<Contract>({ contractJson });
export function OrderTypePicker() {
// fully typed from the contract — enums.OrderType.members.Delivery, .Pickup, …
return <RadioGroup options={Object.values(enums.OrderType.members)} />;
}
```
The same call shape exists on every target (`postgresStatic`,
`sqliteStatic`), and the same object is exposed on a connected client as
`db.context` / `db.contract`.
## The decision
**Expose the `ExecutionContext` as a first-class, client-safe surface —
a new `@prisma-next/<target>/static` entrypoint — symmetrically on all
three target facades.**
The `ExecutionContext` is the driver-free core every query is built
against: the contract, its codecs, the query operations, and the derived
types. Enums, the query builder, and `raw` are all just views over it.
It needs no connection and no driver to construct — so in principle it's
usable anywhere, including the browser. This PR makes that real and
uniform.
## Why it wasn't already usable
The context existed but you couldn't get at it cleanly from client code:
- **Postgres** built it upfront and exposed it as `db.context` — but
only on a fully-wired, driver-carrying client.
- **Mongo** built the equivalent lazily *inside the connect path*, never
exposed it, and typed its contract as `unknown`.
- There was **no driver-free entrypoint** on any target, so importing
the facade to reach enums also imported the DB driver.
So when an app needed typed enums in a `'use client'` form, it fell back
to an interim: hand-calling the low-level `buildNamespacedEnums(...)`
and casting the untyped result in application code. That worked but was
a narrow, per-consumer workaround, not the abstraction.
## What this PR does
- Adds **`@prisma-next/<target>/static`** with `<target>Static({
contractJson })`, returning the `ExecutionContext` plus the surface
derived from it (`enums`, query builder, `raw`, `contract`). It's
driver-free by construction, and a test walks the entrypoint's import
graph to keep it that way.
- Builds the Mongo context **upfront**, types it
`MongoExecutionContext<TContract>`, and exposes `db.context` /
`db.contract` — matching Postgres, so the shape is the same across
targets.
- Routes each facade *and* its `/static` twin through **one shared
builder**, so the enum typing lives in framework code once instead of a
cast in every app.
- Migrates `examples/retail-store` off the interim: `src/enums.ts` now
derives from `mongoStatic(...)`, consumed by the `'use client'` checkout
form — which doubles as the `next build` "no driver in the client
bundle" acceptance test.
## One cross-cutting change worth a look
Making the static context truly driver-free reached one layer deeper
than the facades. Building the context aggregates the target's codecs,
and the Mongo codec imported `ObjectId` from `mongodb` (a value import),
which drags the whole driver into a client bundle. `ObjectId` is really
a `bson` class that `mongodb` re-exports, so the codec now imports it
from the standalone `bson` package. It's a one-line change but it lives
in `packages/3-mongo-target/2-mongo-adapter`, and it's what lets `next
build` succeed with zero driver code client-side.
Extension packs (postgis, pgvector, …) are unaffected: the *facade*
still builds its context from the full stack including
`options.extensions`; only the client-safe `/static` path is
target+adapter-only.
## Testing
Full local gate green (build, typecheck, lint incl.
`lint:deps`/`lint:casts`, `check:upgrade-coverage`, `fixtures:check`,
`test:packages`/`integration`/`e2e`) and CI green. `retail-store` `next
build` produces no `mongodb` in the client bundle. Upgrade instructions
recorded as additive/incidental for 0.14→0.15.
## Alternatives considered
- **Keep the app-code interim** (`buildNamespacedEnums` + a cast).
Rejected: untyped without a per-call cast, duplicated per consumer, and
it leaks a framework detail into every app.
- **An enums-only helper** — the `mongoEnums` /
`@prisma-next/mongo/enums` point-solution prototyped in #880 and pulled
back out. Rejected: it solved one symptom (enums) rather than exposing
the abstraction they derive from. Exposing the whole `ExecutionContext`
gives enums, query building, `raw`, and types from one surface,
symmetrically.
- **Make the existing `/runtime` entrypoint tree-shakeable** instead of
adding `/static`. Rejected: the driver is imported at module scope, so
bundlers can't reliably drop it. A separate driver-free module is a hard
boundary — and the client-safety test enforces it.
Spec:
[`projects/expose-static-execution-context/spec.md`](projects/expose-static-execution-context/spec.md).
Closes TML-2955.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* Added contract “static” entrypoints for Mongo, Postgres, and SQLite
(e.g., `/<target>Static({ contractJson })`) that expose `context`,
`contract`, enums, query/`raw`, and related helpers.
* Added `./static` subpath exports for Mongo/Postgres/SQLite.
* Mongo facade now exposes `db.context` and `db.contract`.
* **Bug Fixes**
* Improved type preservation for `contract` (prevents widening to
`unknown`) and aligned runtime/static surfaces.
* Strengthened validation and errors for required extension packs.
* **Documentation**
* Updated 0.14 → 0.15 upgrade notes for the new static APIs.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Signed-off-by: willbot <w.a.madden+machine@gmail.com>
Signed-off-by: Will Madden <madden@prisma.io>1 parent 4d8ab61 commit 0fa64ac
37 files changed
Lines changed: 1014 additions & 133 deletions
File tree
- examples/retail-store/src
- packages
- 1-framework/0-foundation/contract
- src
- test
- 2-mongo-family/7-runtime
- src
- test
- 3-extensions
- mongo
- src
- exports
- runtime
- static
- test
- postgres
- src
- exports
- runtime
- static
- test
- sqlite
- src
- exports
- runtime
- static
- test
- 3-mongo-target/2-mongo-adapter
- src/core
- test
- projects/expose-static-execution-context
- skills
- extension-author/prisma-next-extension-upgrade/upgrades/0.14-to-0.15
- upgrade/prisma-next-upgrade/upgrades/0.14-to-0.15
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
3 | 2 | | |
4 | 3 | | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
| 4 | + | |
| 5 | + | |
Lines changed: 8 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
63 | 64 | | |
64 | 65 | | |
65 | 66 | | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
71 | 70 | | |
72 | 71 | | |
73 | 72 | | |
74 | 73 | | |
75 | | - | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
76 | 78 | | |
77 | 79 | | |
78 | 80 | | |
| |||
Lines changed: 15 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| 10 | + | |
| 11 | + | |
9 | 12 | | |
10 | 13 | | |
11 | 14 | | |
| |||
187 | 190 | | |
188 | 191 | | |
189 | 192 | | |
190 | | - | |
| 193 | + | |
191 | 194 | | |
192 | 195 | | |
193 | 196 | | |
194 | | - | |
| 197 | + | |
195 | 198 | | |
196 | 199 | | |
197 | 200 | | |
| |||
200 | 203 | | |
201 | 204 | | |
202 | 205 | | |
203 | | - | |
204 | | - | |
| 206 | + | |
| 207 | + | |
205 | 208 | | |
206 | 209 | | |
207 | 210 | | |
208 | | - | |
| 211 | + | |
209 | 212 | | |
210 | 213 | | |
211 | 214 | | |
212 | 215 | | |
213 | 216 | | |
214 | 217 | | |
215 | | - | |
| 218 | + | |
216 | 219 | | |
217 | 220 | | |
218 | | - | |
| 221 | + | |
| 222 | + | |
219 | 223 | | |
220 | 224 | | |
221 | 225 | | |
| |||
224 | 228 | | |
225 | 229 | | |
226 | 230 | | |
227 | | - | |
| 231 | + | |
228 | 232 | | |
229 | 233 | | |
230 | | - | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
231 | 237 | | |
232 | 238 | | |
233 | 239 | | |
| |||
Lines changed: 8 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
114 | | - | |
115 | | - | |
| 114 | + | |
| 115 | + | |
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
120 | | - | |
121 | | - | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
122 | 125 | | |
123 | | - | |
| 126 | + | |
124 | 127 | | |
125 | 128 | | |
126 | 129 | | |
| |||
Lines changed: 13 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
146 | 147 | | |
147 | 148 | | |
148 | 149 | | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
149 | 161 | | |
150 | 162 | | |
151 | 163 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
| 68 | + | |
68 | 69 | | |
69 | 70 | | |
70 | 71 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | 1 | | |
4 | 2 | | |
5 | | - | |
6 | 3 | | |
7 | 4 | | |
8 | 5 | | |
9 | 6 | | |
10 | 7 | | |
11 | 8 | | |
12 | 9 | | |
13 | | - | |
14 | | - | |
| 10 | + | |
15 | 11 | | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
| 12 | + | |
23 | 13 | | |
| 14 | + | |
24 | 15 | | |
25 | 16 | | |
26 | 17 | | |
27 | 18 | | |
28 | 19 | | |
29 | 20 | | |
30 | 21 | | |
31 | | - | |
| 22 | + | |
32 | 23 | | |
33 | 24 | | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
| 25 | + | |
41 | 26 | | |
42 | 27 | | |
43 | 28 | | |
44 | 29 | | |
45 | 30 | | |
46 | | - | |
| 31 | + | |
47 | 32 | | |
48 | 33 | | |
49 | 34 | | |
| 35 | + | |
50 | 36 | | |
51 | 37 | | |
52 | 38 | | |
| |||
133 | 119 | | |
134 | 120 | | |
135 | 121 | | |
136 | | - | |
137 | | - | |
138 | | - | |
| 122 | + | |
139 | 123 | | |
140 | 124 | | |
141 | 125 | | |
| |||
145 | 129 | | |
146 | 130 | | |
147 | 131 | | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | 132 | | |
154 | 133 | | |
155 | 134 | | |
| |||
201 | 180 | | |
202 | 181 | | |
203 | 182 | | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | 183 | | |
211 | 184 | | |
212 | 185 | | |
213 | 186 | | |
214 | 187 | | |
215 | 188 | | |
| 189 | + | |
216 | 190 | | |
217 | 191 | | |
218 | 192 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
0 commit comments