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

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,525 @@
1
+ import type { OperationManifest, TypesImportSpec } from './types';
2
+
3
+ // ============================================================================
4
+ // Framework Component Descriptor Base Types
5
+ // ============================================================================
6
+ //
7
+ // Prisma Next uses a modular architecture where functionality is composed from
8
+ // discrete "components". Each component has a descriptor that identifies it and
9
+ // provides metadata. These base types define the shared structure for all
10
+ // component descriptors across both control-plane (CLI/tooling) and runtime-plane.
11
+ //
12
+ // Component Hierarchy:
13
+ //
14
+ // Family (e.g., 'sql', 'document')
15
+ // └── Target (e.g., 'postgres', 'mysql', 'mongodb')
16
+ // ├── Adapter (protocol/dialect implementation)
17
+ // ├── Driver (connection/execution layer)
18
+ // └── Extension (optional capabilities like pgvector)
19
+ //
20
+ // Key design decisions:
21
+ // - "Component" terminology separates framework building blocks from delivery
22
+ // mechanism ("pack" refers to how components are packaged/distributed)
23
+ // - `kind` is extensible (Kind extends string) - no closed union, allowing
24
+ // ecosystem authors to define new component kinds
25
+ // - Target-bound descriptors are generic in TFamilyId and TTargetId for type-safe
26
+ // composition (e.g., TypeScript rejects Postgres adapter with MySQL target)
27
+ // - Descriptors own declarative fields directly (version, types, operations, etc.)
28
+ // rather than nesting them under a `manifest` property
29
+ //
30
+ // ============================================================================
31
+
32
+ /**
33
+ * Declarative fields that describe component metadata.
34
+ * These fields are owned directly by descriptors (not nested under a manifest).
35
+ */
36
+ export interface ComponentMetadata {
37
+ /** Component version (semver) */
38
+ readonly version: string;
39
+
40
+ /**
41
+ * Capabilities this component provides.
42
+ *
43
+ * For adapters, capabilities must be declared on the adapter descriptor (so they are emitted into
44
+ * the contract) and also exposed in runtime adapter code (e.g. `adapter.profile.capabilities`);
45
+ * keep these declarations in sync. Targets are identifiers/descriptors and typically do not
46
+ * declare capabilities.
47
+ */
48
+ readonly capabilities?: Record<string, unknown>;
49
+
50
+ /** Type imports for contract.d.ts generation */
51
+ readonly types?: {
52
+ readonly codecTypes?: { readonly import: TypesImportSpec };
53
+ readonly operationTypes?: { readonly import: TypesImportSpec };
54
+ readonly storage?: ReadonlyArray<{
55
+ readonly typeId: string;
56
+ readonly familyId: string;
57
+ readonly targetId: string;
58
+ readonly nativeType?: string;
59
+ }>;
60
+ };
61
+
62
+ /** Operation manifests for building operation registries */
63
+ readonly operations?: ReadonlyArray<OperationManifest>;
64
+ }
65
+
66
+ /**
67
+ * Base descriptor for any framework component.
68
+ *
69
+ * All component descriptors share these fundamental properties that identify
70
+ * the component and provide its metadata. This interface is extended by
71
+ * specific descriptor types (FamilyDescriptor, TargetDescriptor, etc.).
72
+ *
73
+ * @template Kind - Discriminator literal identifying the component type.
74
+ * Built-in kinds are 'family', 'target', 'adapter', 'driver', 'extension',
75
+ * but the type accepts any string to allow ecosystem extensions.
76
+ *
77
+ * @example
78
+ * ```ts
79
+ * // All descriptors have these properties
80
+ * descriptor.kind // The Kind type parameter (e.g., 'family', 'target', or custom kinds)
81
+ * descriptor.id // Unique string identifier (e.g., 'sql', 'postgres')
82
+ * descriptor.version // Component version (semver)
83
+ * ```
84
+ */
85
+ export interface ComponentDescriptor<Kind extends string> extends ComponentMetadata {
86
+ /** Discriminator identifying the component type */
87
+ readonly kind: Kind;
88
+
89
+ /** Unique identifier for this component (e.g., 'sql', 'postgres', 'pgvector') */
90
+ readonly id: string;
91
+ }
92
+
93
+ export interface ContractComponentRequirementsCheckInput {
94
+ readonly contract: {
95
+ readonly target: string;
96
+ readonly targetFamily?: string | undefined;
97
+ readonly extensionPacks?: Record<string, unknown> | undefined;
98
+ };
99
+ readonly expectedTargetFamily?: string | undefined;
100
+ readonly expectedTargetId?: string | undefined;
101
+ readonly providedComponentIds: Iterable<string>;
102
+ }
103
+
104
+ export interface ContractComponentRequirementsCheckResult {
105
+ readonly familyMismatch?: { readonly expected: string; readonly actual: string } | undefined;
106
+ readonly targetMismatch?: { readonly expected: string; readonly actual: string } | undefined;
107
+ readonly missingExtensionPackIds: readonly string[];
108
+ }
109
+
110
+ export function checkContractComponentRequirements(
111
+ input: ContractComponentRequirementsCheckInput,
112
+ ): ContractComponentRequirementsCheckResult {
113
+ const providedIds = new Set<string>();
114
+ for (const id of input.providedComponentIds) {
115
+ providedIds.add(id);
116
+ }
117
+
118
+ const requiredExtensionPackIds = input.contract.extensionPacks
119
+ ? Object.keys(input.contract.extensionPacks)
120
+ : [];
121
+ const missingExtensionPackIds = requiredExtensionPackIds.filter((id) => !providedIds.has(id));
122
+
123
+ const expectedTargetFamily = input.expectedTargetFamily;
124
+ const contractTargetFamily = input.contract.targetFamily;
125
+ const familyMismatch =
126
+ expectedTargetFamily !== undefined &&
127
+ contractTargetFamily !== undefined &&
128
+ contractTargetFamily !== expectedTargetFamily
129
+ ? { expected: expectedTargetFamily, actual: contractTargetFamily }
130
+ : undefined;
131
+
132
+ const expectedTargetId = input.expectedTargetId;
133
+ const contractTargetId = input.contract.target;
134
+ const targetMismatch =
135
+ expectedTargetId !== undefined && contractTargetId !== expectedTargetId
136
+ ? { expected: expectedTargetId, actual: contractTargetId }
137
+ : undefined;
138
+
139
+ return {
140
+ ...(familyMismatch ? { familyMismatch } : {}),
141
+ ...(targetMismatch ? { targetMismatch } : {}),
142
+ missingExtensionPackIds,
143
+ };
144
+ }
145
+
146
+ /**
147
+ * Descriptor for a family component.
148
+ *
149
+ * A "family" represents a category of data sources with shared semantics
150
+ * (e.g., SQL databases, document stores). Families define:
151
+ * - Query semantics and operations (SELECT, INSERT, find, aggregate, etc.)
152
+ * - Contract structure (tables vs collections, columns vs fields)
153
+ * - Type system and codecs
154
+ *
155
+ * Families are the top-level grouping. Each family contains multiple targets
156
+ * (e.g., SQL family contains Postgres, MySQL, SQLite targets).
157
+ *
158
+ * Extended by plane-specific descriptors:
159
+ * - `ControlFamilyDescriptor` - adds `hook` for CLI/tooling operations
160
+ * - `RuntimeFamilyDescriptor` - adds runtime-specific factory methods
161
+ *
162
+ * @template TFamilyId - Literal type for the family identifier (e.g., 'sql', 'document')
163
+ *
164
+ * @example
165
+ * ```ts
166
+ * import sql from '@prisma-next/family-sql/control';
167
+ *
168
+ * sql.kind // 'family'
169
+ * sql.familyId // 'sql'
170
+ * sql.id // 'sql'
171
+ * ```
172
+ */
173
+ export interface FamilyDescriptor<TFamilyId extends string> extends ComponentDescriptor<'family'> {
174
+ /** The family identifier (e.g., 'sql', 'document') */
175
+ readonly familyId: TFamilyId;
176
+ }
177
+
178
+ /**
179
+ * Descriptor for a target component.
180
+ *
181
+ * A "target" represents a specific database or data store within a family
182
+ * (e.g., Postgres, MySQL, MongoDB). Targets define:
183
+ * - Native type mappings (e.g., Postgres int4 → TypeScript number)
184
+ * - Target-specific capabilities (e.g., RETURNING, LATERAL joins)
185
+ *
186
+ * Targets are bound to a family and provide the target-specific implementation
187
+ * details that adapters and drivers use.
188
+ *
189
+ * Extended by plane-specific descriptors:
190
+ * - `ControlTargetDescriptor` - adds optional `migrations` capability
191
+ * - `RuntimeTargetDescriptor` - adds runtime factory method
192
+ *
193
+ * @template TFamilyId - Literal type for the family identifier
194
+ * @template TTargetId - Literal type for the target identifier (e.g., 'postgres', 'mysql')
195
+ *
196
+ * @example
197
+ * ```ts
198
+ * import postgres from '@prisma-next/target-postgres/control';
199
+ *
200
+ * postgres.kind // 'target'
201
+ * postgres.familyId // 'sql'
202
+ * postgres.targetId // 'postgres'
203
+ * ```
204
+ */
205
+ export interface TargetDescriptor<TFamilyId extends string, TTargetId extends string>
206
+ extends ComponentDescriptor<'target'> {
207
+ /** The family this target belongs to */
208
+ readonly familyId: TFamilyId;
209
+
210
+ /** The target identifier (e.g., 'postgres', 'mysql', 'mongodb') */
211
+ readonly targetId: TTargetId;
212
+ }
213
+
214
+ /**
215
+ * Base shape for any pack reference.
216
+ * Pack refs are pure JSON-friendly objects safe to import in authoring flows.
217
+ */
218
+ export interface PackRefBase<Kind extends string, TFamilyId extends string>
219
+ extends ComponentMetadata {
220
+ readonly kind: Kind;
221
+ readonly id: string;
222
+ readonly familyId: TFamilyId;
223
+ readonly targetId?: string;
224
+ }
225
+
226
+ export type TargetPackRef<
227
+ TFamilyId extends string = string,
228
+ TTargetId extends string = string,
229
+ > = PackRefBase<'target', TFamilyId> & {
230
+ readonly targetId: TTargetId;
231
+ };
232
+
233
+ export type AdapterPackRef<
234
+ TFamilyId extends string = string,
235
+ TTargetId extends string = string,
236
+ > = PackRefBase<'adapter', TFamilyId> & {
237
+ readonly targetId: TTargetId;
238
+ };
239
+
240
+ export type ExtensionPackRef<
241
+ TFamilyId extends string = string,
242
+ TTargetId extends string = string,
243
+ > = PackRefBase<'extension', TFamilyId> & {
244
+ readonly targetId: TTargetId;
245
+ };
246
+
247
+ export type DriverPackRef<
248
+ TFamilyId extends string = string,
249
+ TTargetId extends string = string,
250
+ > = PackRefBase<'driver', TFamilyId> & {
251
+ readonly targetId: TTargetId;
252
+ };
253
+
254
+ /**
255
+ * Descriptor for an adapter component.
256
+ *
257
+ * An "adapter" provides the protocol and dialect implementation for a target.
258
+ * Adapters handle:
259
+ * - SQL/query generation (lowering AST to target-specific syntax)
260
+ * - Codec registration (encoding/decoding between JS and wire types)
261
+ * - Type mappings and coercions
262
+ *
263
+ * Adapters are bound to a specific family+target combination and work with
264
+ * any compatible driver for that target.
265
+ *
266
+ * Extended by plane-specific descriptors:
267
+ * - `ControlAdapterDescriptor` - control-plane factory
268
+ * - `RuntimeAdapterDescriptor` - runtime factory
269
+ *
270
+ * @template TFamilyId - Literal type for the family identifier
271
+ * @template TTargetId - Literal type for the target identifier
272
+ *
273
+ * @example
274
+ * ```ts
275
+ * import postgresAdapter from '@prisma-next/adapter-postgres/control';
276
+ *
277
+ * postgresAdapter.kind // 'adapter'
278
+ * postgresAdapter.familyId // 'sql'
279
+ * postgresAdapter.targetId // 'postgres'
280
+ * ```
281
+ */
282
+ export interface AdapterDescriptor<TFamilyId extends string, TTargetId extends string>
283
+ extends ComponentDescriptor<'adapter'> {
284
+ /** The family this adapter belongs to */
285
+ readonly familyId: TFamilyId;
286
+
287
+ /** The target this adapter is designed for */
288
+ readonly targetId: TTargetId;
289
+ }
290
+
291
+ /**
292
+ * Descriptor for a driver component.
293
+ *
294
+ * A "driver" provides the connection and execution layer for a target.
295
+ * Drivers handle:
296
+ * - Connection management (pooling, timeouts, retries)
297
+ * - Query execution (sending SQL/commands, receiving results)
298
+ * - Transaction management
299
+ * - Wire protocol communication
300
+ *
301
+ * Drivers are bound to a specific family+target and work with any compatible
302
+ * adapter. Multiple drivers can exist for the same target (e.g., node-postgres
303
+ * vs postgres.js for Postgres).
304
+ *
305
+ * Extended by plane-specific descriptors:
306
+ * - `ControlDriverDescriptor` - creates driver from connection URL
307
+ * - `RuntimeDriverDescriptor` - creates driver with runtime options
308
+ *
309
+ * @template TFamilyId - Literal type for the family identifier
310
+ * @template TTargetId - Literal type for the target identifier
311
+ *
312
+ * @example
313
+ * ```ts
314
+ * import postgresDriver from '@prisma-next/driver-postgres/control';
315
+ *
316
+ * postgresDriver.kind // 'driver'
317
+ * postgresDriver.familyId // 'sql'
318
+ * postgresDriver.targetId // 'postgres'
319
+ * ```
320
+ */
321
+ export interface DriverDescriptor<TFamilyId extends string, TTargetId extends string>
322
+ extends ComponentDescriptor<'driver'> {
323
+ /** The family this driver belongs to */
324
+ readonly familyId: TFamilyId;
325
+
326
+ /** The target this driver connects to */
327
+ readonly targetId: TTargetId;
328
+ }
329
+
330
+ /**
331
+ * Descriptor for an extension component.
332
+ *
333
+ * An "extension" adds optional capabilities to a target. Extensions can provide:
334
+ * - Additional operations (e.g., vector similarity search with pgvector)
335
+ * - Custom types and codecs (e.g., vector type)
336
+ * - Extended query capabilities
337
+ *
338
+ * Extensions are bound to a specific family+target and are registered in the
339
+ * config alongside the core components. Multiple extensions can be used together.
340
+ *
341
+ * Extended by plane-specific descriptors:
342
+ * - `ControlExtensionDescriptor` - control-plane extension factory
343
+ * - `RuntimeExtensionDescriptor` - runtime extension factory
344
+ *
345
+ * @template TFamilyId - Literal type for the family identifier
346
+ * @template TTargetId - Literal type for the target identifier
347
+ *
348
+ * @example
349
+ * ```ts
350
+ * import pgvector from '@prisma-next/extension-pgvector/control';
351
+ *
352
+ * pgvector.kind // 'extension'
353
+ * pgvector.familyId // 'sql'
354
+ * pgvector.targetId // 'postgres'
355
+ * ```
356
+ */
357
+ export interface ExtensionDescriptor<TFamilyId extends string, TTargetId extends string>
358
+ extends ComponentDescriptor<'extension'> {
359
+ /** The family this extension belongs to */
360
+ readonly familyId: TFamilyId;
361
+
362
+ /** The target this extension is designed for */
363
+ readonly targetId: TTargetId;
364
+ }
365
+
366
+ /**
367
+ * Union type for target-bound component descriptors.
368
+ *
369
+ * Target-bound components are those that must be compatible with a specific
370
+ * family+target combination. This includes targets, adapters, drivers, and
371
+ * extensions. Families are not target-bound.
372
+ *
373
+ * This type is used in migration and verification interfaces to enforce
374
+ * type-level compatibility between components.
375
+ *
376
+ * @template TFamilyId - Literal type for the family identifier
377
+ * @template TTargetId - Literal type for the target identifier
378
+ *
379
+ * @example
380
+ * ```ts
381
+ * // All these components must have matching familyId and targetId
382
+ * const components: TargetBoundComponentDescriptor<'sql', 'postgres'>[] = [
383
+ * postgresTarget,
384
+ * postgresAdapter,
385
+ * postgresDriver,
386
+ * pgvectorExtension,
387
+ * ];
388
+ * ```
389
+ */
390
+ export type TargetBoundComponentDescriptor<TFamilyId extends string, TTargetId extends string> =
391
+ | TargetDescriptor<TFamilyId, TTargetId>
392
+ | AdapterDescriptor<TFamilyId, TTargetId>
393
+ | DriverDescriptor<TFamilyId, TTargetId>
394
+ | ExtensionDescriptor<TFamilyId, TTargetId>;
395
+
396
+ // ============================================================================
397
+ // Framework Component Instance Base Types
398
+ // ============================================================================
399
+ //
400
+ // These are minimal, identity-only interfaces for component instances.
401
+ // They carry the component's identity (familyId, targetId) without any
402
+ // behavior methods. Plane-specific interfaces (ControlFamilyInstance,
403
+ // RuntimeFamilyInstance, etc.) extend these bases and add domain actions.
404
+ //
405
+ // ============================================================================
406
+
407
+ /**
408
+ * Base interface for family instances.
409
+ *
410
+ * A family instance is created by a family descriptor's `create()` method.
411
+ * This base interface carries only the identity; plane-specific interfaces
412
+ * add domain actions (e.g., `emitContract`, `verify` on ControlFamilyInstance).
413
+ *
414
+ * @template TFamilyId - Literal type for the family identifier (e.g., 'sql', 'document')
415
+ *
416
+ * @example
417
+ * ```ts
418
+ * const instance = sql.create({ target, adapter, driver, extensions });
419
+ * instance.familyId // 'sql'
420
+ * ```
421
+ */
422
+ export interface FamilyInstance<TFamilyId extends string> {
423
+ /** The family identifier (e.g., 'sql', 'document') */
424
+ readonly familyId: TFamilyId;
425
+ }
426
+
427
+ /**
428
+ * Base interface for target instances.
429
+ *
430
+ * A target instance is created by a target descriptor's `create()` method.
431
+ * This base interface carries only the identity; plane-specific interfaces
432
+ * add target-specific behavior.
433
+ *
434
+ * @template TFamilyId - Literal type for the family identifier
435
+ * @template TTargetId - Literal type for the target identifier (e.g., 'postgres', 'mysql')
436
+ *
437
+ * @example
438
+ * ```ts
439
+ * const instance = postgres.create();
440
+ * instance.familyId // 'sql'
441
+ * instance.targetId // 'postgres'
442
+ * ```
443
+ */
444
+ export interface TargetInstance<TFamilyId extends string, TTargetId extends string> {
445
+ /** The family this target belongs to */
446
+ readonly familyId: TFamilyId;
447
+
448
+ /** The target identifier (e.g., 'postgres', 'mysql', 'mongodb') */
449
+ readonly targetId: TTargetId;
450
+ }
451
+
452
+ /**
453
+ * Base interface for adapter instances.
454
+ *
455
+ * An adapter instance is created by an adapter descriptor's `create()` method.
456
+ * This base interface carries only the identity; plane-specific interfaces
457
+ * add adapter-specific behavior (e.g., codec registration, query lowering).
458
+ *
459
+ * @template TFamilyId - Literal type for the family identifier
460
+ * @template TTargetId - Literal type for the target identifier
461
+ *
462
+ * @example
463
+ * ```ts
464
+ * const instance = postgresAdapter.create();
465
+ * instance.familyId // 'sql'
466
+ * instance.targetId // 'postgres'
467
+ * ```
468
+ */
469
+ export interface AdapterInstance<TFamilyId extends string, TTargetId extends string> {
470
+ /** The family this adapter belongs to */
471
+ readonly familyId: TFamilyId;
472
+
473
+ /** The target this adapter is designed for */
474
+ readonly targetId: TTargetId;
475
+ }
476
+
477
+ /**
478
+ * Base interface for driver instances.
479
+ *
480
+ * A driver instance is created by a driver descriptor's `create()` method.
481
+ * This base interface carries only the identity; plane-specific interfaces
482
+ * add driver-specific behavior (e.g., `query`, `close` on ControlDriverInstance).
483
+ *
484
+ * @template TFamilyId - Literal type for the family identifier
485
+ * @template TTargetId - Literal type for the target identifier
486
+ *
487
+ * @example
488
+ * ```ts
489
+ * const instance = postgresDriver.create({ databaseUrl });
490
+ * instance.familyId // 'sql'
491
+ * instance.targetId // 'postgres'
492
+ * ```
493
+ */
494
+ export interface DriverInstance<TFamilyId extends string, TTargetId extends string> {
495
+ /** The family this driver belongs to */
496
+ readonly familyId: TFamilyId;
497
+
498
+ /** The target this driver connects to */
499
+ readonly targetId: TTargetId;
500
+ }
501
+
502
+ /**
503
+ * Base interface for extension instances.
504
+ *
505
+ * An extension instance is created by an extension descriptor's `create()` method.
506
+ * This base interface carries only the identity; plane-specific interfaces
507
+ * add extension-specific behavior.
508
+ *
509
+ * @template TFamilyId - Literal type for the family identifier
510
+ * @template TTargetId - Literal type for the target identifier
511
+ *
512
+ * @example
513
+ * ```ts
514
+ * const instance = pgvector.create();
515
+ * instance.familyId // 'sql'
516
+ * instance.targetId // 'postgres'
517
+ * ```
518
+ */
519
+ export interface ExtensionInstance<TFamilyId extends string, TTargetId extends string> {
520
+ /** The family this extension belongs to */
521
+ readonly familyId: TFamilyId;
522
+
523
+ /** The target this extension is designed for */
524
+ readonly targetId: TTargetId;
525
+ }
package/src/ir.ts ADDED
@@ -0,0 +1,113 @@
1
+ /**
2
+ * ContractIR types and factories for building contract intermediate representation.
3
+ * ContractIR is family-agnostic and used by authoring, emitter, and no-emit runtime.
4
+ */
5
+
6
+ /**
7
+ * ContractIR represents the intermediate representation of a contract.
8
+ * It is family-agnostic and contains generic storage, models, and relations.
9
+ * Note: coreHash and profileHash are computed by the emitter, not part of the IR.
10
+ */
11
+ export interface ContractIR<
12
+ TStorage extends Record<string, unknown> = Record<string, unknown>,
13
+ TModels extends Record<string, unknown> = Record<string, unknown>,
14
+ TRelations extends Record<string, unknown> = Record<string, unknown>,
15
+ > {
16
+ readonly schemaVersion: string;
17
+ readonly targetFamily: string;
18
+ readonly target: string;
19
+ readonly models: TModels;
20
+ readonly relations: TRelations;
21
+ readonly storage: TStorage;
22
+ readonly extensionPacks: Record<string, unknown>;
23
+ readonly capabilities: Record<string, Record<string, boolean>>;
24
+ readonly meta: Record<string, unknown>;
25
+ readonly sources: Record<string, unknown>;
26
+ }
27
+
28
+ /**
29
+ * Creates the header portion of a ContractIR.
30
+ * Contains schema version, target, target family, core hash, and optional profile hash.
31
+ */
32
+ export function irHeader(opts: {
33
+ target: string;
34
+ targetFamily: string;
35
+ coreHash: string;
36
+ profileHash?: string;
37
+ }): {
38
+ readonly schemaVersion: string;
39
+ readonly target: string;
40
+ readonly targetFamily: string;
41
+ readonly coreHash: string;
42
+ readonly profileHash?: string;
43
+ } {
44
+ return {
45
+ schemaVersion: '1',
46
+ target: opts.target,
47
+ targetFamily: opts.targetFamily,
48
+ coreHash: opts.coreHash,
49
+ ...(opts.profileHash !== undefined && { profileHash: opts.profileHash }),
50
+ };
51
+ }
52
+
53
+ /**
54
+ * Creates the meta portion of a ContractIR.
55
+ * Contains capabilities, extensionPacks, meta, and sources with empty object defaults.
56
+ * If a field is explicitly `undefined`, it will be omitted (for testing validation).
57
+ */
58
+ export function irMeta(opts?: {
59
+ capabilities?: Record<string, Record<string, boolean>> | undefined;
60
+ extensionPacks?: Record<string, unknown> | undefined;
61
+ meta?: Record<string, unknown> | undefined;
62
+ sources?: Record<string, unknown> | undefined;
63
+ }): {
64
+ readonly capabilities: Record<string, Record<string, boolean>>;
65
+ readonly extensionPacks: Record<string, unknown>;
66
+ readonly meta: Record<string, unknown>;
67
+ readonly sources: Record<string, unknown>;
68
+ } {
69
+ return {
70
+ capabilities: opts?.capabilities ?? {},
71
+ extensionPacks: opts?.extensionPacks ?? {},
72
+ meta: opts?.meta ?? {},
73
+ sources: opts?.sources ?? {},
74
+ };
75
+ }
76
+
77
+ /**
78
+ * Creates a complete ContractIR by combining header, meta, and family-specific sections.
79
+ * This is a family-agnostic factory that accepts generic storage, models, and relations.
80
+ */
81
+ export function contractIR<
82
+ TStorage extends Record<string, unknown>,
83
+ TModels extends Record<string, unknown>,
84
+ TRelations extends Record<string, unknown>,
85
+ >(opts: {
86
+ header: {
87
+ readonly schemaVersion: string;
88
+ readonly target: string;
89
+ readonly targetFamily: string;
90
+ readonly coreHash: string;
91
+ readonly profileHash?: string;
92
+ };
93
+ meta: {
94
+ readonly capabilities: Record<string, Record<string, boolean>>;
95
+ readonly extensionPacks: Record<string, unknown>;
96
+ readonly meta: Record<string, unknown>;
97
+ readonly sources: Record<string, unknown>;
98
+ };
99
+ storage: TStorage;
100
+ models: TModels;
101
+ relations: TRelations;
102
+ }): ContractIR<TStorage, TModels, TRelations> {
103
+ // ContractIR doesn't include coreHash or profileHash (those are computed by emitter)
104
+ return {
105
+ schemaVersion: opts.header.schemaVersion,
106
+ target: opts.header.target,
107
+ targetFamily: opts.header.targetFamily,
108
+ ...opts.meta,
109
+ storage: opts.storage,
110
+ models: opts.models,
111
+ relations: opts.relations,
112
+ };
113
+ }