@prisma-next/sql-relational-core 0.5.0-dev.75 → 0.5.0-dev.77

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 (54) hide show
  1. package/dist/{query-lane-context-DaimN0zf.d.mts → codec-types-BUfBna4G.d.mts} +2 -69
  2. package/dist/codec-types-BUfBna4G.d.mts.map +1 -0
  3. package/dist/{errors-Cb03_kgU.mjs → errors-BF7W5uUd.mjs} +1 -1
  4. package/dist/{errors-Cb03_kgU.mjs.map → errors-BF7W5uUd.mjs.map} +1 -1
  5. package/dist/{errors-DCg-36g-.d.mts → errors-BgNpVAT8.d.mts} +2 -2
  6. package/dist/{errors-DCg-36g-.d.mts.map → errors-BgNpVAT8.d.mts.map} +1 -1
  7. package/dist/exports/ast.d.mts +4 -3
  8. package/dist/exports/ast.d.mts.map +1 -1
  9. package/dist/exports/ast.mjs +3 -33
  10. package/dist/exports/ast.mjs.map +1 -1
  11. package/dist/exports/codec-descriptor-registry.d.mts +2 -1
  12. package/dist/exports/codec-descriptor-registry.d.mts.map +1 -1
  13. package/dist/exports/errors.d.mts +1 -1
  14. package/dist/exports/errors.mjs +1 -1
  15. package/dist/exports/expression.d.mts +1 -1
  16. package/dist/exports/expression.mjs +1 -1
  17. package/dist/exports/middleware.d.mts +2 -0
  18. package/dist/exports/middleware.mjs +2 -0
  19. package/dist/exports/plan.d.mts +3 -3
  20. package/dist/exports/plan.mjs +34 -1
  21. package/dist/exports/plan.mjs.map +1 -0
  22. package/dist/exports/query-lane-context.d.mts +1 -1
  23. package/dist/exports/types.d.mts +2 -2
  24. package/dist/index.d.mts +10 -8
  25. package/dist/index.mjs +7 -5
  26. package/dist/middleware-D2Wv9QIf.mjs +82 -0
  27. package/dist/middleware-D2Wv9QIf.mjs.map +1 -0
  28. package/dist/middleware-USDeR8p1.d.mts +121 -0
  29. package/dist/middleware-USDeR8p1.d.mts.map +1 -0
  30. package/dist/plan-DFpOIsKB.d.mts +44 -0
  31. package/dist/plan-DFpOIsKB.d.mts.map +1 -0
  32. package/dist/query-lane-context-C2tLZGp4.d.mts +72 -0
  33. package/dist/query-lane-context-C2tLZGp4.d.mts.map +1 -0
  34. package/dist/{sql-execution-plan-DgcD75jn.d.mts → sql-execution-plan-ffz8TSfr.d.mts} +2 -2
  35. package/dist/{sql-execution-plan-DgcD75jn.d.mts.map → sql-execution-plan-ffz8TSfr.d.mts.map} +1 -1
  36. package/dist/{types-hwPoFZX2.d.mts → types-BZcKgaYA.d.mts} +27 -3
  37. package/dist/{types-hwPoFZX2.d.mts.map → types-BZcKgaYA.d.mts.map} +1 -1
  38. package/dist/{types-vA5134SY.mjs → types-CK9ZJ6OU.mjs} +43 -3
  39. package/dist/{types-vA5134SY.mjs.map → types-CK9ZJ6OU.mjs.map} +1 -1
  40. package/dist/{types-BnLrX_Vr.d.mts → types-CL18G37a.d.mts} +3 -3
  41. package/dist/{types-BnLrX_Vr.d.mts.map → types-CL18G37a.d.mts.map} +1 -1
  42. package/dist/{types-Czw4j_wY.d.mts → types-D6Pk1DTr.d.mts} +4 -4
  43. package/dist/{types-Czw4j_wY.d.mts.map → types-D6Pk1DTr.d.mts.map} +1 -1
  44. package/dist/util-DWmhUCEO.mjs +34 -0
  45. package/dist/util-DWmhUCEO.mjs.map +1 -0
  46. package/package.json +10 -11
  47. package/src/ast/types.ts +55 -1
  48. package/src/exports/middleware.ts +8 -0
  49. package/src/index.ts +1 -0
  50. package/src/middleware/param-ref-mutator.ts +230 -0
  51. package/src/plan.ts +36 -0
  52. package/dist/plan-CZ6CFuSX.d.mts +0 -24
  53. package/dist/plan-CZ6CFuSX.d.mts.map +0 -1
  54. package/dist/query-lane-context-DaimN0zf.d.mts.map +0 -1
