@prisma-next/sql-runtime 0.3.0-pr.99.6 → 0.3.0

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 (171) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +141 -24
  3. package/dist/exports-BO6Fl7yn.mjs +889 -0
  4. package/dist/exports-BO6Fl7yn.mjs.map +1 -0
  5. package/dist/index-n6z6trta.d.mts +186 -0
  6. package/dist/index-n6z6trta.d.mts.map +1 -0
  7. package/dist/index.d.mts +2 -0
  8. package/dist/index.mjs +3 -0
  9. package/dist/test/utils.d.mts +77 -0
  10. package/dist/test/utils.d.mts.map +1 -0
  11. package/dist/test/utils.mjs +221 -0
  12. package/dist/test/utils.mjs.map +1 -0
  13. package/package.json +26 -20
  14. package/src/codecs/decoding.ts +84 -3
  15. package/src/codecs/encoding.ts +5 -15
  16. package/src/codecs/json-schema-validation.ts +61 -0
  17. package/src/codecs/validation.ts +7 -6
  18. package/src/exports/index.ts +20 -9
  19. package/src/lower-sql-plan.ts +9 -9
  20. package/src/middleware/budgets.ts +256 -0
  21. package/src/middleware/lints.ts +192 -0
  22. package/src/middleware/sql-middleware.ts +26 -0
  23. package/src/sql-context.ts +357 -257
  24. package/src/sql-family-adapter.ts +17 -23
  25. package/src/sql-marker.ts +2 -2
  26. package/src/sql-runtime.ts +136 -61
  27. package/test/async-iterable-result.test.ts +42 -37
  28. package/test/budgets.test.ts +431 -0
  29. package/test/context.types.test-d.ts +18 -20
  30. package/test/execution-stack.test.ts +164 -0
  31. package/test/json-schema-validation.test.ts +571 -0
  32. package/test/lints.test.ts +159 -0
  33. package/test/mutation-default-generators.test.ts +254 -0
  34. package/test/parameterized-types.test.ts +181 -205
  35. package/test/sql-context.test.ts +301 -134
  36. package/test/sql-family-adapter.test.ts +37 -20
  37. package/test/sql-runtime.test.ts +261 -49
  38. package/test/utils.ts +101 -67
  39. package/dist/accelerate-EEKAFGN3-P6A6XJWJ.js +0 -137863
  40. package/dist/accelerate-EEKAFGN3-P6A6XJWJ.js.map +0 -1
  41. package/dist/amcheck-24VY6X5V.js +0 -13
  42. package/dist/amcheck-24VY6X5V.js.map +0 -1
  43. package/dist/bloom-VS74NLHT.js +0 -13
  44. package/dist/bloom-VS74NLHT.js.map +0 -1
  45. package/dist/btree_gin-WBC4EAAI.js +0 -13
  46. package/dist/btree_gin-WBC4EAAI.js.map +0 -1
  47. package/dist/btree_gist-UNC6QD3M.js +0 -13
  48. package/dist/btree_gist-UNC6QD3M.js.map +0 -1
  49. package/dist/chunk-3KTOEDFX.js +0 -49
  50. package/dist/chunk-3KTOEDFX.js.map +0 -1
  51. package/dist/chunk-47DZBRQC.js +0 -1280
  52. package/dist/chunk-47DZBRQC.js.map +0 -1
  53. package/dist/chunk-52N6AFZM.js +0 -133
  54. package/dist/chunk-52N6AFZM.js.map +0 -1
  55. package/dist/chunk-7D4SUZUM.js +0 -38
  56. package/dist/chunk-7D4SUZUM.js.map +0 -1
  57. package/dist/chunk-APA6GHYY.js +0 -537
  58. package/dist/chunk-APA6GHYY.js.map +0 -1
  59. package/dist/chunk-ECWIHLAT.js +0 -37
  60. package/dist/chunk-ECWIHLAT.js.map +0 -1
  61. package/dist/chunk-EI626SDC.js +0 -105
  62. package/dist/chunk-EI626SDC.js.map +0 -1
  63. package/dist/chunk-UKKOYUGL.js +0 -578
  64. package/dist/chunk-UKKOYUGL.js.map +0 -1
  65. package/dist/chunk-XPLNMXQV.js +0 -1537
  66. package/dist/chunk-XPLNMXQV.js.map +0 -1
  67. package/dist/citext-T7MXGUY7.js +0 -13
  68. package/dist/citext-T7MXGUY7.js.map +0 -1
  69. package/dist/client-5FENX6AW.js +0 -299
  70. package/dist/client-5FENX6AW.js.map +0 -1
  71. package/dist/cube-TFDQBZCI.js +0 -13
  72. package/dist/cube-TFDQBZCI.js.map +0 -1
  73. package/dist/dict_int-AEUOPGWP.js +0 -13
  74. package/dist/dict_int-AEUOPGWP.js.map +0 -1
  75. package/dist/dict_xsyn-DAAYX3FL.js +0 -13
  76. package/dist/dict_xsyn-DAAYX3FL.js.map +0 -1
  77. package/dist/dist-AQ3LWXOX.js +0 -570
  78. package/dist/dist-AQ3LWXOX.js.map +0 -1
  79. package/dist/dist-LBVX6BJW.js +0 -189
  80. package/dist/dist-LBVX6BJW.js.map +0 -1
  81. package/dist/dist-WLKUVDN2.js +0 -5127
  82. package/dist/dist-WLKUVDN2.js.map +0 -1
  83. package/dist/earthdistance-KIGTF4LE.js +0 -13
  84. package/dist/earthdistance-KIGTF4LE.js.map +0 -1
  85. package/dist/file_fdw-5N55UP6I.js +0 -13
  86. package/dist/file_fdw-5N55UP6I.js.map +0 -1
  87. package/dist/fuzzystrmatch-KN3YWBFP.js +0 -13
  88. package/dist/fuzzystrmatch-KN3YWBFP.js.map +0 -1
  89. package/dist/hstore-YX726NKN.js +0 -13
  90. package/dist/hstore-YX726NKN.js.map +0 -1
  91. package/dist/http-exception-FZY2H4OF.js +0 -8
  92. package/dist/http-exception-FZY2H4OF.js.map +0 -1
  93. package/dist/index.js +0 -30
  94. package/dist/index.js.map +0 -1
  95. package/dist/intarray-NKVXNO2D.js +0 -13
  96. package/dist/intarray-NKVXNO2D.js.map +0 -1
  97. package/dist/isn-FTEMJGEV.js +0 -13
  98. package/dist/isn-FTEMJGEV.js.map +0 -1
  99. package/dist/lo-DB7L4NGI.js +0 -13
  100. package/dist/lo-DB7L4NGI.js.map +0 -1
  101. package/dist/logger-WQ7SHNDD.js +0 -68
  102. package/dist/logger-WQ7SHNDD.js.map +0 -1
  103. package/dist/ltree-Z32TZT6W.js +0 -13
  104. package/dist/ltree-Z32TZT6W.js.map +0 -1
  105. package/dist/nodefs-NM46ACH7.js +0 -31
  106. package/dist/nodefs-NM46ACH7.js.map +0 -1
  107. package/dist/opfs-ahp-NJO33LVZ.js +0 -332
  108. package/dist/opfs-ahp-NJO33LVZ.js.map +0 -1
  109. package/dist/pageinspect-YP3IZR4X.js +0 -13
  110. package/dist/pageinspect-YP3IZR4X.js.map +0 -1
  111. package/dist/pg_buffercache-7TD5J2FB.js +0 -13
  112. package/dist/pg_buffercache-7TD5J2FB.js.map +0 -1
  113. package/dist/pg_dump-SG4KYBUB.js +0 -2492
  114. package/dist/pg_dump-SG4KYBUB.js.map +0 -1
  115. package/dist/pg_freespacemap-DZDNCPZK.js +0 -13
  116. package/dist/pg_freespacemap-DZDNCPZK.js.map +0 -1
  117. package/dist/pg_surgery-J2MUEWEP.js +0 -13
  118. package/dist/pg_surgery-J2MUEWEP.js.map +0 -1
  119. package/dist/pg_trgm-7VNQOYS6.js +0 -13
  120. package/dist/pg_trgm-7VNQOYS6.js.map +0 -1
  121. package/dist/pg_visibility-TTSIPHFL.js +0 -13
  122. package/dist/pg_visibility-TTSIPHFL.js.map +0 -1
  123. package/dist/pg_walinspect-KPFHSHRJ.js +0 -13
  124. package/dist/pg_walinspect-KPFHSHRJ.js.map +0 -1
  125. package/dist/proxy-signals-GUDAMDHV.js +0 -39
  126. package/dist/proxy-signals-GUDAMDHV.js.map +0 -1
  127. package/dist/seg-IYVDLE4O.js +0 -13
  128. package/dist/seg-IYVDLE4O.js.map +0 -1
  129. package/dist/src/codecs/decoding.d.ts +0 -4
  130. package/dist/src/codecs/decoding.d.ts.map +0 -1
  131. package/dist/src/codecs/encoding.d.ts +0 -5
  132. package/dist/src/codecs/encoding.d.ts.map +0 -1
  133. package/dist/src/codecs/validation.d.ts +0 -6
  134. package/dist/src/codecs/validation.d.ts.map +0 -1
  135. package/dist/src/exports/index.d.ts +0 -11
  136. package/dist/src/exports/index.d.ts.map +0 -1
  137. package/dist/src/index.d.ts +0 -2
  138. package/dist/src/index.d.ts.map +0 -1
  139. package/dist/src/lower-sql-plan.d.ts +0 -15
  140. package/dist/src/lower-sql-plan.d.ts.map +0 -1
  141. package/dist/src/sql-context.d.ts +0 -130
  142. package/dist/src/sql-context.d.ts.map +0 -1
  143. package/dist/src/sql-family-adapter.d.ts +0 -10
  144. package/dist/src/sql-family-adapter.d.ts.map +0 -1
  145. package/dist/src/sql-marker.d.ts +0 -22
  146. package/dist/src/sql-marker.d.ts.map +0 -1
  147. package/dist/src/sql-runtime.d.ts +0 -25
  148. package/dist/src/sql-runtime.d.ts.map +0 -1
  149. package/dist/tablefunc-EF4RCS7S.js +0 -13
  150. package/dist/tablefunc-EF4RCS7S.js.map +0 -1
  151. package/dist/tcn-3VT5BQYW.js +0 -13
  152. package/dist/tcn-3VT5BQYW.js.map +0 -1
  153. package/dist/test/utils.d.ts +0 -60
  154. package/dist/test/utils.d.ts.map +0 -1
  155. package/dist/test/utils.js +0 -24635
  156. package/dist/test/utils.js.map +0 -1
  157. package/dist/tiny-CW6F4GX6.js +0 -10
  158. package/dist/tiny-CW6F4GX6.js.map +0 -1
  159. package/dist/tsm_system_rows-ES7KNUQH.js +0 -13
  160. package/dist/tsm_system_rows-ES7KNUQH.js.map +0 -1
  161. package/dist/tsm_system_time-76WEIMBG.js +0 -13
  162. package/dist/tsm_system_time-76WEIMBG.js.map +0 -1
  163. package/dist/unaccent-7RYF3R64.js +0 -13
  164. package/dist/unaccent-7RYF3R64.js.map +0 -1
  165. package/dist/utility-Q5A254LJ-J4HTKZPT.js +0 -347
  166. package/dist/utility-Q5A254LJ-J4HTKZPT.js.map +0 -1
  167. package/dist/uuid_ossp-4ETE4FPE.js +0 -13
  168. package/dist/uuid_ossp-4ETE4FPE.js.map +0 -1
  169. package/dist/vector-74GPNV7V.js +0 -13
  170. package/dist/vector-74GPNV7V.js.map +0 -1
  171. package/src/index.ts +0 -1
