@prisma-next/sql-contract-ts 0.3.0-dev.135 → 0.3.0-dev.146
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/README.md +107 -154
- package/dist/config-types.d.mts +2 -2
- package/dist/config-types.d.mts.map +1 -1
- package/dist/config-types.mjs +2 -2
- package/dist/config-types.mjs.map +1 -1
- package/dist/contract-builder.d.mts +192 -236
- package/dist/contract-builder.d.mts.map +1 -1
- package/dist/contract-builder.mjs +317 -422
- package/dist/contract-builder.mjs.map +1 -1
- package/package.json +8 -6
- package/schemas/data-contract-sql-v1.json +6 -6
- package/src/authoring-helper-runtime.ts +2 -2
- package/src/authoring-type-utils.ts +2 -2
- package/src/build-contract.ts +463 -0
- package/src/composed-authoring-helpers.ts +8 -6
- package/src/config-types.ts +3 -3
- package/src/contract-builder.ts +122 -636
- package/src/{semantic-contract.ts → contract-definition.ts} +33 -16
- package/src/{staged-contract-dsl.ts → contract-dsl.ts} +30 -28
- package/src/{staged-contract-lowering.ts → contract-lowering.ts} +46 -48
- package/src/{staged-contract-types.ts → contract-types.ts} +185 -145
- package/src/{staged-contract-warnings.ts → contract-warnings.ts} +18 -21
- package/src/exports/contract-builder.ts +12 -13
- package/src/contract-ir-builder.ts +0 -475
- package/src/contract.ts +0 -475
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
1
|
+
import type {
|
|
2
|
+
ColumnDefault,
|
|
3
|
+
Contract,
|
|
4
|
+
ContractRelation,
|
|
5
|
+
StorageHashBase,
|
|
6
|
+
} from '@prisma-next/contract/types';
|
|
7
|
+
import type { ExtensionPackRef, TargetPackRef } from '@prisma-next/framework-components/components';
|
|
3
8
|
import type {
|
|
4
9
|
ContractWithTypeMaps,
|
|
5
10
|
Index,
|
|
6
11
|
ReferentialAction,
|
|
7
|
-
SqlContract,
|
|
8
12
|
StorageTypeInstance,
|
|
9
13
|
TypeMaps,
|
|
10
14
|
} from '@prisma-next/sql-contract/types';
|
|
11
15
|
import type { UnionToIntersection } from './authoring-type-utils';
|
|
12
|
-
import type {
|
|
13
|
-
AttributeStageIdFieldNames,
|
|
14
|
-
FieldStateOf,
|
|
15
|
-
ScalarFieldBuilder,
|
|
16
|
-
} from './staged-contract-dsl';
|
|
16
|
+
import type { AttributeStageIdFieldNames, FieldStateOf, ScalarFieldBuilder } from './contract-dsl';
|
|
17
17
|
|
|
18
18
|
export type ExtractCodecTypesFromPack<P> = P extends { __codecTypes?: infer C }
|
|
19
19
|
? C extends Record<string, { output: unknown }>
|
|
@@ -39,38 +39,32 @@ export type MergeExtensionPackRefs<
|
|
|
39
39
|
Added extends Record<string, ExtensionPackRef<'sql', string>>,
|
|
40
40
|
> = Existing extends Record<string, unknown> ? Existing & Added : Added;
|
|
41
41
|
|
|
42
|
-
type
|
|
42
|
+
type DefinitionExtensionPacks<Definition> = Definition extends {
|
|
43
43
|
readonly extensionPacks?: infer Packs extends Record<string, ExtensionPackRef<'sql', string>>;
|
|
44
44
|
}
|
|
45
45
|
? Packs
|
|
46
46
|
: Record<never, never>;
|
|
47
47
|
|
|
48
|
-
type
|
|
48
|
+
type DefinitionCapabilities<Definition> = Definition extends {
|
|
49
49
|
readonly capabilities?: infer Capabilities extends Record<string, Record<string, boolean>>;
|
|
50
50
|
}
|
|
51
51
|
? Capabilities
|
|
52
52
|
: undefined;
|
|
53
53
|
|
|
54
|
-
type
|
|
54
|
+
type DefinitionTargetId<Definition> = Definition extends {
|
|
55
55
|
readonly target: TargetPackRef<'sql', infer Target>;
|
|
56
56
|
}
|
|
57
57
|
? Target
|
|
58
58
|
: never;
|
|
59
59
|
|
|
60
|
-
type StagedDefinitionStorageHash<Definition> = Definition extends {
|
|
61
|
-
readonly storageHash?: infer StorageHash extends string;
|
|
62
|
-
}
|
|
63
|
-
? StorageHash
|
|
64
|
-
: undefined;
|
|
65
|
-
|
|
66
60
|
type Present<T> = Exclude<T, undefined>;
|
|
67
61
|
|
|
68
|
-
type
|
|
62
|
+
type CodecTypesFromDefinition<Definition> = ExtractCodecTypesFromPack<
|
|
69
63
|
Definition extends { readonly target: infer Target } ? Target : never
|
|
70
64
|
> &
|
|
71
|
-
MergeExtensionCodecTypesSafe<
|
|
65
|
+
MergeExtensionCodecTypesSafe<DefinitionExtensionPacks<Definition>>;
|
|
72
66
|
|
|
73
|
-
type
|
|
67
|
+
type DefinitionModels<Definition> = Definition extends {
|
|
74
68
|
readonly models?: unknown;
|
|
75
69
|
}
|
|
76
70
|
? Present<Definition['models']> extends Record<string, unknown>
|
|
@@ -78,7 +72,7 @@ type StagedDefinitionModels<Definition> = Definition extends {
|
|
|
78
72
|
: Record<never, never>
|
|
79
73
|
: Record<never, never>;
|
|
80
74
|
|
|
81
|
-
type
|
|
75
|
+
type DefinitionTypes<Definition> = Definition extends {
|
|
82
76
|
readonly types?: unknown;
|
|
83
77
|
}
|
|
84
78
|
? Present<Definition['types']> extends Record<string, StorageTypeInstance>
|
|
@@ -86,13 +80,13 @@ type StagedDefinitionTypes<Definition> = Definition extends {
|
|
|
86
80
|
: Record<never, never>
|
|
87
81
|
: Record<never, never>;
|
|
88
82
|
|
|
89
|
-
type
|
|
83
|
+
type DefinitionTableNaming<Definition> = Definition extends {
|
|
90
84
|
readonly naming?: { readonly tables?: infer Strategy extends string };
|
|
91
85
|
}
|
|
92
86
|
? Strategy
|
|
93
87
|
: undefined;
|
|
94
88
|
|
|
95
|
-
type
|
|
89
|
+
type DefinitionColumnNaming<Definition> = Definition extends {
|
|
96
90
|
readonly naming?: { readonly columns?: infer Strategy extends string };
|
|
97
91
|
}
|
|
98
92
|
? Strategy
|
|
@@ -139,43 +133,60 @@ type ApplyNamingType<Name extends string, Strategy extends string | undefined> =
|
|
|
139
133
|
? SnakeCase<Name>
|
|
140
134
|
: Name;
|
|
141
135
|
|
|
142
|
-
type
|
|
136
|
+
type ModelNames<Definition> = keyof DefinitionModels<Definition> & string;
|
|
143
137
|
|
|
144
|
-
type
|
|
138
|
+
type ModelFields<
|
|
145
139
|
Definition,
|
|
146
|
-
ModelName extends
|
|
147
|
-
> =
|
|
140
|
+
ModelName extends ModelNames<Definition>,
|
|
141
|
+
> = DefinitionModels<Definition>[ModelName] extends {
|
|
148
142
|
readonly stageOne: {
|
|
149
143
|
readonly fields: Record<string, ScalarFieldBuilder>;
|
|
150
144
|
};
|
|
151
145
|
}
|
|
152
|
-
?
|
|
146
|
+
? DefinitionModels<Definition>[ModelName]['stageOne']['fields']
|
|
147
|
+
: Record<never, never>;
|
|
148
|
+
|
|
149
|
+
type ModelFieldNames<Definition, ModelName extends ModelNames<Definition>> = keyof ModelFields<
|
|
150
|
+
Definition,
|
|
151
|
+
ModelName
|
|
152
|
+
> &
|
|
153
|
+
string;
|
|
154
|
+
|
|
155
|
+
type StagedModelRelations<
|
|
156
|
+
Definition,
|
|
157
|
+
ModelName extends ModelNames<Definition>,
|
|
158
|
+
> = DefinitionModels<Definition>[ModelName] extends {
|
|
159
|
+
readonly stageOne: { readonly relations: infer R };
|
|
160
|
+
}
|
|
161
|
+
? R extends Record<string, unknown>
|
|
162
|
+
? R
|
|
163
|
+
: Record<never, never>
|
|
153
164
|
: Record<never, never>;
|
|
154
165
|
|
|
155
|
-
type
|
|
166
|
+
type StagedModelRelationNames<
|
|
156
167
|
Definition,
|
|
157
|
-
ModelName extends
|
|
158
|
-
> = keyof
|
|
168
|
+
ModelName extends ModelNames<Definition>,
|
|
169
|
+
> = keyof StagedModelRelations<Definition, ModelName> & string;
|
|
159
170
|
|
|
160
|
-
type
|
|
171
|
+
type ModelFieldState<
|
|
161
172
|
Definition,
|
|
162
|
-
ModelName extends
|
|
163
|
-
FieldName extends
|
|
164
|
-
> = FieldStateOf<
|
|
173
|
+
ModelName extends ModelNames<Definition>,
|
|
174
|
+
FieldName extends ModelFieldNames<Definition, ModelName>,
|
|
175
|
+
> = FieldStateOf<ModelFields<Definition, ModelName>[FieldName]>;
|
|
165
176
|
|
|
166
|
-
type
|
|
177
|
+
type ModelSql<
|
|
167
178
|
Definition,
|
|
168
|
-
ModelName extends
|
|
169
|
-
> =
|
|
179
|
+
ModelName extends ModelNames<Definition>,
|
|
180
|
+
> = DefinitionModels<Definition>[ModelName] extends {
|
|
170
181
|
readonly __sql: infer SqlSpec;
|
|
171
182
|
}
|
|
172
183
|
? SqlSpec
|
|
173
184
|
: undefined;
|
|
174
185
|
|
|
175
|
-
type
|
|
186
|
+
type ModelAttributes<
|
|
176
187
|
Definition,
|
|
177
|
-
ModelName extends
|
|
178
|
-
> =
|
|
188
|
+
ModelName extends ModelNames<Definition>,
|
|
189
|
+
> = DefinitionModels<Definition>[ModelName] extends {
|
|
179
190
|
readonly __attributes: infer AttributesSpec;
|
|
180
191
|
}
|
|
181
192
|
? AttributesSpec
|
|
@@ -228,14 +239,14 @@ type DescriptorTypeRef<Descriptor> = Descriptor extends {
|
|
|
228
239
|
: undefined;
|
|
229
240
|
|
|
230
241
|
type LookupNamedStorageTypeKeyByValue<Definition, TypeRef extends StorageTypeInstance> = {
|
|
231
|
-
[TypeName in keyof
|
|
232
|
-
|
|
242
|
+
[TypeName in keyof DefinitionTypes<Definition> & string]: [TypeRef] extends [
|
|
243
|
+
DefinitionTypes<Definition>[TypeName],
|
|
233
244
|
]
|
|
234
|
-
? [
|
|
245
|
+
? [DefinitionTypes<Definition>[TypeName]] extends [TypeRef]
|
|
235
246
|
? TypeName
|
|
236
247
|
: never
|
|
237
248
|
: never;
|
|
238
|
-
}[keyof
|
|
249
|
+
}[keyof DefinitionTypes<Definition> & string];
|
|
239
250
|
|
|
240
251
|
type ResolveNamedStorageTypeKey<Definition, TypeRef> = TypeRef extends string
|
|
241
252
|
? TypeRef
|
|
@@ -247,8 +258,8 @@ type ResolveNamedStorageTypeKey<Definition, TypeRef> = TypeRef extends string
|
|
|
247
258
|
|
|
248
259
|
type ResolveNamedStorageType<Definition, TypeRef> =
|
|
249
260
|
ResolveNamedStorageTypeKey<Definition, TypeRef> extends infer TypeName extends string
|
|
250
|
-
? TypeName extends keyof
|
|
251
|
-
?
|
|
261
|
+
? TypeName extends keyof DefinitionTypes<Definition>
|
|
262
|
+
? DefinitionTypes<Definition>[TypeName]
|
|
252
263
|
: StorageTypeInstance
|
|
253
264
|
: StorageTypeInstance;
|
|
254
265
|
|
|
@@ -270,98 +281,96 @@ type ResolveFieldColumnTypeParams<Definition, FieldState> = [
|
|
|
270
281
|
? undefined
|
|
271
282
|
: DescriptorTypeParams<FieldDescriptorOf<FieldState>>;
|
|
272
283
|
|
|
273
|
-
type
|
|
284
|
+
type ModelTableName<Definition, ModelName extends ModelNames<Definition>> = [
|
|
274
285
|
Present<
|
|
275
|
-
|
|
276
|
-
? TableName
|
|
277
|
-
: never
|
|
286
|
+
ModelSql<Definition, ModelName> extends { readonly table?: infer TableName } ? TableName : never
|
|
278
287
|
>,
|
|
279
288
|
] extends [never]
|
|
280
|
-
? ApplyNamingType<ModelName,
|
|
289
|
+
? ApplyNamingType<ModelName, DefinitionTableNaming<Definition>>
|
|
281
290
|
: Present<
|
|
282
|
-
|
|
291
|
+
ModelSql<Definition, ModelName> extends { readonly table?: infer TableName }
|
|
283
292
|
? TableName
|
|
284
293
|
: never
|
|
285
294
|
> extends infer ExplicitTableName extends string
|
|
286
295
|
? ExplicitTableName
|
|
287
|
-
: ApplyNamingType<ModelName,
|
|
296
|
+
: ApplyNamingType<ModelName, DefinitionTableNaming<Definition>>;
|
|
288
297
|
|
|
289
|
-
type
|
|
298
|
+
type ModelColumnName<
|
|
290
299
|
Definition,
|
|
291
|
-
ModelName extends
|
|
292
|
-
FieldName extends
|
|
293
|
-
> = [FieldColumnOverrideOf<
|
|
294
|
-
? ApplyNamingType<FieldName,
|
|
300
|
+
ModelName extends ModelNames<Definition>,
|
|
301
|
+
FieldName extends ModelFieldNames<Definition, ModelName>,
|
|
302
|
+
> = [FieldColumnOverrideOf<ModelFieldState<Definition, ModelName, FieldName>>] extends [never]
|
|
303
|
+
? ApplyNamingType<FieldName, DefinitionColumnNaming<Definition>>
|
|
295
304
|
: FieldColumnOverrideOf<
|
|
296
|
-
|
|
305
|
+
ModelFieldState<Definition, ModelName, FieldName>
|
|
297
306
|
> extends infer ExplicitColumnName extends string
|
|
298
307
|
? ExplicitColumnName
|
|
299
|
-
: ApplyNamingType<FieldName,
|
|
308
|
+
: ApplyNamingType<FieldName, DefinitionColumnNaming<Definition>>;
|
|
300
309
|
|
|
301
|
-
type
|
|
310
|
+
type FieldNamesToColumnNames<
|
|
302
311
|
Definition,
|
|
303
|
-
ModelName extends
|
|
312
|
+
ModelName extends ModelNames<Definition>,
|
|
304
313
|
FieldNames extends readonly string[],
|
|
305
314
|
> = FieldNames extends readonly []
|
|
306
315
|
? readonly []
|
|
307
316
|
: FieldNames extends readonly [
|
|
308
|
-
infer First extends
|
|
317
|
+
infer First extends ModelFieldNames<Definition, ModelName>,
|
|
309
318
|
...infer Rest extends readonly string[],
|
|
310
319
|
]
|
|
311
320
|
? readonly [
|
|
312
|
-
|
|
313
|
-
...
|
|
321
|
+
ModelColumnName<Definition, ModelName, First>,
|
|
322
|
+
...FieldNamesToColumnNames<Definition, ModelName, Rest>,
|
|
314
323
|
]
|
|
315
324
|
: readonly string[];
|
|
316
325
|
|
|
317
|
-
type
|
|
318
|
-
[FieldName in
|
|
319
|
-
FieldInlineIdSpecOf<
|
|
326
|
+
type InlineIdFieldName<Definition, ModelName extends ModelNames<Definition>> = {
|
|
327
|
+
[FieldName in ModelFieldNames<Definition, ModelName>]: [
|
|
328
|
+
FieldInlineIdSpecOf<ModelFieldState<Definition, ModelName, FieldName>>,
|
|
320
329
|
] extends [never]
|
|
321
330
|
? never
|
|
322
331
|
: FieldName;
|
|
323
|
-
}[
|
|
332
|
+
}[ModelFieldNames<Definition, ModelName>];
|
|
324
333
|
|
|
325
|
-
type
|
|
326
|
-
|
|
334
|
+
type InlineIdFieldNames<Definition, ModelName extends ModelNames<Definition>> = [
|
|
335
|
+
InlineIdFieldName<Definition, ModelName>,
|
|
327
336
|
] extends [never]
|
|
328
337
|
? undefined
|
|
329
|
-
: readonly [
|
|
338
|
+
: readonly [InlineIdFieldName<Definition, ModelName>];
|
|
330
339
|
|
|
331
|
-
type
|
|
332
|
-
[FieldName in
|
|
333
|
-
|
|
340
|
+
type InlineIdName<Definition, ModelName extends ModelNames<Definition>> = {
|
|
341
|
+
[FieldName in ModelFieldNames<Definition, ModelName>]: FieldInlineIdSpecOf<
|
|
342
|
+
ModelFieldState<Definition, ModelName, FieldName>
|
|
334
343
|
> extends { readonly name?: infer Name extends string }
|
|
335
344
|
? Name
|
|
336
345
|
: never;
|
|
337
|
-
}[
|
|
346
|
+
}[ModelFieldNames<Definition, ModelName>];
|
|
338
347
|
|
|
339
|
-
type
|
|
348
|
+
type AttributeIdFieldNames<
|
|
340
349
|
Definition,
|
|
341
|
-
ModelName extends
|
|
342
|
-
> = AttributeStageIdFieldNames<
|
|
350
|
+
ModelName extends ModelNames<Definition>,
|
|
351
|
+
> = AttributeStageIdFieldNames<ModelAttributes<Definition, ModelName>>;
|
|
343
352
|
|
|
344
|
-
type
|
|
345
|
-
|
|
353
|
+
type AttributeIdName<Definition, ModelName extends ModelNames<Definition>> = Present<
|
|
354
|
+
ModelAttributes<Definition, ModelName> extends {
|
|
346
355
|
readonly id?: { readonly name?: infer Name extends string };
|
|
347
356
|
}
|
|
348
357
|
? Name
|
|
349
358
|
: never
|
|
350
359
|
>;
|
|
351
360
|
|
|
352
|
-
type
|
|
353
|
-
|
|
361
|
+
type ModelIdFieldNames<Definition, ModelName extends ModelNames<Definition>> = [
|
|
362
|
+
AttributeIdFieldNames<Definition, ModelName>,
|
|
354
363
|
] extends [undefined]
|
|
355
|
-
?
|
|
356
|
-
:
|
|
364
|
+
? InlineIdFieldNames<Definition, ModelName>
|
|
365
|
+
: AttributeIdFieldNames<Definition, ModelName>;
|
|
357
366
|
|
|
358
|
-
type
|
|
359
|
-
|
|
367
|
+
type ModelIdName<Definition, ModelName extends ModelNames<Definition>> = [
|
|
368
|
+
AttributeIdName<Definition, ModelName>,
|
|
360
369
|
] extends [never]
|
|
361
|
-
? Present<
|
|
362
|
-
:
|
|
370
|
+
? Present<InlineIdName<Definition, ModelName>>
|
|
371
|
+
: AttributeIdName<Definition, ModelName>;
|
|
363
372
|
|
|
364
|
-
type
|
|
373
|
+
type StorageColumn<
|
|
365
374
|
CodecId extends string,
|
|
366
375
|
Nullable extends boolean,
|
|
367
376
|
NativeType extends string,
|
|
@@ -377,67 +386,70 @@ type StagedStorageColumn<
|
|
|
377
386
|
? { readonly typeParams: TypeParams }
|
|
378
387
|
: Record<string, never>);
|
|
379
388
|
|
|
380
|
-
type
|
|
389
|
+
type ModelStorageColumn<
|
|
381
390
|
Definition,
|
|
382
|
-
ModelName extends
|
|
391
|
+
ModelName extends ModelNames<Definition>,
|
|
383
392
|
FieldName extends string,
|
|
384
|
-
> = FieldName extends
|
|
385
|
-
?
|
|
393
|
+
> = FieldName extends ModelFieldNames<Definition, ModelName>
|
|
394
|
+
? StorageColumn<
|
|
386
395
|
DescriptorCodecId<
|
|
387
|
-
ResolveFieldDescriptor<Definition,
|
|
396
|
+
ResolveFieldDescriptor<Definition, ModelFieldState<Definition, ModelName, FieldName>>
|
|
388
397
|
>,
|
|
389
|
-
FieldNullableOf<
|
|
398
|
+
FieldNullableOf<ModelFieldState<Definition, ModelName, FieldName>>,
|
|
390
399
|
DescriptorNativeType<
|
|
391
|
-
ResolveFieldDescriptor<Definition,
|
|
392
|
-
>,
|
|
393
|
-
ResolveFieldColumnTypeRef<
|
|
394
|
-
Definition,
|
|
395
|
-
StagedModelFieldState<Definition, ModelName, FieldName>
|
|
400
|
+
ResolveFieldDescriptor<Definition, ModelFieldState<Definition, ModelName, FieldName>>
|
|
396
401
|
>,
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
StagedModelFieldState<Definition, ModelName, FieldName>
|
|
400
|
-
>
|
|
402
|
+
ResolveFieldColumnTypeRef<Definition, ModelFieldState<Definition, ModelName, FieldName>>,
|
|
403
|
+
ResolveFieldColumnTypeParams<Definition, ModelFieldState<Definition, ModelName, FieldName>>
|
|
401
404
|
>
|
|
402
405
|
: never;
|
|
403
406
|
|
|
404
|
-
type
|
|
405
|
-
readonly [ModelName in
|
|
407
|
+
type BuiltModels<Definition> = {
|
|
408
|
+
readonly [ModelName in ModelNames<Definition>]: {
|
|
406
409
|
readonly storage: {
|
|
407
|
-
readonly table:
|
|
410
|
+
readonly table: ModelTableName<Definition, ModelName>;
|
|
411
|
+
readonly fields: {
|
|
412
|
+
readonly [FieldName in ModelFieldNames<Definition, ModelName>]: {
|
|
413
|
+
readonly column: ModelColumnName<Definition, ModelName, FieldName>;
|
|
414
|
+
};
|
|
415
|
+
};
|
|
408
416
|
};
|
|
409
417
|
readonly fields: {
|
|
410
|
-
readonly [FieldName in
|
|
411
|
-
readonly
|
|
418
|
+
readonly [FieldName in ModelFieldNames<Definition, ModelName>]: {
|
|
419
|
+
readonly nullable: ModelStorageColumn<Definition, ModelName, FieldName>['nullable'];
|
|
420
|
+
readonly type: {
|
|
421
|
+
readonly kind: 'scalar';
|
|
422
|
+
readonly codecId: ModelStorageColumn<Definition, ModelName, FieldName>['codecId'];
|
|
423
|
+
};
|
|
412
424
|
};
|
|
413
425
|
};
|
|
426
|
+
readonly relations: {
|
|
427
|
+
readonly [RelName in StagedModelRelationNames<Definition, ModelName>]: ContractRelation;
|
|
428
|
+
};
|
|
414
429
|
};
|
|
415
430
|
};
|
|
416
431
|
|
|
417
|
-
type
|
|
432
|
+
type BuiltModelColumnMappings<
|
|
418
433
|
Definition,
|
|
419
|
-
ModelName extends
|
|
420
|
-
> =
|
|
434
|
+
ModelName extends ModelNames<Definition>,
|
|
435
|
+
> = BuiltModels<Definition>[ModelName]['storage']['fields'];
|
|
421
436
|
|
|
422
|
-
type
|
|
437
|
+
type BuiltModelTableName<
|
|
423
438
|
Definition,
|
|
424
|
-
ModelName extends
|
|
425
|
-
> =
|
|
439
|
+
ModelName extends ModelNames<Definition>,
|
|
440
|
+
> = BuiltModels<Definition>[ModelName]['storage']['table'];
|
|
426
441
|
|
|
427
|
-
type
|
|
428
|
-
readonly [FieldName in keyof
|
|
429
|
-
string as
|
|
442
|
+
type BuiltStorageTableColumns<Definition, ModelName extends ModelNames<Definition>> = {
|
|
443
|
+
readonly [FieldName in keyof BuiltModelColumnMappings<Definition, ModelName> &
|
|
444
|
+
string as BuiltModelColumnMappings<
|
|
430
445
|
Definition,
|
|
431
446
|
ModelName
|
|
432
|
-
>[FieldName]['column']]:
|
|
447
|
+
>[FieldName]['column']]: ModelStorageColumn<Definition, ModelName, FieldName>;
|
|
433
448
|
};
|
|
434
449
|
|
|
435
|
-
type
|
|
436
|
-
readonly [ModelName in
|
|
437
|
-
Definition,
|
|
438
|
-
ModelName
|
|
439
|
-
>]: {
|
|
440
|
-
readonly columns: StagedBuiltStorageTableColumns<Definition, ModelName>;
|
|
450
|
+
type BuiltStorageTables<Definition> = {
|
|
451
|
+
readonly [ModelName in ModelNames<Definition> as BuiltModelTableName<Definition, ModelName>]: {
|
|
452
|
+
readonly columns: BuiltStorageTableColumns<Definition, ModelName>;
|
|
441
453
|
readonly uniques: ReadonlyArray<{
|
|
442
454
|
readonly columns: readonly string[];
|
|
443
455
|
readonly name?: string;
|
|
@@ -452,43 +464,71 @@ type StagedBuiltStorageTables<Definition> = {
|
|
|
452
464
|
readonly constraint: boolean;
|
|
453
465
|
readonly index: boolean;
|
|
454
466
|
}>;
|
|
455
|
-
} & (
|
|
467
|
+
} & (ModelIdFieldNames<Definition, ModelName> extends readonly string[]
|
|
456
468
|
? {
|
|
457
469
|
readonly primaryKey: {
|
|
458
|
-
readonly columns:
|
|
470
|
+
readonly columns: FieldNamesToColumnNames<
|
|
459
471
|
Definition,
|
|
460
472
|
ModelName,
|
|
461
|
-
|
|
473
|
+
ModelIdFieldNames<Definition, ModelName>
|
|
462
474
|
>;
|
|
463
|
-
readonly name?:
|
|
475
|
+
readonly name?: ModelIdName<Definition, ModelName>;
|
|
464
476
|
};
|
|
465
477
|
}
|
|
466
478
|
: Record<string, never>);
|
|
467
479
|
};
|
|
468
480
|
|
|
469
|
-
type
|
|
470
|
-
readonly
|
|
471
|
-
readonly
|
|
481
|
+
type BuiltStorage<Definition> = {
|
|
482
|
+
readonly storageHash: StorageHashBase<string>;
|
|
483
|
+
readonly tables: BuiltStorageTables<Definition>;
|
|
484
|
+
readonly types: DefinitionTypes<Definition>;
|
|
485
|
+
};
|
|
486
|
+
|
|
487
|
+
type FieldOutputType<
|
|
488
|
+
Definition,
|
|
489
|
+
ModelName extends ModelNames<Definition>,
|
|
490
|
+
FieldName extends ModelFieldNames<Definition, ModelName>,
|
|
491
|
+
> = ModelStorageColumn<Definition, ModelName, FieldName> extends infer Col
|
|
492
|
+
? Col extends { readonly codecId: infer Id extends string }
|
|
493
|
+
? Id extends keyof CodecTypesFromDefinition<Definition>
|
|
494
|
+
? CodecTypesFromDefinition<Definition>[Id] extends { readonly output: infer O }
|
|
495
|
+
? Col extends { readonly nullable: true }
|
|
496
|
+
? O | null
|
|
497
|
+
: O
|
|
498
|
+
: unknown
|
|
499
|
+
: unknown
|
|
500
|
+
: unknown
|
|
501
|
+
: unknown;
|
|
502
|
+
|
|
503
|
+
type FieldOutputTypes<Definition> = {
|
|
504
|
+
readonly [ModelName in ModelNames<Definition>]: {
|
|
505
|
+
readonly [FieldName in ModelFieldNames<Definition, ModelName>]: FieldOutputType<
|
|
506
|
+
Definition,
|
|
507
|
+
ModelName,
|
|
508
|
+
FieldName
|
|
509
|
+
>;
|
|
510
|
+
};
|
|
472
511
|
};
|
|
473
512
|
|
|
474
513
|
export type SqlContractResult<Definition> = ContractWithTypeMaps<
|
|
475
|
-
|
|
476
|
-
readonly
|
|
477
|
-
readonly target: StagedDefinitionTargetId<Definition>;
|
|
514
|
+
Contract<BuiltStorage<Definition>, BuiltModels<Definition>> & {
|
|
515
|
+
readonly target: DefinitionTargetId<Definition>;
|
|
478
516
|
readonly targetFamily: 'sql';
|
|
479
|
-
readonly storageHash: StagedDefinitionStorageHash<Definition> extends string
|
|
480
|
-
? StagedDefinitionStorageHash<Definition>
|
|
481
|
-
: string;
|
|
482
517
|
} & {
|
|
483
|
-
readonly extensionPacks: keyof
|
|
518
|
+
readonly extensionPacks: keyof DefinitionExtensionPacks<Definition> extends never
|
|
484
519
|
? Record<string, never>
|
|
485
|
-
:
|
|
486
|
-
readonly capabilities:
|
|
520
|
+
: DefinitionExtensionPacks<Definition>;
|
|
521
|
+
readonly capabilities: DefinitionCapabilities<Definition> extends Record<
|
|
487
522
|
string,
|
|
488
523
|
Record<string, boolean>
|
|
489
524
|
>
|
|
490
|
-
?
|
|
525
|
+
? DefinitionCapabilities<Definition>
|
|
491
526
|
: Record<string, Record<string, boolean>>;
|
|
492
527
|
},
|
|
493
|
-
TypeMaps<
|
|
528
|
+
TypeMaps<
|
|
529
|
+
CodecTypesFromDefinition<Definition>,
|
|
530
|
+
Record<string, never>,
|
|
531
|
+
Record<string, never>,
|
|
532
|
+
FieldOutputTypes<Definition>
|
|
533
|
+
>
|
|
494
534
|
>;
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import type { StorageTypeInstance } from '@prisma-next/sql-contract/types';
|
|
2
2
|
import {
|
|
3
|
+
type ContractModelBuilder,
|
|
3
4
|
type ModelAttributesSpec,
|
|
4
5
|
normalizeRelationFieldNames,
|
|
5
6
|
type RelationBuilder,
|
|
7
|
+
type RelationState,
|
|
6
8
|
type ScalarFieldBuilder,
|
|
7
9
|
type SqlStageSpec,
|
|
8
|
-
|
|
9
|
-
type RelationState as StagedRelationState,
|
|
10
|
-
} from './staged-contract-dsl';
|
|
10
|
+
} from './contract-dsl';
|
|
11
11
|
|
|
12
|
-
type
|
|
12
|
+
type RuntimeModel = ContractModelBuilder<
|
|
13
13
|
string | undefined,
|
|
14
14
|
Record<string, ScalarFieldBuilder>,
|
|
15
|
-
Record<string, RelationBuilder<
|
|
15
|
+
Record<string, RelationBuilder<RelationState>>,
|
|
16
16
|
ModelAttributesSpec | undefined,
|
|
17
17
|
SqlStageSpec | undefined
|
|
18
18
|
>;
|
|
@@ -20,20 +20,17 @@ type RuntimeStagedModel = StagedModelBuilder<
|
|
|
20
20
|
type RuntimeModelSpec = {
|
|
21
21
|
readonly modelName: string;
|
|
22
22
|
readonly tableName: string;
|
|
23
|
-
readonly relations: Record<string, RelationBuilder<
|
|
23
|
+
readonly relations: Record<string, RelationBuilder<RelationState>>;
|
|
24
24
|
readonly sqlSpec: SqlStageSpec | undefined;
|
|
25
25
|
};
|
|
26
26
|
|
|
27
|
-
type
|
|
27
|
+
type RuntimeCollection = {
|
|
28
28
|
readonly storageTypes: Record<string, StorageTypeInstance>;
|
|
29
|
-
readonly models: Record<string,
|
|
29
|
+
readonly models: Record<string, RuntimeModel>;
|
|
30
30
|
readonly modelSpecs: ReadonlyMap<string, RuntimeModelSpec>;
|
|
31
31
|
};
|
|
32
32
|
|
|
33
|
-
function hasNamedModelToken(
|
|
34
|
-
models: Record<string, RuntimeStagedModel>,
|
|
35
|
-
modelName: string,
|
|
36
|
-
): boolean {
|
|
33
|
+
function hasNamedModelToken(models: Record<string, RuntimeModel>, modelName: string): boolean {
|
|
37
34
|
return models[modelName]?.stageOne.modelName === modelName;
|
|
38
35
|
}
|
|
39
36
|
|
|
@@ -65,8 +62,8 @@ function formatConstraintsRefCall(modelName: string, fieldNames: readonly string
|
|
|
65
62
|
|
|
66
63
|
function formatRelationModelDisplay(
|
|
67
64
|
relationModel:
|
|
68
|
-
|
|
|
69
|
-
| Extract<
|
|
65
|
+
| RelationState['toModel']
|
|
66
|
+
| Extract<RelationState, { kind: 'manyToMany' }>['through'],
|
|
70
67
|
): string {
|
|
71
68
|
if (relationModel.kind === 'lazyRelationModelName') {
|
|
72
69
|
return `() => ${relationModel.resolve()}`;
|
|
@@ -77,7 +74,7 @@ function formatRelationModelDisplay(
|
|
|
77
74
|
: relationModel.modelName;
|
|
78
75
|
}
|
|
79
76
|
|
|
80
|
-
function formatRelationCall(relation:
|
|
77
|
+
function formatRelationCall(relation: RelationState, targetModelDisplay: string): string {
|
|
81
78
|
if (relation.kind === 'belongsTo') {
|
|
82
79
|
const from = formatFieldSelection(normalizeRelationFieldNames(relation.from));
|
|
83
80
|
const to = formatFieldSelection(normalizeRelationFieldNames(relation.to));
|
|
@@ -96,7 +93,7 @@ function formatRelationCall(relation: StagedRelationState, targetModelDisplay: s
|
|
|
96
93
|
}
|
|
97
94
|
|
|
98
95
|
function formatManyToManyCallWithThrough(
|
|
99
|
-
relation: Extract<
|
|
96
|
+
relation: Extract<RelationState, { kind: 'manyToMany' }>,
|
|
100
97
|
throughDisplay: string,
|
|
101
98
|
): string {
|
|
102
99
|
const targetDisplay = formatRelationModelDisplay(relation.toModel);
|
|
@@ -120,7 +117,7 @@ function flushWarnings(warnings: readonly string[]): void {
|
|
|
120
117
|
}
|
|
121
118
|
|
|
122
119
|
process.emitWarning(
|
|
123
|
-
`${warnings.length}
|
|
120
|
+
`${warnings.length} contract references use string fallbacks where typed alternatives are available. ` +
|
|
124
121
|
'Use named model tokens and typed storage type refs for autocomplete and type safety.\n' +
|
|
125
122
|
warnings.map((w) => ` - ${w}`).join('\n'),
|
|
126
123
|
{ code: 'PN_CONTRACT_TYPED_FALLBACK_AVAILABLE' },
|
|
@@ -129,13 +126,13 @@ function flushWarnings(warnings: readonly string[]): void {
|
|
|
129
126
|
|
|
130
127
|
function formatFallbackWarning(location: string, current: string, suggested: string): string {
|
|
131
128
|
return (
|
|
132
|
-
`
|
|
129
|
+
`Contract ${location} uses ${current}. ` +
|
|
133
130
|
`Use ${suggested} when the named model token is available in the same contract to keep typed relation targets and model refs.`
|
|
134
131
|
);
|
|
135
132
|
}
|
|
136
133
|
|
|
137
134
|
export function emitTypedNamedTypeFallbackWarnings(
|
|
138
|
-
models: Record<string,
|
|
135
|
+
models: Record<string, RuntimeModel>,
|
|
139
136
|
storageTypes: Record<string, StorageTypeInstance>,
|
|
140
137
|
): void {
|
|
141
138
|
const warnings: string[] = [];
|
|
@@ -155,7 +152,7 @@ export function emitTypedNamedTypeFallbackWarnings(
|
|
|
155
152
|
warnedFields.add(warningKey);
|
|
156
153
|
|
|
157
154
|
warnings.push(
|
|
158
|
-
`
|
|
155
|
+
`Contract field "${modelName}.${fieldName}" uses field.namedType('${fieldState.typeRef}'). ` +
|
|
159
156
|
`Use field.namedType(types.${fieldState.typeRef}) when the storage type is declared in the same contract to keep autocomplete and typed local refs.`,
|
|
160
157
|
);
|
|
161
158
|
}
|
|
@@ -164,7 +161,7 @@ export function emitTypedNamedTypeFallbackWarnings(
|
|
|
164
161
|
flushWarnings(warnings);
|
|
165
162
|
}
|
|
166
163
|
|
|
167
|
-
export function emitTypedCrossModelFallbackWarnings(collection:
|
|
164
|
+
export function emitTypedCrossModelFallbackWarnings(collection: RuntimeCollection): void {
|
|
168
165
|
const warnings: string[] = [];
|
|
169
166
|
const warnedKeys = new Set<string>();
|
|
170
167
|
|