1
1
import type { Generated , RawBuilder } from 'kysely'
2
2
3
3
export type Arrayable < T > = T | Array < T >
4
+
4
5
export type ColumnType =
5
6
| 'string'
6
7
| 'boolean'
@@ -9,7 +10,7 @@ export type ColumnType =
9
10
| 'date'
10
11
| 'blob'
11
12
| 'object'
12
-
13
+ export type InferGenereated < T > = T extends Generated < infer P > ? P : T
13
14
export type InferColumnTypeByString < T > =
14
15
T extends 'string' ? string :
15
16
T extends 'boolean' ? boolean :
@@ -23,40 +24,41 @@ export type InferColumnTypeByString<T> =
23
24
export type InferStringByColumnType < T > =
24
25
T extends string ? 'string' :
25
26
T extends boolean ? 'boolean' :
26
- T extends Generated < number > ? 'increments' :
27
+ T extends Generated < number > ? 'increments' | 'int' | 'float' :
27
28
T extends number ? 'int' | 'float' :
28
29
T extends Date ? 'date' :
29
30
T extends ArrayBufferLike ? 'blob' :
30
- T extends object ? 'object' :
31
- never
31
+ T extends Generated < infer P > ? InferStringByColumnType < P > :
32
+ T extends object ? 'object' :
33
+ never
32
34
33
35
export type ColumnProperty <
34
- T extends ColumnType = ColumnType ,
35
- DefaultTo extends InferColumnTypeByString < T > | null = InferColumnTypeByString < T > | null ,
36
+ ColType extends ColumnType = ColumnType ,
37
+ DefaultTo extends InferColumnTypeByString < ColType > | null = InferColumnTypeByString < ColType > | null ,
36
38
NotNull extends true | null = true | null ,
37
39
> = {
38
- type : T
40
+ type : ColType
39
41
defaultTo ?: DefaultTo | RawBuilder < unknown >
40
42
notNull ?: NotNull
41
43
}
42
44
43
45
export type TimeTriggerOptions <
44
- C extends string | true | null ,
45
- U extends string | true | null ,
46
+ Create extends string | true | null ,
47
+ Update extends string | true | null ,
46
48
> = {
47
- create ?: C
48
- update ?: U
49
+ create ?: Create
50
+ update ?: Update
49
51
}
50
52
51
53
export type TableProperty <
52
- T extends Columns ,
53
- C extends string | true | null = null ,
54
- U extends string | true | null = null ,
54
+ Cols extends Columns ,
55
+ Create extends string | true | null = null ,
56
+ Update extends string | true | null = null ,
55
57
> = {
56
- primary ?: Arrayable < keyof T & string >
57
- unique ?: Arrayable < keyof T & string > [ ]
58
- index ?: Arrayable < keyof T & string > [ ]
59
- timeTrigger ?: TimeTriggerOptions < C , U >
58
+ primary ?: Arrayable < keyof Cols & string >
59
+ unique ?: Arrayable < keyof Cols & string > [ ]
60
+ index ?: Arrayable < keyof Cols & string > [ ]
61
+ timeTrigger ?: TimeTriggerOptions < Create , Update >
60
62
}
61
63
export type Columns = Record < string , ColumnProperty >
62
64
@@ -81,10 +83,19 @@ export type ColumnsWithErrorInfo<T extends Columns> = {
81
83
} ;
82
84
}
83
85
export type Table <
84
- T extends Columns = any ,
85
- C extends string | true | null = null ,
86
- U extends string | true | null = null ,
86
+ Cols extends Columns = any ,
87
+ Create extends string | true | null = null ,
88
+ Update extends string | true | null = null ,
87
89
> = {
88
- columns : ColumnsWithErrorInfo < T >
89
- } & TableProperty < T , C , U >
90
- export type Tables = Record < string , Table < any , any , any > >
90
+ columns : ColumnsWithErrorInfo < Cols >
91
+ } & TableProperty < Cols , Create , Update >
92
+ export type Schema = Record < string , Table < any , any , any > >
93
+ export type FilterGenerated <
94
+ Table extends object ,
95
+ EscapeKeys extends string = never ,
96
+ > = {
97
+ [ K in keyof Table ] : K extends EscapeKeys
98
+ ? Table [ K ]
99
+ : InferGenereated < Table [ K ] >
100
+ }
101
+ export type IsNotNull < T > = ( T extends null ? T : never ) extends never ? true : false
0 commit comments