Skip to content

Commit 1a899f9

Browse files
committed
feat(builder): add afterUpdate hook
1 parent f9c6c1a commit 1a899f9

File tree

1 file changed

+13
-3
lines changed
  • packages/sqlite-builder/src/sync

1 file changed

+13
-3
lines changed

packages/sqlite-builder/src/sync/core.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { type Kysely, sql } from 'kysely'
22
import { getOrSetDBVersion } from 'kysely-sqlite-utils'
3+
import type { Promisable } from '@subframe7536/type-utils'
34
import type { DBLogger } from '../types'
4-
import type { Columns, Schema, Table } from './types'
5+
import type { Columns, InferDatabase, Schema, Table } from './types'
56
import {
67
parseColumnType,
78
runCreateTable,
@@ -44,6 +45,11 @@ export type SyncOptions<T extends Schema> = {
4445
* reserve old data in temp, clear after destroy
4546
*/
4647
reserveOldData?: boolean
48+
/**
49+
* after update hook
50+
* @param db kysely instance
51+
*/
52+
afterUpdate?: (db: Kysely<InferDatabase<T>>) => Promisable<void>
4753
}
4854

4955
export async function syncTables<T extends Schema>(
@@ -58,6 +64,7 @@ export async function syncTables<T extends Schema>(
5864
log,
5965
version: { current, skipSyncWhenSame } = {},
6066
excludeTablePrefix,
67+
afterUpdate,
6168
} = options
6269

6370
if (current) {
@@ -69,7 +76,7 @@ export async function syncTables<T extends Schema>(
6976

7077
const debug = (e: any) => log && logger?.debug(e)
7178
const { existTables, indexList, triggerList } = await parseExistDB(db, excludeTablePrefix)
72-
debug('====== sync tables start ======')
79+
debug('====== update tables start ======')
7380

7481
const truncateTableSet = new Set(
7582
Array.isArray(truncateIfExists)
@@ -102,7 +109,10 @@ export async function syncTables<T extends Schema>(
102109
await runCreateTableWithIndexAndTrigger(db, targetTableName, targetTable)
103110
}
104111
}
105-
debug('======= sync tables end =======')
112+
debug('======= after update hook =======')
113+
await afterUpdate?.(db)
114+
115+
debug('======= update tables end =======')
106116

107117
async function diffTable(
108118
tableName: string,

0 commit comments

Comments
 (0)