@@ -0,0 +1,230 @@
1
+ import type { ParamRefMutator } from '@prisma-next/framework-components/runtime';
2
+ import type { SqlColumnRef } from '../ast/codec-types';
3
+ import type { ParamRef } from '../ast/types';
4
+ import { collectOrderedParamRefs } from '../ast/util';
5
+ import type { SqlExecutionPlan } from '../sql-execution-plan';
6
+
7
+ /**
8
+ * Brand applied to {@link ParamRefHandle} so user-constructed handles
9
+ * are rejected by the type system. The mutator only accepts handles it
10
+ * produced from `entries()`.
11
+ *
12
+ * The brand is a phantom type — there is no runtime token. At runtime
13
+ * the handle is the underlying `ParamRef` instance from the plan's
14
+ * `ast`; the brand only narrows the type-level surface so callers
15
+ * cannot fabricate a handle from a fresh `ParamRef.of(...)`.
16
+ */
17
+ declare const paramRefHandleBrand: unique symbol;
18
+
19
+ /**
20
+ * Opaque token identifying a single `ParamRef` in the plan. Produced by
21
+ * {@link SqlParamRefMutator.entries}; consumed by `replaceValue` /
22
+ * `replaceValues`.
23
+ *
24
+ * The phantom `TCodecId` parameter records the codec id of the
25
+ * referenced `ParamRef` so type-level inference can route replacement
26
+ * values through `TCodecMap` to the codec's declared `TInput`.
27
+ */
28
+ export interface ParamRefHandle<TCodecId extends string | undefined = string | undefined> {
29
+ readonly [paramRefHandleBrand]: TCodecId;
30
+ }
31
+
32
+ /**
33
+ * One outbound `ParamRef` slot in the plan exposed to middleware.
34
+ * `value` is the current value (post any prior middleware mutations);
35
+ * `codecId` is the codec id declared on the underlying `ParamRef`;
36
+ * `column` is populated for `ParamRef`s the lowering site could resolve
37
+ * to a single `(table, column)` via `ParamRef.refs` (encode-side column
38
+ * metadata is the middleware's domain — encode itself currently leaves
39
+ * `ctx.column` unset).
40
+ */
41
+ export interface ParamRefEntry<TCodecId extends string | undefined = string | undefined> {
42
+ readonly ref: ParamRefHandle<TCodecId>;
43
+ readonly value: unknown;
44
+ readonly codecId: TCodecId;
45
+ readonly column?: SqlColumnRef;
46
+ }
47
+
48
+ /**
49
+ * Discriminated entry union over a codec map. For each `K` in
50
+ * `TCodecMap`, `entries()` may yield a `ParamRefEntry<K>`; ParamRefs
51
+ * with no codec id (or a codec id outside the map) yield a
52
+ * `ParamRefEntry<undefined>`. Pattern-matching on `entry.codecId`
53
+ * narrows `entry.ref` to a `ParamRefHandle<K>`, which routes through
54
+ * the typed `replaceValue` overload.
55
+ */
56
+ export type ParamRefEntryUnion<TCodecMap extends Record<string, unknown>> =
57
+ | { [K in keyof TCodecMap & string]: ParamRefEntry<K> }[keyof TCodecMap & string]
58
+ | ParamRefEntry<undefined>;
59
+
60
+ /**
61
+ * SQL-family mutator threaded into `SqlMiddleware.beforeExecute` as
62
+ * `params`. Scope is `ParamRef.value` slots only — middleware cannot
63
+ * insert / remove `ParamRef`s, rewrite SQL, or modify projection. The
64
+ * type-level `ParamRefHandle` brand and the `replaceValue(ref,
65
+ * newValue)` shape enforce this at compile time.
66
+ *
67
+ * Allocation discipline: the mutator is constructed lazily from the
68
+ * plan. `entries()` walks the plan's existing AST without allocating
69
+ * an intermediate array; the working params buffer is only allocated
70
+ * on the first `replaceValue` / `replaceValues` call. If no middleware
71
+ * mutates, `currentParams()` returns the plan's original `params` by
72
+ * reference identity.
73
+ *
74
+ * The `TCodecMap` parameter is a record keyed by codec id; `replaceValue`
75
+ * infers `newValue` from `TCodecMap[H['codecId']]` for handles whose
76
+ * codec id is statically resolvable. For codec ids the type system
77
+ * cannot resolve, `newValue` falls back to `unknown` and the middleware
78
+ * is on the hook for runtime correctness.
79
+ */
80
+ export interface SqlParamRefMutator<
81
+ TCodecMap extends Record<string, unknown> = Record<string, unknown>,
82
+ > extends ParamRefMutator {
83
+ /** Iterate every outbound `ParamRef` the plan currently carries, in canonical order. */
84
+ entries(): IterableIterator<ParamRefEntryUnion<TCodecMap>>;
85
+
86
+ /**
87
+ * Replace one `ParamRef`'s value with the result of bulk processing.
88
+ * `newValue` is constrained to the codec's declared `TInput` for codec
89
+ * ids the type system can resolve via `TCodecMap`; for unresolvable
90
+ * codec ids `newValue` is `unknown` (the second overload).
91
+ */
92
+ replaceValue<TCodecId extends keyof TCodecMap & string>(
93
+ ref: ParamRefHandle<TCodecId>,
94
+ newValue: TCodecMap[TCodecId],
95
+ ): void;
96
+ replaceValue(ref: ParamRefHandle<undefined>, newValue: unknown): void;
97
+
98
+ /** Replace many at once (typical for bulk-pattern middleware). */
99
+ replaceValues(
100
+ updates: Iterable<{
101
+ readonly ref: ParamRefHandle<(keyof TCodecMap & string) | undefined>;
102
+ readonly newValue: unknown;
103
+ }>,
104
+ ): void;
105
+ }
106
+
107
+ /**
108
+ * Internal-only view of the mutator that exposes the post-mutation params
109
+ * array to the SQL runtime. The runtime calls `currentParams()` after the
110
+ * `beforeExecute` chain has run; the result is the plan's original
111
+ * `params` by reference identity if no middleware mutated, otherwise a
112
+ * frozen new array carrying the mutations applied in chain order.
113
+ *
114
+ * Family-internal contract — `SqlMiddleware` consumers never see this
115
+ * shape; they receive the public `SqlParamRefMutator` view above.
116
+ */
117
+ export interface SqlParamRefMutatorInternal<
118
+ TCodecMap extends Record<string, unknown> = Record<string, unknown>,
119
+ > extends SqlParamRefMutator<TCodecMap> {
120
+ currentParams(): readonly unknown[];
121
+ }
122
+
123
+ type AnyHandle = ParamRefHandle<string | undefined>;
124
+
125
+ /**
126
+ * Build a {@link SqlParamRefMutatorInternal} for the given lowered plan.
127
+ *
128
+ * The mutator captures `plan.params` by reference and walks
129
+ * `plan.ast` (via `collectOrderedParamRefs`) on demand to build
130
+ * entries. Mutations write to a lazily-allocated working copy so the
131
+ * fast path (no mutation) preserves bit-for-bit reference identity to
132
+ * the original `plan.params`.
133
+ *
134
+ * Threading: `plan.ast` carries the canonical `ParamRef` ordering used
135
+ * by every consumer (renderer's `$N` index map, encode-side metadata
136
+ * walk, etc.). The mutator's `entries()` yields the same order so
137
+ * middleware that filters by codec id sees ParamRefs in the order the
138
+ * runtime will encode them.
139
+ */
140
+ export function createSqlParamRefMutator<
141
+ TCodecMap extends Record<string, unknown> = Record<string, unknown>,
142
+ >(plan: SqlExecutionPlan): SqlParamRefMutatorInternal<TCodecMap> {
143
+ const originalParams = plan.params;
144
+ const refs: ReadonlyArray<ParamRef> = plan.ast ? collectOrderedParamRefs(plan.ast) : [];
145
+ let workingParams: unknown[] | undefined;
146
+
147
+ const indexOfRef = (handle: AnyHandle): number => {
148
+ // The handle is the underlying ParamRef instance the mutator yielded
149
+ // from entries(); equality is identity equality on the ParamRef. The
150
+ // brand on ParamRefHandle is unforgeable from outside, so the only
151
+ // legal handles came from this mutator's entries().
152
+ return refs.indexOf(handle as unknown as ParamRef);
153
+ };
154
+
155
+ const ensureWorkingParams = (): unknown[] => {
156
+ if (!workingParams) {
157
+ workingParams = [...originalParams];
158
+ }
159
+ return workingParams;
160
+ };
161
+
162
+ const writeAt = (index: number, value: unknown): void => {
163
+ const buffer = ensureWorkingParams();
164
+ buffer[index] = value;
165
+ };
166
+
167
+ function* entries(): IterableIterator<ParamRefEntryUnion<TCodecMap>> {
168
+ const view = workingParams ?? originalParams;
169
+ for (let i = 0; i < refs.length; i++) {
170
+ const ref = refs[i];
171
+ if (!ref) continue;
172
+ const handle = ref as unknown as ParamRefHandle<string | undefined>;
173
+ const value = i < view.length ? view[i] : ref.value;
174
+ const codecId = ref.codecId;
175
+ // Surface the column binding the lowering site resolved onto
176
+ // `ParamRef.refs` so column-aware middleware (e.g. cipherstash
177
+ // routing-key resolution) can address the cell without
178
+ // re-walking the AST. ParamRefs without a column binding stay
179
+ // `column: undefined`.
180
+ const column = ref.refs
181
+ ? ({ table: ref.refs.table, name: ref.refs.column } as const)
182
+ : undefined;
183
+ // The runtime erases the discriminated union to a single shape; the
184
+ // public type pins each entry's `ref` to the matching `codecId`
185
+ // arm at compile time.
186
+ const entry: ParamRefEntry<string | undefined> = column
187
+ ? { ref: handle, value, codecId, column }
188
+ : { ref: handle, value, codecId };
189
+ yield entry as ParamRefEntryUnion<TCodecMap>;
190
+ }
191
+ }
192
+
193
+ function replaceValue(handle: AnyHandle, newValue: unknown): void {
194
+ const index = indexOfRef(handle);
195
+ if (index < 0) {
196
+ // Handle does not belong to this plan. The type system pins this
197
+ // at the brand level; this runtime check guards against handles
198
+ // smuggled across plans.
199
+ return;
200
+ }
201
+ writeAt(index, newValue);
202
+ }
203
+
204
+ function replaceValues(
205
+ updates: Iterable<{ readonly ref: AnyHandle; readonly newValue: unknown }>,
206
+ ): void {
207
+ for (const { ref, newValue } of updates) {
208
+ const index = indexOfRef(ref);
209
+ if (index < 0) continue;
210
+ writeAt(index, newValue);
211
+ }
212
+ }
213
+
214
+ // The public `SqlParamRefMutator` declares overloaded `replaceValue`
215
+ // signatures (typed-by-codec / unresolvable-codec). The implementation
216
+ // is one function with a permissive runtime signature; the cast is the
217
+ // single point at which the runtime function meets the typed overload
218
+ // surface, matching the overload-implementation pattern.
219
+ return {
220
+ entries,
221
+ replaceValue: replaceValue as SqlParamRefMutator<TCodecMap>['replaceValue'],
222
+ replaceValues,
223
+ currentParams(): readonly unknown[] {
224
+ if (!workingParams) {
225
+ return originalParams;
226
+ }
227
+ return Object.freeze([...workingParams]);
228
+ },
229
+ };
230
+ }
package/src/plan.ts CHANGED
@@ -1,4 +1,6 @@
1
+ import type { Contract } from '@prisma-next/contract/types';
1
2
  import type { QueryPlan } from '@prisma-next/framework-components/runtime';
