@prisma-next/contract 0.3.0-dev.135 → 0.3.0-dev.137

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.
@@ -1,552 +0,0 @@
1
- import type { AuthoringContributions } from './framework-authoring';
2
- import type { RenderTypeContext, TypesImportSpec } from './types';
3
-
4
- /**
5
- * A template-based type renderer (structured form).
6
- * Uses mustache-style placeholders (e.g., `Vector<{{length}}>`) that are
7
- * replaced with typeParams values during rendering.
8
- *
9
- * @example
10
- * ```ts
11
- * { kind: 'template', template: 'Vector<{{length}}>' }
12
- * // With typeParams { length: 1536 }, renders: 'Vector<1536>'
13
- * ```
14
- */
15
- export interface TypeRendererTemplate {
16
- readonly kind: 'template';
17
- /** Template string with `{{key}}` placeholders for typeParams values */
18
- readonly template: string;
19
- }
20
-
21
- /**
22
- * A function-based type renderer for full control over type expression generation.
23
- *
24
- * @example
25
- * ```ts
26
- * {
27
- * kind: 'function',
28
- * render: (params, ctx) => `Vector<${params.length}>`
29
- * }
30
- * ```
31
- */
32
- export interface TypeRendererFunction {
33
- readonly kind: 'function';
34
- /** Render function that produces a TypeScript type expression */
35
- readonly render: (params: Record<string, unknown>, ctx: RenderTypeContext) => string;
36
- }
37
-
38
- /**
39
- * A raw template string type renderer (convenience form).
40
- * Shorthand for TypeRendererTemplate - just the template string without wrapper.
41
- *
42
- * @example
43
- * ```ts
44
- * 'Vector<{{length}}>'
45
- * // Equivalent to: { kind: 'template', template: 'Vector<{{length}}>' }
46
- * ```
47
- */
48
- export type TypeRendererString = string;
49
-
50
- /**
51
- * A raw function type renderer (convenience form).
52
- * Shorthand for TypeRendererFunction - just the function without wrapper.
53
- *
54
- * @example
55
- * ```ts
56
- * (params, ctx) => `Vector<${params.length}>`
57
- * // Equivalent to: { kind: 'function', render: ... }
58
- * ```
59
- */
60
- export type TypeRendererRawFunction = (
61
- params: Record<string, unknown>,
62
- ctx: RenderTypeContext,
63
- ) => string;
64
-
65
- /**
66
- * Union of type renderer formats.
67
- *
68
- * Supports both structured forms (with `kind` discriminator) and convenience forms:
69
- * - `string` - Template string with `{{key}}` placeholders (manifest-safe, JSON-serializable)
70
- * - `function` - Render function for full control (requires runtime execution)
71
- * - `{ kind: 'template', template: string }` - Structured template form
72
- * - `{ kind: 'function', render: fn }` - Structured function form
73
- *
74
- * Templates are normalized to functions during pack assembly.
75
- * **Prefer template strings** for most cases - they are JSON-serializable.
76
- */
77
- export type TypeRenderer =
78
- | TypeRendererString
79
- | TypeRendererRawFunction
80
- | TypeRendererTemplate
81
- | TypeRendererFunction;
82
-
83
- /**
84
- * Normalized type renderer - always a function after assembly.
85
- * This is the form received by the emitter.
86
- */
87
- export interface NormalizedTypeRenderer {
88
- readonly codecId: string;
89
- readonly render: (params: Record<string, unknown>, ctx: RenderTypeContext) => string;
90
- }
91
-
92
- /**
93
- * Interpolates a template string with params values.
94
- * Used internally by normalizeRenderer to compile templates to functions.
95
- *
96
- * @throws Error if a placeholder key is not found in params (except 'CodecTypes')
97
- */
98
- export function interpolateTypeTemplate(
99
- template: string,
100
- params: Record<string, unknown>,
101
- ctx: RenderTypeContext,
102
- ): string {
103
- return template.replace(/\{\{(\w+)\}\}/g, (_, key: string) => {
104
- if (key === 'CodecTypes') return ctx.codecTypesName;
105
- const value = params[key];
106
- if (value === undefined) {
107
- throw new Error(
108
- `Missing template parameter "${key}" in template "${template}". ` +
109
- `Available params: ${Object.keys(params).join(', ') || '(none)'}`,
110
- );
111
- }
112
- return String(value);
113
- });
114
- }
115
-
116
- /**
117
- * Normalizes a TypeRenderer to function form.
118
- * Called during pack assembly, not at emission time.
119
- *
120
- * Handles all TypeRenderer forms:
121
- * - Raw string template: `'Vector<{{length}}>'`
122
- * - Raw function: `(params, ctx) => ...`
123
- * - Structured template: `{ kind: 'template', template: '...' }`
124
- * - Structured function: `{ kind: 'function', render: fn }`
125
- */
126
- export function normalizeRenderer(codecId: string, renderer: TypeRenderer): NormalizedTypeRenderer {
127
- // Handle raw string (template shorthand)
128
- if (typeof renderer === 'string') {
129
- return {
130
- codecId,
131
- render: (params, ctx) => interpolateTypeTemplate(renderer, params, ctx),
132
- };
133
- }
134
-
135
- // Handle raw function (function shorthand)
136
- if (typeof renderer === 'function') {
137
- return { codecId, render: renderer };
138
- }
139
-
140
- // Handle structured function form
141
- if (renderer.kind === 'function') {
142
- return { codecId, render: renderer.render };
143
- }
144
-
145
- // Handle structured template form
146
- const { template } = renderer;
147
- return {
148
- codecId,
149
- render: (params, ctx) => interpolateTypeTemplate(template, params, ctx),
150
- };
151
- }
152
-
153
- /**
154
- * Declarative fields that describe component metadata.
155
- */
156
- export interface ComponentMetadata {
157
- /** Component version (semver) */
158
- readonly version: string;
159
-
160
- /**
161
- * Capabilities this component provides.
162
- *
163
- * For adapters, capabilities must be declared on the adapter descriptor (so they are emitted into
164
- * the contract) and also exposed in runtime adapter code (e.g. `adapter.profile.capabilities`);
165
- * keep these declarations in sync. Targets are identifiers/descriptors and typically do not
166
- * declare capabilities.
167
- */
168
- readonly capabilities?: Record<string, unknown>;
169
-
170
- /** Type imports for contract.d.ts generation */
171
- readonly types?: {
172
- readonly codecTypes?: {
173
- /**
174
- * Base codec types import spec.
175
- * Optional: adapters typically provide this, extensions usually don't.
176
- */
177
- readonly import?: TypesImportSpec;
178
- /**
179
- * Optional renderers for parameterized codecs owned by this component.
180
- * Key is codecId (e.g., 'pg/vector@1'), value is the type renderer.
181
- *
182
- * Templates are normalized to functions during pack assembly.
183
- * Duplicate codecId across descriptors is a hard error.
184
- */
185
- readonly parameterized?: Record<string, TypeRenderer>;
186
- /**
187
- * Optional additional type-only imports required by parameterized renderers.
188
- *
189
- * These imports are included in generated `contract.d.ts` but are NOT treated as
190
- * codec type maps (i.e., they should not be intersected into `export type CodecTypes = ...`).
191
- *
192
- * Example: `Vector<N>` for pgvector renderers that emit `Vector<{{length}}>`
193
- */
194
- readonly typeImports?: ReadonlyArray<TypesImportSpec>;
195
- /**
196
- * Optional control-plane hooks keyed by codecId.
197
- * Used by family-specific planners/verifiers to handle storage types.
198
- */
199
- readonly controlPlaneHooks?: Record<string, unknown>;
200
- };
201
- readonly operationTypes?: { readonly import: TypesImportSpec };
202
- readonly storage?: ReadonlyArray<{
203
- readonly typeId: string;
204
- readonly familyId: string;
205
- readonly targetId: string;
206
- readonly nativeType?: string;
207
- }>;
208
- };
209
-
210
- /**
211
- * Optional pure-data authoring contributions exposed by this component.
212
- *
213
- * These contributions are safe to include on pack refs and descriptors because
214
- * they contain only declarative metadata. Higher-level authoring packages may
215
- * project them into concrete helper functions for TS-first workflows.
216
- */
217
- readonly authoring?: AuthoringContributions;
218
- }
219
-
220
- /**
221
- * Base descriptor for any framework component.
222
- *
223
- * All component descriptors share these fundamental properties that identify
224
- * the component and provide its metadata. This interface is extended by
225
- * specific descriptor types (FamilyDescriptor, TargetDescriptor, etc.).
226
- *
227
- * @template Kind - Discriminator literal identifying the component type.
228
- * Built-in kinds are 'family', 'target', 'adapter', 'driver', 'extension',
229
- * but the type accepts any string to allow ecosystem extensions.
230
- *
231
- * @example
232
- * ```ts
233
- * // All descriptors have these properties
234
- * descriptor.kind // The Kind type parameter (e.g., 'family', 'target', or custom kinds)
235
- * descriptor.id // Unique string identifier (e.g., 'sql', 'postgres')
236
- * descriptor.version // Component version (semver)
237
- * ```
238
- */
239
- export interface ComponentDescriptor<Kind extends string> extends ComponentMetadata {
240
- /** Discriminator identifying the component type */
241
- readonly kind: Kind;
242
-
243
- /** Unique identifier for this component (e.g., 'sql', 'postgres', 'pgvector') */
244
- readonly id: string;
245
- }
246
-
247
- export interface ContractComponentRequirementsCheckInput {
248
- readonly contract: {
249
- readonly target: string;
250
- readonly targetFamily?: string | undefined;
251
- readonly extensionPacks?: Record<string, unknown> | undefined;
252
- };
253
- readonly expectedTargetFamily?: string | undefined;
254
- readonly expectedTargetId?: string | undefined;
255
- readonly providedComponentIds: Iterable<string>;
256
- }
257
-
258
- export interface ContractComponentRequirementsCheckResult {
259
- readonly familyMismatch?: { readonly expected: string; readonly actual: string } | undefined;
260
- readonly targetMismatch?: { readonly expected: string; readonly actual: string } | undefined;
261
- readonly missingExtensionPackIds: readonly string[];
262
- }
263
-
264
- export function checkContractComponentRequirements(
265
- input: ContractComponentRequirementsCheckInput,
266
- ): ContractComponentRequirementsCheckResult {
267
- const providedIds = new Set<string>();
268
- for (const id of input.providedComponentIds) {
269
- providedIds.add(id);
270
- }
271
-
272
- const requiredExtensionPackIds = input.contract.extensionPacks
273
- ? Object.keys(input.contract.extensionPacks)
274
- : [];
275
- const missingExtensionPackIds = requiredExtensionPackIds.filter((id) => !providedIds.has(id));
276
-
277
- const expectedTargetFamily = input.expectedTargetFamily;
278
- const contractTargetFamily = input.contract.targetFamily;
279
- const familyMismatch =
280
- expectedTargetFamily !== undefined &&
281
- contractTargetFamily !== undefined &&
282
- contractTargetFamily !== expectedTargetFamily
283
- ? { expected: expectedTargetFamily, actual: contractTargetFamily }
284
- : undefined;
285
-
286
- const expectedTargetId = input.expectedTargetId;
287
- const contractTargetId = input.contract.target;
288
- const targetMismatch =
289
- expectedTargetId !== undefined && contractTargetId !== expectedTargetId
290
- ? { expected: expectedTargetId, actual: contractTargetId }
291
- : undefined;
292
-
293
- return {
294
- ...(familyMismatch ? { familyMismatch } : {}),
295
- ...(targetMismatch ? { targetMismatch } : {}),
296
- missingExtensionPackIds,
297
- };
298
- }
299
-
300
- /**
301
- * Descriptor for a family component.
302
- *
303
- * A "family" represents a category of data sources with shared semantics
304
- * (e.g., SQL databases, document stores). Families define:
305
- * - Query semantics and operations (SELECT, INSERT, find, aggregate, etc.)
306
- * - Contract structure (tables vs collections, columns vs fields)
307
- * - Type system and codecs
308
- *
309
- * Families are the top-level grouping. Each family contains multiple targets
310
- * (e.g., SQL family contains Postgres, MySQL, SQLite targets).
311
- *
312
- * Extended by plane-specific descriptors:
313
- * - `ControlFamilyDescriptor` - adds `hook` for CLI/tooling operations
314
- * - `RuntimeFamilyDescriptor` - adds runtime-specific factory methods
315
- *
316
- * @template TFamilyId - Literal type for the family identifier (e.g., 'sql', 'document')
317
- *
318
- * @example
319
- * ```ts
320
- * import sql from '@prisma-next/family-sql/control';
321
- *
322
- * sql.kind // 'family'
323
- * sql.familyId // 'sql'
324
- * sql.id // 'sql'
325
- * ```
326
- */
327
- export interface FamilyDescriptor<TFamilyId extends string> extends ComponentDescriptor<'family'> {
328
- /** The family identifier (e.g., 'sql', 'document') */
329
- readonly familyId: TFamilyId;
330
- }
331
-
332
- /**
333
- * Descriptor for a target component.
334
- *
335
- * A "target" represents a specific database or data store within a family
336
- * (e.g., Postgres, MySQL, MongoDB). Targets define:
337
- * - Native type mappings (e.g., Postgres int4 → TypeScript number)
338
- * - Target-specific capabilities (e.g., RETURNING, LATERAL joins)
339
- *
340
- * Targets are bound to a family and provide the target-specific implementation
341
- * details that adapters and drivers use.
342
- *
343
- * Extended by plane-specific descriptors:
344
- * - `ControlTargetDescriptor` - adds optional `migrations` capability
345
- * - `RuntimeTargetDescriptor` - adds runtime factory method
346
- *
347
- * @template TFamilyId - Literal type for the family identifier
348
- * @template TTargetId - Literal type for the target identifier (e.g., 'postgres', 'mysql')
349
- *
350
- * @example
351
- * ```ts
352
- * import postgres from '@prisma-next/target-postgres/control';
353
- *
354
- * postgres.kind // 'target'
355
- * postgres.familyId // 'sql'
356
- * postgres.targetId // 'postgres'
357
- * ```
358
- */
359
- export interface TargetDescriptor<TFamilyId extends string, TTargetId extends string>
360
- extends ComponentDescriptor<'target'> {
361
- /** The family this target belongs to */
362
- readonly familyId: TFamilyId;
363
-
364
- /** The target identifier (e.g., 'postgres', 'mysql', 'mongodb') */
365
- readonly targetId: TTargetId;
366
- }
367
-
368
- /**
369
- * Base shape for any pack reference.
370
- * Pack refs are pure JSON-friendly objects safe to import in authoring flows.
371
- */
372
- export interface PackRefBase<Kind extends string, TFamilyId extends string>
373
- extends ComponentMetadata {
374
- readonly kind: Kind;
375
- readonly id: string;
376
- readonly familyId: TFamilyId;
377
- readonly targetId?: string;
378
- readonly authoring?: AuthoringContributions;
379
- }
380
-
381
- export type FamilyPackRef<TFamilyId extends string = string> = PackRefBase<'family', TFamilyId>;
382
-
383
- export type TargetPackRef<
384
- TFamilyId extends string = string,
385
- TTargetId extends string = string,
386
- > = PackRefBase<'target', TFamilyId> & {
387
- readonly targetId: TTargetId;
388
- };
389
-
390
- export type AdapterPackRef<
391
- TFamilyId extends string = string,
392
- TTargetId extends string = string,
393
- > = PackRefBase<'adapter', TFamilyId> & {
394
- readonly targetId: TTargetId;
395
- };
396
-
397
- export type ExtensionPackRef<
398
- TFamilyId extends string = string,
399
- TTargetId extends string = string,
400
- > = PackRefBase<'extension', TFamilyId> & {
401
- readonly targetId: TTargetId;
402
- };
403
-
404
- export type DriverPackRef<
405
- TFamilyId extends string = string,
406
- TTargetId extends string = string,
407
- > = PackRefBase<'driver', TFamilyId> & {
408
- readonly targetId: TTargetId;
409
- };
410
-
411
- /**
412
- * Descriptor for an adapter component.
413
- *
414
- * An "adapter" provides the protocol and dialect implementation for a target.
415
- * Adapters handle:
416
- * - SQL/query generation (lowering AST to target-specific syntax)
417
- * - Codec registration (encoding/decoding between JS and wire types)
418
- * - Type mappings and coercions
419
- *
420
- * Adapters are bound to a specific family+target combination and work with
421
- * any compatible driver for that target.
422
- *
423
- * Extended by plane-specific descriptors:
424
- * - `ControlAdapterDescriptor` - control-plane factory
425
- * - `RuntimeAdapterDescriptor` - runtime factory
426
- *
427
- * @template TFamilyId - Literal type for the family identifier
428
- * @template TTargetId - Literal type for the target identifier
429
- *
430
- * @example
431
- * ```ts
432
- * import postgresAdapter from '@prisma-next/adapter-postgres/control';
433
- *
434
- * postgresAdapter.kind // 'adapter'
435
- * postgresAdapter.familyId // 'sql'
436
- * postgresAdapter.targetId // 'postgres'
437
- * ```
438
- */
439
- export interface AdapterDescriptor<TFamilyId extends string, TTargetId extends string>
440
- extends ComponentDescriptor<'adapter'> {
441
- /** The family this adapter belongs to */
442
- readonly familyId: TFamilyId;
443
-
444
- /** The target this adapter is designed for */
445
- readonly targetId: TTargetId;
446
- }
447
-
448
- /**
449
- * Descriptor for a driver component.
450
- *
451
- * A "driver" provides the connection and execution layer for a target.
452
- * Drivers handle:
453
- * - Connection management (pooling, timeouts, retries)
454
- * - Query execution (sending SQL/commands, receiving results)
455
- * - Transaction management
456
- * - Wire protocol communication
457
- *
458
- * Drivers are bound to a specific family+target and work with any compatible
459
- * adapter. Multiple drivers can exist for the same target (e.g., node-postgres
460
- * vs postgres.js for Postgres).
461
- *
462
- * Extended by plane-specific descriptors:
463
- * - `ControlDriverDescriptor` - creates driver from connection URL
464
- * - `RuntimeDriverDescriptor` - creates driver with runtime options
465
- *
466
- * @template TFamilyId - Literal type for the family identifier
467
- * @template TTargetId - Literal type for the target identifier
468
- *
469
- * @example
470
- * ```ts
471
- * import postgresDriver from '@prisma-next/driver-postgres/control';
472
- *
473
- * postgresDriver.kind // 'driver'
474
- * postgresDriver.familyId // 'sql'
475
- * postgresDriver.targetId // 'postgres'
476
- * ```
477
- */
478
- export interface DriverDescriptor<TFamilyId extends string, TTargetId extends string>
479
- extends ComponentDescriptor<'driver'> {
480
- /** The family this driver belongs to */
481
- readonly familyId: TFamilyId;
482
-
483
- /** The target this driver connects to */
484
- readonly targetId: TTargetId;
485
- }
486
-
487
- /**
488
- * Descriptor for an extension component.
489
- *
490
- * An "extension" adds optional capabilities to a target. Extensions can provide:
491
- * - Additional operations (e.g., vector similarity search with pgvector)
492
- * - Custom types and codecs (e.g., vector type)
493
- * - Extended query capabilities
494
- *
495
- * Extensions are bound to a specific family+target and are registered in the
496
- * config alongside the core components. Multiple extensions can be used together.
497
- *
498
- * Extended by plane-specific descriptors:
499
- * - `ControlExtensionDescriptor` - control-plane extension factory
500
- * - `RuntimeExtensionDescriptor` - runtime extension factory
501
- *
502
- * @template TFamilyId - Literal type for the family identifier
503
- * @template TTargetId - Literal type for the target identifier
504
- *
505
- * @example
506
- * ```ts
507
- * import pgvector from '@prisma-next/extension-pgvector/control';
508
- *
509
- * pgvector.kind // 'extension'
510
- * pgvector.familyId // 'sql'
511
- * pgvector.targetId // 'postgres'
512
- * ```
513
- */
514
- export interface ExtensionDescriptor<TFamilyId extends string, TTargetId extends string>
515
- extends ComponentDescriptor<'extension'> {
516
- /** The family this extension belongs to */
517
- readonly familyId: TFamilyId;
518
-
519
- /** The target this extension is designed for */
520
- readonly targetId: TTargetId;
521
- }
522
-
523
- /** Components bound to a specific family+target combination. */
524
- export type TargetBoundComponentDescriptor<TFamilyId extends string, TTargetId extends string> =
525
- | TargetDescriptor<TFamilyId, TTargetId>
526
- | AdapterDescriptor<TFamilyId, TTargetId>
527
- | DriverDescriptor<TFamilyId, TTargetId>
528
- | ExtensionDescriptor<TFamilyId, TTargetId>;
529
-
530
- export interface FamilyInstance<TFamilyId extends string> {
531
- readonly familyId: TFamilyId;
532
- }
533
-
534
- export interface TargetInstance<TFamilyId extends string, TTargetId extends string> {
535
- readonly familyId: TFamilyId;
536
- readonly targetId: TTargetId;
537
- }
538
-
539
- export interface AdapterInstance<TFamilyId extends string, TTargetId extends string> {
540
- readonly familyId: TFamilyId;
541
- readonly targetId: TTargetId;
542
- }
543
-
544
- export interface DriverInstance<TFamilyId extends string, TTargetId extends string> {
545
- readonly familyId: TFamilyId;
546
- readonly targetId: TTargetId;
547
- }
548
-
549
- export interface ExtensionInstance<TFamilyId extends string, TTargetId extends string> {
550
- readonly familyId: TFamilyId;
551
- readonly targetId: TTargetId;
552
- }