|
1 |
| -import type { Generated, Kysely } from 'kysely' |
2 |
| -import type { IsNotNull } from '@subframe7536/type-utils' |
| 1 | +import type { Kysely } from 'kysely' |
3 | 2 | import type { DBLogger, SyncTableFn } from '../types'
|
4 |
| -import type { ColumnProperty, Columns, Schema, TimeTriggerOptions } from './types' |
| 3 | +import type { Schema } from './types' |
5 | 4 | import type { SyncOptions } from './core'
|
6 | 5 | import { syncTables } from './core'
|
7 | 6 |
|
8 | 7 | export * from './types'
|
9 | 8 | export { defineTable, defineLiteral, defineObject } from './define'
|
10 | 9 |
|
| 10 | +/** |
| 11 | + * create sync schema function |
| 12 | + * @param schema table schema, see {@link defineTable} |
| 13 | + * @param options sync options |
| 14 | + */ |
11 | 15 | export function createAutoSyncSchemaFn<T extends Schema>(
|
12 |
| - tables: T, |
| 16 | + schema: T, |
13 | 17 | options: SyncOptions<T> = {},
|
14 | 18 | ): SyncTableFn {
|
15 | 19 | const { log } = options
|
16 | 20 | return async (db: Kysely<any>, logger?: DBLogger) => {
|
17 |
| - await syncTables(db, tables, options, log ? logger : undefined) |
| 21 | + await syncTables(db, schema, options, log ? logger : undefined) |
18 | 22 | }
|
19 | 23 | }
|
20 |
| - |
21 |
| -type Prettify<T> = { |
22 |
| - [K in keyof T]: T[K] |
23 |
| -} & {} |
24 |
| - |
25 |
| -type ERROR_INFO = 'HAVE_TYPE_ERROR_AT_DEFINITION' |
26 |
| - |
27 |
| -type TriggerKey<A, B> = |
28 |
| - | (A extends true ? 'createAt' : A extends string ? A : never) |
29 |
| - | (B extends true ? 'updateAt' : B extends string ? B : never) |
30 |
| - |
31 |
| -type ParseTableWithTrigger< |
32 |
| - T extends Columns, |
33 |
| - P extends TimeTriggerOptions<any, any> | undefined, |
34 |
| -> = P extends TimeTriggerOptions<infer A, infer B> |
35 |
| - ? Omit<T, TriggerKey<A, B>> & ( |
36 |
| - { [K in TriggerKey<A, B>]: { |
37 |
| - type: 'increments' // #hack to ensure Generated |
38 |
| - defaultTo: Generated<Date> | null |
39 |
| - notNull: null |
40 |
| - } } |
41 |
| - ) |
42 |
| - : never |
43 |
| - |
44 |
| -/** |
45 |
| - * util type for infering type of table |
46 |
| - */ |
47 |
| -export type InferTable< |
48 |
| - T extends { |
49 |
| - columns: Columns |
50 |
| - timeTrigger?: TimeTriggerOptions<any, any> |
51 |
| - }, |
52 |
| - P = ParseTableWithTrigger<T['columns'], T['timeTrigger']>, |
53 |
| -> = Prettify<{ |
54 |
| - [K in keyof P]: P[K] extends ColumnProperty |
55 |
| - ? IsNotNull<P[K]['notNull']> extends true // if not null |
56 |
| - ? Exclude<P[K]['defaultTo'], null> // return required defaultTo |
57 |
| - : P[K]['type'] extends 'increments' // if type is "increments" |
58 |
| - ? Exclude<P[K]['defaultTo'], null> // return "Generated<...>" |
59 |
| - : IsNotNull<P[K]['defaultTo']> extends true // if defaultTo is required |
60 |
| - ? Generated<Exclude<P[K]['defaultTo'], null>> // return Generated |
61 |
| - : P[K]['defaultTo'] | null // return optional |
62 |
| - : ERROR_INFO // return error info |
63 |
| -}> |
64 |
| - |
65 |
| -/** |
66 |
| - * util type for infering type of database |
67 |
| - * |
68 |
| - * if infered type contains `"HAVE_DEFAULT_VALUE_TYPE_ERROR"`, |
69 |
| - * there is some error in target table's default value type |
70 |
| - * |
71 |
| - * use {@link InferTable} to check details |
72 |
| - */ |
73 |
| -export type InferDatabase<T extends Schema> = Prettify<{ |
74 |
| - [K in keyof T]: T[K] extends { |
75 |
| - columns: Columns |
76 |
| - timeTrigger?: TimeTriggerOptions<any, any> |
77 |
| - } |
78 |
| - ? InferTable<T[K]> |
79 |
| - : ERROR_INFO |
80 |
| -}> |
0 commit comments