@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.
package/src/types.ts ADDED
@@ -0,0 +1,245 @@
1
+ import type { OperationRegistry } from '@prisma-next/operations';
2
+ import type { ContractIR } from './ir';
3
+
4
+ export interface ContractBase {
5
+ readonly schemaVersion: string;
6
+ readonly target: string;
7
+ readonly targetFamily: string;
8
+ readonly coreHash: string;
9
+ readonly profileHash?: string;
10
+ readonly capabilities?: Record<string, Record<string, boolean>>;
11
+ readonly extensionPacks?: Record<string, unknown>;
12
+ readonly meta?: Record<string, unknown>;
13
+ readonly sources?: Record<string, Source>;
14
+ }
15
+
16
+ export interface FieldType {
17
+ readonly type: string;
18
+ readonly nullable: boolean;
19
+ readonly items?: FieldType;
20
+ readonly properties?: Record<string, FieldType>;
21
+ }
22
+
23
+ export interface Source {
24
+ readonly readOnly: boolean;
25
+ readonly projection: Record<string, FieldType>;
26
+ readonly origin?: Record<string, unknown>;
27
+ readonly capabilities?: Record<string, boolean>;
28
+ }
29
+
30
+ // Document family types
31
+ export interface DocIndex {
32
+ readonly name: string;
33
+ readonly keys: Record<string, 'asc' | 'desc'>;
34
+ readonly unique?: boolean;
35
+ readonly where?: Expr;
36
+ }
37
+
38
+ export type Expr =
39
+ | { readonly kind: 'eq'; readonly path: ReadonlyArray<string>; readonly value: unknown }
40
+ | { readonly kind: 'exists'; readonly path: ReadonlyArray<string> };
41
+
42
+ export interface DocCollection {
43
+ readonly name: string;
44
+ readonly id?: {
45
+ readonly strategy: 'auto' | 'client' | 'uuid' | 'cuid' | 'objectId';
46
+ };
47
+ readonly fields: Record<string, FieldType>;
48
+ readonly indexes?: ReadonlyArray<DocIndex>;
49
+ readonly readOnly?: boolean;
50
+ }
51
+
52
+ export interface DocumentStorage {
53
+ readonly document: {
54
+ readonly collections: Record<string, DocCollection>;
55
+ };
56
+ }
57
+
58
+ export interface DocumentContract extends ContractBase {
59
+ // Accept string to work with JSON imports; runtime validation ensures 'document'
60
+ readonly targetFamily: string;
61
+ readonly storage: DocumentStorage;
62
+ }
63
+
64
+ // Plan types - target-family agnostic execution types
65
+ export interface ParamDescriptor {
66
+ readonly index?: number;
67
+ readonly name?: string;
68
+ readonly codecId?: string;
69
+ readonly nativeType?: string;
70
+ readonly nullable?: boolean;
71
+ readonly source: 'dsl' | 'raw';
72
+ readonly refs?: { table: string; column: string };
73
+ }
74
+
75
+ export interface PlanRefs {
76
+ readonly tables?: readonly string[];
77
+ readonly columns?: ReadonlyArray<{ table: string; column: string }>;
78
+ readonly indexes?: ReadonlyArray<{
79
+ readonly table: string;
80
+ readonly columns: ReadonlyArray<string>;
81
+ readonly name?: string;
82
+ }>;
83
+ }
84
+
85
+ export interface PlanMeta {
86
+ readonly target: string;
87
+ readonly targetFamily?: string;
88
+ readonly coreHash: string;
89
+ readonly profileHash?: string;
90
+ readonly lane: string;
91
+ readonly annotations?: {
92
+ codecs?: Record<string, string>; // alias/param → codec id ('ns/name@v')
93
+ [key: string]: unknown;
94
+ };
95
+ readonly paramDescriptors: ReadonlyArray<ParamDescriptor>;
96
+ readonly refs?: PlanRefs;
97
+ readonly projection?: Record<string, string> | ReadonlyArray<string>;
98
+ /**
99
+ * Optional mapping of projection alias → column type ID (fully qualified ns/name@version).
100
+ * Used for codec resolution when AST+refs don't provide enough type info.
101
+ */
102
+ readonly projectionTypes?: Record<string, string>;
103
+ }
104
+
105
+ /**
106
+ * Canonical execution plan shape used by runtimes.
107
+ *
108
+ * - Row is the inferred result row type (TypeScript-only).
109
+ * - Ast is the optional, family-specific AST type (e.g. SQL QueryAst).
110
+ *
111
+ * The payload executed by the runtime is represented by the sql + params pair
112
+ * for now; future families can specialize this via Ast or additional metadata.
113
+ */
114
+ export interface ExecutionPlan<Row = unknown, Ast = unknown> {
115
+ readonly sql: string;
116
+ readonly params: readonly unknown[];
117
+ readonly ast?: Ast;
118
+ readonly meta: PlanMeta;
119
+ /**
120
+ * Phantom property to carry the Row generic for type-level utilities.
121
+ * Not set at runtime; used only for ResultType extraction.
122
+ */
123
+ readonly _row?: Row;
124
+ }
125
+
126
+ /**
127
+ * Utility type to extract the Row type from an ExecutionPlan.
128
+ * Example: `type Row = ResultType<typeof plan>`
129
+ *
130
+ * Works with both ExecutionPlan and SqlQueryPlan (SQL query plans before lowering).
131
+ * SqlQueryPlan includes a phantom `_Row` property to preserve the generic parameter
132
+ * for type extraction.
133
+ */
134
+ export type ResultType<P> = P extends ExecutionPlan<infer R, unknown>
135
+ ? R
136
+ : P extends { readonly _Row?: infer R }
137
+ ? R
138
+ : never;
139
+
140
+ /**
141
+ * Type guard to check if a contract is a Document contract
142
+ */
143
+ export function isDocumentContract(contract: unknown): contract is DocumentContract {
144
+ return (
145
+ typeof contract === 'object' &&
146
+ contract !== null &&
147
+ 'targetFamily' in contract &&
148
+ contract.targetFamily === 'document'
149
+ );
150
+ }
151
+
152
+ /**
153
+ * Contract marker record stored in the database.
154
+ * Represents the current contract identity for a database.
155
+ */
156
+ export interface ContractMarkerRecord {
157
+ readonly coreHash: string;
158
+ readonly profileHash: string;
159
+ readonly contractJson: unknown | null;
160
+ readonly canonicalVersion: number | null;
161
+ readonly updatedAt: Date;
162
+ readonly appTag: string | null;
163
+ readonly meta: Record<string, unknown>;
164
+ }
165
+
166
+ // Emitter types - moved from @prisma-next/emitter to shared location
167
+ /**
168
+ * Specifies how to import TypeScript types from a package.
169
+ * Used in extension pack manifests to declare codec and operation type imports.
170
+ */
171
+ export interface TypesImportSpec {
172
+ readonly package: string;
173
+ readonly named: string;
174
+ readonly alias: string;
175
+ }
176
+
177
+ /**
178
+ * Validation context passed to TargetFamilyHook.validateTypes().
179
+ * Contains pre-assembled operation registry, type imports, and extension IDs.
180
+ */
181
+ export interface ValidationContext {
182
+ readonly operationRegistry?: OperationRegistry;
183
+ readonly codecTypeImports?: ReadonlyArray<TypesImportSpec>;
184
+ readonly operationTypeImports?: ReadonlyArray<TypesImportSpec>;
185
+ readonly extensionIds?: ReadonlyArray<string>;
186
+ }
187
+
188
+ /**
189
+ * SPI interface for target family hooks that extend emission behavior.
190
+ * Implemented by family-specific emitter hooks (e.g., SQL family).
191
+ */
192
+ export interface TargetFamilyHook {
193
+ readonly id: string;
194
+
195
+ /**
196
+ * Validates that all type IDs in the contract come from referenced extension packs.
197
+ * @param ir - Contract IR to validate
198
+ * @param ctx - Validation context with operation registry and extension IDs
199
+ */
200
+ validateTypes(ir: ContractIR, ctx: ValidationContext): void;
201
+
202
+ /**
203
+ * Validates family-specific contract structure.
204
+ * @param ir - Contract IR to validate
205
+ */
206
+ validateStructure(ir: ContractIR): void;
207
+
208
+ /**
209
+ * Generates contract.d.ts file content.
210
+ * @param ir - Contract IR
211
+ * @param codecTypeImports - Array of codec type import specs
212
+ * @param operationTypeImports - Array of operation type import specs
213
+ * @returns Generated TypeScript type definitions as string
214
+ */
215
+ generateContractTypes(
216
+ ir: ContractIR,
217
+ codecTypeImports: ReadonlyArray<TypesImportSpec>,
218
+ operationTypeImports: ReadonlyArray<TypesImportSpec>,
219
+ ): string;
220
+ }
221
+
222
+ // Extension pack manifest types - moved from @prisma-next/core-control-plane to shared location
223
+ export type ArgSpecManifest =
224
+ | { readonly kind: 'typeId'; readonly type: string }
225
+ | { readonly kind: 'param' }
226
+ | { readonly kind: 'literal' };
227
+
228
+ export type ReturnSpecManifest =
229
+ | { readonly kind: 'typeId'; readonly type: string }
230
+ | { readonly kind: 'builtin'; readonly type: 'number' | 'boolean' | 'string' };
231
+
232
+ export interface LoweringSpecManifest {
233
+ readonly targetFamily: 'sql';
234
+ readonly strategy: 'infix' | 'function';
235
+ readonly template: string;
236
+ }
237
+
238
+ export interface OperationManifest {
239
+ readonly for: string;
240
+ readonly method: string;
241
+ readonly args: ReadonlyArray<ArgSpecManifest>;
242
+ readonly returns: ReturnSpecManifest;
243
+ readonly lowering: LoweringSpecManifest;
244
+ readonly capabilities?: ReadonlyArray<string>;
245
+ }