@prisma-next/sql-runtime 0.3.0-dev.2 → 0.3.0-dev.21
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/README.md +1 -2
- package/dist/{accelerate-EEKAFGN3-SHR4XFVV.js → accelerate-EEKAFGN3-P6A6XJWJ.js} +28 -28
- package/dist/{accelerate-EEKAFGN3-SHR4XFVV.js.map → accelerate-EEKAFGN3-P6A6XJWJ.js.map} +1 -1
- package/dist/{dist-LCVVJCGI.js → dist-AQ3LWXOX.js} +13 -13
- package/dist/{dist-LCVVJCGI.js.map → dist-AQ3LWXOX.js.map} +1 -1
- package/dist/src/codecs/decoding.d.ts +4 -0
- package/dist/src/codecs/decoding.d.ts.map +1 -0
- package/dist/src/codecs/encoding.d.ts +5 -0
- package/dist/src/codecs/encoding.d.ts.map +1 -0
- package/dist/src/codecs/validation.d.ts +6 -0
- package/dist/src/codecs/validation.d.ts.map +1 -0
- package/dist/src/exports/index.d.ts +11 -0
- package/dist/src/exports/index.d.ts.map +1 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/lower-sql-plan.d.ts +15 -0
- package/dist/src/lower-sql-plan.d.ts.map +1 -0
- package/dist/src/sql-context.d.ts +65 -0
- package/dist/src/sql-context.d.ts.map +1 -0
- package/dist/src/sql-family-adapter.d.ts +10 -0
- package/dist/src/sql-family-adapter.d.ts.map +1 -0
- package/dist/src/sql-marker.d.ts +22 -0
- package/dist/src/sql-marker.d.ts.map +1 -0
- package/dist/src/sql-runtime.d.ts +25 -0
- package/dist/src/sql-runtime.d.ts.map +1 -0
- package/dist/test/utils.d.ts +20 -24
- package/dist/test/utils.d.ts.map +1 -0
- package/dist/test/utils.js +25 -25
- package/dist/test/utils.js.map +1 -1
- package/package.json +22 -21
- package/src/codecs/decoding.ts +140 -0
- package/src/codecs/encoding.ts +76 -0
- package/src/codecs/validation.ts +67 -0
- package/src/exports/index.ts +38 -0
- package/src/index.ts +1 -0
- package/src/lower-sql-plan.ts +32 -0
- package/src/sql-context.ts +156 -0
- package/src/sql-family-adapter.ts +43 -0
- package/src/sql-marker.ts +105 -0
- package/src/sql-runtime.ts +166 -0
- package/test/async-iterable-result.test.ts +136 -0
- package/test/sql-context.test.ts +217 -0
- package/test/sql-family-adapter.test.ts +86 -0
- package/test/sql-runtime.test.ts +155 -0
- package/test/utils.ts +266 -0
- package/dist/index.d.ts +0 -29
- package/dist/sql-runtime-DgEbg2OP.d.ts +0 -109
package/package.json
CHANGED
|
@@ -1,33 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma-next/sql-runtime",
|
|
3
|
-
"version": "0.3.0-dev.
|
|
3
|
+
"version": "0.3.0-dev.21",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"description": "SQL runtime implementation for Prisma Next",
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@prisma-next/contract": "0.3.0-dev.
|
|
9
|
-
"@prisma-next/core-execution-plane": "0.3.0-dev.
|
|
10
|
-
"@prisma-next/operations": "0.3.0-dev.
|
|
11
|
-
"@prisma-next/runtime-executor": "0.3.0-dev.
|
|
12
|
-
"@prisma-next/sql-contract": "0.3.0-dev.
|
|
13
|
-
"@prisma-next/sql-operations": "0.3.0-dev.
|
|
14
|
-
"@prisma-next/sql-relational-core": "0.3.0-dev.
|
|
8
|
+
"@prisma-next/contract": "0.3.0-dev.21",
|
|
9
|
+
"@prisma-next/core-execution-plane": "0.3.0-dev.21",
|
|
10
|
+
"@prisma-next/operations": "0.3.0-dev.21",
|
|
11
|
+
"@prisma-next/runtime-executor": "0.3.0-dev.21",
|
|
12
|
+
"@prisma-next/sql-contract": "0.3.0-dev.21",
|
|
13
|
+
"@prisma-next/sql-operations": "0.3.0-dev.21",
|
|
14
|
+
"@prisma-next/sql-relational-core": "0.3.0-dev.21"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
|
-
"@types/pg": "
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"vitest": "^4.0.16",
|
|
17
|
+
"@types/pg": "8.16.0",
|
|
18
|
+
"pg": "8.16.3",
|
|
19
|
+
"tsup": "8.5.1",
|
|
20
|
+
"typescript": "5.9.3",
|
|
21
|
+
"vitest": "4.0.16",
|
|
23
22
|
"@prisma-next/test-utils": "0.0.1"
|
|
24
23
|
},
|
|
25
24
|
"files": [
|
|
26
|
-
"dist"
|
|
25
|
+
"dist",
|
|
26
|
+
"src",
|
|
27
|
+
"test"
|
|
27
28
|
],
|
|
28
29
|
"exports": {
|
|
29
30
|
".": {
|
|
30
|
-
"types": "./dist/index.d.ts",
|
|
31
|
+
"types": "./dist/src/index.d.ts",
|
|
31
32
|
"import": "./dist/index.js"
|
|
32
33
|
},
|
|
33
34
|
"./test/utils": {
|
|
@@ -36,13 +37,13 @@
|
|
|
36
37
|
}
|
|
37
38
|
},
|
|
38
39
|
"scripts": {
|
|
39
|
-
"build": "tsup --config tsup.config.ts",
|
|
40
|
+
"build": "tsup --config tsup.config.ts && tsc --project tsconfig.build.json",
|
|
40
41
|
"test": "vitest run --passWithNoTests",
|
|
41
42
|
"test:coverage": "vitest run --coverage --passWithNoTests",
|
|
42
43
|
"typecheck": "tsc --project tsconfig.json --noEmit",
|
|
43
|
-
"lint": "biome check . --
|
|
44
|
-
"lint:fix": "biome check --write .
|
|
45
|
-
"lint:fix:unsafe": "biome check --write --unsafe .
|
|
46
|
-
"clean": "
|
|
44
|
+
"lint": "biome check . --error-on-warnings",
|
|
45
|
+
"lint:fix": "biome check --write .",
|
|
46
|
+
"lint:fix:unsafe": "biome check --write --unsafe .",
|
|
47
|
+
"clean": "rm -rf dist coverage .tmp-output"
|
|
47
48
|
}
|
|
48
49
|
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import type { ExecutionPlan } from '@prisma-next/contract/types';
|
|
2
|
+
import type { Codec, CodecRegistry } from '@prisma-next/sql-relational-core/ast';
|
|
3
|
+
|
|
4
|
+
function resolveRowCodec(
|
|
5
|
+
alias: string,
|
|
6
|
+
plan: ExecutionPlan,
|
|
7
|
+
registry: CodecRegistry,
|
|
8
|
+
): Codec | null {
|
|
9
|
+
const planCodecId = plan.meta.annotations?.codecs?.[alias] as string | undefined;
|
|
10
|
+
if (planCodecId) {
|
|
11
|
+
const codec = registry.get(planCodecId);
|
|
12
|
+
if (codec) {
|
|
13
|
+
return codec;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (plan.meta.projectionTypes) {
|
|
18
|
+
const typeId = plan.meta.projectionTypes[alias];
|
|
19
|
+
if (typeId) {
|
|
20
|
+
const codec = registry.get(typeId);
|
|
21
|
+
if (codec) {
|
|
22
|
+
return codec;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function decodeRow(
|
|
31
|
+
row: Record<string, unknown>,
|
|
32
|
+
plan: ExecutionPlan,
|
|
33
|
+
registry: CodecRegistry,
|
|
34
|
+
): Record<string, unknown> {
|
|
35
|
+
const decoded: Record<string, unknown> = {};
|
|
36
|
+
|
|
37
|
+
let aliases: readonly string[];
|
|
38
|
+
const projection = plan.meta.projection;
|
|
39
|
+
if (projection && !Array.isArray(projection)) {
|
|
40
|
+
aliases = Object.keys(projection);
|
|
41
|
+
} else if (projection && Array.isArray(projection)) {
|
|
42
|
+
aliases = projection;
|
|
43
|
+
} else {
|
|
44
|
+
aliases = Object.keys(row);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
for (const alias of aliases) {
|
|
48
|
+
const wireValue = row[alias];
|
|
49
|
+
|
|
50
|
+
const projection = plan.meta.projection;
|
|
51
|
+
const projectionValue =
|
|
52
|
+
projection && typeof projection === 'object' && !Array.isArray(projection)
|
|
53
|
+
? (projection as Record<string, string>)[alias]
|
|
54
|
+
: undefined;
|
|
55
|
+
|
|
56
|
+
if (typeof projectionValue === 'string' && projectionValue.startsWith('include:')) {
|
|
57
|
+
if (wireValue === null || wireValue === undefined) {
|
|
58
|
+
decoded[alias] = [];
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
try {
|
|
63
|
+
let parsed: unknown;
|
|
64
|
+
if (typeof wireValue === 'string') {
|
|
65
|
+
parsed = JSON.parse(wireValue);
|
|
66
|
+
} else if (Array.isArray(wireValue)) {
|
|
67
|
+
parsed = wireValue;
|
|
68
|
+
} else {
|
|
69
|
+
parsed = JSON.parse(String(wireValue));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (!Array.isArray(parsed)) {
|
|
73
|
+
throw new Error(`Expected array for include alias '${alias}', got ${typeof parsed}`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
decoded[alias] = parsed;
|
|
77
|
+
} catch (error) {
|
|
78
|
+
const decodeError = new Error(
|
|
79
|
+
`Failed to parse JSON array for include alias '${alias}': ${error instanceof Error ? error.message : String(error)}`,
|
|
80
|
+
) as Error & {
|
|
81
|
+
code: string;
|
|
82
|
+
category: string;
|
|
83
|
+
severity: string;
|
|
84
|
+
details?: Record<string, unknown>;
|
|
85
|
+
};
|
|
86
|
+
decodeError.code = 'RUNTIME.DECODE_FAILED';
|
|
87
|
+
decodeError.category = 'RUNTIME';
|
|
88
|
+
decodeError.severity = 'error';
|
|
89
|
+
decodeError.details = {
|
|
90
|
+
alias,
|
|
91
|
+
wirePreview:
|
|
92
|
+
typeof wireValue === 'string' && wireValue.length > 100
|
|
93
|
+
? `${wireValue.substring(0, 100)}...`
|
|
94
|
+
: String(wireValue).substring(0, 100),
|
|
95
|
+
};
|
|
96
|
+
throw decodeError;
|
|
97
|
+
}
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (wireValue === null || wireValue === undefined) {
|
|
102
|
+
decoded[alias] = wireValue;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const codec = resolveRowCodec(alias, plan, registry);
|
|
107
|
+
|
|
108
|
+
if (!codec) {
|
|
109
|
+
decoded[alias] = wireValue;
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
try {
|
|
114
|
+
decoded[alias] = codec.decode(wireValue);
|
|
115
|
+
} catch (error) {
|
|
116
|
+
const decodeError = new Error(
|
|
117
|
+
`Failed to decode row alias '${alias}' with codec '${codec.id}': ${error instanceof Error ? error.message : String(error)}`,
|
|
118
|
+
) as Error & {
|
|
119
|
+
code: string;
|
|
120
|
+
category: string;
|
|
121
|
+
severity: string;
|
|
122
|
+
details?: Record<string, unknown>;
|
|
123
|
+
};
|
|
124
|
+
decodeError.code = 'RUNTIME.DECODE_FAILED';
|
|
125
|
+
decodeError.category = 'RUNTIME';
|
|
126
|
+
decodeError.severity = 'error';
|
|
127
|
+
decodeError.details = {
|
|
128
|
+
alias,
|
|
129
|
+
codec: codec.id,
|
|
130
|
+
wirePreview:
|
|
131
|
+
typeof wireValue === 'string' && wireValue.length > 100
|
|
132
|
+
? `${wireValue.substring(0, 100)}...`
|
|
133
|
+
: String(wireValue).substring(0, 100),
|
|
134
|
+
};
|
|
135
|
+
throw decodeError;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return decoded;
|
|
140
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { ExecutionPlan, ParamDescriptor } from '@prisma-next/contract/types';
|
|
2
|
+
import type { Codec, CodecRegistry } from '@prisma-next/sql-relational-core/ast';
|
|
3
|
+
|
|
4
|
+
function resolveParamCodec(
|
|
5
|
+
paramDescriptor: ParamDescriptor,
|
|
6
|
+
plan: ExecutionPlan,
|
|
7
|
+
registry: CodecRegistry,
|
|
8
|
+
): Codec | null {
|
|
9
|
+
const paramName = paramDescriptor.name ?? `param_${paramDescriptor.index ?? 0}`;
|
|
10
|
+
|
|
11
|
+
const planCodecId = plan.meta.annotations?.codecs?.[paramName] as string | undefined;
|
|
12
|
+
if (planCodecId) {
|
|
13
|
+
const codec = registry.get(planCodecId);
|
|
14
|
+
if (codec) {
|
|
15
|
+
return codec;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (paramDescriptor.codecId) {
|
|
20
|
+
const codec = registry.get(paramDescriptor.codecId);
|
|
21
|
+
if (codec) {
|
|
22
|
+
return codec;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function encodeParam(
|
|
30
|
+
value: unknown,
|
|
31
|
+
paramDescriptor: ParamDescriptor,
|
|
32
|
+
plan: ExecutionPlan,
|
|
33
|
+
registry: CodecRegistry,
|
|
34
|
+
): unknown {
|
|
35
|
+
if (value === null || value === undefined) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const codec = resolveParamCodec(paramDescriptor, plan, registry);
|
|
40
|
+
if (!codec) {
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (codec.encode) {
|
|
45
|
+
try {
|
|
46
|
+
return codec.encode(value);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
throw new Error(
|
|
49
|
+
`Failed to encode parameter ${paramDescriptor.name ?? paramDescriptor.index}: ${error instanceof Error ? error.message : String(error)}`,
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function encodeParams(plan: ExecutionPlan, registry: CodecRegistry): readonly unknown[] {
|
|
58
|
+
if (plan.params.length === 0) {
|
|
59
|
+
return plan.params;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const encoded: unknown[] = [];
|
|
63
|
+
|
|
64
|
+
for (let i = 0; i < plan.params.length; i++) {
|
|
65
|
+
const paramValue = plan.params[i];
|
|
66
|
+
const paramDescriptor = plan.meta.paramDescriptors[i];
|
|
67
|
+
|
|
68
|
+
if (paramDescriptor) {
|
|
69
|
+
encoded.push(encodeParam(paramValue, paramDescriptor, plan, registry));
|
|
70
|
+
} else {
|
|
71
|
+
encoded.push(paramValue);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return Object.freeze(encoded);
|
|
76
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { runtimeError } from '@prisma-next/runtime-executor';
|
|
2
|
+
import type { SqlContract, SqlStorage } from '@prisma-next/sql-contract/types';
|
|
3
|
+
import type { CodecRegistry } from '@prisma-next/sql-relational-core/ast';
|
|
4
|
+
|
|
5
|
+
export function extractCodecIds(contract: SqlContract<SqlStorage>): Set<string> {
|
|
6
|
+
const codecIds = new Set<string>();
|
|
7
|
+
|
|
8
|
+
for (const table of Object.values(contract.storage.tables)) {
|
|
9
|
+
for (const column of Object.values(table.columns)) {
|
|
10
|
+
const codecId = column.codecId;
|
|
11
|
+
codecIds.add(codecId);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return codecIds;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function extractCodecIdsFromColumns(contract: SqlContract<SqlStorage>): Map<string, string> {
|
|
19
|
+
const codecIds = new Map<string, string>();
|
|
20
|
+
|
|
21
|
+
for (const [tableName, table] of Object.entries(contract.storage.tables)) {
|
|
22
|
+
for (const [columnName, column] of Object.entries(table.columns)) {
|
|
23
|
+
const codecId = column.codecId;
|
|
24
|
+
const key = `${tableName}.${columnName}`;
|
|
25
|
+
codecIds.set(key, codecId);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return codecIds;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function validateContractCodecMappings(
|
|
33
|
+
registry: CodecRegistry,
|
|
34
|
+
contract: SqlContract<SqlStorage>,
|
|
35
|
+
): void {
|
|
36
|
+
const codecIds = extractCodecIdsFromColumns(contract);
|
|
37
|
+
const invalidCodecs: Array<{ table: string; column: string; codecId: string }> = [];
|
|
38
|
+
|
|
39
|
+
for (const [key, codecId] of codecIds.entries()) {
|
|
40
|
+
if (!registry.has(codecId)) {
|
|
41
|
+
const parts = key.split('.');
|
|
42
|
+
const table = parts[0] ?? '';
|
|
43
|
+
const column = parts[1] ?? '';
|
|
44
|
+
invalidCodecs.push({ table, column, codecId });
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (invalidCodecs.length > 0) {
|
|
49
|
+
const details: Record<string, unknown> = {
|
|
50
|
+
contractTarget: contract.target,
|
|
51
|
+
invalidCodecs,
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
throw runtimeError(
|
|
55
|
+
'RUNTIME.CODEC_MISSING',
|
|
56
|
+
`Missing codec implementations for column codecIds: ${invalidCodecs.map((c) => `${c.table}.${c.column} (${c.codecId})`).join(', ')}`,
|
|
57
|
+
details,
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function validateCodecRegistryCompleteness(
|
|
63
|
+
registry: CodecRegistry,
|
|
64
|
+
contract: SqlContract<SqlStorage>,
|
|
65
|
+
): void {
|
|
66
|
+
validateContractCodecMappings(registry, contract);
|
|
67
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
AfterExecuteResult,
|
|
3
|
+
BudgetsOptions,
|
|
4
|
+
LintsOptions,
|
|
5
|
+
Log,
|
|
6
|
+
Plugin,
|
|
7
|
+
PluginContext,
|
|
8
|
+
} from '@prisma-next/runtime-executor';
|
|
9
|
+
export { budgets, lints } from '@prisma-next/runtime-executor';
|
|
10
|
+
export {
|
|
11
|
+
extractCodecIds,
|
|
12
|
+
validateCodecRegistryCompleteness,
|
|
13
|
+
validateContractCodecMappings,
|
|
14
|
+
} from '../codecs/validation';
|
|
15
|
+
export { lowerSqlPlan } from '../lower-sql-plan';
|
|
16
|
+
export type {
|
|
17
|
+
CreateRuntimeContextOptions,
|
|
18
|
+
RuntimeContext,
|
|
19
|
+
SqlRuntimeAdapterInstance,
|
|
20
|
+
SqlRuntimeExtensionDescriptor,
|
|
21
|
+
SqlRuntimeExtensionInstance,
|
|
22
|
+
} from '../sql-context';
|
|
23
|
+
export { createRuntimeContext } from '../sql-context';
|
|
24
|
+
export type { SqlStatement } from '../sql-marker';
|
|
25
|
+
export {
|
|
26
|
+
ensureSchemaStatement,
|
|
27
|
+
ensureTableStatement,
|
|
28
|
+
readContractMarker,
|
|
29
|
+
writeContractMarker,
|
|
30
|
+
} from '../sql-marker';
|
|
31
|
+
export type {
|
|
32
|
+
Runtime,
|
|
33
|
+
RuntimeOptions,
|
|
34
|
+
RuntimeTelemetryEvent,
|
|
35
|
+
RuntimeVerifyOptions,
|
|
36
|
+
TelemetryOutcome,
|
|
37
|
+
} from '../sql-runtime';
|
|
38
|
+
export { createRuntime } from '../sql-runtime';
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './exports';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ExecutionPlan } from '@prisma-next/contract/types';
|
|
2
|
+
import type { SqlQueryPlan } from '@prisma-next/sql-relational-core/plan';
|
|
3
|
+
import type { RuntimeContext } from './sql-context';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Lowers a SQL query plan to an executable Plan by calling the adapter's lower method.
|
|
7
|
+
*
|
|
8
|
+
* This function is responsible for converting a lane-produced SqlQueryPlan (which contains
|
|
9
|
+
* AST and params but no SQL) into a fully executable Plan (which includes SQL string).
|
|
10
|
+
*
|
|
11
|
+
* @param context - Runtime context containing the adapter
|
|
12
|
+
* @param queryPlan - SQL query plan from a lane (contains AST, params, meta, but no SQL)
|
|
13
|
+
* @returns Fully executable Plan with SQL string
|
|
14
|
+
*/
|
|
15
|
+
export function lowerSqlPlan<Row>(
|
|
16
|
+
context: RuntimeContext,
|
|
17
|
+
queryPlan: SqlQueryPlan<Row>,
|
|
18
|
+
): ExecutionPlan<Row> {
|
|
19
|
+
const lowered = context.adapter.lower(queryPlan.ast, {
|
|
20
|
+
contract: context.contract,
|
|
21
|
+
params: queryPlan.params,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const body = lowered.body;
|
|
25
|
+
|
|
26
|
+
return Object.freeze({
|
|
27
|
+
sql: body.sql,
|
|
28
|
+
params: body.params ?? queryPlan.params,
|
|
29
|
+
ast: queryPlan.ast,
|
|
30
|
+
meta: queryPlan.meta,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
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 } from '@prisma-next/sql-contract/types';
|
|
10
|
+
import type { SqlOperationSignature } from '@prisma-next/sql-operations';
|
|
11
|
+
import type {
|
|
12
|
+
Adapter,
|
|
13
|
+
CodecRegistry,
|
|
14
|
+
LoweredStatement,
|
|
15
|
+
QueryAst,
|
|
16
|
+
} from '@prisma-next/sql-relational-core/ast';
|
|
17
|
+
import { createCodecRegistry } from '@prisma-next/sql-relational-core/ast';
|
|
18
|
+
import type { QueryLaneContext } from '@prisma-next/sql-relational-core/query-lane-context';
|
|
19
|
+
|
|
20
|
+
// ============================================================================
|
|
21
|
+
// SQL Runtime Extension Types
|
|
22
|
+
// ============================================================================
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* SQL runtime extension instance.
|
|
26
|
+
* Extends the framework RuntimeExtensionInstance with SQL-specific hooks
|
|
27
|
+
* for contributing codecs and operations to the runtime context.
|
|
28
|
+
*
|
|
29
|
+
* @template TTargetId - The target ID (e.g., 'postgres', 'mysql')
|
|
30
|
+
*/
|
|
31
|
+
export interface SqlRuntimeExtensionInstance<TTargetId extends string>
|
|
32
|
+
extends RuntimeExtensionInstance<'sql', TTargetId> {
|
|
33
|
+
/** Returns codecs to register in the runtime context. */
|
|
34
|
+
codecs?(): CodecRegistry;
|
|
35
|
+
/** Returns operations to register in the runtime context. */
|
|
36
|
+
operations?(): ReadonlyArray<SqlOperationSignature>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* SQL runtime extension descriptor.
|
|
41
|
+
* Extends the framework RuntimeExtensionDescriptor with SQL-specific instance type.
|
|
42
|
+
*
|
|
43
|
+
* @template TTargetId - The target ID (e.g., 'postgres', 'mysql')
|
|
44
|
+
*/
|
|
45
|
+
export interface SqlRuntimeExtensionDescriptor<TTargetId extends string>
|
|
46
|
+
extends RuntimeExtensionDescriptor<'sql', TTargetId, SqlRuntimeExtensionInstance<TTargetId>> {
|
|
47
|
+
create(): SqlRuntimeExtensionInstance<TTargetId>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// ============================================================================
|
|
51
|
+
// SQL Runtime Adapter Instance
|
|
52
|
+
// ============================================================================
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* SQL runtime adapter instance interface.
|
|
56
|
+
* Combines RuntimeAdapterInstance identity with SQL Adapter behavior.
|
|
57
|
+
* The instance IS an Adapter (via intersection), not HAS an adapter property.
|
|
58
|
+
*
|
|
59
|
+
* @template TTargetId - The target ID (e.g., 'postgres', 'mysql')
|
|
60
|
+
*/
|
|
61
|
+
export type SqlRuntimeAdapterInstance<TTargetId extends string = string> = RuntimeAdapterInstance<
|
|
62
|
+
'sql',
|
|
63
|
+
TTargetId
|
|
64
|
+
> &
|
|
65
|
+
Adapter<QueryAst, SqlContract<SqlStorage>, LoweredStatement>;
|
|
66
|
+
|
|
67
|
+
// ============================================================================
|
|
68
|
+
// SQL Runtime Context
|
|
69
|
+
// ============================================================================
|
|
70
|
+
|
|
71
|
+
export interface RuntimeContext<TContract extends SqlContract<SqlStorage> = SqlContract<SqlStorage>>
|
|
72
|
+
extends QueryLaneContext<TContract> {
|
|
73
|
+
readonly adapter:
|
|
74
|
+
| Adapter<QueryAst, TContract, LoweredStatement>
|
|
75
|
+
| Adapter<QueryAst, SqlContract<SqlStorage>, LoweredStatement>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Descriptor-first options for creating a SQL runtime context.
|
|
80
|
+
* Takes the same framework composition as control-plane: target, adapter, extensionPacks.
|
|
81
|
+
*
|
|
82
|
+
* @template TContract - The SQL contract type
|
|
83
|
+
* @template TTargetId - The target ID (e.g., 'postgres', 'mysql')
|
|
84
|
+
*/
|
|
85
|
+
export interface CreateRuntimeContextOptions<
|
|
86
|
+
TContract extends SqlContract<SqlStorage> = SqlContract<SqlStorage>,
|
|
87
|
+
TTargetId extends string = string,
|
|
88
|
+
> {
|
|
89
|
+
readonly contract: TContract;
|
|
90
|
+
readonly target: RuntimeTargetDescriptor<'sql', TTargetId>;
|
|
91
|
+
readonly adapter: RuntimeAdapterDescriptor<
|
|
92
|
+
'sql',
|
|
93
|
+
TTargetId,
|
|
94
|
+
SqlRuntimeAdapterInstance<TTargetId>
|
|
95
|
+
>;
|
|
96
|
+
readonly extensionPacks?: ReadonlyArray<SqlRuntimeExtensionDescriptor<TTargetId>>;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Creates a SQL runtime context from descriptor-first composition.
|
|
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)
|
|
107
|
+
*
|
|
108
|
+
* @param options - Descriptor-first composition options
|
|
109
|
+
* @returns RuntimeContext with registries wired from all components
|
|
110
|
+
*/
|
|
111
|
+
export function createRuntimeContext<
|
|
112
|
+
TContract extends SqlContract<SqlStorage> = SqlContract<SqlStorage>,
|
|
113
|
+
TTargetId extends string = string,
|
|
114
|
+
>(options: CreateRuntimeContextOptions<TContract, TTargetId>): RuntimeContext<TContract> {
|
|
115
|
+
const { contract, adapter: adapterDescriptor, extensionPacks } = options;
|
|
116
|
+
|
|
117
|
+
// Create adapter instance from descriptor
|
|
118
|
+
// The adapter instance IS an Adapter (via intersection)
|
|
119
|
+
const adapterInstance = adapterDescriptor.create();
|
|
120
|
+
|
|
121
|
+
// Create registries
|
|
122
|
+
const codecRegistry = createCodecRegistry();
|
|
123
|
+
const operationRegistry = createOperationRegistry();
|
|
124
|
+
|
|
125
|
+
// Register adapter codecs (adapter instance has profile.codecs())
|
|
126
|
+
const adapterCodecs = adapterInstance.profile.codecs();
|
|
127
|
+
for (const codec of adapterCodecs.values()) {
|
|
128
|
+
codecRegistry.register(codec);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Create extension instances and register their codecs/operations
|
|
132
|
+
for (const extDescriptor of extensionPacks ?? []) {
|
|
133
|
+
const extInstance = extDescriptor.create();
|
|
134
|
+
|
|
135
|
+
const extCodecs = extInstance.codecs?.();
|
|
136
|
+
if (extCodecs) {
|
|
137
|
+
for (const codec of extCodecs.values()) {
|
|
138
|
+
codecRegistry.register(codec);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const extOperations = extInstance.operations?.();
|
|
143
|
+
if (extOperations) {
|
|
144
|
+
for (const operation of extOperations) {
|
|
145
|
+
operationRegistry.register(operation);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
contract,
|
|
152
|
+
adapter: adapterInstance,
|
|
153
|
+
operations: operationRegistry,
|
|
154
|
+
codecs: codecRegistry,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { ExecutionPlan } from '@prisma-next/contract/types';
|
|
2
|
+
import type {
|
|
3
|
+
MarkerReader,
|
|
4
|
+
MarkerStatement,
|
|
5
|
+
RuntimeFamilyAdapter,
|
|
6
|
+
} from '@prisma-next/runtime-executor';
|
|
7
|
+
import { runtimeError } from '@prisma-next/runtime-executor';
|
|
8
|
+
import type { SqlContract, SqlStorage } from '@prisma-next/sql-contract/types';
|
|
9
|
+
import { readContractMarker } from './sql-marker';
|
|
10
|
+
|
|
11
|
+
class SqlMarkerReader implements MarkerReader {
|
|
12
|
+
readMarkerStatement(): MarkerStatement {
|
|
13
|
+
return readContractMarker();
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class SqlFamilyAdapter<TContract extends SqlContract<SqlStorage>>
|
|
18
|
+
implements RuntimeFamilyAdapter<TContract>
|
|
19
|
+
{
|
|
20
|
+
readonly contract: TContract;
|
|
21
|
+
readonly markerReader: MarkerReader;
|
|
22
|
+
|
|
23
|
+
constructor(contract: TContract) {
|
|
24
|
+
this.contract = contract;
|
|
25
|
+
this.markerReader = new SqlMarkerReader();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
validatePlan(plan: ExecutionPlan, contract: TContract): void {
|
|
29
|
+
if (plan.meta.target !== contract.target) {
|
|
30
|
+
throw runtimeError('PLAN.TARGET_MISMATCH', 'Plan target does not match runtime target', {
|
|
31
|
+
planTarget: plan.meta.target,
|
|
32
|
+
runtimeTarget: contract.target,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (plan.meta.coreHash !== contract.coreHash) {
|
|
37
|
+
throw runtimeError('PLAN.HASH_MISMATCH', 'Plan core hash does not match runtime contract', {
|
|
38
|
+
planCoreHash: plan.meta.coreHash,
|
|
39
|
+
runtimeCoreHash: contract.coreHash,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|