@@ -1,220 +1,206 @@
1
- import type {
2
- RuntimeAdapterDescriptor,
3
- RuntimeAdapterInstance,
4
- RuntimeExtensionDescriptor,
5
- RuntimeExtensionInstance,
6
- RuntimeTargetDescriptor,
7
- } from '@prisma-next/core-execution-plane/types';
8
- import { createOperationRegistry } from '@prisma-next/operations';
9
- import type { SqlContract, SqlStorage, StorageTypeInstance } from '@prisma-next/sql-contract/types';
10
- import type { SqlOperationSignature } from '@prisma-next/sql-operations';
1
+ import type { Contract, ExecutionMutationDefaultValue } from '@prisma-next/contract/types';
2
+ import type { ComponentDescriptor } from '@prisma-next/framework-components/components';
3
+ import { checkContractComponentRequirements } from '@prisma-next/framework-components/components';
4
+ import {
5
+ createExecutionStack,
6
+ type ExecutionStack,
7
+ type RuntimeAdapterDescriptor,
8
+ type RuntimeAdapterInstance,
9
+ type RuntimeDriverDescriptor,
10
+ type RuntimeDriverInstance,
11
+ type RuntimeExtensionDescriptor,
12
+ type RuntimeExtensionInstance,
13
+ type RuntimeTargetDescriptor,
14
+ type RuntimeTargetInstance,
15
+ } from '@prisma-next/framework-components/execution';
16
+ import { runtimeError } from '@prisma-next/framework-components/runtime';
17
+ import type { SqlStorage, StorageTypeInstance } from '@prisma-next/sql-contract/types';
18
+ import {
19
+ createSqlOperationRegistry,
20
+ type SqlOperationDescriptor,
21
+ } from '@prisma-next/sql-operations';
11
22
  import type {
12
23
  Adapter,
24
+ AnyQueryAst,
25
+ CodecParamsDescriptor,
13
26
  CodecRegistry,
14
27
  LoweredStatement,
15
- QueryAst,
28
+ SqlDriver,
16
29
  } from '@prisma-next/sql-relational-core/ast';
