@@ -11,6 +11,8 @@ import { CompiledQuery, Kysely } from 'kysely'
1111import { SerializePlugin , defaultSerializer } from 'kysely-plugin-serialize'
1212import {
1313 type QueryBuilderOutput ,
14+ type SqliteExecutor ,
15+ basicExecutorFn ,
1416 checkPrecompileKey ,
1517 createKyselyLogger ,
1618 getPrecompileParam ,
@@ -50,25 +52,31 @@ type TransactionOptions<T> = {
5052 */
5153 onRollback ?: ( err : unknown ) => Promisable < void >
5254}
53-
54- export class SqliteBuilder < DB extends Record < string , any > > {
55- public kysely : Kysely < DB >
55+ // ({select, ..., kysely})=>
56+ export class SqliteBuilder < DB extends Record < string , any > , Extra extends Record < string , any > = { } > {
57+ private _kysely : Kysely < DB >
5658 public trxCount = 0
5759 private trx ?: Transaction < DB >
5860 private logger ?: DBLogger
61+ private executor : SqliteExecutor < DB , Extra >
5962 private serializer = defaultSerializer
6063
64+ public get kysely ( ) {
65+ return this . trx || this . _kysely
66+ }
67+
6168 /**
6269 * sqlite builder
6370 * @param options options
6471 */
65- constructor ( options : SqliteBuilderOptions ) {
72+ constructor ( options : SqliteBuilderOptions < DB , Extra > ) {
6673 const {
6774 dialect,
6875 logger,
6976 onQuery,
7077 plugins = [ ] ,
7178 serializerPluginOptions,
79+ executorFn = basicExecutorFn < DB > ,
7280 } = options
7381 this . logger = logger
7482
@@ -87,7 +95,8 @@ export class SqliteBuilder<DB extends Record<string, any>> {
8795 log = createKyselyLogger ( onQuery )
8896 }
8997
90- this . kysely = new Kysely < DB > ( { dialect, log, plugins } )
98+ this . _kysely = new Kysely < DB > ( { dialect, log, plugins } )
99+ this . executor = executorFn ( ( ) => this . kysely ) as any
91100 }
92101
93102 /**
@@ -138,11 +147,11 @@ export class SqliteBuilder<DB extends Record<string, any>> {
138147 */
139148 public async syncDB ( updater : TableUpdater , checkIntegrity ?: boolean ) : Promise < StatusResult > {
140149 try {
141- if ( checkIntegrity && ! ( await runCheckIntegrity ( this . kysely ) ) ) {
150+ if ( checkIntegrity && ! ( await runCheckIntegrity ( this . _kysely ) ) ) {
142151 this . logger ?. error ( 'integrity check fail' )
143152 return { ready : false , error : new IntegrityError ( ) }
144153 }
145- const result = await updater ( this . kysely , this . logger )
154+ const result = await updater ( this . _kysely , this . logger )
146155 this . logger ?. info ( 'table updated' )
147156 return result
148157 } catch ( error ) {
@@ -154,13 +163,6 @@ export class SqliteBuilder<DB extends Record<string, any>> {
154163 }
155164 }
156165
157- /**
158- * get current db instance, auto detect transaction
159- */
160- private getDB ( ) {
161- return this . trx || this . kysely
162- }
163-
164166 private logError ( e : unknown , errorMsg ?: string ) {
165167 errorMsg && this . logger ?. error ( errorMsg , e instanceof Error ? e : undefined )
166168 }
@@ -173,7 +175,7 @@ export class SqliteBuilder<DB extends Record<string, any>> {
173175 options : TransactionOptions < O > = { } ,
174176 ) : Promise < O | undefined > {
175177 if ( ! this . trx ) {
176- return await this . kysely . transaction ( )
178+ return await this . _kysely . transaction ( )
177179 . execute ( async ( trx ) => {
178180 this . trx = trx
179181 this . logger ?. debug ( 'run in transaction' )
@@ -213,14 +215,14 @@ export class SqliteBuilder<DB extends Record<string, any>> {
213215 query : CompiledQuery < O > ,
214216 ) : Promise < QueryResult < O > >
215217 public async execute < O > (
216- fn : ( db : Kysely < DB > | Transaction < DB > ) => AvailableBuilder < DB , O > ,
218+ fn : ( db : SqliteExecutor < DB , Extra > ) => AvailableBuilder < DB , O > ,
217219 ) : Promise < Simplify < O > [ ] | undefined >
218220 public async execute < O > (
219- data : CompiledQuery < O > | ( ( db : Kysely < DB > | Transaction < DB > ) => AvailableBuilder < DB , O > ) ,
221+ data : CompiledQuery < O > | ( ( db : SqliteExecutor < DB , Extra > ) => AvailableBuilder < DB , O > ) ,
220222 ) : Promise < QueryResult < O > | Simplify < O > [ ] | undefined > {
221223 return typeof data === 'function'
222- ? await data ( this . getDB ( ) ) . execute ( )
223- : await this . getDB ( ) . executeQuery ( data )
224+ ? await data ( this . executor ) . execute ( )
225+ : await this . kysely . executeQuery ( data )
224226 }
225227
226228 /**
@@ -229,13 +231,13 @@ export class SqliteBuilder<DB extends Record<string, any>> {
229231 * if is `select`, auto append `.limit(1)`
230232 */
231233 public async executeTakeFirst < O > (
232- fn : ( db : Kysely < DB > | Transaction < DB > ) => AvailableBuilder < DB , O > ,
234+ fn : ( db : SqliteExecutor < DB , Extra > ) => AvailableBuilder < DB , O > ,
233235 ) : Promise < Simplify < O > | undefined > {
234- let _sql = fn ( this . getDB ( ) )
236+ let _sql = fn ( this . executor )
235237 if ( isSelectQueryBuilder ( _sql ) ) {
236238 _sql = _sql . limit ( 1 )
237239 }
238- return await _sql . executeTakeFirstOrThrow ( )
240+ return await _sql . executeTakeFirst ( )
239241 }
240242
241243 /**
@@ -270,7 +272,7 @@ export class SqliteBuilder<DB extends Record<string, any>> {
270272 * @returns function to {@link CompileFn compile}
271273 */
272274 build : < O > (
273- queryBuilder : ( db : Kysely < DB > , param : < K extends keyof T > ( name : K ) => T [ K ] ) => Compilable < O > ,
275+ queryBuilder : ( db : SqliteExecutor < DB > , param : < K extends keyof T > ( name : K ) => T [ K ] ) => Compilable < O > ,
274276 ) => {
275277 let compiled : CompiledQuery < Compilable < O > > | null
276278 const dispose = ( ) => compiled = null
@@ -279,7 +281,7 @@ export class SqliteBuilder<DB extends Record<string, any>> {
279281 dispose,
280282 compile : ( param : T ) => {
281283 if ( ! compiled ) {
282- const { parameters, sql, query } = queryBuilder ( this . kysely , getPrecompileParam ) . compile ( )
284+ const { parameters, sql, query } = queryBuilder ( this . executor , getPrecompileParam ) . compile ( )
283285 compiled = {
284286 sql,
285287 query : processRootOperatorNode ( query ) as any ,
@@ -314,8 +316,8 @@ export class SqliteBuilder<DB extends Record<string, any>> {
314316 parameters ?: unknown [ ] ,
315317 ) : Promise < QueryResult < O | unknown > > {
316318 return typeof rawSql === 'string'
317- ? await this . getDB ( ) . executeQuery ( CompiledQuery . raw ( rawSql , parameters ) )
318- : await rawSql . execute ( this . getDB ( ) )
319+ ? await this . kysely . executeQuery ( CompiledQuery . raw ( rawSql , parameters ) )
320+ : await rawSql . execute ( this . kysely )
319321 }
320322
321323 /**
@@ -333,7 +335,7 @@ export class SqliteBuilder<DB extends Record<string, any>> {
333335 */
334336 public async destroy ( ) {
335337 this . logger ?. info ( 'destroyed' )
336- await this . kysely . destroy ( )
338+ await this . _kysely . destroy ( )
337339 this . trx = undefined
338340 }
339341}
0 commit comments