@prisma-next/sql-runtime 0.5.0-dev.1 → 0.5.0-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.
Files changed (41) hide show
  1. package/README.md +29 -21
  2. package/dist/{exports-TJ70Qw3r.mjs → exports-BOHa3Emo.mjs} +502 -121
  3. package/dist/exports-BOHa3Emo.mjs.map +1 -0
  4. package/dist/{index-DyDQ4fyK.d.mts → index-CZmC2kD3.d.mts} +87 -23
  5. package/dist/index-CZmC2kD3.d.mts.map +1 -0
  6. package/dist/index.d.mts +2 -2
  7. package/dist/index.mjs +1 -1
  8. package/dist/test/utils.d.mts +6 -5
  9. package/dist/test/utils.d.mts.map +1 -1
  10. package/dist/test/utils.mjs +7 -2
  11. package/dist/test/utils.mjs.map +1 -1
  12. package/package.json +12 -14
  13. package/src/codecs/decoding.ts +172 -116
  14. package/src/codecs/encoding.ts +59 -21
  15. package/src/exports/index.ts +10 -7
  16. package/src/fingerprint.ts +22 -0
  17. package/src/guardrails/raw.ts +214 -0
  18. package/src/lower-sql-plan.ts +3 -3
  19. package/src/marker.ts +82 -0
  20. package/src/middleware/before-compile-chain.ts +59 -0
  21. package/src/middleware/budgets.ts +25 -33
  22. package/src/middleware/lints.ts +5 -5
  23. package/src/middleware/sql-middleware.ts +36 -6
  24. package/src/runtime-spi.ts +43 -0
  25. package/src/sql-family-adapter.ts +3 -2
  26. package/src/sql-marker.ts +1 -1
  27. package/src/sql-runtime.ts +279 -101
  28. package/dist/exports-TJ70Qw3r.mjs.map +0 -1
  29. package/dist/index-DyDQ4fyK.d.mts.map +0 -1
  30. package/test/async-iterable-result.test.ts +0 -141
  31. package/test/budgets.test.ts +0 -431
  32. package/test/context.types.test-d.ts +0 -68
  33. package/test/execution-stack.test.ts +0 -161
  34. package/test/json-schema-validation.test.ts +0 -571
  35. package/test/lints.test.ts +0 -159
  36. package/test/mutation-default-generators.test.ts +0 -254
  37. package/test/parameterized-types.test.ts +0 -529
  38. package/test/sql-context.test.ts +0 -384
  39. package/test/sql-family-adapter.test.ts +0 -103
  40. package/test/sql-runtime.test.ts +0 -634
  41. package/test/utils.ts +0 -297
package/README.md CHANGED
@@ -10,7 +10,7 @@ SQL runtime implementation for Prisma Next.
10
10
 
11
11
  ## Overview
12
12
 
13
- The SQL runtime package implements the SQL family runtime by composing `@prisma-next/runtime-executor` with SQL-specific adapters, drivers, and codecs. It provides the public runtime API for SQL-based databases, including descriptor-based static context derivation via `SqlStaticContributions` and execution-plane composition via `ExecutionStack`.
13
+ The SQL runtime package implements the SQL family runtime by extending the abstract `RuntimeCore` base class from `@prisma-next/framework-components/runtime` with SQL-specific adapters, drivers, codecs, marker verification, telemetry fingerprinting, and a `beforeCompile` middleware chain. It provides the public runtime API for SQL-based databases, including descriptor-based static context derivation via `SqlStaticContributions` and execution-plane composition via `ExecutionStack`.
14
14
 
15
15
  ## Purpose
16
16
 
@@ -26,13 +26,16 @@ Execute SQL query Plans with deterministic verification, guardrails, and feedbac
26
26
  - **SQL Marker Management**: Provide SQL statements for reading/writing contract markers
27
27
  - **Codec Encoding/Decoding**: Encode parameters and decode rows using SQL codec registries
28
28
  - **Codec Validation**: Validate that codec registries contain all required codecs