17
30
  import { createCodecRegistry } from '@prisma-next/sql-relational-core/ast';
18
31
  import type {
19
- QueryLaneContext,
32
+ AppliedMutationDefault,
33
+ ExecutionContext,
34
+ JsonSchemaValidateFn,
35
+ JsonSchemaValidatorRegistry,
36
+ MutationDefaultsOptions,
20
37
  TypeHelperRegistry,
21
38
  } from '@prisma-next/sql-relational-core/query-lane-context';
22
- import type { Type } from 'arktype';
23
39
  import { type as arktype } from 'arktype';
24
40
 
25
- // ============================================================================
26
- // Runtime Parameterized Codec Descriptor Types
27
- // ============================================================================
28
-
29
41
  /**
30
42
  * Runtime parameterized codec descriptor.
31
43
  * Provides validation schema and optional init hook for codecs that support type parameters.
32
44
  * Used at runtime to validate typeParams and create type helpers.
45
+ *
46
+ * This is a type alias for `CodecParamsDescriptor` from the AST layer,
47
+ * which is the shared definition used by both adapter and runtime.
33
48
  */
34
- export interface RuntimeParameterizedCodecDescriptor<
49
+ export type RuntimeParameterizedCodecDescriptor<
35
50
  TParams = Record<string, unknown>,
36
51
  THelper = unknown,
37
- > {
38
- /** The codec ID this descriptor applies to (e.g., 'pg/vector@1') */
39
- readonly codecId: string;
40
-
41
- /**
42
- * Arktype schema for validating typeParams.
43
- * The schema is used to validate both storage.types entries and inline column typeParams.
44
- */
45
- readonly paramsSchema: Type<TParams>;
46
-
47
- /**
48
- * Optional init hook called during runtime context creation.
49
- * Receives validated params and returns a helper object to be stored in context.types.
50
- * If not provided, the validated params are stored directly.
51
- */
52
- readonly init?: (params: TParams) => THelper;
53
- }
52
+ > = CodecParamsDescriptor<TParams, THelper>;
54
53
 
