@prisma-next/sql-runtime 0.4.1 → 0.4.3
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 +29 -21
- package/dist/exports-CrHMfIKo.mjs +1564 -0
- package/dist/exports-CrHMfIKo.mjs.map +1 -0
- package/dist/{index-DyDQ4fyK.d.mts → index-_dXSGeho.d.mts} +112 -32
- package/dist/index-_dXSGeho.d.mts.map +1 -0
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +2 -2
- package/dist/test/utils.d.mts +6 -5
- package/dist/test/utils.d.mts.map +1 -1
- package/dist/test/utils.mjs +16 -13
- package/dist/test/utils.mjs.map +1 -1
- package/package.json +12 -14
- package/src/codecs/decoding.ts +294 -173
- package/src/codecs/encoding.ts +162 -37
- package/src/codecs/validation.ts +22 -3
- package/src/exports/index.ts +11 -7
- package/src/fingerprint.ts +22 -0
- package/src/guardrails/raw.ts +165 -0
- package/src/lower-sql-plan.ts +5 -7
- package/src/marker.ts +75 -0
- package/src/middleware/before-compile-chain.ts +29 -0
- package/src/middleware/budgets.ts +34 -115
- package/src/middleware/lints.ts +5 -5
- package/src/middleware/sql-middleware.ts +36 -6
- package/src/runtime-spi.ts +44 -0
- package/src/sql-context.ts +332 -78
- package/src/sql-family-adapter.ts +3 -2
- package/src/sql-marker.ts +62 -47
- package/src/sql-runtime.ts +339 -104
- package/dist/exports-Cv7I7ZD5.mjs +0 -953
- package/dist/exports-Cv7I7ZD5.mjs.map +0 -1
- package/dist/index-DyDQ4fyK.d.mts.map +0 -1
- package/test/async-iterable-result.test.ts +0 -141
- package/test/budgets.test.ts +0 -431
- package/test/context.types.test-d.ts +0 -68
- package/test/execution-stack.test.ts +0 -164
- package/test/json-schema-validation.test.ts +0 -571
- package/test/lints.test.ts +0 -159
- package/test/mutation-default-generators.test.ts +0 -254
- package/test/parameterized-types.test.ts +0 -529
- package/test/sql-context.test.ts +0 -384
- package/test/sql-family-adapter.test.ts +0 -103
- package/test/sql-runtime.test.ts +0 -637
- package/test/utils.ts +0 -300
package/test/utils.ts
DELETED
|
@@ -1,300 +0,0 @@
|
|
|
1
|
-
import type { Contract, ExecutionPlan, ResultType } from '@prisma-next/contract/types';
|
|
2
|
-
import { coreHash, profileHash } from '@prisma-next/contract/types';
|
|
3
|
-
import {
|
|
4
|
-
instantiateExecutionStack,
|
|
5
|
-
type RuntimeDriverDescriptor,
|
|
6
|
-
} from '@prisma-next/framework-components/execution';
|
|
7
|
-
import { builtinGeneratorIds } from '@prisma-next/ids';
|
|
8
|
-
import { generateId } from '@prisma-next/ids/runtime';
|
|
9
|
-
import type { SqlStorage } from '@prisma-next/sql-contract/types';
|
|
10
|
-
import type { Adapter, LoweredStatement, SelectAst } from '@prisma-next/sql-relational-core/ast';
|
|
11
|
-
import { codec, createCodecRegistry } from '@prisma-next/sql-relational-core/ast';
|
|
12
|
-
import type { SqlQueryPlan } from '@prisma-next/sql-relational-core/plan';
|
|
13
|
-
import { collectAsync, drainAsyncIterable } from '@prisma-next/test-utils';
|
|
14
|
-
import type { Client } from 'pg';
|
|
15
|
-
import type { SqlStatement } from '../src/exports';
|
|
16
|
-
import {
|
|
17
|
-
createExecutionContext,
|
|
18
|
-
type createRuntime,
|
|
19
|
-
createSqlExecutionStack,
|
|
20
|
-
ensureSchemaStatement,
|
|
21
|
-
ensureTableStatement,
|
|
22
|
-
writeContractMarker,
|
|
23
|
-
} from '../src/exports';
|
|
24
|
-
import type {
|
|
25
|
-
ExecutionContext,
|
|
26
|
-
SqlRuntimeAdapterDescriptor,
|
|
27
|
-
SqlRuntimeAdapterInstance,
|
|
28
|
-
SqlRuntimeDriverInstance,
|
|
29
|
-
SqlRuntimeExtensionDescriptor,
|
|
30
|
-
SqlRuntimeTargetDescriptor,
|
|
31
|
-
} from '../src/sql-context';
|
|
32
|
-
|
|
33
|
-
function createTestMutationDefaultGenerators() {
|
|
34
|
-
return builtinGeneratorIds.map((id) => ({
|
|
35
|
-
id,
|
|
36
|
-
generate: (params?: Record<string, unknown>) => generateId(params ? { id, params } : { id }),
|
|
37
|
-
}));
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Executes a plan and collects all results into an array.
|
|
42
|
-
* This helper DRYs up the common pattern of executing plans in tests.
|
|
43
|
-
* The return type is inferred from the plan's type parameter.
|
|
44
|
-
*/
|
|
45
|
-
export async function executePlanAndCollect<
|
|
46
|
-
P extends ExecutionPlan<ResultType<P>> | SqlQueryPlan<ResultType<P>>,
|
|
47
|
-
>(runtime: ReturnType<typeof createRuntime>, plan: P): Promise<ResultType<P>[]> {
|
|
48
|
-
type Row = ResultType<P>;
|
|
49
|
-
return collectAsync<Row>(runtime.execute<Row>(plan));
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Drains a plan execution, consuming all results without collecting them.
|
|
54
|
-
* Useful for testing side effects without memory overhead.
|
|
55
|
-
*/
|
|
56
|
-
export async function drainPlanExecution(
|
|
57
|
-
runtime: ReturnType<typeof createRuntime>,
|
|
58
|
-
plan: ExecutionPlan | SqlQueryPlan<unknown>,
|
|
59
|
-
): Promise<void> {
|
|
60
|
-
return drainAsyncIterable(runtime.execute(plan));
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Executes a SQL statement on a database client.
|
|
65
|
-
*/
|
|
66
|
-
export async function executeStatement(client: Client, statement: SqlStatement): Promise<void> {
|
|
67
|
-
if (statement.params.length > 0) {
|
|
68
|
-
await client.query(statement.sql, [...statement.params]);
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
await client.query(statement.sql);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Sets up database schema and data, then writes the contract marker.
|
|
77
|
-
* This helper DRYs up the common pattern of database setup in tests.
|
|
78
|
-
*/
|
|
79
|
-
export async function setupTestDatabase(
|
|
80
|
-
client: Client,
|
|
81
|
-
contract: Contract<SqlStorage>,
|
|
82
|
-
setupFn: (client: Client) => Promise<void>,
|
|
83
|
-
): Promise<void> {
|
|
84
|
-
await client.query('drop schema if exists prisma_contract cascade');
|
|
85
|
-
await client.query('create schema if not exists public');
|
|
86
|
-
|
|
87
|
-
await setupFn(client);
|
|
88
|
-
|
|
89
|
-
await executeStatement(client, ensureSchemaStatement);
|
|
90
|
-
await executeStatement(client, ensureTableStatement);
|
|
91
|
-
const write = writeContractMarker({
|
|
92
|
-
storageHash: contract.storage.storageHash,
|
|
93
|
-
profileHash: contract.profileHash,
|
|
94
|
-
contractJson: contract,
|
|
95
|
-
canonicalVersion: 1,
|
|
96
|
-
});
|
|
97
|
-
await executeStatement(client, write.insert);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Writes a contract marker to the database.
|
|
102
|
-
* This helper DRYs up the common pattern of writing contract markers in tests.
|
|
103
|
-
*/
|
|
104
|
-
export async function writeTestContractMarker(
|
|
105
|
-
client: Client,
|
|
106
|
-
contract: Contract<SqlStorage>,
|
|
107
|
-
): Promise<void> {
|
|
108
|
-
const write = writeContractMarker({
|
|
109
|
-
storageHash: contract.storage.storageHash,
|
|
110
|
-
profileHash: contract.profileHash,
|
|
111
|
-
contractJson: contract,
|
|
112
|
-
canonicalVersion: 1,
|
|
113
|
-
});
|
|
114
|
-
await executeStatement(client, write.insert);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Creates a test adapter descriptor from a raw adapter.
|
|
119
|
-
* Wraps the adapter in an SqlRuntimeAdapterDescriptor with static contributions
|
|
120
|
-
* derived from the adapter's codec registry.
|
|
121
|
-
*/
|
|
122
|
-
export function createTestAdapterDescriptor(
|
|
123
|
-
adapter: Adapter<SelectAst, Contract<SqlStorage>, LoweredStatement>,
|
|
124
|
-
): SqlRuntimeAdapterDescriptor<'postgres'> {
|
|
125
|
-
const codecRegistry = adapter.profile.codecs();
|
|
126
|
-
return {
|
|
127
|
-
kind: 'adapter' as const,
|
|
128
|
-
id: 'test-adapter',
|
|
129
|
-
version: '0.0.1',
|
|
130
|
-
familyId: 'sql' as const,
|
|
131
|
-
targetId: 'postgres' as const,
|
|
132
|
-
codecs: () => codecRegistry,
|
|
133
|
-
parameterizedCodecs: () => [],
|
|
134
|
-
mutationDefaultGenerators: createTestMutationDefaultGenerators,
|
|
135
|
-
create(): SqlRuntimeAdapterInstance<'postgres'> {
|
|
136
|
-
return Object.assign({ familyId: 'sql' as const, targetId: 'postgres' as const }, adapter);
|
|
137
|
-
},
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Creates a test target descriptor with empty static contributions.
|
|
143
|
-
*/
|
|
144
|
-
export function createTestTargetDescriptor(): SqlRuntimeTargetDescriptor<'postgres'> {
|
|
145
|
-
return {
|
|
146
|
-
kind: 'target' as const,
|
|
147
|
-
id: 'postgres',
|
|
148
|
-
version: '0.0.1',
|
|
149
|
-
familyId: 'sql' as const,
|
|
150
|
-
targetId: 'postgres' as const,
|
|
151
|
-
codecs: () => createCodecRegistry(),
|
|
152
|
-
parameterizedCodecs: () => [],
|
|
153
|
-
create() {
|
|
154
|
-
return { familyId: 'sql' as const, targetId: 'postgres' as const };
|
|
155
|
-
},
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Creates an ExecutionContext for testing.
|
|
161
|
-
* This helper DRYs up the common pattern of context creation in tests.
|
|
162
|
-
*
|
|
163
|
-
* Accepts a raw adapter and optional extension descriptors, wrapping the
|
|
164
|
-
* adapter in a descriptor internally for descriptor-first context creation.
|
|
165
|
-
*/
|
|
166
|
-
export function createTestContext<TContract extends Contract<SqlStorage>>(
|
|
167
|
-
contract: TContract,
|
|
168
|
-
adapter: Adapter<SelectAst, Contract<SqlStorage>, LoweredStatement>,
|
|
169
|
-
options?: {
|
|
170
|
-
extensionPacks?: ReadonlyArray<SqlRuntimeExtensionDescriptor<'postgres'>>;
|
|
171
|
-
},
|
|
172
|
-
): ExecutionContext<TContract> {
|
|
173
|
-
return createExecutionContext({
|
|
174
|
-
contract,
|
|
175
|
-
stack: {
|
|
176
|
-
target: createTestTargetDescriptor(),
|
|
177
|
-
adapter: createTestAdapterDescriptor(adapter),
|
|
178
|
-
extensionPacks: options?.extensionPacks ?? [],
|
|
179
|
-
},
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
export function createTestStackInstance(options?: {
|
|
184
|
-
extensionPacks?: ReadonlyArray<SqlRuntimeExtensionDescriptor<'postgres'>>;
|
|
185
|
-
driver?: RuntimeDriverDescriptor<
|
|
186
|
-
'sql',
|
|
187
|
-
'postgres',
|
|
188
|
-
unknown,
|
|
189
|
-
SqlRuntimeDriverInstance<'postgres'>
|
|
190
|
-
>;
|
|
191
|
-
}) {
|
|
192
|
-
const stack = createSqlExecutionStack({
|
|
193
|
-
target: createTestTargetDescriptor(),
|
|
194
|
-
adapter: createTestAdapterDescriptor(createStubAdapter()),
|
|
195
|
-
driver: options?.driver,
|
|
196
|
-
extensionPacks: options?.extensionPacks ?? [],
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
return instantiateExecutionStack(stack);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Creates a stub adapter for testing.
|
|
204
|
-
* This helper DRYs up the common pattern of adapter creation in tests.
|
|
205
|
-
*
|
|
206
|
-
* The stub adapter includes simple codecs for common test types (pg/int4@1, pg/text@1, pg/timestamptz@1)
|
|
207
|
-
* to enable type inference in tests without requiring the postgres adapter package.
|
|
208
|
-
*/
|
|
209
|
-
export function createStubAdapter(): Adapter<SelectAst, Contract<SqlStorage>, LoweredStatement> {
|
|
210
|
-
const codecRegistry = createCodecRegistry();
|
|
211
|
-
|
|
212
|
-
// Register stub codecs for common test types
|
|
213
|
-
// These match the codec IDs used in test contracts (pg/int4@1, pg/text@1, pg/timestamptz@1)
|
|
214
|
-
// but don't require importing from the postgres adapter package
|
|
215
|
-
codecRegistry.register(
|
|
216
|
-
codec({
|
|
217
|
-
typeId: 'pg/int4@1',
|
|
218
|
-
targetTypes: ['int4'],
|
|
219
|
-
encode: (value: number) => value,
|
|
220
|
-
decode: (wire: number) => wire,
|
|
221
|
-
}),
|
|
222
|
-
);
|
|
223
|
-
|
|
224
|
-
codecRegistry.register(
|
|
225
|
-
codec({
|
|
226
|
-
typeId: 'pg/text@1',
|
|
227
|
-
targetTypes: ['text'],
|
|
228
|
-
encode: (value: string) => value,
|
|
229
|
-
decode: (wire: string) => wire,
|
|
230
|
-
}),
|
|
231
|
-
);
|
|
232
|
-
|
|
233
|
-
codecRegistry.register(
|
|
234
|
-
codec({
|
|
235
|
-
typeId: 'pg/timestamptz@1',
|
|
236
|
-
targetTypes: ['timestamptz'],
|
|
237
|
-
encode: (value: string | Date) => (value instanceof Date ? value.toISOString() : value),
|
|
238
|
-
decode: (wire: string | Date) => (wire instanceof Date ? wire : new Date(wire)),
|
|
239
|
-
}),
|
|
240
|
-
);
|
|
241
|
-
|
|
242
|
-
return {
|
|
243
|
-
profile: {
|
|
244
|
-
id: 'stub-profile',
|
|
245
|
-
target: 'postgres',
|
|
246
|
-
capabilities: {},
|
|
247
|
-
codecs() {
|
|
248
|
-
return codecRegistry;
|
|
249
|
-
},
|
|
250
|
-
readMarkerStatement() {
|
|
251
|
-
return {
|
|
252
|
-
sql: 'select core_hash, profile_hash, contract_json, canonical_version, updated_at, app_tag, meta from prisma_contract.marker where id = $1',
|
|
253
|
-
params: [1],
|
|
254
|
-
};
|
|
255
|
-
},
|
|
256
|
-
},
|
|
257
|
-
lower(ast: SelectAst, ctx: { contract: Contract<SqlStorage>; params?: readonly unknown[] }) {
|
|
258
|
-
const sqlText = JSON.stringify(ast);
|
|
259
|
-
return {
|
|
260
|
-
profileId: this.profile.id,
|
|
261
|
-
body: Object.freeze({ sql: sqlText, params: ctx.params ? [...ctx.params] : [] }),
|
|
262
|
-
};
|
|
263
|
-
},
|
|
264
|
-
};
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
export function createTestContract(
|
|
268
|
-
contract: Partial<Omit<Contract<SqlStorage>, 'profileHash' | 'storage'>> & {
|
|
269
|
-
storageHash?: string;
|
|
270
|
-
profileHash?: string;
|
|
271
|
-
storage?: Omit<SqlStorage, 'storageHash'>;
|
|
272
|
-
},
|
|
273
|
-
): Contract<SqlStorage> {
|
|
274
|
-
const { execution, ...rest } = contract;
|
|
275
|
-
const storageHashValue = coreHash(rest['storageHash'] ?? 'sha256:testcore');
|
|
276
|
-
|
|
277
|
-
return {
|
|
278
|
-
target: rest['target'] ?? 'postgres',
|
|
279
|
-
targetFamily: rest['targetFamily'] ?? 'sql',
|
|
280
|
-
storage: rest['storage']
|
|
281
|
-
? { ...rest['storage'], storageHash: storageHashValue }
|
|
282
|
-
: { storageHash: storageHashValue, tables: {} },
|
|
283
|
-
models: rest['models'] ?? {},
|
|
284
|
-
roots: rest['roots'] ?? {},
|
|
285
|
-
capabilities: rest['capabilities'] ?? {},
|
|
286
|
-
extensionPacks: rest['extensionPacks'] ?? {},
|
|
287
|
-
meta: rest['meta'] ?? {},
|
|
288
|
-
...(execution ? { execution } : {}),
|
|
289
|
-
profileHash: profileHash(rest['profileHash'] ?? 'sha256:testprofile'),
|
|
290
|
-
};
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
// Re-export generic utilities from test-utils
|
|
294
|
-
export {
|
|
295
|
-
collectAsync,
|
|
296
|
-
createDevDatabase,
|
|
297
|
-
type DevDatabase,
|
|
298
|
-
teardownTestDatabase,
|
|
299
|
-
withClient,
|
|
300
|
-
} from '@prisma-next/test-utils';
|