@prisma-next/contract 0.3.0-dev.3 → 0.3.0-dev.30

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.
@@ -0,0 +1,717 @@
1
+ import type { OperationManifest, TypesImportSpec } from './types';
2
+
3
+ // ============================================================================
4
+ // Type Renderer Types (for parameterized codec emission)
5
+ // ============================================================================
6
+ //
7
+ // TypeRenderer supports author-friendly authoring (template strings) that are
8
+ // normalized to functions during pack assembly. The emitter only receives
9
+ // normalized (function-form) renderers.
10
+ //
11
+ // Lifecycle:
12
+ // 1. Authoring: Descriptor author uses template string or function
13
+ // 2. Assembly: Templates are compiled to functions via normalizeRenderer()
14
+ // 3. Emission: Emitter calls normalized render functions
15
+ //
16
+ // ============================================================================
17
+
18
+ /**
19
+ * Context passed to type renderers during contract.d.ts generation.
20
+ */
21
+ export interface RenderTypeContext {
22
+ /** The name of the CodecTypes type alias (typically 'CodecTypes') */
23
+ readonly codecTypesName: string;
24
+ }
25
+
26
+ /**
27
+ * A template-based type renderer (structured form).
28
+ * Uses mustache-style placeholders (e.g., `Vector<{{length}}>`) that are
29
+ * replaced with typeParams values during rendering.
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * { kind: 'template', template: 'Vector<{{length}}>' }
34
+ * // With typeParams { length: 1536 }, renders: 'Vector<1536>'
35
+ * ```
36
+ */
37
+ export interface TypeRendererTemplate {
38
+ readonly kind: 'template';
39
+ /** Template string with `{{key}}` placeholders for typeParams values */
40
+ readonly template: string;
41
+ }
42
+
43
+ /**
44
+ * A function-based type renderer for full control over type expression generation.
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * {
49
+ * kind: 'function',
50
+ * render: (params, ctx) => `Vector<${params.length}>`
51
+ * }
52
+ * ```
53
+ */
54
+ export interface TypeRendererFunction {
55
+ readonly kind: 'function';
56
+ /** Render function that produces a TypeScript type expression */
57
+ readonly render: (params: Record<string, unknown>, ctx: RenderTypeContext) => string;
58
+ }
59
+
60
+ /**
61
+ * A raw template string type renderer (convenience form).
62
+ * Shorthand for TypeRendererTemplate - just the template string without wrapper.
63
+ *
64
+ * @example
65
+ * ```ts
66
+ * 'Vector<{{length}}>'
67
+ * // Equivalent to: { kind: 'template', template: 'Vector<{{length}}>' }
68
+ * ```
69
+ */
70
+ export type TypeRendererString = string;
71
+
72
+ /**
73
+ * A raw function type renderer (convenience form).
74
+ * Shorthand for TypeRendererFunction - just the function without wrapper.
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * (params, ctx) => `Vector<${params.length}>`
79
+ * // Equivalent to: { kind: 'function', render: ... }
80
+ * ```
81
+ */
82
+ export type TypeRendererRawFunction = (
83
+ params: Record<string, unknown>,
84
+ ctx: RenderTypeContext,
85
+ ) => string;
86
+
87
+ /**
88
+ * Union of type renderer formats.
89
+ *
90
+ * Supports both structured forms (with `kind` discriminator) and convenience forms:
91
+ * - `string` - Template string with `{{key}}` placeholders (manifest-safe, JSON-serializable)
92
+ * - `function` - Render function for full control (requires runtime execution)
93
+ * - `{ kind: 'template', template: string }` - Structured template form
94
+ * - `{ kind: 'function', render: fn }` - Structured function form
95
+ *
96
+ * Templates are normalized to functions during pack assembly.
97
+ * **Prefer template strings** for most cases - they are JSON-serializable.
98
+ */
99
+ export type TypeRenderer =
100
+ | TypeRendererString
101
+ | TypeRendererRawFunction
102
+ | TypeRendererTemplate
103
+ | TypeRendererFunction;
104
+
105
+ /**
106
+ * Normalized type renderer - always a function after assembly.
107
+ * This is the form received by the emitter.
108
+ */
109
+ export interface NormalizedTypeRenderer {
110
+ readonly codecId: string;
111
+ readonly render: (params: Record<string, unknown>, ctx: RenderTypeContext) => string;
112
+ }
113
+
114
+ /**
115
+ * Interpolates a template string with params values.
116
+ * Used internally by normalizeRenderer to compile templates to functions.
117
+ *
118
+ * @throws Error if a placeholder key is not found in params (except 'CodecTypes')
119
+ */
120
+ export function interpolateTypeTemplate(
121
+ template: string,
122
+ params: Record<string, unknown>,
123
+ ctx: RenderTypeContext,
124
+ ): string {
125
+ return template.replace(/\{\{(\w+)\}\}/g, (_, key: string) => {
126
+ if (key === 'CodecTypes') return ctx.codecTypesName;
127
+ const value = params[key];
128
+ if (value === undefined) {
129
+ throw new Error(
130
+ `Missing template parameter "${key}" in template "${template}". ` +
131
+ `Available params: ${Object.keys(params).join(', ') || '(none)'}`,
132
+ );
133
+ }
134
+ return String(value);
135
+ });
136
+ }
137
+
138
+ /**
139
+ * Normalizes a TypeRenderer to function form.
140
+ * Called during pack assembly, not at emission time.
141
+ *
142
+ * Handles all TypeRenderer forms:
143
+ * - Raw string template: `'Vector<{{length}}>'`
144
+ * - Raw function: `(params, ctx) => ...`
145
+ * - Structured template: `{ kind: 'template', template: '...' }`
146
+ * - Structured function: `{ kind: 'function', render: fn }`
147
+ */
148
+ export function normalizeRenderer(codecId: string, renderer: TypeRenderer): NormalizedTypeRenderer {
149
+ // Handle raw string (template shorthand)
150
+ if (typeof renderer === 'string') {
151
+ return {
152
+ codecId,
153
+ render: (params, ctx) => interpolateTypeTemplate(renderer, params, ctx),
154
+ };
155
+ }
156
+
157
+ // Handle raw function (function shorthand)
158
+ if (typeof renderer === 'function') {
159
+ return { codecId, render: renderer };
160
+ }
161
+
162
+ // Handle structured function form
163
+ if (renderer.kind === 'function') {
164
+ return { codecId, render: renderer.render };
165
+ }
166
+
167
+ // Handle structured template form
168
+ const { template } = renderer;
169
+ return {
170
+ codecId,
171
+ render: (params, ctx) => interpolateTypeTemplate(template, params, ctx),
172
+ };
173
+ }
174
+
175
+ // ============================================================================
176
+ // Framework Component Descriptor Base Types
177
+ // ============================================================================
178
+ //
179
+ // Prisma Next uses a modular architecture where functionality is composed from
180
+ // discrete "components". Each component has a descriptor that identifies it and
181
+ // provides metadata. These base types define the shared structure for all
182
+ // component descriptors across both control-plane (CLI/tooling) and runtime-plane.
183
+ //
184
+ // Component Hierarchy:
185
+ //
186
+ // Family (e.g., 'sql', 'document')
187
+ // └── Target (e.g., 'postgres', 'mysql', 'mongodb')
188
+ // ├── Adapter (protocol/dialect implementation)
189
+ // ├── Driver (connection/execution layer)
190
+ // └── Extension (optional capabilities like pgvector)
191
+ //
192
+ // Key design decisions:
193
+ // - "Component" terminology separates framework building blocks from delivery
194
+ // mechanism ("pack" refers to how components are packaged/distributed)
195
+ // - `kind` is extensible (Kind extends string) - no closed union, allowing
196
+ // ecosystem authors to define new component kinds
197
+ // - Target-bound descriptors are generic in TFamilyId and TTargetId for type-safe
198
+ // composition (e.g., TypeScript rejects Postgres adapter with MySQL target)
199
+ // - Descriptors own declarative fields directly (version, types, operations, etc.)
200
+ // rather than nesting them under a `manifest` property
201
+ //
202
+ // ============================================================================
203
+
204
+ /**
205
+ * Declarative fields that describe component metadata.
206
+ * These fields are owned directly by descriptors (not nested under a manifest).
207
+ */
208
+ export interface ComponentMetadata {
209
+ /** Component version (semver) */
210
+ readonly version: string;
211
+
212
+ /**
213
+ * Capabilities this component provides.
214
+ *
215
+ * For adapters, capabilities must be declared on the adapter descriptor (so they are emitted into
216
+ * the contract) and also exposed in runtime adapter code (e.g. `adapter.profile.capabilities`);
217
+ * keep these declarations in sync. Targets are identifiers/descriptors and typically do not
218
+ * declare capabilities.
219
+ */
220
+ readonly capabilities?: Record<string, unknown>;
221
+
222
+ /** Type imports for contract.d.ts generation */
223
+ readonly types?: {
224
+ readonly codecTypes?: {
225
+ /**
226
+ * Base codec types import spec.
227
+ * Optional: adapters typically provide this, extensions usually don't.
228
+ */
229
+ readonly import?: TypesImportSpec;
230
+ /**
231
+ * Optional renderers for parameterized codecs owned by this component.
232
+ * Key is codecId (e.g., 'pg/vector@1'), value is the type renderer.
233
+ *
234
+ * Templates are normalized to functions during pack assembly.
235
+ * Duplicate codecId across descriptors is a hard error.
236
+ */
237
+ readonly parameterized?: Record<string, TypeRenderer>;
238
+ /**
239
+ * Optional type imports for parameterized codecs.
240
+ * These imports are added to contract.d.ts when parameterized renderers
241
+ * reference types from external packages.
242
+ */
243
+ readonly parameterizedImports?: ReadonlyArray<TypesImportSpec>;
244
+ };
245
+ readonly operationTypes?: { readonly import: TypesImportSpec };
246
+ readonly storage?: ReadonlyArray<{
247
+ readonly typeId: string;
248
+ readonly familyId: string;
249
+ readonly targetId: string;
250
+ readonly nativeType?: string;
251
+ }>;
252
+ };
253
+
254
+ /** Operation manifests for building operation registries */
255
+ readonly operations?: ReadonlyArray<OperationManifest>;
256
+ }
257
+
258
+ /**
259
+ * Base descriptor for any framework component.
260
+ *
261
+ * All component descriptors share these fundamental properties that identify
262
+ * the component and provide its metadata. This interface is extended by
263
+ * specific descriptor types (FamilyDescriptor, TargetDescriptor, etc.).
264
+ *
265
+ * @template Kind - Discriminator literal identifying the component type.
266
+ * Built-in kinds are 'family', 'target', 'adapter', 'driver', 'extension',
267
+ * but the type accepts any string to allow ecosystem extensions.
268
+ *
269
+ * @example
270
+ * ```ts
271
+ * // All descriptors have these properties
272
+ * descriptor.kind // The Kind type parameter (e.g., 'family', 'target', or custom kinds)
273
+ * descriptor.id // Unique string identifier (e.g., 'sql', 'postgres')
274
+ * descriptor.version // Component version (semver)
275
+ * ```
276
+ */
277
+ export interface ComponentDescriptor<Kind extends string> extends ComponentMetadata {
278
+ /** Discriminator identifying the component type */
279
+ readonly kind: Kind;
280
+
281
+ /** Unique identifier for this component (e.g., 'sql', 'postgres', 'pgvector') */
282
+ readonly id: string;
283
+ }
284
+
285
+ export interface ContractComponentRequirementsCheckInput {
286
+ readonly contract: {
287
+ readonly target: string;
288
+ readonly targetFamily?: string | undefined;
289
+ readonly extensionPacks?: Record<string, unknown> | undefined;
290
+ };
291
+ readonly expectedTargetFamily?: string | undefined;
292
+ readonly expectedTargetId?: string | undefined;
293
+ readonly providedComponentIds: Iterable<string>;
294
+ }
295
+
296
+ export interface ContractComponentRequirementsCheckResult {
297
+ readonly familyMismatch?: { readonly expected: string; readonly actual: string } | undefined;
298
+ readonly targetMismatch?: { readonly expected: string; readonly actual: string } | undefined;
299
+ readonly missingExtensionPackIds: readonly string[];
300
+ }
301
+
302
+ export function checkContractComponentRequirements(
303
+ input: ContractComponentRequirementsCheckInput,
304
+ ): ContractComponentRequirementsCheckResult {
305
+ const providedIds = new Set<string>();
306
+ for (const id of input.providedComponentIds) {
307
+ providedIds.add(id);
308
+ }
309
+
310
+ const requiredExtensionPackIds = input.contract.extensionPacks
311
+ ? Object.keys(input.contract.extensionPacks)
312
+ : [];
313
+ const missingExtensionPackIds = requiredExtensionPackIds.filter((id) => !providedIds.has(id));
314
+
315
+ const expectedTargetFamily = input.expectedTargetFamily;
316
+ const contractTargetFamily = input.contract.targetFamily;
317
+ const familyMismatch =
318
+ expectedTargetFamily !== undefined &&
319
+ contractTargetFamily !== undefined &&
320
+ contractTargetFamily !== expectedTargetFamily
321
+ ? { expected: expectedTargetFamily, actual: contractTargetFamily }
322
+ : undefined;
323
+
324
+ const expectedTargetId = input.expectedTargetId;
325
+ const contractTargetId = input.contract.target;
326
+ const targetMismatch =
327
+ expectedTargetId !== undefined && contractTargetId !== expectedTargetId
328
+ ? { expected: expectedTargetId, actual: contractTargetId }
329
+ : undefined;
330
+
331
+ return {
332
+ ...(familyMismatch ? { familyMismatch } : {}),
333
+ ...(targetMismatch ? { targetMismatch } : {}),
334
+ missingExtensionPackIds,
335
+ };
336
+ }
337
+
338
+ /**
339
+ * Descriptor for a family component.
340
+ *
341
+ * A "family" represents a category of data sources with shared semantics
342
+ * (e.g., SQL databases, document stores). Families define:
343
+ * - Query semantics and operations (SELECT, INSERT, find, aggregate, etc.)
344
+ * - Contract structure (tables vs collections, columns vs fields)
345
+ * - Type system and codecs
346
+ *
347
+ * Families are the top-level grouping. Each family contains multiple targets
348
+ * (e.g., SQL family contains Postgres, MySQL, SQLite targets).
349
+ *
350
+ * Extended by plane-specific descriptors:
351
+ * - `ControlFamilyDescriptor` - adds `hook` for CLI/tooling operations
352
+ * - `RuntimeFamilyDescriptor` - adds runtime-specific factory methods
353
+ *
354
+ * @template TFamilyId - Literal type for the family identifier (e.g., 'sql', 'document')
355
+ *
356
+ * @example
357
+ * ```ts
358
+ * import sql from '@prisma-next/family-sql/control';
359
+ *
360
+ * sql.kind // 'family'
361
+ * sql.familyId // 'sql'
362
+ * sql.id // 'sql'
363
+ * ```
364
+ */
365
+ export interface FamilyDescriptor<TFamilyId extends string> extends ComponentDescriptor<'family'> {
366
+ /** The family identifier (e.g., 'sql', 'document') */
367
+ readonly familyId: TFamilyId;
368
+ }
369
+
370
+ /**
371
+ * Descriptor for a target component.
372
+ *
373
+ * A "target" represents a specific database or data store within a family
374
+ * (e.g., Postgres, MySQL, MongoDB). Targets define:
375
+ * - Native type mappings (e.g., Postgres int4 → TypeScript number)
376
+ * - Target-specific capabilities (e.g., RETURNING, LATERAL joins)
377
+ *
378
+ * Targets are bound to a family and provide the target-specific implementation
379
+ * details that adapters and drivers use.
380
+ *
381
+ * Extended by plane-specific descriptors:
382
+ * - `ControlTargetDescriptor` - adds optional `migrations` capability
383
+ * - `RuntimeTargetDescriptor` - adds runtime factory method
384
+ *
385
+ * @template TFamilyId - Literal type for the family identifier
386
+ * @template TTargetId - Literal type for the target identifier (e.g., 'postgres', 'mysql')
387
+ *
388
+ * @example
389
+ * ```ts
390
+ * import postgres from '@prisma-next/target-postgres/control';
391
+ *
392
+ * postgres.kind // 'target'
393
+ * postgres.familyId // 'sql'
394
+ * postgres.targetId // 'postgres'
395
+ * ```
396
+ */
397
+ export interface TargetDescriptor<TFamilyId extends string, TTargetId extends string>
398
+ extends ComponentDescriptor<'target'> {
399
+ /** The family this target belongs to */
400
+ readonly familyId: TFamilyId;
401
+
402
+ /** The target identifier (e.g., 'postgres', 'mysql', 'mongodb') */
403
+ readonly targetId: TTargetId;
404
+ }
405
+
406
+ /**
407
+ * Base shape for any pack reference.
408
+ * Pack refs are pure JSON-friendly objects safe to import in authoring flows.
409
+ */
410
+ export interface PackRefBase<Kind extends string, TFamilyId extends string>
411
+ extends ComponentMetadata {
412
+ readonly kind: Kind;
413
+ readonly id: string;
414
+ readonly familyId: TFamilyId;
415
+ readonly targetId?: string;
416
+ }
417
+
418
+ export type TargetPackRef<
419
+ TFamilyId extends string = string,
420
+ TTargetId extends string = string,
421
+ > = PackRefBase<'target', TFamilyId> & {
422
+ readonly targetId: TTargetId;
423
+ };
424
+
425
+ export type AdapterPackRef<
426
+ TFamilyId extends string = string,
427
+ TTargetId extends string = string,
428
+ > = PackRefBase<'adapter', TFamilyId> & {
429
+ readonly targetId: TTargetId;
430
+ };
431
+
432
+ export type ExtensionPackRef<
433
+ TFamilyId extends string = string,
434
+ TTargetId extends string = string,
435
+ > = PackRefBase<'extension', TFamilyId> & {
436
+ readonly targetId: TTargetId;
437
+ };
438
+
439
+ export type DriverPackRef<
440
+ TFamilyId extends string = string,
441
+ TTargetId extends string = string,
442
+ > = PackRefBase<'driver', TFamilyId> & {
443
+ readonly targetId: TTargetId;
444
+ };
445
+
446
+ /**
447
+ * Descriptor for an adapter component.
448
+ *
449
+ * An "adapter" provides the protocol and dialect implementation for a target.
450
+ * Adapters handle:
451
+ * - SQL/query generation (lowering AST to target-specific syntax)
452
+ * - Codec registration (encoding/decoding between JS and wire types)
453
+ * - Type mappings and coercions
454
+ *
455
+ * Adapters are bound to a specific family+target combination and work with
456
+ * any compatible driver for that target.
457
+ *
458
+ * Extended by plane-specific descriptors:
459
+ * - `ControlAdapterDescriptor` - control-plane factory
460
+ * - `RuntimeAdapterDescriptor` - runtime factory
461
+ *
462
+ * @template TFamilyId - Literal type for the family identifier
463
+ * @template TTargetId - Literal type for the target identifier
464
+ *
465
+ * @example
466
+ * ```ts
467
+ * import postgresAdapter from '@prisma-next/adapter-postgres/control';
468
+ *
469
+ * postgresAdapter.kind // 'adapter'
470
+ * postgresAdapter.familyId // 'sql'
471
+ * postgresAdapter.targetId // 'postgres'
472
+ * ```
473
+ */
474
+ export interface AdapterDescriptor<TFamilyId extends string, TTargetId extends string>
475
+ extends ComponentDescriptor<'adapter'> {
476
+ /** The family this adapter belongs to */
477
+ readonly familyId: TFamilyId;
478
+
479
+ /** The target this adapter is designed for */
480
+ readonly targetId: TTargetId;
481
+ }
482
+
483
+ /**
484
+ * Descriptor for a driver component.
485
+ *
486
+ * A "driver" provides the connection and execution layer for a target.
487
+ * Drivers handle:
488
+ * - Connection management (pooling, timeouts, retries)
489
+ * - Query execution (sending SQL/commands, receiving results)
490
+ * - Transaction management
491
+ * - Wire protocol communication
492
+ *
493
+ * Drivers are bound to a specific family+target and work with any compatible
494
+ * adapter. Multiple drivers can exist for the same target (e.g., node-postgres
495
+ * vs postgres.js for Postgres).
496
+ *
497
+ * Extended by plane-specific descriptors:
498
+ * - `ControlDriverDescriptor` - creates driver from connection URL
499
+ * - `RuntimeDriverDescriptor` - creates driver with runtime options
500
+ *
501
+ * @template TFamilyId - Literal type for the family identifier
502
+ * @template TTargetId - Literal type for the target identifier
503
+ *
504
+ * @example
505
+ * ```ts
506
+ * import postgresDriver from '@prisma-next/driver-postgres/control';
507
+ *
508
+ * postgresDriver.kind // 'driver'
509
+ * postgresDriver.familyId // 'sql'
510
+ * postgresDriver.targetId // 'postgres'
511
+ * ```
512
+ */
513
+ export interface DriverDescriptor<TFamilyId extends string, TTargetId extends string>
514
+ extends ComponentDescriptor<'driver'> {
515
+ /** The family this driver belongs to */
516
+ readonly familyId: TFamilyId;
517
+
518
+ /** The target this driver connects to */
519
+ readonly targetId: TTargetId;
520
+ }
521
+
522
+ /**
523
+ * Descriptor for an extension component.
524
+ *
525
+ * An "extension" adds optional capabilities to a target. Extensions can provide:
526
+ * - Additional operations (e.g., vector similarity search with pgvector)
527
+ * - Custom types and codecs (e.g., vector type)
528
+ * - Extended query capabilities
529
+ *
530
+ * Extensions are bound to a specific family+target and are registered in the
531
+ * config alongside the core components. Multiple extensions can be used together.
532
+ *
533
+ * Extended by plane-specific descriptors:
534
+ * - `ControlExtensionDescriptor` - control-plane extension factory
535
+ * - `RuntimeExtensionDescriptor` - runtime extension factory
536
+ *
537
+ * @template TFamilyId - Literal type for the family identifier
538
+ * @template TTargetId - Literal type for the target identifier
539
+ *
540
+ * @example
541
+ * ```ts
542
+ * import pgvector from '@prisma-next/extension-pgvector/control';
543
+ *
544
+ * pgvector.kind // 'extension'
545
+ * pgvector.familyId // 'sql'
546
+ * pgvector.targetId // 'postgres'
547
+ * ```
548
+ */
549
+ export interface ExtensionDescriptor<TFamilyId extends string, TTargetId extends string>
550
+ extends ComponentDescriptor<'extension'> {
551
+ /** The family this extension belongs to */
552
+ readonly familyId: TFamilyId;
553
+
554
+ /** The target this extension is designed for */
555
+ readonly targetId: TTargetId;
556
+ }
557
+
558
+ /**
559
+ * Union type for target-bound component descriptors.
560
+ *
561
+ * Target-bound components are those that must be compatible with a specific
562
+ * family+target combination. This includes targets, adapters, drivers, and
563
+ * extensions. Families are not target-bound.
564
+ *
565
+ * This type is used in migration and verification interfaces to enforce
566
+ * type-level compatibility between components.
567
+ *
568
+ * @template TFamilyId - Literal type for the family identifier
569
+ * @template TTargetId - Literal type for the target identifier
570
+ *
571
+ * @example
572
+ * ```ts
573
+ * // All these components must have matching familyId and targetId
574
+ * const components: TargetBoundComponentDescriptor<'sql', 'postgres'>[] = [
575
+ * postgresTarget,
576
+ * postgresAdapter,
577
+ * postgresDriver,
578
+ * pgvectorExtension,
579
+ * ];
580
+ * ```
581
+ */
582
+ export type TargetBoundComponentDescriptor<TFamilyId extends string, TTargetId extends string> =
583
+ | TargetDescriptor<TFamilyId, TTargetId>
584
+ | AdapterDescriptor<TFamilyId, TTargetId>
585
+ | DriverDescriptor<TFamilyId, TTargetId>
586
+ | ExtensionDescriptor<TFamilyId, TTargetId>;
587
+
588
+ // ============================================================================
589
+ // Framework Component Instance Base Types
590
+ // ============================================================================
591
+ //
592
+ // These are minimal, identity-only interfaces for component instances.
593
+ // They carry the component's identity (familyId, targetId) without any
594
+ // behavior methods. Plane-specific interfaces (ControlFamilyInstance,
595
+ // RuntimeFamilyInstance, etc.) extend these bases and add domain actions.
596
+ //
597
+ // ============================================================================
598
+
599
+ /**
600
+ * Base interface for family instances.
601
+ *
602
+ * A family instance is created by a family descriptor's `create()` method.
603
+ * This base interface carries only the identity; plane-specific interfaces
604
+ * add domain actions (e.g., `emitContract`, `verify` on ControlFamilyInstance).
605
+ *
606
+ * @template TFamilyId - Literal type for the family identifier (e.g., 'sql', 'document')
607
+ *
608
+ * @example
609
+ * ```ts
610
+ * const instance = sql.create({ target, adapter, driver, extensions });
611
+ * instance.familyId // 'sql'
612
+ * ```
613
+ */
614
+ export interface FamilyInstance<TFamilyId extends string> {
615
+ /** The family identifier (e.g., 'sql', 'document') */
616
+ readonly familyId: TFamilyId;
617
+ }
618
+
619
+ /**
620
+ * Base interface for target instances.
621
+ *
622
+ * A target instance is created by a target descriptor's `create()` method.
623
+ * This base interface carries only the identity; plane-specific interfaces
624
+ * add target-specific behavior.
625
+ *
626
+ * @template TFamilyId - Literal type for the family identifier
627
+ * @template TTargetId - Literal type for the target identifier (e.g., 'postgres', 'mysql')
628
+ *
629
+ * @example
630
+ * ```ts
631
+ * const instance = postgres.create();
632
+ * instance.familyId // 'sql'
633
+ * instance.targetId // 'postgres'
634
+ * ```
635
+ */
636
+ export interface TargetInstance<TFamilyId extends string, TTargetId extends string> {
637
+ /** The family this target belongs to */
638
+ readonly familyId: TFamilyId;
639
+
640
+ /** The target identifier (e.g., 'postgres', 'mysql', 'mongodb') */
641
+ readonly targetId: TTargetId;
642
+ }
643
+
644
+ /**
645
+ * Base interface for adapter instances.
646
+ *
647
+ * An adapter instance is created by an adapter descriptor's `create()` method.
648
+ * This base interface carries only the identity; plane-specific interfaces
649
+ * add adapter-specific behavior (e.g., codec registration, query lowering).
650
+ *
651
+ * @template TFamilyId - Literal type for the family identifier
652
+ * @template TTargetId - Literal type for the target identifier
653
+ *
654
+ * @example
655
+ * ```ts
656
+ * const instance = postgresAdapter.create();
657
+ * instance.familyId // 'sql'
658
+ * instance.targetId // 'postgres'
659
+ * ```
660
+ */
661
+ export interface AdapterInstance<TFamilyId extends string, TTargetId extends string> {
662
+ /** The family this adapter belongs to */
663
+ readonly familyId: TFamilyId;
664
+
665
+ /** The target this adapter is designed for */
666
+ readonly targetId: TTargetId;
667
+ }
668
+
669
+ /**
670
+ * Base interface for driver instances.
671
+ *
672
+ * A driver instance is created by a driver descriptor's `create()` method.
673
+ * This base interface carries only the identity; plane-specific interfaces
674
+ * add driver-specific behavior (e.g., `query`, `close` on ControlDriverInstance).
675
+ *
676
+ * @template TFamilyId - Literal type for the family identifier
677
+ * @template TTargetId - Literal type for the target identifier
678
+ *
679
+ * @example
680
+ * ```ts
681
+ * const instance = postgresDriver.create({ databaseUrl });
682
+ * instance.familyId // 'sql'
683
+ * instance.targetId // 'postgres'
684
+ * ```
685
+ */
686
+ export interface DriverInstance<TFamilyId extends string, TTargetId extends string> {
687
+ /** The family this driver belongs to */
688
+ readonly familyId: TFamilyId;
689
+
690
+ /** The target this driver connects to */
691
+ readonly targetId: TTargetId;
692
+ }
693
+
694
+ /**
695
+ * Base interface for extension instances.
696
+ *
697
+ * An extension instance is created by an extension descriptor's `create()` method.
698
+ * This base interface carries only the identity; plane-specific interfaces
699
+ * add extension-specific behavior.
700
+ *
701
+ * @template TFamilyId - Literal type for the family identifier
702
+ * @template TTargetId - Literal type for the target identifier
703
+ *
704
+ * @example
705
+ * ```ts
706
+ * const instance = pgvector.create();
707
+ * instance.familyId // 'sql'
708
+ * instance.targetId // 'postgres'
709
+ * ```
710
+ */
711
+ export interface ExtensionInstance<TFamilyId extends string, TTargetId extends string> {
712
+ /** The family this extension belongs to */
713
+ readonly familyId: TFamilyId;
714
+
715
+ /** The target this extension is designed for */
716
+ readonly targetId: TTargetId;
717
+ }