29
- - **SQL Family Adapter**: Implement `RuntimeFamilyAdapter` for SQL contracts
30
- - **SQL Runtime**: Compose runtime-executor with SQL-specific logic
29
+ - **SQL Family Adapter**: Implement `RuntimeFamilyAdapter` for SQL contracts (defined in `runtime-spi.ts`)
30
+ - **Marker Verification**: Parse contract-marker rows from the database (`marker.ts`) and gate execution on hash equality
31
+ - **Telemetry Fingerprinting**: Compute SQL fingerprints for telemetry events (`fingerprint.ts`)
32
+ - **Raw-SQL Guardrails**: Heuristic safety checks for raw SQL plans (`guardrails/raw.ts`)
33
+ - **`beforeCompile` Chain**: AST-rewrite middleware chain run pre-lowering (`middleware/before-compile-chain.ts`)
34
+ - **SQL Runtime**: `SqlRuntime` extends `RuntimeCore<SqlQueryPlan, SqlExecutionPlan, SqlMiddleware>` and overrides `lower`, `runDriver`, `runBeforeCompile`, and `close` with SQL-specific behaviour
31
35
 
32
36
  ## Dependencies
33
37
 
34
- - `@prisma-next/framework-components` - Runtime component descriptor types (via `./execution`)
35
- - `@prisma-next/runtime-executor` - Target-neutral execution engine
38
+ - `@prisma-next/framework-components` - Runtime component descriptor types (`./execution`) and the abstract `RuntimeCore` base class plus `runWithMiddleware` helper (`./runtime`)
36
39
  - `@prisma-next/sql-contract` - SQL contract types (via `@prisma-next/sql-contract/types`)
37
40
  - `@prisma-next/operations` - Operation registry
38
41
 
@@ -44,7 +47,12 @@ import postgresDriver from '@prisma-next/driver-postgres/runtime';
44
47
  import pgvector from '@prisma-next/extension-pgvector/runtime';
45
48
  import postgresTarget from '@prisma-next/target-postgres/runtime';
46
49
  import { instantiateExecutionStack } from '@prisma-next/framework-components/execution';
47
- import { createExecutionContext, createRuntime, createSqlExecutionStack } from '@prisma-next/sql-runtime';
50
+ import {
51
+ budgets,
52
+ createExecutionContext,
53
+ createRuntime,
54
+ createSqlExecutionStack,
55
+ } from '@prisma-next/sql-runtime';
48
56
 
49
57
  const contract = validateContract<Contract>(contractJson);
