-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfield.ts
More file actions
525 lines (469 loc) · 17.1 KB
/
field.ts
File metadata and controls
525 lines (469 loc) · 17.1 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
import type { Column, Relation } from 'drizzle-orm'
import {
type FindTableByDBName,
getTableName,
is,
One,
type TableRelationalConfig,
} from 'drizzle-orm'
import type { Simplify } from 'type-fest'
import type { ZodObject } from 'zod'
import z from 'zod'
import {
appendFieldNameToFields,
type GetPrimaryColumn,
getPrimaryColumn,
getPrimaryColumnTsName,
} from './utils'
export type OptionCallback<
TType extends string | number,
TContext extends Record<string, unknown> = {},
> = (args: TContext) => Promise<Array<{ label: string; value: TType }>>
export type FieldsWithFieldName<TFields extends Record<string, FieldBase>> = {
[TKey in keyof TFields]: TFields[TKey] & { fieldName: string }
}
export interface FieldMetadataColumns {
source: 'column'
columnTsName: string
column: Column
}
export interface FieldMetadataRelations {
source: 'relation'
relation: Relation
relationTsName: string
referencedTableTsName: string
sourceTableTsName: string
primaryColumn: Column
primaryColumnTsName: string
}
export type FieldMetadata = FieldMetadataColumns | FieldMetadataRelations
export const FieldMutateModeCollection = {
ENABLED: 'enabled',
DISABLED: 'disabled',
HIDDEN: 'hidden',
} as const
export type FieldMutateModeCollection = typeof FieldMutateModeCollection
export type FieldMutateMode =
(typeof FieldMutateModeCollection)[keyof typeof FieldMutateModeCollection]
export type FieldBase = {
label?: string
placeholder?: string
update?: FieldMutateMode
create?: FieldMutateMode
}
export interface FieldColumnStringCollectionOptions<
TContext extends Record<string, unknown> = Record<string, unknown>,
> {
text: {
type: 'text'
default?: string
label?: string
placeholder?: string
// editor?: Editor // TODO: Support rich text editor
} & FieldBase
selectText: {
type: 'selectText'
options: OptionCallback<string, TContext>
default?: string
} & FieldBase
time: {
type: 'time'
default?: Date
} & FieldBase
media: {
type: 'media'
mimeTypes: string[]
} & FieldBase
}
export interface FieldColumnStringArrayCollectionOptions<
TContext extends Record<string, unknown> = Record<string, unknown>,
> {
comboboxText: {
type: 'comboboxText'
options: OptionCallback<string, TContext>
label?: string
} & FieldBase
}
export interface FieldColumnNumberCollectionOptions<
TContext extends Record<string, unknown> = Record<string, unknown>,
> {
number: {
type: 'number'
default?: number
} & FieldBase
selectNumber: {
type: 'selectNumber'
options: OptionCallback<number, TContext>
default?: number
} & FieldBase
}
export interface FieldColumnNumberArrayCollectionOptions<
TContext extends Record<string, unknown> = Record<string, unknown>,
> {
comboboxNumber: {
type: 'comboboxNumber'
options: OptionCallback<number, TContext>
label?: string
} & FieldBase
}
export interface FieldColumnBooleanCollectionOptions {
checkbox: {
type: 'checkbox'
default?: boolean
} & FieldBase
switch: {
type: 'switch'
default?: boolean
} & FieldBase
}
export interface FieldColumnBooleanArrayCollectionOptions {
comboboxBoolean: {
type: 'comboboxBoolean'
default?: boolean[]
} & FieldBase
}
export interface FieldColumnDateCollectionOptions {
date: {
type: 'date'
default?: Date
} & FieldBase
}
export type FieldRelationCollectionOptions<
TContext extends Record<string, unknown> = Record<string, unknown>,
TInputType extends string | number = string | number,
> = {
connect: {
type: 'connect'
fields: FieldsInitial<TContext>
options: OptionCallback<TInputType, TContext>
} & FieldBase
create: {
type: 'create'
fields: FieldsInitial<TContext>
} & FieldBase
connectOrCreate: {
type: 'connectOrCreate'
fields: FieldsInitial<TContext>
options: OptionCallback<TInputType, TContext>
} & FieldBase
}
export type FieldColumnCollection<
TContext extends Record<string, unknown> = Record<string, unknown>,
> = FieldColumnStringCollectionOptions<TContext> &
FieldColumnStringArrayCollectionOptions<TContext> &
FieldColumnNumberCollectionOptions<TContext> &
FieldColumnNumberArrayCollectionOptions<TContext> &
FieldColumnBooleanCollectionOptions &
FieldColumnBooleanArrayCollectionOptions &
FieldColumnDateCollectionOptions
export type FieldColumn<TContext extends Record<string, unknown> = Record<string, unknown>> =
FieldColumnCollection<TContext>[keyof FieldColumnCollection<TContext>] & {
_: FieldMetadataColumns
}
export type FieldRelationCollection<
TContext extends Record<string, unknown> = Record<string, unknown>,
> = Simplify<
FieldRelationCollectionOptions<TContext> & {
connect: {
fields: Fields<TContext>
}
create: {
fields: Fields<TContext>
}
connectOrCreate: {
fields: Fields<TContext>
}
}
>
export type FieldRelation<TContext extends Record<string, unknown> = Record<string, unknown>> =
FieldRelationCollection<TContext>[keyof FieldRelationCollection<TContext>] & {
_: FieldMetadataRelations
mode: 'one' | 'many'
}
export type FieldCollection<TContext extends Record<string, unknown> = Record<string, unknown>> =
FieldColumnCollection<TContext> & FieldRelationCollection<TContext>
export type FieldOptions<TContext extends Record<string, unknown> = Record<string, unknown>> =
FieldCollection<TContext>[keyof FieldCollection<TContext>]
export type Field<TContext extends Record<string, unknown> = Record<string, unknown>> =
| FieldColumn<TContext>
| FieldRelation<TContext>
export type FieldsInitial<TContext extends Record<string, unknown> = Record<string, unknown>> =
Record<string, Field<TContext>>
export type Fields<
TContext extends Record<string, unknown> = Record<string, unknown>,
TFields extends FieldsInitial<TContext> = FieldsInitial<any>,
> = FieldsWithFieldName<TFields>
export type FieldClient = Omit<Field, 'options'>
export type FieldsClientInitial = Record<string, FieldClient>
export type FieldsClient<TFields extends FieldsClientInitial = FieldsClientInitial> =
FieldsWithFieldName<TFields>
export type FieldColumnOptionsFromTable<
TColumn extends Column<any>,
TContext extends Record<string, unknown> = Record<string, unknown>,
> = TColumn['_']['dataType'] extends 'string'
? FieldColumnStringCollectionOptions<TContext>[keyof FieldColumnStringCollectionOptions<TContext>]
: TColumn['_']['dataType'] extends 'number'
? FieldColumnNumberCollectionOptions<TContext>[keyof FieldColumnNumberCollectionOptions<TContext>]
: TColumn['_']['dataType'] extends 'boolean'
? FieldColumnBooleanCollectionOptions[keyof FieldColumnBooleanCollectionOptions]
: TColumn['_']['dataType'] extends 'date'
? FieldColumnDateCollectionOptions[keyof FieldColumnDateCollectionOptions]
: TColumn['_']['dataType'] extends 'array'
? TColumn['_']['data'] extends string[]
? FieldColumnStringArrayCollectionOptions<TContext>[keyof FieldColumnStringArrayCollectionOptions<TContext>]
: TColumn['_']['data'] extends number[]
? FieldColumnNumberArrayCollectionOptions<TContext>[keyof FieldColumnNumberArrayCollectionOptions<TContext>]
: TColumn['_']['data'] extends boolean[]
? FieldColumnBooleanArrayCollectionOptions[keyof FieldColumnBooleanArrayCollectionOptions]
: never
: never
type RelationFieldOptionsFromTable<
TRelationPrimaryColumn extends Column,
TContext extends Record<string, unknown> = {},
> = TRelationPrimaryColumn['_']['data'] extends infer TType extends string | number
? FieldRelationCollectionOptions<TContext, TType>['connect' | 'create' | 'connectOrCreate']
: TRelationPrimaryColumn
type GetReferencedPrimaryColumn<
TTableRelationConfigByTableTsName extends Record<string, TableRelationalConfig>,
TRelation extends Relation,
> =
FindTableByDBName<
TTableRelationConfigByTableTsName,
TRelation['referencedTableName']
> extends infer TReferencedTableRelationalConfig extends TableRelationalConfig
? GetPrimaryColumn<TReferencedTableRelationalConfig>
: never
type GetReferencedTableTsName<
TTableRelationConfigByTableTsName extends Record<string, TableRelationalConfig>,
TRelation extends Relation,
> =
FindTableByDBName<
TTableRelationConfigByTableTsName,
TRelation['referencedTableName']
> extends infer TReferencedTableRelationalConfig extends TableRelationalConfig
? TReferencedTableRelationalConfig['tsName']
: never
type GetRelationMode<TRelation extends Relation> = TRelation extends One ? 'one' : 'many'
export class FieldBuilder<
TFullSchema extends Record<string, unknown>,
TTableRelationConfigByTableTsName extends Record<string, TableRelationalConfig>,
TTableTsName extends string,
TContext extends Record<string, unknown> = Record<string, unknown>,
> {
private readonly tableRelationalConfig: TTableRelationConfigByTableTsName[TTableTsName]
constructor(
tableTsName: TTableTsName,
private readonly tableRelationalConfigByTableTsName: TTableRelationConfigByTableTsName
) {
this.tableRelationalConfig = tableRelationalConfigByTableTsName[
tableTsName
] as TTableRelationConfigByTableTsName[TTableTsName]
}
columns<
TColumnTsName extends Extract<
keyof TTableRelationConfigByTableTsName[TTableTsName]['columns'],
string
>,
const TOptions extends FieldColumnOptionsFromTable<
TTableRelationConfigByTableTsName[TTableTsName]['columns'][TColumnTsName],
TContext
>,
>(columnTsName: TColumnTsName, options: TOptions) {
const fieldMetadata = {
source: 'column',
columnTsName: columnTsName,
column: this.tableRelationalConfig['columns'][
columnTsName
] as TTableRelationConfigByTableTsName[TTableTsName]['columns'][TColumnTsName],
} satisfies FieldMetadata
return {
_: fieldMetadata,
...options,
} as Simplify<TOptions & { _: typeof fieldMetadata }>
}
relations<
TRelationTsName extends Extract<
keyof TTableRelationConfigByTableTsName[TTableTsName]['relations'],
string
>,
const TOptions extends RelationFieldOptionsFromTable<
GetReferencedPrimaryColumn<
TTableRelationConfigByTableTsName,
TTableRelationConfigByTableTsName[TTableTsName]['relations'][TRelationTsName]
>,
TContext
>,
>(
relationTsName: TRelationTsName,
optionsFn: (
fb: FieldBuilder<
TFullSchema,
TTableRelationConfigByTableTsName,
GetReferencedTableTsName<
TTableRelationConfigByTableTsName,
TTableRelationConfigByTableTsName[TTableTsName]['relations'][TRelationTsName]
>,
TContext
>
) => TOptions
) {
const relation = this.tableRelationalConfig['relations'][
relationTsName
] as TTableRelationConfigByTableTsName[TTableTsName]['relations'][TRelationTsName]
type ReferencedTableRelationalConfig = FindTableByDBName<
TTableRelationConfigByTableTsName,
TTableRelationConfigByTableTsName[TTableTsName]['relations'][TRelationTsName]['referencedTableName']
>
const referencedTableRelationalConfig = Object.values(
this.tableRelationalConfigByTableTsName
).find((t) => t.dbName === relation.referencedTableName) as
| ReferencedTableRelationalConfig
| undefined
if (!referencedTableRelationalConfig) {
throw new Error(`Referenced table ${relation.referencedTableName} not found in schema`)
}
type SourceTableRelationalConfig = FindTableByDBName<
TTableRelationConfigByTableTsName,
TTableRelationConfigByTableTsName[TTableTsName]['relations'][TRelationTsName]['sourceTable']['_']['name']
>
const sourceTableRelationalConfig = Object.values(this.tableRelationalConfigByTableTsName).find(
(t) => t.dbName === getTableName(relation.sourceTable)
) as SourceTableRelationalConfig | undefined
if (!sourceTableRelationalConfig) {
throw new Error(`Source table ${relation.sourceTable._.name} not found in schema`)
}
const primaryColumn = getPrimaryColumn(referencedTableRelationalConfig)
const primaryColumnTsName = getPrimaryColumnTsName(referencedTableRelationalConfig)
const fb = new FieldBuilder(
referencedTableRelationalConfig.tsName,
this.tableRelationalConfigByTableTsName
) as FieldBuilder<
TFullSchema,
TTableRelationConfigByTableTsName,
GetReferencedTableTsName<
TTableRelationConfigByTableTsName,
TTableRelationConfigByTableTsName[TTableTsName]['relations'][TRelationTsName]
>,
TContext
>
const options = optionsFn(fb)
if (options.type === 'connectOrCreate' || options.type === 'create') {
options.fields = appendFieldNameToFields(options.fields)
}
const fieldMetadata = {
source: 'relation',
relationTsName: relationTsName,
referencedTableTsName: referencedTableRelationalConfig.tsName,
sourceTableTsName: sourceTableRelationalConfig.tsName,
relation: relation,
primaryColumn: primaryColumn as GetReferencedPrimaryColumn<
TTableRelationConfigByTableTsName,
TTableRelationConfigByTableTsName[TTableTsName]['relations'][TRelationTsName]
>,
primaryColumnTsName: primaryColumnTsName as string,
} satisfies FieldMetadata
const mode = (is(relation, One) ? 'one' : 'many') as GetRelationMode<
TTableRelationConfigByTableTsName[TTableTsName]['relations'][TRelationTsName]
>
type Result = TOptions & {
_: typeof fieldMetadata
mode: GetRelationMode<
TTableRelationConfigByTableTsName[TTableTsName]['relations'][TRelationTsName]
>
}
return {
_: fieldMetadata,
...options,
mode,
} as Simplify<
Result &
(Result extends FieldRelationCollection<any>['connectOrCreate' | 'create']
? { fields: FieldsWithFieldName<Result['fields']> }
: {})
>
}
fields<TFields extends FieldsInitial<TContext>>(
tableTsName: TTableTsName,
optionsFn: (
fb: FieldBuilder<TFullSchema, TTableRelationConfigByTableTsName, TTableTsName, TContext>
) => TFields
): Simplify<FieldsWithFieldName<TFields>> {
const fb = new FieldBuilder(tableTsName, this.tableRelationalConfigByTableTsName)
return appendFieldNameToFields(optionsFn(fb as any))
}
}
type FieldToZodScheama<TField extends Field<any>> =
TField extends FieldColumnStringCollectionOptions<any>[keyof FieldColumnStringCollectionOptions<any>]
? z.ZodString
: TField extends FieldColumnStringArrayCollectionOptions<any>[keyof FieldColumnStringArrayCollectionOptions<any>]
? z.ZodArray<z.ZodString>
: TField extends FieldColumnNumberCollectionOptions<any>[keyof FieldColumnNumberCollectionOptions<any>]
? z.ZodNumber
: TField extends FieldColumnNumberArrayCollectionOptions<any>[keyof FieldColumnNumberArrayCollectionOptions<any>]
? z.ZodArray<z.ZodNumber>
: TField extends FieldColumnBooleanCollectionOptions[keyof FieldColumnBooleanCollectionOptions]
? z.ZodBoolean
: TField extends FieldColumnBooleanArrayCollectionOptions[keyof FieldColumnBooleanArrayCollectionOptions]
? z.ZodArray<z.ZodBoolean>
: TField extends FieldColumnDateCollectionOptions[keyof FieldColumnDateCollectionOptions]
? z.ZodDate
: never
// TODO: Relation input
// TODO: Optioanl and default values
export function fieldToZodScheama<TField extends Field<any>>(
field: TField
): FieldToZodScheama<TField> {
// TODO: More options
switch (field.type) {
// string input
case 'text':
case 'selectText':
case 'time':
case 'media':
return z.string() as FieldToZodScheama<TField>
// string[] input
case 'comboboxText':
return z.array(z.string()) as FieldToZodScheama<TField>
// number input
case 'number':
case 'selectNumber':
return z.number() as FieldToZodScheama<TField>
// number[] input
case 'comboboxNumber':
return z.array(z.number()) as FieldToZodScheama<TField>
// boolean input
case 'checkbox':
case 'switch':
return z.boolean() as FieldToZodScheama<TField>
// boolean[] input
case 'comboboxBoolean':
return z.array(z.boolean()) as FieldToZodScheama<TField>
// date input
case 'date':
return z.date() as FieldToZodScheama<TField>
// TODO: relation input
case 'connect':
return z.any() as unknown as FieldToZodScheama<TField>
case 'create':
return z.any() as unknown as FieldToZodScheama<TField>
case 'connectOrCreate':
return z.any() as unknown as FieldToZodScheama<TField>
default:
throw new Error(`Unknown field type: ${(field as any).type}`)
}
}
type FieldsToZodObject<TFields extends Fields<any>> = ZodObject<{
[TKey in keyof TFields]: TFields[TKey] extends Field<any> ? z.ZodTypeAny : never
}>
export function fieldsToZodObject<TFields extends Fields<any>>(
fields: TFields
): FieldsToZodObject<TFields> {
const zodObject = Object.entries(fields).reduce(
(acc, [key, field]) => {
acc[key] = fieldToZodScheama(field)
return acc
},
{} as Record<string, z.ZodTypeAny>
)
return z.object(zodObject) as FieldsToZodObject<TFields>
}