-
-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathtypes.ts
More file actions
366 lines (340 loc) · 13.8 KB
/
types.ts
File metadata and controls
366 lines (340 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import type {
FieldHasDefault,
FieldIsArray,
FieldIsComputed,
FieldIsDelegateDiscriminator,
FieldIsRelation,
GetEnum,
GetEnums,
GetModelFields,
GetModelFieldType,
GetModels,
GetTypeDefFields,
GetTypeDefFieldType,
GetTypeDefs,
ModelFieldIsOptional,
SchemaDef,
TypeDefFieldIsArray,
TypeDefFieldIsOptional,
} from '@zenstackhq/schema';
import type Decimal from 'decimal.js';
import type z from 'zod';
/**
* Scalar-only shape returned by the no-options `makeModelSchema` overload.
* Relation fields are excluded by default — use `include` or `select` to opt in.
*/
export type GetModelFieldsShape<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
[Field in GetModelFields<Schema, Model> as FieldIsRelation<Schema, Model, Field> extends true
? never
: Field]: ZodOptionalAndNullableIf<
ZodArrayIf<MapModelFieldToZod<Schema, Model, Field>, FieldIsArray<Schema, Model, Field>>,
ModelFieldIsOptional<Schema, Model, Field>
>;
};
/**
* Full shape including both scalar and relation fields — used internally for
* type lookups (e.g. resolving relation field Zod types in include/select).
*/
type GetAllModelFieldsShape<Schema extends SchemaDef, Model extends GetModels<Schema>> = GetModelFieldsShape<
Schema,
Model
> & {
// relation fields, always optional
[Field in GetModelFields<Schema, Model> as FieldIsRelation<Schema, Model, Field> extends true
? Field
: never]: ZodNullableIf<
z.ZodOptional<
ZodArrayIf<
z.ZodObject<
GetModelFieldsShape<
Schema,
GetModelFieldType<Schema, Model, Field> extends GetModels<Schema>
? GetModelFieldType<Schema, Model, Field>
: never
>,
z.core.$strict
>,
FieldIsArray<Schema, Model, Field>
>
>,
ModelFieldIsOptional<Schema, Model, Field>
>;
};
export type GetModelCreateFieldsShape<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
[Field in GetModelFields<Schema, Model> as FieldIsRelation<Schema, Model, Field> extends true
? never
: FieldIsComputed<Schema, Model, Field> extends true
? never
: FieldIsDelegateDiscriminator<Schema, Model, Field> extends true
? never
: Field]: ZodOptionalIf<
ZodOptionalAndNullableIf<
ZodArrayIf<MapModelFieldToZod<Schema, Model, Field>, FieldIsArray<Schema, Model, Field>>,
ModelFieldIsOptional<Schema, Model, Field>
>,
FieldHasDefault<Schema, Model, Field>
>;
};
export type GetModelUpdateFieldsShape<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
[Field in GetModelFields<Schema, Model> as FieldIsRelation<Schema, Model, Field> extends true
? never
: FieldIsComputed<Schema, Model, Field> extends true
? never
: FieldIsDelegateDiscriminator<Schema, Model, Field> extends true
? never
: Field]: z.ZodOptional<
ZodOptionalAndNullableIf<
ZodArrayIf<MapModelFieldToZod<Schema, Model, Field>, FieldIsArray<Schema, Model, Field>>,
ModelFieldIsOptional<Schema, Model, Field>
>
>;
};
export type GetTypeDefFieldsShape<Schema extends SchemaDef, Type extends GetTypeDefs<Schema>> = {
[Field in GetTypeDefFields<Schema, Type>]: ZodOptionalAndNullableIf<
ZodArrayIf<MapTypeDefFieldToZod<Schema, Type, Field>, TypeDefFieldIsArray<Schema, Type, Field>>,
TypeDefFieldIsOptional<Schema, Type, Field>
>;
};
type FieldTypeZodMap = {
String: z.ZodString;
Int: z.ZodNumber;
BigInt: z.ZodBigInt;
Float: z.ZodNumber;
Decimal: z.ZodType<Decimal>;
Boolean: z.ZodBoolean;
DateTime: z.ZodType<Date>;
Bytes: z.ZodType<Uint8Array>;
Json: JsonZodType;
};
type MapModelFieldToZod<
Schema extends SchemaDef,
Model extends GetModels<Schema>,
Field extends GetModelFields<Schema, Model>,
FieldType = GetModelFieldType<Schema, Model, Field>,
> = MapFieldTypeToZod<Schema, FieldType>;
type MapTypeDefFieldToZod<
Schema extends SchemaDef,
Type extends GetTypeDefs<Schema>,
Field extends GetTypeDefFields<Schema, Type>,
FieldType = GetTypeDefFieldType<Schema, Type, Field>,
> = MapFieldTypeToZod<Schema, FieldType>;
type MapFieldTypeToZod<Schema extends SchemaDef, FieldType> = FieldType extends keyof FieldTypeZodMap
? FieldTypeZodMap[FieldType]
: FieldType extends GetEnums<Schema>
? EnumZodType<Schema, FieldType>
: FieldType extends GetTypeDefs<Schema>
? z.ZodObject<GetTypeDefFieldsShape<Schema, FieldType>, z.core.$strict>
: z.ZodUnknown;
type JsonZodType =
| z.ZodObject<Record<string, z.ZodType>, z.core.$loose>
| z.ZodArray<z.ZodType>
| z.ZodString
| z.ZodNumber
| z.ZodBoolean
| z.ZodNull;
type EnumZodType<Schema extends SchemaDef, EnumName extends GetEnums<Schema>> = z.ZodEnum<{
[Key in keyof GetEnum<Schema, EnumName>]: GetEnum<Schema, EnumName>[Key];
}>;
type ZodOptionalAndNullableIf<T extends z.ZodType, Condition extends boolean> = Condition extends true
? z.ZodOptional<z.ZodNullable<T>>
: T;
type ZodOptionalIf<T extends z.ZodType, Condition extends boolean> = Condition extends true ? z.ZodOptional<T> : T;
type ZodNullableIf<T extends z.ZodType, Condition extends boolean> = Condition extends true ? z.ZodNullable<T> : T;
type ZodArrayIf<T extends z.ZodType, Condition extends boolean> = Condition extends true ? z.ZodArray<T> : T;
// -------------------------------------------------------------------------
// Query options types (ORM-style include / select / omit)
// -------------------------------------------------------------------------
/**
* The non-relation scalar fields of a model (excludes relation fields and
* foreign-key fields that back a relation).
*/
type ScalarModelFields<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
[Field in GetModelFields<Schema, Model> as FieldIsRelation<Schema, Model, Field> extends true
? never
: Field]: Field;
};
/**
* The relation fields of a model.
*/
type RelationModelFields<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
[Field in GetModelFields<Schema, Model> as FieldIsRelation<Schema, Model, Field> extends true
? Field
: never]: Field;
};
/**
* For a relation field, resolve the related model name.
*/
type RelatedModel<
Schema extends SchemaDef,
Model extends GetModels<Schema>,
Field extends GetModelFields<Schema, Model>,
> = GetModelFieldType<Schema, Model, Field> extends GetModels<Schema> ? GetModelFieldType<Schema, Model, Field> : never;
/**
* ORM-style query options accepted by `makeModelSchema`.
*
* Exactly mirrors the `select` / `include` / `omit` vocabulary:
* - `select` — pick specific fields (scalars and/or relations). Mutually
* exclusive with `include` and `omit`.
* - `include` — start with all scalar fields, then add the named relation
* fields. Can be combined with `omit`.
* - `omit` — remove named scalar fields from the default scalar set.
* Can be combined with `include`, mutually exclusive with
* `select`.
*/
export type ModelSchemaOptions<Schema extends SchemaDef, Model extends GetModels<Schema>> =
| {
/**
* Pick only the listed fields. Values must be `true` (include with
* default shape) or a nested options object (for relation fields).
* Only `true` is accepted — ORM convention.
*/
select: {
[Field in GetModelFields<Schema, Model>]?: FieldIsRelation<Schema, Model, Field> extends true
? true | ModelSchemaOptions<Schema, RelatedModel<Schema, Model, Field>>
: true;
};
include?: never;
omit?: never;
}
| {
select?: never;
/**
* Add the listed relation fields on top of the scalar fields.
* Values must be `true` (default shape) or a nested options object.
* Only `true` is accepted — ORM convention.
*/
include?: {
[Field in keyof RelationModelFields<Schema, Model>]?: Field extends GetModelFields<Schema, Model>
? true | ModelSchemaOptions<Schema, RelatedModel<Schema, Model, Field>>
: never;
};
/**
* Remove the listed scalar fields from the output.
* Only `true` is accepted — ORM convention.
*/
omit?: {
[Field in keyof ScalarModelFields<Schema, Model>]?: true;
};
};
// ---- Output shape helpers ------------------------------------------------
/**
* Narrows `Field` so it can safely index `GetModelFieldsShape`. The mapped
* type uses a `as`-remapping clause, so TypeScript widens the key set and
* `Field extends GetModelFields<…>` alone is not enough for indexing.
*/
type FieldInShape<
Schema extends SchemaDef,
Model extends GetModels<Schema>,
Field extends GetModelFields<Schema, Model>,
> = Field & keyof GetAllModelFieldsShape<Schema, Model>;
/**
* Zod shape produced when a relation field is included via `include: { field:
* true }` or `select: { field: true }` — identical to how the existing
* `makeModelSchema` (no-options) represents relation fields: optional, carries
* array-ness and nullability from the field definition.
*/
type RelationFieldZodDefault<
Schema extends SchemaDef,
Model extends GetModels<Schema>,
Field extends GetModelFields<Schema, Model>,
> = GetAllModelFieldsShape<Schema, Model>[FieldInShape<Schema, Model, Field>];
/**
* Zod shape for a relation field included with nested options. We recurse
* into `GetModelSchemaShapeWithOptions` for the related model, then re-apply
* the same optional/array/nullable wrappers as the default relation field.
*/
type RelationFieldZodWithOptions<
Schema extends SchemaDef,
Model extends GetModels<Schema>,
Field extends GetModelFields<Schema, Model>,
Options,
> =
RelatedModel<Schema, Model, Field> extends GetModels<Schema>
? ZodNullableIf<
z.ZodOptional<
ZodArrayIf<
z.ZodObject<
GetModelSchemaShapeWithOptions<Schema, RelatedModel<Schema, Model, Field>, Options>,
z.core.$strict
>,
FieldIsArray<Schema, Model, Field>
>
>,
ModelFieldIsOptional<Schema, Model, Field>
>
: never;
/**
* Resolve the Zod type for a single field given a select-entry value (`true`
* or a nested options object).
*/
type SelectEntryToZod<
Schema extends SchemaDef,
Model extends GetModels<Schema>,
Field extends GetModelFields<Schema, Model>,
Value,
> = Value extends boolean
? // `true` or widened `boolean` — use the default shape for this field.
// Handling `boolean` (not just literal `true`) prevents the type from
// collapsing to `never` when callers use a boolean variable instead of
// a literal (e.g. `const pick: boolean = true`).
GetAllModelFieldsShape<Schema, Model>[FieldInShape<Schema, Model, Field>]
: Value extends object
? // nested options — must be a relation field
RelationFieldZodWithOptions<Schema, Model, Field, Value>
: never;
/**
* Build the Zod shape for the `select` branch: only the listed fields,
* recursing into relations when given nested options.
*/
type BuildSelectShape<Schema extends SchemaDef, Model extends GetModels<Schema>, S extends Record<string, unknown>> = {
[Field in keyof S & GetModelFields<Schema, Model>]: SelectEntryToZod<Schema, Model, Field, S[Field]>;
};
/**
* Build the Zod shape for the `include` + `omit` branch:
* - All scalar fields, minus any that appear in `omit` with value `true`.
* - Plus the relation fields listed in `include`.
*/
type BuildIncludeOmitShape<
Schema extends SchemaDef,
Model extends GetModels<Schema>,
I extends Record<string, unknown> | undefined,
O extends Record<string, unknown> | undefined,
> =
// scalar fields, omitting those explicitly excluded (only `true` omits a field)
{
[Field in GetModelFields<Schema, Model> as FieldIsRelation<Schema, Model, Field> extends true
? never
: O extends object
? Field extends keyof O
? O[Field] extends true
? never
: Field
: Field
: Field]: GetAllModelFieldsShape<Schema, Model>[FieldInShape<Schema, Model, Field>];
} & (I extends object // included relation fields
? {
[Field in keyof I & GetModelFields<Schema, Model>]: I[Field] extends object
? RelationFieldZodWithOptions<Schema, Model, Field, I[Field]>
: RelationFieldZodDefault<Schema, Model, Field>;
}
: // no include — empty, so the intersection is a no-op
{});
/**
* The top-level conditional that maps options → Zod shape.
*
* - No options / undefined → existing `GetModelFieldsShape` (no change).
* - `{ select: S }` → `BuildSelectShape`.
* - `{ include?, omit? }` → `BuildIncludeOmitShape`.
*/
export type GetModelSchemaShapeWithOptions<
Schema extends SchemaDef,
Model extends GetModels<Schema>,
Options,
> = Options extends { select: infer S extends Record<string, unknown> }
? BuildSelectShape<Schema, Model, S>
: Options extends {
include?: infer I extends Record<string, unknown> | undefined;
omit?: infer O extends Record<string, unknown> | undefined;
}
? BuildIncludeOmitShape<Schema, Model, I, O>
: GetModelFieldsShape<Schema, Model>;