50
58
  const stack = createSqlExecutionStack({
@@ -65,7 +73,7 @@ const runtime = createRuntime({
65
73
  context,
66
74
  driver,
67
75
  verify: { mode: 'onFirstUse', requireMarker: false },
68
- plugins: [budgets()],
76
+ middleware: [budgets()],
69
77
  });
70
78
 
71
79
  for await (const row of runtime.execute(plan)) {
@@ -116,44 +124,44 @@ for await (const row of runtime.execute(plan)) {
116
124
 
117
125
  - `lowerSqlPlan` - SQL plan lowering via adapter
118
126
 
119
- ### Plugins
127
+ ### Middleware
120
128
 
121
- - `budgets` - **AST-first budget plugin** (canonical in SQL domain), inspects `plan.ast` when present for row estimation
122
- - `lints` - **AST-first lint plugin** (canonical in SQL domain), inspects `plan.ast` when present
123
- - `BudgetsOptions`, `LintsOptions` - Plugin option types
124
- - `Plugin`, `PluginContext` - Plugin interface types (from runtime-executor)
125
- - `AfterExecuteResult` - Plugin hook result type
126
- - `Log` - Log entry type
129
+ - `budgets` - **AST-first budget middleware** (canonical in SQL domain), inspects `plan.ast` when present for row estimation
130
+ - `lints` - **AST-first lint middleware** (canonical in SQL domain), inspects `plan.ast` when present
131
+ - `SqlMiddleware`, `SqlMiddlewareContext` - SQL-family middleware interface and per-execution context
132
+ - `BudgetsOptions`, `LintsOptions` - Middleware option types
133
+ - `AfterExecuteResult` - Middleware `afterExecute` hook result type (re-exported from `@prisma-next/framework-components/runtime`)
134
+ - `Log` - Log entry type (re-exported from `@prisma-next/framework-components/runtime`)
127
135
 
128
- #### Lints plugin (SQL domain)
136
+ #### Lints middleware (SQL domain)
129
137
 
130
- The `lints` plugin operates on `plan.ast` when it is a SQL `QueryAst`:
138
+ The `lints` middleware operates on `plan.ast` when it is a SQL `QueryAst`:
131
139
 
132
140
  - **DELETE without WHERE** — blocks execution (configurable severity)
133
141
  - **UPDATE without WHERE** — blocks execution (configurable severity)
134
142
  - **Unbounded SELECT** — warns/errors when `limit` is missing
135
143
  - **SELECT \* intent** — warns/errors when `selectAllIntent` is present
136
144
 
137
- When `plan.ast` is missing, the plugin falls back to raw heuristic guardrails (`fallbackWhenAstMissing: 'raw'`) or skips linting (`fallbackWhenAstMissing: 'skip'`). Default is `'raw'`.
145
+ When `plan.ast` is missing, the middleware falls back to raw heuristic guardrails (`fallbackWhenAstMissing: 'raw'`) or skips linting (`fallbackWhenAstMissing: 'skip'`). Default is `'raw'`.
138
146
 
139
147
  ```typescript
140
148
  import { createRuntime, lints } from '@prisma-next/sql-runtime';
141
149
 
142
150
  const runtime = createRuntime({
143
151
  // ...
144
- plugins: [lints({ severities: { noLimit: 'error' } })],
152
+ middleware: [lints({ severities: { noLimit: 'error' } })],
145
153
  });
146
154
  ```
147
155
 
148
156
  ## Architecture
149
157
 
150
- The SQL runtime composes runtime-executor with SQL-specific implementations. Descriptors implement `SqlStaticContributions` so `ExecutionContext` can be derived from the descriptors-only stack without instantiation.
158
+ The SQL runtime extends the abstract `RuntimeCore` base class from `@prisma-next/framework-components/runtime` with SQL-specific implementations. Descriptors implement `SqlStaticContributions` so `ExecutionContext` can be derived from the descriptors-only stack without instantiation.
151
159
 
152
160
  1. **ExecutionStack**: Descriptors-only stack (from `@prisma-next/framework-components/execution`)
153
161
  2. **SqlStaticContributions**: Codecs, operation signatures, parameterized codecs, and mutation default generators contributed by each descriptor
154
162
  3. **ExecutionContext**: Built from contract + stack descriptors (no instantiation)
155
163
  4. **ExecutionStackInstance**: Instantiated components used at runtime for execution
156
- 5. **SqlRuntime**: Wraps `RuntimeCore` and adds SQL-specific encoding/decoding
164
+ 5. **SqlRuntime**: `class SqlRuntimeImpl extends RuntimeCore<SqlQueryPlan, SqlExecutionPlan, SqlMiddleware>` — overrides `lower` (with codec param-encoding), `runDriver`, `runBeforeCompile` (delegates to the SQL `beforeCompile` chain), and `close`. The execution path also wraps the `runWithMiddleware` helper from `framework-components/runtime` with codec row-decoding, marker verification (via the `RuntimeFamilyAdapter` defined in `runtime-spi.ts`), and telemetry fingerprinting (via `computeSqlFingerprint` from `fingerprint.ts`).
157
165
  6. **SqlMarker**: Provides SQL statements for marker management
158
166
 
159
167
  ```mermaid
@@ -164,7 +172,7 @@ flowchart LR
164
172
  Stack --> AdapterDesc[Adapter Descriptor]
165
173
  Stack --> Packs[Extension Packs]
166
174
  Context --> Runtime[SqlRuntime]
167
- Runtime --> Core[RuntimeCore]
175
+ Runtime -.extends.-> Core[RuntimeCore]
168
176
  DriverDesc --> DriverInst[Driver Instance]
169
177
  AdapterDesc --> AdapterInst[Adapter Instance]
170
178
  Runtime --> DriverInst