@@ -2,7 +2,7 @@ import type { Compilable, CompiledQuery, KyselyPlugin, LogEvent, QueryResult, Ra
2
2
import { Kysely , sql } from 'kysely'
3
3
import { SqliteSerializePlugin } from 'kysely-plugin-serialize'
4
4
import { parseTableMap , runCreateTable } from './util'
5
- import type { ITable , SqliteBuilderOption } from './types'
5
+ import type { ITable , Logger , SqliteBuilderOption } from './types'
6
6
7
7
const enum DBStatus {
8
8
'needDrop' ,
@@ -13,15 +13,17 @@ export class SqliteBuilder<DB extends Record<string, any>> {
13
13
public kysely : Kysely < DB >
14
14
private status : DBStatus
15
15
private tableMap : Map < string , ITable < DB [ Extract < keyof DB , string > ] > >
16
+ private logger ?: Logger
16
17
public constructor ( option : SqliteBuilderOption < DB > ) {
17
- const { dialect, tables, dropTableBeforeInit : truncateBeforeInit , onError, onQuery, plugins : additionalPlugin } = option
18
+ const { dialect, tables, dropTableBeforeInit : truncateBeforeInit , onQuery, plugins : additionalPlugin , logger } = option
19
+ this . logger = logger
18
20
const plugins : KyselyPlugin [ ] = additionalPlugin ?? [ ]
19
21
plugins . push ( new SqliteSerializePlugin ( ) )
20
22
this . kysely = new Kysely < DB > ( {
21
23
dialect,
22
24
log : ( event : LogEvent ) => {
23
25
event . level === 'error'
24
- ? onError ?. ( event . error )
26
+ ? this . logger ?. error ( 'uncaught db error' , event . error as Error )
25
27
: onQuery ?.( event . query , event . queryDurationMillis )
26
28
} ,
27
29
plugins,
@@ -39,44 +41,51 @@ export class SqliteBuilder<DB extends Record<string, any>> {
39
41
return this
40
42
}
41
43
42
- private async checkInit ( ) {
44
+ private async isEmptyTable ( ) : Promise < boolean > {
43
45
this . status !== DBStatus . ready && await this . init ( )
44
- if ( this . status ! == DBStatus . ready ) {
45
- throw new Error ( 'fail to init table' )
46
+ if ( this . status = == DBStatus . ready ) {
47
+ return false
46
48
}
49
+ this . logger ?. error ( 'fail to init table' )
50
+ return true
47
51
}
48
52
49
53
public async transaction < T > (
50
54
cb : ( trx : Transaction < DB > ) => Promise < T > ,
51
- errorLog = false ,
55
+ errorMsg = 'transaction error' ,
52
56
) : Promise < T | undefined > {
53
- await this . checkInit ( )
57
+ if ( await this . isEmptyTable ( ) ) {
58
+ return undefined
59
+ }
54
60
return await this . kysely . transaction ( ) . execute ( cb )
55
61
. catch ( ( err ) => {
56
- errorLog && console . error ( err )
62
+ this . logger ?. error ( errorMsg , err )
57
63
return undefined
58
64
} )
59
65
}
60
66
61
67
public async exec < T > (
62
68
cb : ( db : Kysely < DB > ) => Promise < T > ,
63
- errorLog = false ,
69
+ errorMsg = 'execute error' ,
64
70
) : Promise < T | undefined > {
65
- await this . checkInit ( )
71
+ if ( await this . isEmptyTable ( ) ) {
72
+ return undefined
73
+ }
66
74
return cb ( this . kysely )
67
75
. catch ( ( err ) => {
68
- errorLog && console . error ( err )
76
+ this . logger ?. error ( errorMsg , err )
69
77
return undefined
70
78
} )
71
79
}
72
80
73
81
public async toSQL < T extends Compilable > ( cb : ( db : Kysely < DB > ) => T ) : Promise < CompiledQuery < unknown > > {
74
- await this . checkInit ( )
75
82
return cb ( this . kysely ) . compile ( )
76
83
}
77
84
78
- public async raw < T = any > ( rawSql : ( s : Sql ) => RawBuilder < T > ) : Promise < QueryResult < T > > {
79
- await this . checkInit ( )
85
+ public async raw < T = any > ( rawSql : ( s : Sql ) => RawBuilder < T > ) : Promise < QueryResult < T > | undefined > {
86
+ if ( await this . isEmptyTable ( ) ) {
87
+ return undefined
88
+ }
80
89
return rawSql ( sql ) . execute ( this . kysely )
81
90
}
82
91
}
0 commit comments