3
+ import type { SqlStorage } from '@prisma-next/sql-contract/types';
2
4
  import type { AnyQueryAst } from './ast/types';
3
5
 
4
6
  /**
@@ -18,3 +20,37 @@ export interface SqlQueryPlan<Row = unknown> extends QueryPlan<Row> {
18
20
  readonly ast: AnyQueryAst;
19
21
  readonly params: readonly unknown[];
20
22
  }
23
+
24
+ /**
25
+ * Wraps an `AnyQueryAst` (typically a `RawSqlExpr` constructed package-internally
26
+ * by an extension's migration factory) in a fully-populated `SqlQueryPlan`
27
+ * whose `meta` is sourced from the supplied contract.
28
+ *
29
+ * Centralising the envelope here means consumers (cipherstash migration
30
+ * factories today; future raw-sql callers) cannot drift on `storageHash` /
31
+ * `target` / `targetFamily`, which would otherwise surface as a subtle
32
+ * `assertContractMatches` failure inside `dataTransform`. `params` defaults
33
+ * to `[]` because parameters embedded in the AST as `ParamRef`s are resolved
34
+ * at lowering time (`encodeParams` walks `plan.ast.collectParamRefs()`),
35
+ * not at plan-construction time.
36
+ *
37
+ * The default `laneId` of `'raw'` reflects raw-SQL plans' standard lane tag;
38
+ * callers (e.g. a future `sql-raw-factory`) may override to differentiate
39
+ * the plan's provenance.
40
+ */
41
+ export function planFromAst<Row = unknown>(
42
+ ast: AnyQueryAst,
43
+ contract: Contract<SqlStorage>,
44
+ laneId = 'raw',
45
+ ): SqlQueryPlan<Row> {
46
+ return {
47
+ ast,
48
+ params: [],
49
+ meta: {
50
+ target: contract.target,
51
+ targetFamily: contract.targetFamily,
52
+ storageHash: contract.storage.storageHash,
53
+ lane: laneId,
54
+ },
55
+ };
56
+ }
@@ -1,24 +0,0 @@
1
- import { d as AnyQueryAst } from "./types-hwPoFZX2.mjs";
2
- import { QueryPlan } from "@prisma-next/framework-components/runtime";
3
-
4
- //#region src/plan.d.ts
5
- /**
6
- * SQL query plan produced by lanes before lowering.
7
- *
8
- * Lanes build ASTs and metadata but do not perform SQL lowering. The `sql`
9
- * field is absent — `RuntimeCore` (the runtime base class in
10
- * `@prisma-next/framework-components/runtime`) drives lowering via the
11
- * SQL adapter and produces a `SqlExecutionPlan`.
12
- *
13
- * Extends the framework-level `QueryPlan<Row>` marker (`meta + _row`) and
14
- * adds SQL-specific fields (`ast`, `params`). The phantom `_row` property
15
- * (inherited from `QueryPlan`) is what `ResultType<P>` inspects to recover
16
- * the row type.
17
- */
18
- interface SqlQueryPlan<Row = unknown> extends QueryPlan<Row> {
19
- readonly ast: AnyQueryAst;
20
- readonly params: readonly unknown[];
21
- }
22
- //#endregion
23
- export { SqlQueryPlan as t };
24
- //# sourceMappingURL=plan-CZ6CFuSX.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"plan-CZ6CFuSX.d.mts","names":[],"sources":["../src/plan.ts"],"mappings":";;;;;;AAgBA;;;;;;;;;;;UAAiB,YAAA,wBAAoC,SAAA,CAAU,GAAA;EAAA,SACpD,GAAA,EAAK,WAAA;EAAA,SACL,MAAA;AAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"query-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