1
1
import { type Kysely , sql } from 'kysely'
2
2
import { getOrSetDBVersion } from 'kysely-sqlite-utils'
3
+ import type { Promisable } from '@subframe7536/type-utils'
3
4
import type { DBLogger } from '../types'
4
- import type { Columns , Schema , Table } from './types'
5
+ import type { Columns , InferDatabase , Schema , Table } from './types'
5
6
import {
6
7
parseColumnType ,
7
8
runCreateTable ,
@@ -44,6 +45,11 @@ export type SyncOptions<T extends Schema> = {
44
45
* reserve old data in temp, clear after destroy
45
46
*/
46
47
reserveOldData ?: boolean
48
+ /**
49
+ * after update hook
50
+ * @param db kysely instance
51
+ */
52
+ afterUpdate ?: ( db : Kysely < InferDatabase < T > > ) => Promisable < void >
47
53
}
48
54
49
55
export async function syncTables < T extends Schema > (
@@ -58,6 +64,7 @@ export async function syncTables<T extends Schema>(
58
64
log,
59
65
version : { current, skipSyncWhenSame } = { } ,
60
66
excludeTablePrefix,
67
+ afterUpdate,
61
68
} = options
62
69
63
70
if ( current ) {
@@ -69,7 +76,7 @@ export async function syncTables<T extends Schema>(
69
76
70
77
const debug = ( e : any ) => log && logger ?. debug ( e )
71
78
const { existTables, indexList, triggerList } = await parseExistDB ( db , excludeTablePrefix )
72
- debug ( '====== sync tables start ======' )
79
+ debug ( '====== update tables start ======' )
73
80
74
81
const truncateTableSet = new Set (
75
82
Array . isArray ( truncateIfExists )
@@ -102,7 +109,10 @@ export async function syncTables<T extends Schema>(
102
109
await runCreateTableWithIndexAndTrigger ( db , targetTableName , targetTable )
103
110
}
104
111
}
105
- debug ( '======= sync tables end =======' )
112
+ debug ( '======= after update hook =======' )
113
+ await afterUpdate ?.( db )
114
+
115
+ debug ( '======= update tables end =======' )
106
116
107
117
async function diffTable (
108
118
tableName : string ,
0 commit comments