55
- // ============================================================================
56
- // SQL Runtime Extension Types
57
- // ============================================================================
58
-
59
- /**
60
- * SQL runtime extension instance.
61
- * Extends the framework RuntimeExtensionInstance with SQL-specific hooks
62
- * for contributing codecs and operations to the runtime context.
63
- *
64
- * @template TTargetId - The target ID (e.g., 'postgres', 'mysql')
65
- */
66
- export interface SqlRuntimeExtensionInstance<TTargetId extends string>
67
- extends RuntimeExtensionInstance<'sql', TTargetId> {
68
- /** Returns codecs to register in the runtime context. */
69
- codecs?(): CodecRegistry;
70
- /** Returns operations to register in the runtime context. */
71
- operations?(): ReadonlyArray<SqlOperationSignature>;
72
- /**
73
- * Returns parameterized codec descriptors for type validation and helper creation.
74
- * Uses unknown for type parameters to allow any concrete descriptor types.
75
- */
54
+ export interface SqlStaticContributions {
55
+ readonly codecs: () => CodecRegistry;
76
56
  // biome-ignore lint/suspicious/noExplicitAny: needed for covariance with concrete descriptor types
77
- parameterizedCodecs?(): ReadonlyArray<RuntimeParameterizedCodecDescriptor<any, any>>;
57
+ readonly parameterizedCodecs: () => ReadonlyArray<RuntimeParameterizedCodecDescriptor<any, any>>;
58
+ readonly queryOperations?: () => ReadonlyArray<SqlOperationDescriptor>;
59
+ readonly mutationDefaultGenerators?: () => ReadonlyArray<RuntimeMutationDefaultGenerator>;
78
60
  }
79
61
 
80
- /**
81
- * SQL runtime extension descriptor.
82
- * Extends the framework RuntimeExtensionDescriptor with SQL-specific instance type.
83
- *
84
- * @template TTargetId - The target ID (e.g., 'postgres', 'mysql')
85
- */
86
- export interface SqlRuntimeExtensionDescriptor<TTargetId extends string>
87
- extends RuntimeExtensionDescriptor<'sql', TTargetId, SqlRuntimeExtensionInstance<TTargetId>> {
62
+ export interface RuntimeMutationDefaultGenerator {
63
+ readonly id: string;
64
+ readonly generate: (params?: Record<string, unknown>) => unknown;
65
+ }
66
+
67
+ export interface SqlRuntimeTargetDescriptor<
68
+ TTargetId extends string = string,
69
+ TTargetInstance extends RuntimeTargetInstance<'sql', TTargetId> = RuntimeTargetInstance<
70
+ 'sql',
71
+ TTargetId
72
+ >,
73
+ > extends RuntimeTargetDescriptor<'sql', TTargetId, TTargetInstance>,
74
+ SqlStaticContributions {}
75
+
76
+ export interface SqlRuntimeAdapterDescriptor<
77
+ TTargetId extends string = string,
78
+ TAdapterInstance extends RuntimeAdapterInstance<
79
+ 'sql',
80
+ TTargetId
81
+ > = SqlRuntimeAdapterInstance<TTargetId>,
82
+ > extends RuntimeAdapterDescriptor<'sql', TTargetId, TAdapterInstance>,
83
+ SqlStaticContributions {}
84
+
85
+ export interface SqlRuntimeExtensionDescriptor<TTargetId extends string = string>
86
+ extends RuntimeExtensionDescriptor<'sql', TTargetId, SqlRuntimeExtensionInstance<TTargetId>>,
87
+ SqlStaticContributions {
88
88
  create(): SqlRuntimeExtensionInstance<TTargetId>;
89
89
  }
90
90
 
91
- // ============================================================================
92
- // SQL Runtime Adapter Instance
93
- // ============================================================================
91
+ export interface SqlExecutionStack<TTargetId extends string = string> {
92
+ readonly target: SqlRuntimeTargetDescriptor<TTargetId>;
93
+ readonly adapter: SqlRuntimeAdapterDescriptor<TTargetId>;
94
+ readonly extensionPacks: readonly SqlRuntimeExtensionDescriptor<TTargetId>[];
95
+ }
96
+
97
+ export type SqlExecutionStackWithDriver<TTargetId extends string = string> = Omit<
98
+ ExecutionStack<
99
+ 'sql',
100
+ TTargetId,
101
+ SqlRuntimeAdapterInstance<TTargetId>,
102
+ SqlRuntimeDriverInstance<TTargetId>,
103
+ SqlRuntimeExtensionInstance<TTargetId>
104
+ >,
105
+ 'target' | 'adapter' | 'driver' | 'extensionPacks'
106
+ > & {
107
+ readonly target: SqlRuntimeTargetDescriptor<TTargetId>;
108
+ readonly adapter: SqlRuntimeAdapterDescriptor<TTargetId, SqlRuntimeAdapterInstance<TTargetId>>;
109
+ readonly driver:
110
+ | RuntimeDriverDescriptor<'sql', TTargetId, unknown, SqlRuntimeDriverInstance<TTargetId>>
111
+ | undefined;
112
+ readonly extensionPacks: readonly SqlRuntimeExtensionDescriptor<TTargetId>[];
113
+ };
114
+
115
+ export interface SqlRuntimeExtensionInstance<TTargetId extends string>
116
+ extends RuntimeExtensionInstance<'sql', TTargetId> {}
94
117
 
95
- /**
96
- * SQL runtime adapter instance interface.
97
- * Combines RuntimeAdapterInstance identity with SQL Adapter behavior.
98
- * The instance IS an Adapter (via intersection), not HAS an adapter property.
99
- *
100
- * @template TTargetId - The target ID (e.g., 'postgres', 'mysql')
101
- */
102
118
  export type SqlRuntimeAdapterInstance<TTargetId extends string = string> = RuntimeAdapterInstance<
103
119
  'sql',
104
120
  TTargetId
105
121
  > &
106
- Adapter<QueryAst, SqlContract<SqlStorage>, LoweredStatement>;
107
-
108
- // ============================================================================
109
- // SQL Runtime Context
110
- // ============================================================================
111
-
112
- export type { TypeHelperRegistry };
113
-
114
- export interface RuntimeContext<TContract extends SqlContract<SqlStorage> = SqlContract<SqlStorage>>
115
- extends QueryLaneContext<TContract> {
116
- readonly adapter:
117
- | Adapter<QueryAst, TContract, LoweredStatement>
118
- | Adapter<QueryAst, SqlContract<SqlStorage>, LoweredStatement>;
119
-
120
- /**
121
- * Initialized type helpers from storage.types.
122
- * Each entry corresponds to a named type instance in the contract's storage.types.
123
- * The value is the result of calling the codec's init hook (if provided)
124
- * or the validated typeParams (if no init hook).
125
- */
126
- readonly types?: TypeHelperRegistry;
127
- }
122
+ Adapter<AnyQueryAst, Contract<SqlStorage>, LoweredStatement>;
128
123
 
129
124
  /**
130
- * Descriptor-first options for creating a SQL runtime context.
131
- * Takes the same framework composition as control-plane: target, adapter, extensionPacks.
132
- *
133
- * @template TContract - The SQL contract type
134
- * @template TTargetId - The target ID (e.g., 'postgres', 'mysql')
125
+ * NOTE: Binding type is intentionally erased to unknown at this shared runtime layer.
126
+ * Target clients (for example `postgres()`) validate and construct the concrete binding
127
+ * before calling `driver.connect(binding)`, which keeps runtime behavior safe today.
128
+ * A future follow-up can preserve TBinding through stack/context generics end-to-end.
135
129
  */
136
- export interface CreateRuntimeContextOptions<
137
- TContract extends SqlContract<SqlStorage> = SqlContract<SqlStorage>,
138
- TTargetId extends string = string,
139
- > {
140
- readonly contract: TContract;
141
- readonly target: RuntimeTargetDescriptor<'sql', TTargetId>;
142
- readonly adapter: RuntimeAdapterDescriptor<
143
- 'sql',
144
- TTargetId,
145
- SqlRuntimeAdapterInstance<TTargetId>
146
- >;
147
- readonly extensionPacks?: ReadonlyArray<SqlRuntimeExtensionDescriptor<TTargetId>>;
130
+ export type SqlRuntimeDriverInstance<TTargetId extends string = string> = RuntimeDriverInstance<
131
+ 'sql',
132
+ TTargetId
133
+ > &
134
+ SqlDriver<unknown>;
135
+
136
+ export function createSqlExecutionStack<TTargetId extends string>(options: {
137
+ readonly target: SqlRuntimeTargetDescriptor<TTargetId>;
138
+ readonly adapter: SqlRuntimeAdapterDescriptor<TTargetId>;
139
+ readonly driver?:
140
+ | RuntimeDriverDescriptor<'sql', TTargetId, unknown, SqlRuntimeDriverInstance<TTargetId>>
141
+ | undefined;
142
+ readonly extensionPacks?: readonly SqlRuntimeExtensionDescriptor<TTargetId>[] | undefined;
143
+ }): SqlExecutionStackWithDriver<TTargetId> {
144
+ return createExecutionStack({
145
+ target: options.target,
146
+ adapter: options.adapter,
147
+ driver: options.driver,
148
+ extensionPacks: options.extensionPacks,
149
+ });
148
150
  }
149
151
 
150
- // ============================================================================
151
- // Runtime Error Types and Helpers
152
- // ============================================================================
152
+ export type { ExecutionContext, JsonSchemaValidatorRegistry, TypeHelperRegistry };
153
153
 
154
- /**
155
- * Structured error thrown by the SQL runtime.
156
- *
157
- * Aligns with the repository's error envelope convention:
158
- * - `code`: Stable error code for programmatic handling (e.g., `RUNTIME.TYPE_PARAMS_INVALID`)
159
- * - `category`: Error source category (`RUNTIME`)
160
- * - `severity`: Error severity level (`error`)
161
- * - `details`: Optional structured details for debugging
162
- *
163
- * @example
164
- * ```typescript
165
- * try {
166
- * createRuntimeContext({ ... });
167
- * } catch (e) {
168
- * if ((e as RuntimeError).code === 'RUNTIME.TYPE_PARAMS_INVALID') {
169
- * console.error('Invalid type parameters:', (e as RuntimeError).details);
170
- * }
171
- * }
172
- * ```
173
- */
174
- export interface RuntimeError extends Error {
175
- /** Stable error code for programmatic handling (e.g., `RUNTIME.TYPE_PARAMS_INVALID`) */
176
- readonly code: string;
177
- /** Error source category */
178
- readonly category: 'RUNTIME';
179
- /** Error severity level */
180
- readonly severity: 'error';
181
- /** Optional structured details for debugging */
182
- readonly details?: Record<string, unknown>;
183
- }
154
+ export function assertExecutionStackContractRequirements(
155
+ contract: Contract<SqlStorage>,
156
+ stack: SqlExecutionStack,
157
+ ): void {
158
+ const providedComponentIds = new Set<string>([
159
+ stack.target.id,
160
+ stack.adapter.id,
161
+ ...stack.extensionPacks.map((pack) => pack.id),
162
+ ]);
184
163
 
185
- /**
186
- * Creates a RuntimeError for invalid type parameters.
187
- *
188
- * Error code: `RUNTIME.TYPE_PARAMS_INVALID`
189
- *
190
- * Thrown when:
191
- * - `storage.types` entries have typeParams that fail codec schema validation
192
- * - Column inline typeParams fail codec schema validation
193
- *
194
- * @internal
195
- */
196
- function runtimeTypeParamsInvalid(
197
- message: string,
198
- details?: Record<string, unknown>,
199
- ): RuntimeError {
200
- const error = new Error(message) as RuntimeError;
201
- Object.defineProperty(error, 'name', { value: 'RuntimeError', configurable: true });
202
- return Object.assign(error, {
203
- code: 'RUNTIME.TYPE_PARAMS_INVALID',
204
- category: 'RUNTIME' as const,
205
- severity: 'error' as const,
206
- details,
164
+ const result = checkContractComponentRequirements({
165
+ contract,
166
+ expectedTargetFamily: 'sql',
167
+ expectedTargetId: stack.target.targetId,
168
+ providedComponentIds,
207
169
  });
208
- }
209
170
 
210
- // ============================================================================
211
- // Parameterized Type Validation
212
- // ============================================================================
171
+ if (result.familyMismatch) {
172
+ throw runtimeError(
173
+ 'RUNTIME.CONTRACT_FAMILY_MISMATCH',
174
+ `Contract target family '${result.familyMismatch.actual}' does not match runtime family '${result.familyMismatch.expected}'.`,
175
+ {
176
+ actual: result.familyMismatch.actual,
177
+ expected: result.familyMismatch.expected,
178
+ },
179
+ );
180
+ }
181
+
182
+ if (result.targetMismatch) {
183
+ throw runtimeError(
184
+ 'RUNTIME.CONTRACT_TARGET_MISMATCH',
185
+ `Contract target '${result.targetMismatch.actual}' does not match runtime target descriptor '${result.targetMismatch.expected}'.`,
186
+ {
187
+ actual: result.targetMismatch.actual,
188
+ expected: result.targetMismatch.expected,
189
+ },
190
+ );
191
+ }
192
+
193
+ if (result.missingExtensionPackIds.length > 0) {
194
+ const packIds = result.missingExtensionPackIds;
195
+ const packList = packIds.map((id) => `'${id}'`).join(', ');
196
+ throw runtimeError(
197
+ 'RUNTIME.MISSING_EXTENSION_PACK',
198
+ `Contract requires extension pack(s) ${packList}, but runtime descriptors do not provide matching component(s).`,
199
+ { packIds },
200
+ );
201
+ }
202
+ }
213
203
 
214
- /**
215
- * Validates typeParams against the codec's paramsSchema.
216
- * @throws RuntimeError with code RUNTIME.TYPE_PARAMS_INVALID if validation fails
217
- */
218
204
  function validateTypeParams(
219
205
  typeParams: Record<string, unknown>,
220
206
  codecDescriptor: RuntimeParameterizedCodecDescriptor,
@@ -226,7 +212,8 @@ function validateTypeParams(
226
212
  const locationInfo = context.typeName
227
213
  ? `type '${context.typeName}'`
228
214
  : `column '${context.tableName}.${context.columnName}'`;
229
- throw runtimeTypeParamsInvalid(
215
+ throw runtimeError(
216
+ 'RUNTIME.TYPE_PARAMS_INVALID',
230
217
  `Invalid typeParams for ${locationInfo} (codecId: ${codecDescriptor.codecId}): ${messages}`,
231
218
  { ...context, codecId: codecDescriptor.codecId, typeParams },
232
219
  );
@@ -234,43 +221,27 @@ function validateTypeParams(
234
221
  return result as Record<string, unknown>;
235
222
  }
236
223
 
237
- /**
238
- * Collects parameterized codec descriptors from extension instances.
239
- * Returns a map of codecId → descriptor for quick lookup.
240
- */
241
224
  function collectParameterizedCodecDescriptors(
242
- extensionInstances: ReadonlyArray<SqlRuntimeExtensionInstance<string>>,
225
+ contributors: ReadonlyArray<SqlStaticContributions>,
243
226
  ): Map<string, RuntimeParameterizedCodecDescriptor> {
244
227
  const descriptors = new Map<string, RuntimeParameterizedCodecDescriptor>();
245
228
 
246
- for (const extInstance of extensionInstances) {
247
- const paramCodecs = extInstance.parameterizedCodecs?.();
248
- if (paramCodecs) {
249
- for (const descriptor of paramCodecs) {
250
- if (descriptors.has(descriptor.codecId)) {
251
- console.warn(
252
- `Duplicate parameterized codec descriptor for codecId '${descriptor.codecId}' - using later registration`,
253
- );
254
- }
255
- descriptors.set(descriptor.codecId, descriptor);
229
+ for (const contributor of contributors) {
230
+ for (const descriptor of contributor.parameterizedCodecs()) {
231
+ if (descriptors.has(descriptor.codecId)) {
232
+ throw runtimeError(
233
+ 'RUNTIME.DUPLICATE_PARAMETERIZED_CODEC',
234
+ `Duplicate parameterized codec descriptor for codecId '${descriptor.codecId}'.`,
235
+ { codecId: descriptor.codecId },
236
+ );
256
237
  }
238
+ descriptors.set(descriptor.codecId, descriptor);
257
239
  }
258
240
  }
259
241
 
260
242
  return descriptors;
261
243
  }
262
244
 
263
- /**
264
- * Initializes type helpers from storage.types using codec descriptors.
265
- *
266
- * For each named type instance in `storage.types`:
267
- * - If a codec descriptor exists with an `init` hook: calls the hook and stores the result
268
- * - Otherwise: stores the full type instance metadata directly (codecId, nativeType, typeParams)
269
- *
270
- * This matches the typing in `ExtractSchemaTypes<Contract>` which extracts
271
- * `Contract['storage']['types']` directly, ensuring runtime values match static types
272
- * when no init hook transforms the value.
273
- */
274
245
  function initializeTypeHelpers(
275
246
  storageTypes: Record<string, StorageTypeInstance> | undefined,
276
247
  codecDescriptors: Map<string, RuntimeParameterizedCodecDescriptor>,
@@ -285,20 +256,16 @@ function initializeTypeHelpers(
285
256
  const descriptor = codecDescriptors.get(typeInstance.codecId);
286
257
 
287
258
  if (descriptor) {
288
- // Validate typeParams against the codec's schema
289
259
  const validatedParams = validateTypeParams(typeInstance.typeParams, descriptor, {
290
260
  typeName,
291
261
  });
292
262
 
293
- // Call init hook if provided, otherwise store full type instance
294
263
  if (descriptor.init) {
295
264
  helpers[typeName] = descriptor.init(validatedParams);
296
265
  } else {
297
- // No init hook: expose full type instance metadata (matches contract typing)
298
266
  helpers[typeName] = typeInstance;
299
267
  }
300
268
  } else {
301
- // No descriptor found: expose full type instance (no validation possible)
302
269
  helpers[typeName] = typeInstance;
303
270
  }
304
271
  }
@@ -306,10 +273,6 @@ function initializeTypeHelpers(
306
273
  return helpers;
307
274
  }
308
275
 
309
- /**
310
- * Validates inline column typeParams across all tables.
311
- * @throws RuntimeError with code RUNTIME.TYPE_PARAMS_INVALID if validation fails
312
- */
313
276
  function validateColumnTypeParams(
314
277
  storage: SqlStorage,
315
278
  codecDescriptors: Map<string, RuntimeParameterizedCodecDescriptor>,
@@ -327,76 +290,213 @@ function validateColumnTypeParams(
327
290
  }
328
291
 
329
292
  /**
330
- * Creates a SQL runtime context from descriptor-first composition.
293
+ * Builds a registry of compiled JSON Schema validators by scanning the contract
294
+ * for columns whose codec descriptor provides an `init` hook returning `{ validate }`.
331
295
  *
332
- * The context includes:
333
- * - The validated contract
334
- * - The adapter instance (created from descriptor)
335
- * - Codec registry (populated from adapter + extension instances)
336
- * - Operation registry (populated from extension instances)
337
- * - Types registry (initialized helpers from storage.types)
338
- *
339
- * @param options - Descriptor-first composition options
340
- * @returns RuntimeContext with registries wired from all components
296
+ * Handles both:
297
+ * - Inline `typeParams.schema` on columns
298
+ * - `typeRef` `storage.types[ref]` with init hook results already in `types` registry
341
299
  */
342
- export function createRuntimeContext<
343
- TContract extends SqlContract<SqlStorage> = SqlContract<SqlStorage>,
344
- TTargetId extends string = string,
345
- >(options: CreateRuntimeContextOptions<TContract, TTargetId>): RuntimeContext<TContract> {
346
- const { contract, adapter: adapterDescriptor, extensionPacks } = options;
300
+ function buildJsonSchemaValidatorRegistry(
301
+ contract: Contract<SqlStorage>,
302
+ types: TypeHelperRegistry,
303
+ codecDescriptors: Map<string, RuntimeParameterizedCodecDescriptor>,
304
+ ): JsonSchemaValidatorRegistry | undefined {
305
+ const validators = new Map<string, JsonSchemaValidateFn>();
306
+
307
+ // Collect codec IDs that have init hooks (these produce { validate } helpers)
308
+ const codecIdsWithInit = new Set<string>();
309
+ for (const [codecId, descriptor] of codecDescriptors) {
310
+ if (descriptor.init) {
311
+ codecIdsWithInit.add(codecId);
312
+ }
313
+ }
347
314
 
348
- // Create adapter instance from descriptor
349
- // The adapter instance IS an Adapter (via intersection)
350
- const adapterInstance = adapterDescriptor.create();
315
+ if (codecIdsWithInit.size === 0) {
316
+ return undefined;
317
+ }
351
318
 
352
- // Create registries
353
- const codecRegistry = createCodecRegistry();
354
- const operationRegistry = createOperationRegistry();
319
+ for (const [tableName, table] of Object.entries(contract.storage.tables)) {
320
+ for (const [columnName, column] of Object.entries(table.columns)) {
321
+ if (!codecIdsWithInit.has(column.codecId)) continue;
355
322
 
356
- // Register adapter codecs (adapter instance has profile.codecs())
357
- const adapterCodecs = adapterInstance.profile.codecs();
358
- for (const codec of adapterCodecs.values()) {
359
- codecRegistry.register(codec);
360
- }
323
+ const key = `${tableName}.${columnName}`;
361
324
 
362
- // Create extension instances and collect their contributions
363
- const extensionInstances: SqlRuntimeExtensionInstance<TTargetId>[] = [];
325
+ // Case 1: column references a named type → validator already compiled via init hook
326
+ if (column.typeRef) {
327
+ const helper = types[column.typeRef] as { validate?: JsonSchemaValidateFn } | undefined;
328
+ if (helper?.validate) {
329
+ validators.set(key, helper.validate);
330
+ }
331
+ continue;
332
+ }
364
333
 
365
- for (const extDescriptor of extensionPacks ?? []) {
366
- const extInstance = extDescriptor.create();
367
- extensionInstances.push(extInstance);
334
+ // Case 2: inline typeParams with schema → compile via init hook
335
+ if (column.typeParams) {
336
+ const descriptor = codecDescriptors.get(column.codecId);
337
+ if (descriptor?.init) {
338
+ const helper = descriptor.init(column.typeParams) as
339
+ | { validate?: JsonSchemaValidateFn }
340
+ | undefined;
341
+ if (helper?.validate) {
342
+ validators.set(key, helper.validate);
343
+ }
344
+ }
345
+ }
346
+ }
347
+ }
368
348
 
369
- const extCodecs = extInstance.codecs?.();
370
- if (extCodecs) {
371
- for (const codec of extCodecs.values()) {
372
- codecRegistry.register(codec);
349
+ if (validators.size === 0) return undefined;
350
+ return {
351
+ get: (key: string) => validators.get(key),
352
+ size: validators.size,
353
+ };
354
+ }
355
+
356
+ function collectMutationDefaultGenerators(
357
+ contributors: ReadonlyArray<SqlStaticContributions & { readonly id: string }>,
358
+ ): ReadonlyMap<string, RuntimeMutationDefaultGenerator> {
359
+ const generators = new Map<string, RuntimeMutationDefaultGenerator>();
360
+ const owners = new Map<string, string>();
361
+
362
+ for (const contributor of contributors) {
363
+ const nextGenerators = contributor.mutationDefaultGenerators?.() ?? [];
364
+ for (const generator of nextGenerators) {
365
+ const existingOwner = owners.get(generator.id);
366
+ if (existingOwner !== undefined) {
367
+ throw runtimeError(
368
+ 'RUNTIME.DUPLICATE_MUTATION_DEFAULT_GENERATOR',
369
+ `Duplicate mutation default generator '${generator.id}'.`,
370
+ {
371
+ id: generator.id,
372
+ existingOwner,
373
+ incomingOwner: contributor.id,
374
+ },
375
+ );
373
376
  }
377
+ generators.set(generator.id, generator);
378
+ owners.set(generator.id, contributor.id);
374
379
  }
380
+ }
381
+
382
+ return generators;
383
+ }
375
384
 
376
- const extOperations = extInstance.operations?.();
377
- if (extOperations) {
378
- for (const operation of extOperations) {
379
- operationRegistry.register(operation);
385
+ function computeExecutionDefaultValue(
386
+ spec: ExecutionMutationDefaultValue,
387
+ generatorRegistry: ReadonlyMap<string, RuntimeMutationDefaultGenerator>,
388
+ ): unknown {
389
+ switch (spec.kind) {
390
+ case 'generator': {
391
+ const generator = generatorRegistry.get(spec.id);
392
+ if (!generator) {
393
+ throw runtimeError(
394
+ 'RUNTIME.MUTATION_DEFAULT_GENERATOR_MISSING',
395
+ `Contract references mutation default generator '${spec.id}' but no runtime component provides it.`,
396
+ {
397
+ id: spec.id,
398
+ },
399
+ );
380
400
  }
401
+ // nosemgrep: javascript.express.security.express-wkhtml-injection.express-wkhtmltoimage-injection
402
+ return generator.generate(spec.params);
403
+ }
404
+ }
405
+ }
406
+
407
+ function applyMutationDefaults(
408
+ contract: Contract<SqlStorage>,
409
+ generatorRegistry: ReadonlyMap<string, RuntimeMutationDefaultGenerator>,
410
+ options: MutationDefaultsOptions,
411
+ ): ReadonlyArray<AppliedMutationDefault> {
412
+ const defaults = contract.execution?.mutations.defaults ?? [];
413
+ if (defaults.length === 0) {
414
+ return [];
415
+ }
416
+
417
+ const applied: AppliedMutationDefault[] = [];
418
+ const appliedColumns = new Set<string>();
419
+
420
+ for (const mutationDefault of defaults) {
421
+ if (mutationDefault.ref.table !== options.table) {
422
+ continue;
381
423
  }
424
+
425
+ const defaultSpec =
426
+ options.op === 'create' ? mutationDefault.onCreate : mutationDefault.onUpdate;
427
+ if (!defaultSpec) {
428
+ continue;
429
+ }
430
+
431
+ const columnName = mutationDefault.ref.column;
432
+ if (Object.hasOwn(options.values, columnName) || appliedColumns.has(columnName)) {
433
+ continue;
434
+ }
435
+
436
+ applied.push({
437
+ column: columnName,
438
+ value: computeExecutionDefaultValue(defaultSpec, generatorRegistry),
439
+ });
440
+ appliedColumns.add(columnName);
382
441
  }
383
442
 
384
- // Collect parameterized codec descriptors from extensions
385
- const parameterizedCodecDescriptors = collectParameterizedCodecDescriptors(extensionInstances);
443
+ return applied;
444
+ }
445
+
446
+ export function createExecutionContext<
447
+ TContract extends Contract<SqlStorage> = Contract<SqlStorage>,
448
+ TTargetId extends string = string,
449
+ >(options: {
450
+ readonly contract: TContract;
451
+ readonly stack: SqlExecutionStack<TTargetId>;
452
+ }): ExecutionContext<TContract> {
453
+ const { contract, stack } = options;
454
+
455
+ assertExecutionStackContractRequirements(contract, stack);
456
+
457
+ const codecRegistry = createCodecRegistry();
458
+
459
+ const contributors: Array<SqlStaticContributions & ComponentDescriptor<string>> = [
460
+ stack.target,
461
+ stack.adapter,
462
+ ...stack.extensionPacks,
463
+ ];
464
+
465
+ for (const contributor of contributors) {
466
+ for (const c of contributor.codecs().values()) {
467
+ codecRegistry.register(c);
468
+ }
469
+ }
470
+
471
+ const queryOperationRegistry = createSqlOperationRegistry();
472
+ for (const contributor of contributors) {
473
+ for (const op of contributor.queryOperations?.() ?? []) {
474
+ queryOperationRegistry.register(op);
475
+ }
476
+ }
477
+
478
+ const parameterizedCodecDescriptors = collectParameterizedCodecDescriptors(contributors);
479
+ const mutationDefaultGeneratorRegistry = collectMutationDefaultGenerators(contributors);
386
480
 
387
- // Validate column typeParams if any descriptors are registered
388
481
  if (parameterizedCodecDescriptors.size > 0) {
389
482
  validateColumnTypeParams(contract.storage, parameterizedCodecDescriptors);
390
483
  }
391
484
 
392
- // Initialize type helpers from storage.types
393
485
  const types = initializeTypeHelpers(contract.storage.types, parameterizedCodecDescriptors);
394
486
 
487
+ const jsonSchemaValidators = buildJsonSchemaValidatorRegistry(
488
+ contract,
489
+ types,
490
+ parameterizedCodecDescriptors,
491
+ );
492
+
395
493
  return {
396
494
  contract,
397
- adapter: adapterInstance,
398
- operations: operationRegistry,
399
495
  codecs: codecRegistry,
496
+ queryOperations: queryOperationRegistry,
400
497
  types,
498
+ ...(jsonSchemaValidators ? { jsonSchemaValidators } : {}),
499
+ applyMutationDefaults: (options) =>
500
+ applyMutationDefaults(contract, mutationDefaultGeneratorRegistry, options),
401
501
  };
402
502
  }