@prisma-next/sql-runtime 0.3.0-dev.14 → 0.3.0-dev.146
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.
- package/LICENSE +201 -0
- package/README.md +141 -24
- package/dist/exports-DGa0ipuP.mjs +956 -0
- package/dist/exports-DGa0ipuP.mjs.map +1 -0
- package/dist/index-CDbmlDcn.d.mts +177 -0
- package/dist/index-CDbmlDcn.d.mts.map +1 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +3 -0
- package/dist/test/utils.d.mts +77 -0
- package/dist/test/utils.d.mts.map +1 -0
- package/dist/test/utils.mjs +221 -0
- package/dist/test/utils.mjs.map +1 -0
- package/package.json +29 -22
- package/src/codecs/decoding.ts +84 -3
- package/src/codecs/encoding.ts +5 -15
- package/src/codecs/json-schema-validation.ts +61 -0
- package/src/codecs/validation.ts +6 -5
- package/src/exports/index.ts +19 -7
- package/src/lower-sql-plan.ts +9 -9
- package/src/plugins/budgets.ts +375 -0
- package/src/plugins/lints.ts +211 -0
- package/src/sql-context.ts +454 -108
- package/src/sql-family-adapter.ts +16 -22
- package/src/sql-marker.ts +2 -2
- package/src/sql-runtime.ts +136 -47
- package/test/async-iterable-result.test.ts +42 -37
- package/test/budgets.test.ts +481 -0
- package/test/context.types.test-d.ts +68 -0
- package/test/execution-stack.test.ts +164 -0
- package/test/json-schema-validation.test.ts +571 -0
- package/test/lints.test.ts +161 -0
- package/test/mutation-default-generators.test.ts +254 -0
- package/test/parameterized-types.test.ts +529 -0
- package/test/sql-context.test.ts +301 -134
- package/test/sql-family-adapter.test.ts +37 -20
- package/test/sql-runtime.test.ts +220 -49
- package/test/utils.ts +101 -67
- package/dist/accelerate-EEKAFGN3-P6A6XJWJ.js +0 -137863
- package/dist/accelerate-EEKAFGN3-P6A6XJWJ.js.map +0 -1
- package/dist/amcheck-24VY6X5V.js +0 -13
- package/dist/amcheck-24VY6X5V.js.map +0 -1
- package/dist/bloom-VS74NLHT.js +0 -13
- package/dist/bloom-VS74NLHT.js.map +0 -1
- package/dist/btree_gin-WBC4EAAI.js +0 -13
- package/dist/btree_gin-WBC4EAAI.js.map +0 -1
- package/dist/btree_gist-UNC6QD3M.js +0 -13
- package/dist/btree_gist-UNC6QD3M.js.map +0 -1
- package/dist/chunk-3KTOEDFX.js +0 -49
- package/dist/chunk-3KTOEDFX.js.map +0 -1
- package/dist/chunk-47DZBRQC.js +0 -1280
- package/dist/chunk-47DZBRQC.js.map +0 -1
- package/dist/chunk-52N6AFZM.js +0 -133
- package/dist/chunk-52N6AFZM.js.map +0 -1
- package/dist/chunk-7D4SUZUM.js +0 -38
- package/dist/chunk-7D4SUZUM.js.map +0 -1
- package/dist/chunk-C6I3V3DM.js +0 -455
- package/dist/chunk-C6I3V3DM.js.map +0 -1
- package/dist/chunk-ECWIHLAT.js +0 -37
- package/dist/chunk-ECWIHLAT.js.map +0 -1
- package/dist/chunk-EI626SDC.js +0 -105
- package/dist/chunk-EI626SDC.js.map +0 -1
- package/dist/chunk-UKKOYUGL.js +0 -578
- package/dist/chunk-UKKOYUGL.js.map +0 -1
- package/dist/chunk-XPLNMXQV.js +0 -1537
- package/dist/chunk-XPLNMXQV.js.map +0 -1
- package/dist/citext-T7MXGUY7.js +0 -13
- package/dist/citext-T7MXGUY7.js.map +0 -1
- package/dist/client-5FENX6AW.js +0 -299
- package/dist/client-5FENX6AW.js.map +0 -1
- package/dist/cube-TFDQBZCI.js +0 -13
- package/dist/cube-TFDQBZCI.js.map +0 -1
- package/dist/dict_int-AEUOPGWP.js +0 -13
- package/dist/dict_int-AEUOPGWP.js.map +0 -1
- package/dist/dict_xsyn-DAAYX3FL.js +0 -13
- package/dist/dict_xsyn-DAAYX3FL.js.map +0 -1
- package/dist/dist-AQ3LWXOX.js +0 -570
- package/dist/dist-AQ3LWXOX.js.map +0 -1
- package/dist/dist-LBVX6BJW.js +0 -189
- package/dist/dist-LBVX6BJW.js.map +0 -1
- package/dist/dist-WLKUVDN2.js +0 -5127
- package/dist/dist-WLKUVDN2.js.map +0 -1
- package/dist/earthdistance-KIGTF4LE.js +0 -13
- package/dist/earthdistance-KIGTF4LE.js.map +0 -1
- package/dist/file_fdw-5N55UP6I.js +0 -13
- package/dist/file_fdw-5N55UP6I.js.map +0 -1
- package/dist/fuzzystrmatch-KN3YWBFP.js +0 -13
- package/dist/fuzzystrmatch-KN3YWBFP.js.map +0 -1
- package/dist/hstore-YX726NKN.js +0 -13
- package/dist/hstore-YX726NKN.js.map +0 -1
- package/dist/http-exception-FZY2H4OF.js +0 -8
- package/dist/http-exception-FZY2H4OF.js.map +0 -1
- package/dist/index.js +0 -30
- package/dist/index.js.map +0 -1
- package/dist/intarray-NKVXNO2D.js +0 -13
- package/dist/intarray-NKVXNO2D.js.map +0 -1
- package/dist/isn-FTEMJGEV.js +0 -13
- package/dist/isn-FTEMJGEV.js.map +0 -1
- package/dist/lo-DB7L4NGI.js +0 -13
- package/dist/lo-DB7L4NGI.js.map +0 -1
- package/dist/logger-WQ7SHNDD.js +0 -68
- package/dist/logger-WQ7SHNDD.js.map +0 -1
- package/dist/ltree-Z32TZT6W.js +0 -13
- package/dist/ltree-Z32TZT6W.js.map +0 -1
- package/dist/nodefs-NM46ACH7.js +0 -31
- package/dist/nodefs-NM46ACH7.js.map +0 -1
- package/dist/opfs-ahp-NJO33LVZ.js +0 -332
- package/dist/opfs-ahp-NJO33LVZ.js.map +0 -1
- package/dist/pageinspect-YP3IZR4X.js +0 -13
- package/dist/pageinspect-YP3IZR4X.js.map +0 -1
- package/dist/pg_buffercache-7TD5J2FB.js +0 -13
- package/dist/pg_buffercache-7TD5J2FB.js.map +0 -1
- package/dist/pg_dump-SG4KYBUB.js +0 -2492
- package/dist/pg_dump-SG4KYBUB.js.map +0 -1
- package/dist/pg_freespacemap-DZDNCPZK.js +0 -13
- package/dist/pg_freespacemap-DZDNCPZK.js.map +0 -1
- package/dist/pg_surgery-J2MUEWEP.js +0 -13
- package/dist/pg_surgery-J2MUEWEP.js.map +0 -1
- package/dist/pg_trgm-7VNQOYS6.js +0 -13
- package/dist/pg_trgm-7VNQOYS6.js.map +0 -1
- package/dist/pg_visibility-TTSIPHFL.js +0 -13
- package/dist/pg_visibility-TTSIPHFL.js.map +0 -1
- package/dist/pg_walinspect-KPFHSHRJ.js +0 -13
- package/dist/pg_walinspect-KPFHSHRJ.js.map +0 -1
- package/dist/proxy-signals-GUDAMDHV.js +0 -39
- package/dist/proxy-signals-GUDAMDHV.js.map +0 -1
- package/dist/seg-IYVDLE4O.js +0 -13
- package/dist/seg-IYVDLE4O.js.map +0 -1
- package/dist/src/codecs/decoding.d.ts +0 -4
- package/dist/src/codecs/decoding.d.ts.map +0 -1
- package/dist/src/codecs/encoding.d.ts +0 -5
- package/dist/src/codecs/encoding.d.ts.map +0 -1
- package/dist/src/codecs/validation.d.ts +0 -6
- package/dist/src/codecs/validation.d.ts.map +0 -1
- package/dist/src/exports/index.d.ts +0 -11
- package/dist/src/exports/index.d.ts.map +0 -1
- package/dist/src/index.d.ts +0 -2
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/lower-sql-plan.d.ts +0 -15
- package/dist/src/lower-sql-plan.d.ts.map +0 -1
- package/dist/src/sql-context.d.ts +0 -65
- package/dist/src/sql-context.d.ts.map +0 -1
- package/dist/src/sql-family-adapter.d.ts +0 -10
- package/dist/src/sql-family-adapter.d.ts.map +0 -1
- package/dist/src/sql-marker.d.ts +0 -22
- package/dist/src/sql-marker.d.ts.map +0 -1
- package/dist/src/sql-runtime.d.ts +0 -25
- package/dist/src/sql-runtime.d.ts.map +0 -1
- package/dist/tablefunc-EF4RCS7S.js +0 -13
- package/dist/tablefunc-EF4RCS7S.js.map +0 -1
- package/dist/tcn-3VT5BQYW.js +0 -13
- package/dist/tcn-3VT5BQYW.js.map +0 -1
- package/dist/test/utils.d.ts +0 -60
- package/dist/test/utils.d.ts.map +0 -1
- package/dist/test/utils.js +0 -24635
- package/dist/test/utils.js.map +0 -1
- package/dist/tiny-CW6F4GX6.js +0 -10
- package/dist/tiny-CW6F4GX6.js.map +0 -1
- package/dist/tsm_system_rows-ES7KNUQH.js +0 -13
- package/dist/tsm_system_rows-ES7KNUQH.js.map +0 -1
- package/dist/tsm_system_time-76WEIMBG.js +0 -13
- package/dist/tsm_system_time-76WEIMBG.js.map +0 -1
- package/dist/unaccent-7RYF3R64.js +0 -13
- package/dist/unaccent-7RYF3R64.js.map +0 -1
- package/dist/utility-Q5A254LJ-J4HTKZPT.js +0 -347
- package/dist/utility-Q5A254LJ-J4HTKZPT.js.map +0 -1
- package/dist/uuid_ossp-4ETE4FPE.js +0 -13
- package/dist/uuid_ossp-4ETE4FPE.js.map +0 -1
- package/dist/vector-74GPNV7V.js +0 -13
- package/dist/vector-74GPNV7V.js.map +0 -1
- package/src/index.ts +0 -1
package/src/sql-context.ts
CHANGED
|
@@ -1,156 +1,502 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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/runtime-executor';
|
|
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
|
-
|
|
28
|
+
SqlDriver,
|
|
16
29
|
} from '@prisma-next/sql-relational-core/ast';
|
|
17
30
|
import { createCodecRegistry } from '@prisma-next/sql-relational-core/ast';
|
|
18
|
-
import type {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
31
|
+
import type {
|
|
32
|
+
AppliedMutationDefault,
|
|
33
|
+
ExecutionContext,
|
|
34
|
+
JsonSchemaValidateFn,
|
|
35
|
+
JsonSchemaValidatorRegistry,
|
|
36
|
+
MutationDefaultsOptions,
|
|
37
|
+
TypeHelperRegistry,
|
|
38
|
+
} from '@prisma-next/sql-relational-core/query-lane-context';
|
|
39
|
+
import { type as arktype } from 'arktype';
|
|
23
40
|
|
|
24
41
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
42
|
+
* Runtime parameterized codec descriptor.
|
|
43
|
+
* Provides validation schema and optional init hook for codecs that support type parameters.
|
|
44
|
+
* Used at runtime to validate typeParams and create type helpers.
|
|
28
45
|
*
|
|
29
|
-
*
|
|
46
|
+
* This is a type alias for `CodecParamsDescriptor` from the AST layer,
|
|
47
|
+
* which is the shared definition used by both adapter and runtime.
|
|
30
48
|
*/
|
|
31
|
-
export
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
49
|
+
export type RuntimeParameterizedCodecDescriptor<
|
|
50
|
+
TParams = Record<string, unknown>,
|
|
51
|
+
THelper = unknown,
|
|
52
|
+
> = CodecParamsDescriptor<TParams, THelper>;
|
|
53
|
+
|
|
54
|
+
export interface SqlStaticContributions {
|
|
55
|
+
readonly codecs: () => CodecRegistry;
|
|
56
|
+
// biome-ignore lint/suspicious/noExplicitAny: needed for covariance with concrete descriptor types
|
|
57
|
+
readonly parameterizedCodecs: () => ReadonlyArray<RuntimeParameterizedCodecDescriptor<any, any>>;
|
|
58
|
+
readonly queryOperations?: () => ReadonlyArray<SqlOperationDescriptor>;
|
|
59
|
+
readonly mutationDefaultGenerators?: () => ReadonlyArray<RuntimeMutationDefaultGenerator>;
|
|
37
60
|
}
|
|
38
61
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
extends
|
|
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 {
|
|
47
88
|
create(): SqlRuntimeExtensionInstance<TTargetId>;
|
|
48
89
|
}
|
|
49
90
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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> {}
|
|
117
|
+
|
|
118
|
+
export type SqlRuntimeAdapterInstance<TTargetId extends string = string> = RuntimeAdapterInstance<
|
|
119
|
+
'sql',
|
|
120
|
+
TTargetId
|
|
121
|
+
> &
|
|
122
|
+
Adapter<AnyQueryAst, Contract<SqlStorage>, LoweredStatement>;
|
|
53
123
|
|
|
54
124
|
/**
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* @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.
|
|
60
129
|
*/
|
|
61
|
-
export type
|
|
130
|
+
export type SqlRuntimeDriverInstance<TTargetId extends string = string> = RuntimeDriverInstance<
|
|
62
131
|
'sql',
|
|
63
132
|
TTargetId
|
|
64
133
|
> &
|
|
65
|
-
|
|
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
|
+
});
|
|
150
|
+
}
|
|
66
151
|
|
|
67
|
-
|
|
68
|
-
// SQL Runtime Context
|
|
69
|
-
// ============================================================================
|
|
152
|
+
export type { ExecutionContext, JsonSchemaValidatorRegistry, TypeHelperRegistry };
|
|
70
153
|
|
|
71
|
-
export
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
+
]);
|
|
163
|
+
|
|
164
|
+
const result = checkContractComponentRequirements({
|
|
165
|
+
contract,
|
|
166
|
+
expectedTargetFamily: 'sql',
|
|
167
|
+
expectedTargetId: stack.target.targetId,
|
|
168
|
+
providedComponentIds,
|
|
169
|
+
});
|
|
170
|
+
|
|
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
|
+
}
|
|
76
202
|
}
|
|
77
203
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
>;
|
|
96
|
-
|
|
204
|
+
function validateTypeParams(
|
|
205
|
+
typeParams: Record<string, unknown>,
|
|
206
|
+
codecDescriptor: RuntimeParameterizedCodecDescriptor,
|
|
207
|
+
context: { typeName?: string; tableName?: string; columnName?: string },
|
|
208
|
+
): Record<string, unknown> {
|
|
209
|
+
const result = codecDescriptor.paramsSchema(typeParams);
|
|
210
|
+
if (result instanceof arktype.errors) {
|
|
211
|
+
const messages = result.map((p: { message: string }) => p.message).join('; ');
|
|
212
|
+
const locationInfo = context.typeName
|
|
213
|
+
? `type '${context.typeName}'`
|
|
214
|
+
: `column '${context.tableName}.${context.columnName}'`;
|
|
215
|
+
throw runtimeError(
|
|
216
|
+
'RUNTIME.TYPE_PARAMS_INVALID',
|
|
217
|
+
`Invalid typeParams for ${locationInfo} (codecId: ${codecDescriptor.codecId}): ${messages}`,
|
|
218
|
+
{ ...context, codecId: codecDescriptor.codecId, typeParams },
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
return result as Record<string, unknown>;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function collectParameterizedCodecDescriptors(
|
|
225
|
+
contributors: ReadonlyArray<SqlStaticContributions>,
|
|
226
|
+
): Map<string, RuntimeParameterizedCodecDescriptor> {
|
|
227
|
+
const descriptors = new Map<string, RuntimeParameterizedCodecDescriptor>();
|
|
228
|
+
|
|
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
|
+
);
|
|
237
|
+
}
|
|
238
|
+
descriptors.set(descriptor.codecId, descriptor);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return descriptors;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function initializeTypeHelpers(
|
|
246
|
+
storageTypes: Record<string, StorageTypeInstance> | undefined,
|
|
247
|
+
codecDescriptors: Map<string, RuntimeParameterizedCodecDescriptor>,
|
|
248
|
+
): TypeHelperRegistry {
|
|
249
|
+
const helpers: TypeHelperRegistry = {};
|
|
250
|
+
|
|
251
|
+
if (!storageTypes) {
|
|
252
|
+
return helpers;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
for (const [typeName, typeInstance] of Object.entries(storageTypes)) {
|
|
256
|
+
const descriptor = codecDescriptors.get(typeInstance.codecId);
|
|
257
|
+
|
|
258
|
+
if (descriptor) {
|
|
259
|
+
const validatedParams = validateTypeParams(typeInstance.typeParams, descriptor, {
|
|
260
|
+
typeName,
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
if (descriptor.init) {
|
|
264
|
+
helpers[typeName] = descriptor.init(validatedParams);
|
|
265
|
+
} else {
|
|
266
|
+
helpers[typeName] = typeInstance;
|
|
267
|
+
}
|
|
268
|
+
} else {
|
|
269
|
+
helpers[typeName] = typeInstance;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return helpers;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function validateColumnTypeParams(
|
|
277
|
+
storage: SqlStorage,
|
|
278
|
+
codecDescriptors: Map<string, RuntimeParameterizedCodecDescriptor>,
|
|
279
|
+
): void {
|
|
280
|
+
for (const [tableName, table] of Object.entries(storage.tables)) {
|
|
281
|
+
for (const [columnName, column] of Object.entries(table.columns)) {
|
|
282
|
+
if (column.typeParams) {
|
|
283
|
+
const descriptor = codecDescriptors.get(column.codecId);
|
|
284
|
+
if (descriptor) {
|
|
285
|
+
validateTypeParams(column.typeParams, descriptor, { tableName, columnName });
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
97
290
|
}
|
|
98
291
|
|
|
99
292
|
/**
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
* The context includes:
|
|
103
|
-
* - The validated contract
|
|
104
|
-
* - The adapter instance (created from descriptor)
|
|
105
|
-
* - Codec registry (populated from adapter + extension instances)
|
|
106
|
-
* - Operation registry (populated from extension instances)
|
|
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 }`.
|
|
107
295
|
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
296
|
+
* Handles both:
|
|
297
|
+
* - Inline `typeParams.schema` on columns
|
|
298
|
+
* - `typeRef` → `storage.types[ref]` with init hook results already in `types` registry
|
|
110
299
|
*/
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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
|
+
}
|
|
116
314
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
315
|
+
if (codecIdsWithInit.size === 0) {
|
|
316
|
+
return undefined;
|
|
317
|
+
}
|
|
120
318
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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;
|
|
124
322
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
323
|
+
const key = `${tableName}.${columnName}`;
|
|
324
|
+
|
|
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
|
+
}
|
|
333
|
+
|
|
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
|
+
}
|
|
129
347
|
}
|
|
130
348
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
349
|
+
if (validators.size === 0) return undefined;
|
|
350
|
+
return {
|
|
351
|
+
get: (key: string) => validators.get(key),
|
|
352
|
+
size: validators.size,
|
|
353
|
+
};
|
|
354
|
+
}
|
|
134
355
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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
|
+
);
|
|
139
376
|
}
|
|
377
|
+
generators.set(generator.id, generator);
|
|
378
|
+
owners.set(generator.id, contributor.id);
|
|
140
379
|
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
return generators;
|
|
383
|
+
}
|
|
141
384
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
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
|
+
);
|
|
146
400
|
}
|
|
401
|
+
// nosemgrep: javascript.express.security.express-wkhtml-injection.express-wkhtmltoimage-injection
|
|
402
|
+
return generator.generate(spec.params);
|
|
147
403
|
}
|
|
148
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;
|
|
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);
|
|
441
|
+
}
|
|
442
|
+
|
|
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);
|
|
480
|
+
|
|
481
|
+
if (parameterizedCodecDescriptors.size > 0) {
|
|
482
|
+
validateColumnTypeParams(contract.storage, parameterizedCodecDescriptors);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
const types = initializeTypeHelpers(contract.storage.types, parameterizedCodecDescriptors);
|
|
486
|
+
|
|
487
|
+
const jsonSchemaValidators = buildJsonSchemaValidatorRegistry(
|
|
488
|
+
contract,
|
|
489
|
+
types,
|
|
490
|
+
parameterizedCodecDescriptors,
|
|
491
|
+
);
|
|
149
492
|
|
|
150
493
|
return {
|
|
151
494
|
contract,
|
|
152
|
-
adapter: adapterInstance,
|
|
153
|
-
operations: operationRegistry,
|
|
154
495
|
codecs: codecRegistry,
|
|
496
|
+
queryOperations: queryOperationRegistry,
|
|
497
|
+
types,
|
|
498
|
+
...(jsonSchemaValidators ? { jsonSchemaValidators } : {}),
|
|
499
|
+
applyMutationDefaults: (options) =>
|
|
500
|
+
applyMutationDefaults(contract, mutationDefaultGeneratorRegistry, options),
|
|
155
501
|
};
|
|
156
502
|
}
|
|
@@ -1,28 +1,18 @@
|
|
|
1
|
-
import type { ExecutionPlan } from '@prisma-next/contract/types';
|
|
2
|
-
import type {
|
|
3
|
-
MarkerReader,
|
|
4
|
-
MarkerStatement,
|
|
5
|
-
RuntimeFamilyAdapter,
|
|
6
|
-
} from '@prisma-next/runtime-executor';
|
|
1
|
+
import type { Contract, ExecutionPlan } from '@prisma-next/contract/types';
|
|
2
|
+
import type { MarkerReader, RuntimeFamilyAdapter } from '@prisma-next/runtime-executor';
|
|
7
3
|
import { runtimeError } from '@prisma-next/runtime-executor';
|
|
8
|
-
import type {
|
|
9
|
-
import {
|
|
4
|
+
import type { SqlStorage } from '@prisma-next/sql-contract/types';
|
|
5
|
+
import type { AdapterProfile } from '@prisma-next/sql-relational-core/ast';
|
|
10
6
|
|
|
11
|
-
class
|
|
12
|
-
readMarkerStatement(): MarkerStatement {
|
|
13
|
-
return readContractMarker();
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export class SqlFamilyAdapter<TContract extends SqlContract<SqlStorage>>
|
|
7
|
+
export class SqlFamilyAdapter<TContract extends Contract<SqlStorage>>
|
|
18
8
|
implements RuntimeFamilyAdapter<TContract>
|
|
19
9
|
{
|
|
20
10
|
readonly contract: TContract;
|
|
21
11
|
readonly markerReader: MarkerReader;
|
|
22
12
|
|
|
23
|
-
constructor(contract: TContract) {
|
|
13
|
+
constructor(contract: TContract, adapterProfile: AdapterProfile) {
|
|
24
14
|
this.contract = contract;
|
|
25
|
-
this.markerReader =
|
|
15
|
+
this.markerReader = adapterProfile;
|
|
26
16
|
}
|
|
27
17
|
|
|
28
18
|
validatePlan(plan: ExecutionPlan, contract: TContract): void {
|
|
@@ -33,11 +23,15 @@ export class SqlFamilyAdapter<TContract extends SqlContract<SqlStorage>>
|
|
|
33
23
|
});
|
|
34
24
|
}
|
|
35
25
|
|
|
36
|
-
if (plan.meta.
|
|
37
|
-
throw runtimeError(
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
26
|
+
if (plan.meta.storageHash !== contract.storage.storageHash) {
|
|
27
|
+
throw runtimeError(
|
|
28
|
+
'PLAN.HASH_MISMATCH',
|
|
29
|
+
'Plan storage hash does not match runtime contract',
|
|
30
|
+
{
|
|
31
|
+
planStorageHash: plan.meta.storageHash,
|
|
32
|
+
runtimeStorageHash: contract.storage.storageHash,
|
|
33
|
+
},
|
|
34
|
+
);
|
|
41
35
|
}
|
|
42
36
|
}
|
|
43
37
|
}
|