1
- import type { Compilable , CompiledQuery , KyselyPlugin , LogEvent , QueryResult , RawBuilder , Sql , Transaction } from 'kysely'
1
+ import type { Compilable , CompiledQuery , KyselyPlugin , LogEvent , QueryResult , RawBuilder , Simplify , Sql , Transaction } from 'kysely'
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 , Logger , SqliteBuilderOption } from './types'
5
+ import type { AvailableBuilder , ITable , Logger , SqliteBuilderOption } from './types'
6
6
7
7
const enum DBStatus {
8
8
'needDrop' ,
@@ -23,7 +23,7 @@ export class SqliteBuilder<DB extends Record<string, any>> {
23
23
dialect,
24
24
log : ( event : LogEvent ) => {
25
25
event . level === 'error'
26
- ? this . logger ?. error ( 'uncaught db error ' , event . error as Error )
26
+ ? this . logger ?. error ( 'Uncaught DB Error ' , event . error as Error )
27
27
: onQuery ?.( event . query , event . queryDurationMillis )
28
28
} ,
29
29
plugins,
@@ -52,28 +52,56 @@ export class SqliteBuilder<DB extends Record<string, any>> {
52
52
53
53
public async transaction < T > (
54
54
cb : ( trx : Transaction < DB > ) => Promise < T > ,
55
- errorMsg = 'transaction error' ,
55
+ errorMsg ?: string ,
56
56
) : Promise < T | undefined > {
57
57
if ( await this . isEmptyTable ( ) ) {
58
58
return undefined
59
59
}
60
60
return await this . kysely . transaction ( ) . execute ( cb )
61
61
. catch ( ( err ) => {
62
- this . logger ?. error ( errorMsg , err )
62
+ errorMsg && this . logger ?. error ( errorMsg , err )
63
63
return undefined
64
64
} )
65
65
}
66
66
67
67
public async exec < T > (
68
68
cb : ( db : Kysely < DB > ) => Promise < T > ,
69
- errorMsg = 'execute error' ,
69
+ errorMsg ?: string ,
70
70
) : Promise < T | undefined > {
71
71
if ( await this . isEmptyTable ( ) ) {
72
72
return undefined
73
73
}
74
74
return cb ( this . kysely )
75
75
. catch ( ( err ) => {
76
- this . logger ?. error ( errorMsg , err )
76
+ errorMsg && this . logger ?. error ( errorMsg , err )
77
+ return undefined
78
+ } )
79
+ }
80
+
81
+ public async execOne < O > (
82
+ cb : ( db : Kysely < DB > ) => AvailableBuilder < DB , O > ,
83
+ errorMsg ?: string ,
84
+ ) : Promise < Simplify < O > | undefined > {
85
+ if ( await this . isEmptyTable ( ) ) {
86
+ return undefined
87
+ }
88
+ return cb ( this . kysely ) . executeTakeFirstOrThrow ( )
89
+ . catch ( ( err ) => {
90
+ errorMsg && this . logger ?. error ( errorMsg , err )
91
+ return undefined
92
+ } )
93
+ }
94
+
95
+ public async execList < O > (
96
+ cb : ( db : Kysely < DB > ) => AvailableBuilder < DB , O > ,
97
+ errorMsg ?: string ,
98
+ ) : Promise < Simplify < O > [ ] | undefined > {
99
+ if ( await this . isEmptyTable ( ) ) {
100
+ return undefined
101
+ }
102
+ return cb ( this . kysely ) . execute ( )
103
+ . catch ( ( err ) => {
104
+ errorMsg && this . logger ?. error ( errorMsg , err )
77
105
return undefined
78
106
} )
79
107
}
0 commit comments