@prisma-next/framework-components 0.0.1

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,402 @@
1
+ import type { Codec } from './codec-types';
2
+ import type { AuthoringContributions } from './framework-authoring';
3
+ import type { TypesImportSpec } from './types-import-spec';
4
+
5
+ /**
6
+ * Declarative fields that describe component metadata.
7
+ */
8
+ export interface ComponentMetadata {
9
+ /** Component version (semver) */
10
+ readonly version: string;
11
+
12
+ /**
13
+ * Capabilities this component provides.
14
+ *
15
+ * For adapters, capabilities must be declared on the adapter descriptor (so they are emitted into
16
+ * the contract) and also exposed in runtime adapter code (e.g. `adapter.profile.capabilities`);
17
+ * keep these declarations in sync. Targets are identifiers/descriptors and typically do not
18
+ * declare capabilities.
19
+ */
20
+ readonly capabilities?: Record<string, unknown>;
21
+
22
+ /** Type imports for contract.d.ts generation */
23
+ readonly types?: {
24
+ readonly codecTypes?: {
25
+ /**
26
+ * Base codec types import spec.
27
+ * Optional: adapters typically provide this, extensions usually don't.
28
+ */
29
+ readonly import?: TypesImportSpec;
30
+ /**
31
+ * Additional type-only imports for parameterized codec branded types.
32
+ *
33
+ * These imports are included in generated `contract.d.ts` but are NOT treated as
34
+ * codec type maps (i.e., they should not be intersected into `export type CodecTypes = ...`).
35
+ *
36
+ * Example: `Vector<N>` for pgvector codecs that emit `Vector<1536>`
37
+ */
38
+ readonly typeImports?: ReadonlyArray<TypesImportSpec>;
39
+ /**
40
+ * Optional control-plane hooks keyed by codecId.
41
+ * Used by family-specific planners/verifiers to handle storage types.
42
+ */
43
+ readonly controlPlaneHooks?: Record<string, unknown>;
44
+ /**
45
+ * Codec instances contributed by this component.
46
+ * Used to build a CodecLookup for codec-dispatched type rendering during emission.
47
+ */
48
+ readonly codecInstances?: ReadonlyArray<Codec>;
49
+ };
50
+ readonly operationTypes?: { readonly import: TypesImportSpec };
51
+ readonly queryOperationTypes?: { readonly import: TypesImportSpec };
52
+ readonly storage?: ReadonlyArray<{
53
+ readonly typeId: string;
54
+ readonly familyId: string;
55
+ readonly targetId: string;
56
+ readonly nativeType?: string;
57
+ }>;
58
+ };
59
+
60
+ /**
61
+ * Optional pure-data authoring contributions exposed by this component.
62
+ *
63
+ * These contributions are safe to include on pack refs and descriptors because
64
+ * they contain only declarative metadata. Higher-level authoring packages may
65
+ * project them into concrete helper functions for TS-first workflows.
66
+ */
67
+ readonly authoring?: AuthoringContributions;
68
+ }
69
+
70
+ /**
71
+ * Base descriptor for any framework component.
72
+ *
73
+ * All component descriptors share these fundamental properties that identify
74
+ * the component and provide its metadata. This interface is extended by
75
+ * specific descriptor types (FamilyDescriptor, TargetDescriptor, etc.).
76
+ *
77
+ * @template Kind - Discriminator literal identifying the component type.
78
+ * Built-in kinds are 'family', 'target', 'adapter', 'driver', 'extension',
79
+ * but the type accepts any string to allow ecosystem extensions.
80
+ *
81
+ * @example
82
+ * ```ts
83
+ * // All descriptors have these properties
84
+ * descriptor.kind // The Kind type parameter (e.g., 'family', 'target', or custom kinds)
85
+ * descriptor.id // Unique string identifier (e.g., 'sql', 'postgres')
86
+ * descriptor.version // Component version (semver)
87
+ * ```
88
+ */
89
+ export interface ComponentDescriptor<Kind extends string> extends ComponentMetadata {
90
+ /** Discriminator identifying the component type */
91
+ readonly kind: Kind;
92
+
93
+ /** Unique identifier for this component (e.g., 'sql', 'postgres', 'pgvector') */
94
+ readonly id: string;
95
+ }
96
+
97
+ export interface ContractComponentRequirementsCheckInput {
98
+ readonly contract: {
99
+ readonly target: string;
100
+ readonly targetFamily?: string | undefined;
101
+ readonly extensionPacks?: Record<string, unknown> | undefined;
102
+ };
103
+ readonly expectedTargetFamily?: string | undefined;
104
+ readonly expectedTargetId?: string | undefined;
105
+ readonly providedComponentIds: Iterable<string>;
106
+ }
107
+
108
+ export interface ContractComponentRequirementsCheckResult {
109
+ readonly familyMismatch?: { readonly expected: string; readonly actual: string } | undefined;
110
+ readonly targetMismatch?: { readonly expected: string; readonly actual: string } | undefined;
111
+ readonly missingExtensionPackIds: readonly string[];
112
+ }
113
+
114
+ export function checkContractComponentRequirements(
115
+ input: ContractComponentRequirementsCheckInput,
116
+ ): ContractComponentRequirementsCheckResult {
117
+ const providedIds = new Set<string>();
118
+ for (const id of input.providedComponentIds) {
119
+ providedIds.add(id);
120
+ }
121
+
122
+ const requiredExtensionPackIds = input.contract.extensionPacks
123
+ ? Object.keys(input.contract.extensionPacks)
124
+ : [];
125
+ const missingExtensionPackIds = requiredExtensionPackIds.filter((id) => !providedIds.has(id));
126
+
127
+ const expectedTargetFamily = input.expectedTargetFamily;
128
+ const contractTargetFamily = input.contract.targetFamily;
129
+ const familyMismatch =
130
+ expectedTargetFamily !== undefined &&
131
+ contractTargetFamily !== undefined &&
132
+ contractTargetFamily !== expectedTargetFamily
133
+ ? { expected: expectedTargetFamily, actual: contractTargetFamily }
134
+ : undefined;
135
+
136
+ const expectedTargetId = input.expectedTargetId;
137
+ const contractTargetId = input.contract.target;
138
+ const targetMismatch =
139
+ expectedTargetId !== undefined && contractTargetId !== expectedTargetId
140
+ ? { expected: expectedTargetId, actual: contractTargetId }
141
+ : undefined;
142
+
143
+ return {
144
+ ...(familyMismatch ? { familyMismatch } : {}),
145
+ ...(targetMismatch ? { targetMismatch } : {}),
146
+ missingExtensionPackIds,
147
+ };
148
+ }
149
+
150
+ /**
151
+ * Descriptor for a family component.
152
+ *
153
+ * A "family" represents a category of data sources with shared semantics
154
+ * (e.g., SQL databases, document stores). Families define:
155
+ * - Query semantics and operations (SELECT, INSERT, find, aggregate, etc.)
156
+ * - Contract structure (tables vs collections, columns vs fields)
157
+ * - Type system and codecs
158
+ *
159
+ * Families are the top-level grouping. Each family contains multiple targets
160
+ * (e.g., SQL family contains Postgres, MySQL, SQLite targets).
161
+ *
162
+ * Extended by plane-specific descriptors:
163
+ * - `ControlFamilyDescriptor` - adds `emission` for CLI/tooling operations
164
+ * - `RuntimeFamilyDescriptor` - adds runtime-specific factory methods
165
+ *
166
+ * @template TFamilyId - Literal type for the family identifier (e.g., 'sql', 'document')
167
+ *
168
+ * @example
169
+ * ```ts
170
+ * import sql from '@prisma-next/family-sql/control';
171
+ *
172
+ * sql.kind // 'family'
173
+ * sql.familyId // 'sql'
174
+ * sql.id // 'sql'
175
+ * ```
176
+ */
177
+ export interface FamilyDescriptor<TFamilyId extends string> extends ComponentDescriptor<'family'> {
178
+ /** The family identifier (e.g., 'sql', 'document') */
179
+ readonly familyId: TFamilyId;
180
+ }
181
+
182
+ /**
183
+ * Descriptor for a target component.
184
+ *
185
+ * A "target" represents a specific database or data store within a family
186
+ * (e.g., Postgres, MySQL, MongoDB). Targets define:
187
+ * - Native type mappings (e.g., Postgres int4 → TypeScript number)
188
+ * - Target-specific capabilities (e.g., RETURNING, LATERAL joins)
189
+ *
190
+ * Targets are bound to a family and provide the target-specific implementation
191
+ * details that adapters and drivers use.
192
+ *
193
+ * Extended by plane-specific descriptors:
194
+ * - `ControlTargetDescriptor` - adds optional `migrations` capability
195
+ * - `RuntimeTargetDescriptor` - adds runtime factory method
196
+ *
197
+ * @template TFamilyId - Literal type for the family identifier
198
+ * @template TTargetId - Literal type for the target identifier (e.g., 'postgres', 'mysql')
199
+ *
200
+ * @example
201
+ * ```ts
202
+ * import postgres from '@prisma-next/target-postgres/control';
203
+ *
204
+ * postgres.kind // 'target'
205
+ * postgres.familyId // 'sql'
206
+ * postgres.targetId // 'postgres'
207
+ * ```
208
+ */
209
+ export interface TargetDescriptor<TFamilyId extends string, TTargetId extends string>
210
+ extends ComponentDescriptor<'target'> {
211
+ /** The family this target belongs to */
212
+ readonly familyId: TFamilyId;
213
+
214
+ /** The target identifier (e.g., 'postgres', 'mysql', 'mongodb') */
215
+ readonly targetId: TTargetId;
216
+ }
217
+
218
+ /**
219
+ * Base shape for any pack reference.
220
+ * Pack refs are pure JSON-friendly objects safe to import in authoring flows.
221
+ */
222
+ export interface PackRefBase<Kind extends string, TFamilyId extends string>
223
+ extends ComponentMetadata {
224
+ readonly kind: Kind;
225
+ readonly id: string;
226
+ readonly familyId: TFamilyId;
227
+ readonly targetId?: string;
228
+ readonly authoring?: AuthoringContributions;
229
+ }
230
+
231
+ export type FamilyPackRef<TFamilyId extends string = string> = PackRefBase<'family', TFamilyId>;
232
+
233
+ export type TargetPackRef<
234
+ TFamilyId extends string = string,
235
+ TTargetId extends string = string,
236
+ > = PackRefBase<'target', TFamilyId> & {
237
+ readonly targetId: TTargetId;
238
+ };
239
+
240
+ export type AdapterPackRef<
241
+ TFamilyId extends string = string,
242
+ TTargetId extends string = string,
243
+ > = PackRefBase<'adapter', TFamilyId> & {
244
+ readonly targetId: TTargetId;
245
+ };
246
+
247
+ export type ExtensionPackRef<
248
+ TFamilyId extends string = string,
249
+ TTargetId extends string = string,
250
+ > = PackRefBase<'extension', TFamilyId> & {
251
+ readonly targetId: TTargetId;
252
+ };
253
+
254
+ export type DriverPackRef<
255
+ TFamilyId extends string = string,
256
+ TTargetId extends string = string,
257
+ > = PackRefBase<'driver', TFamilyId> & {
258
+ readonly targetId: TTargetId;
259
+ };
260
+
261
+ /**
262
+ * Descriptor for an adapter component.
263
+ *
264
+ * An "adapter" provides the protocol and dialect implementation for a target.
265
+ * Adapters handle:
266
+ * - SQL/query generation (lowering AST to target-specific syntax)
267
+ * - Codec registration (encoding/decoding between JS and wire types)
268
+ * - Type mappings and coercions
269
+ *
270
+ * Adapters are bound to a specific family+target combination and work with
271
+ * any compatible driver for that target.
272
+ *
273
+ * Extended by plane-specific descriptors:
274
+ * - `ControlAdapterDescriptor` - control-plane factory
275
+ * - `RuntimeAdapterDescriptor` - runtime factory
276
+ *
277
+ * @template TFamilyId - Literal type for the family identifier
278
+ * @template TTargetId - Literal type for the target identifier
279
+ *
280
+ * @example
281
+ * ```ts
282
+ * import postgresAdapter from '@prisma-next/adapter-postgres/control';
283
+ *
284
+ * postgresAdapter.kind // 'adapter'
285
+ * postgresAdapter.familyId // 'sql'
286
+ * postgresAdapter.targetId // 'postgres'
287
+ * ```
288
+ */
289
+ export interface AdapterDescriptor<TFamilyId extends string, TTargetId extends string>
290
+ extends ComponentDescriptor<'adapter'> {
291
+ /** The family this adapter belongs to */
292
+ readonly familyId: TFamilyId;
293
+
294
+ /** The target this adapter is designed for */
295
+ readonly targetId: TTargetId;
296
+ }
297
+
298
+ /**
299
+ * Descriptor for a driver component.
300
+ *
301
+ * A "driver" provides the connection and execution layer for a target.
302
+ * Drivers handle:
303
+ * - Connection management (pooling, timeouts, retries)
304
+ * - Query execution (sending SQL/commands, receiving results)
305
+ * - Transaction management
306
+ * - Wire protocol communication
307
+ *
308
+ * Drivers are bound to a specific family+target and work with any compatible
309
+ * adapter. Multiple drivers can exist for the same target (e.g., node-postgres
310
+ * vs postgres.js for Postgres).
311
+ *
312
+ * Extended by plane-specific descriptors:
313
+ * - `ControlDriverDescriptor` - creates driver from connection URL
314
+ * - `RuntimeDriverDescriptor` - creates driver with runtime options
315
+ *
316
+ * @template TFamilyId - Literal type for the family identifier
317
+ * @template TTargetId - Literal type for the target identifier
318
+ *
319
+ * @example
320
+ * ```ts
321
+ * import postgresDriver from '@prisma-next/driver-postgres/control';
322
+ *
323
+ * postgresDriver.kind // 'driver'
324
+ * postgresDriver.familyId // 'sql'
325
+ * postgresDriver.targetId // 'postgres'
326
+ * ```
327
+ */
328
+ export interface DriverDescriptor<TFamilyId extends string, TTargetId extends string>
329
+ extends ComponentDescriptor<'driver'> {
330
+ /** The family this driver belongs to */
331
+ readonly familyId: TFamilyId;
332
+
333
+ /** The target this driver connects to */
334
+ readonly targetId: TTargetId;
335
+ }
336
+
337
+ /**
338
+ * Descriptor for an extension component.
339
+ *
340
+ * An "extension" adds optional capabilities to a target. Extensions can provide:
341
+ * - Additional operations (e.g., vector similarity search with pgvector)
342
+ * - Custom types and codecs (e.g., vector type)
343
+ * - Extended query capabilities
344
+ *
345
+ * Extensions are bound to a specific family+target and are registered in the
346
+ * config alongside the core components. Multiple extensions can be used together.
347
+ *
348
+ * Extended by plane-specific descriptors:
349
+ * - `ControlExtensionDescriptor` - control-plane extension factory
350
+ * - `RuntimeExtensionDescriptor` - runtime extension factory
351
+ *
352
+ * @template TFamilyId - Literal type for the family identifier
353
+ * @template TTargetId - Literal type for the target identifier
354
+ *
355
+ * @example
356
+ * ```ts
357
+ * import pgvector from '@prisma-next/extension-pgvector/control';
358
+ *
359
+ * pgvector.kind // 'extension'
360
+ * pgvector.familyId // 'sql'
361
+ * pgvector.targetId // 'postgres'
362
+ * ```
363
+ */
364
+ export interface ExtensionDescriptor<TFamilyId extends string, TTargetId extends string>
365
+ extends ComponentDescriptor<'extension'> {
366
+ /** The family this extension belongs to */
367
+ readonly familyId: TFamilyId;
368
+
369
+ /** The target this extension is designed for */
370
+ readonly targetId: TTargetId;
371
+ }
372
+
373
+ /** Components bound to a specific family+target combination. */
374
+ export type TargetBoundComponentDescriptor<TFamilyId extends string, TTargetId extends string> =
375
+ | TargetDescriptor<TFamilyId, TTargetId>
376
+ | AdapterDescriptor<TFamilyId, TTargetId>
377
+ | DriverDescriptor<TFamilyId, TTargetId>
378
+ | ExtensionDescriptor<TFamilyId, TTargetId>;
379
+
380
+ export interface FamilyInstance<TFamilyId extends string> {
381
+ readonly familyId: TFamilyId;
382
+ }
383
+
384
+ export interface TargetInstance<TFamilyId extends string, TTargetId extends string> {
385
+ readonly familyId: TFamilyId;
386
+ readonly targetId: TTargetId;
387
+ }
388
+
389
+ export interface AdapterInstance<TFamilyId extends string, TTargetId extends string> {
390
+ readonly familyId: TFamilyId;
391
+ readonly targetId: TTargetId;
392
+ }
393
+
394
+ export interface DriverInstance<TFamilyId extends string, TTargetId extends string> {
395
+ readonly familyId: TFamilyId;
396
+ readonly targetId: TTargetId;
397
+ }
398
+
399
+ export interface ExtensionInstance<TFamilyId extends string, TTargetId extends string> {
400
+ readonly familyId: TFamilyId;
401
+ readonly targetId: TTargetId;
402
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Specifies how to import TypeScript types from a package.
3
+ * Used in extension pack manifests to declare codec and operation type imports.
4
+ */
5
+ export interface TypesImportSpec {
6
+ readonly package: string;
7
+ readonly named: string;
8
+ readonly alias: string;
9
+ }