1
1
import type { RawBuilder } from 'kysely'
2
2
import type { IsNotNull } from '@subframe7536/type-utils'
3
3
import type {
4
+ ColumnType ,
4
5
Columns ,
5
6
ColumnsWithErrorInfo ,
6
7
Table ,
@@ -11,18 +12,16 @@ import type {
11
12
export const TGR = '__TIME_TRIGGER__'
12
13
13
14
/**
14
- * define table function
15
+ * define table, use it with { @link Column}
15
16
*
16
- * if you want to explicitly declare column type,
17
- * use {@link defineObject} or {@link defineLiteral}
18
17
* @example
19
18
* const testTable = defineTable({
20
- * id: { type: 'increments' } ,
21
- * person: { type: 'object', defaultTo: { name: 'test' } } ,
22
- * gender: { type: 'boolean', notNull: true } ,
23
- * array: defineObject <string[]>(),
24
- * literal: defineLiteral <'l1' | 'l2'>(),
25
- * buffer: { type: 'blob' } ,
19
+ * id: Column.Increments() ,
20
+ * person: Column.Object({ name: 'test' }) ,
21
+ * gender: Column.Boolean().NotNull() ,
22
+ * array: Column.Object <string[]>(),
23
+ * literal: Column.String <'l1' | 'l2'>(),
24
+ * buffer: Column.Blob() ,
26
25
* }, {
27
26
* primary: 'id',
28
27
* index: ['person', ['id', 'gender']],
@@ -61,20 +60,9 @@ export function defineTable<
61
60
}
62
61
}
63
62
64
- /**
65
- * explicitly declare object column type
66
- *
67
- * @example
68
- * ```ts
69
- * const pet = defineTable({
70
- * // NotNull is optional
71
- * owner: defineColumn<{ name: string }>().NotNull(),
72
- * }
73
- * ```
74
- */
75
- export function defineObject < T extends object > ( defaultTo ?: T | RawBuilder < unknown > | null ) {
63
+ function base < T > ( type : ColumnType , defaultTo ?: T | RawBuilder < unknown > | null ) {
76
64
const base = {
77
- type : 'object' ,
65
+ type,
78
66
defaultTo : defaultTo as IsNotNull < typeof defaultTo > extends true ? T : T | null ,
79
67
} as const
80
68
return {
@@ -87,29 +75,41 @@ export function defineObject<T extends object>(defaultTo?: T | RawBuilder<unknow
87
75
} ,
88
76
}
89
77
}
78
+
90
79
/**
91
- * explicitly declare string column type
92
- *
93
- * @example
94
- * ```ts
95
- * const typeTable = defineTable({
96
- * // NotNull is optional
97
- * type: defineColumn<'generic' | 'custom'>().NotNull(),
98
- * }
99
- * ```
80
+ * define column
100
81
*/
101
- export function defineLiteral < T extends string > ( defaultTo ?: T | RawBuilder < unknown > | null ) {
102
- const base = {
103
- type : 'string' ,
104
- defaultTo : defaultTo as IsNotNull < typeof defaultTo > extends true ? T : T | null ,
105
- } as const
106
- return {
107
- ...base ,
108
- NotNull ( ) {
109
- return {
110
- ...base ,
111
- notNull : true ,
112
- } as const
113
- } ,
114
- }
82
+ export const Column = {
83
+ /**
84
+ * column type: text
85
+ */
86
+ String : < T extends string > ( defaultTo ?: T | RawBuilder < unknown > | null ) => base ( 'string' , defaultTo ) ,
87
+ /**
88
+ * column type: integer
89
+ */
90
+ Int : < T extends number > ( defaultTo ?: T | RawBuilder < unknown > | null ) => base ( 'int' , defaultTo ) ,
91
+ /**
92
+ * column type: real
93
+ */
94
+ Float : < T extends number > ( defaultTo ?: T | RawBuilder < unknown > | null ) => base ( 'float' , defaultTo ) ,
95
+ /**
96
+ * column type: blob
97
+ */
98
+ Blob : ( ) => base < ArrayBufferLike > ( 'blob' ) ,
99
+ /**
100
+ * column type: interger auto increment
101
+ */
102
+ Increments : ( ) => ( { type : 'increments' } as const ) ,
103
+ /**
104
+ * column type: text (parse with `JSON.parse`)
105
+ */
106
+ Boolean : ( defaultTo ?: boolean | RawBuilder < unknown > | null ) => base ( 'boolean' , defaultTo ) ,
107
+ /**
108
+ * column type: text (parse with `JSON.parse`)
109
+ */
110
+ Date : ( defaultTo ?: Date | RawBuilder < unknown > | null ) => base ( 'date' , defaultTo ) ,
111
+ /**
112
+ * column type: text (parse with `JSON.parse`)
113
+ */
114
+ Object : < T extends object > ( defaultTo ?: T | RawBuilder < unknown > | null ) => base ( 'object' , defaultTo ) ,
115
115
}
0 commit comments