@@ -11,7 +11,6 @@ import type {
1111
1212import {
1313 CompiledQuery ,
14- createQueryId ,
1514 IdentifierNode ,
1615 RawNode ,
1716 SqliteAdapter ,
@@ -67,27 +66,48 @@ class ConnectionMutex {
6766 }
6867}
6968
70- export function parseSavepointCommand ( command : string , savepointName : string ) {
71- return RawNode . createWithChildren ( [
72- RawNode . createWithSql ( `${ command } ` ) ,
73- IdentifierNode . create ( savepointName ) , // ensures savepointName gets sanitized
74- ] )
69+ async function runSavepoint (
70+ command : string ,
71+ createQueryId : ( ) => { readonly queryId : string } ,
72+ connection : DatabaseConnection ,
73+ savepointName : string ,
74+ compileQuery : QueryCompiler [ 'compileQuery' ] ,
75+ ) : Promise < void > {
76+ await connection . executeQuery (
77+ compileQuery (
78+ RawNode . createWithChildren ( [
79+ RawNode . createWithSql ( `${ command } ` ) ,
80+ IdentifierNode . create ( savepointName ) , // ensures savepointName gets sanitized
81+ ] ) ,
82+ createQueryId ( ) ,
83+ ) ,
84+ )
7585}
7686
7787export abstract class BaseSqliteDriver implements Driver {
7888 private mutex = new ConnectionMutex ( )
7989 public conn ?: DatabaseConnection
90+ savepoint : ( ( connection : DatabaseConnection , savepointName : string , compileQuery : QueryCompiler [ 'compileQuery' ] ) => Promise < void > ) | undefined
91+ releaseSavepoint : ( ( connection : DatabaseConnection , savepointName : string , compileQuery : QueryCompiler [ 'compileQuery' ] ) => Promise < void > ) | undefined
92+ rollbackToSavepoint : ( ( connection : DatabaseConnection , savepointName : string , compileQuery : QueryCompiler [ 'compileQuery' ] ) => Promise < void > ) | undefined
93+ init : ( ) => Promise < void >
8094 /**
8195 * Base abstract class that implements {@link Driver}
8296 *
8397 * You **MUST** assign `this.conn` in `init` and implement `destroy` method
8498 */
8599 constructor ( init : ( ) => Promise < void > ) {
86- this . init = init
100+ this . init = ( ) => import ( 'kysely' )
101+ . then ( ( { createQueryId } ) => {
102+ if ( createQueryId ) {
103+ this . savepoint = runSavepoint . bind ( null , 'savepoint' , createQueryId )
104+ this . releaseSavepoint = runSavepoint . bind ( null , 'release' , createQueryId )
105+ this . rollbackToSavepoint = runSavepoint . bind ( null , 'rollback to' , createQueryId )
106+ }
107+ } )
108+ . then ( init )
87109 }
88110
89- init : ( ) => Promise < void >
90-
91111 async acquireConnection ( ) : Promise < DatabaseConnection > {
92112 // SQLite only has one single connection. We use a mutex here to wait
93113 // until the single connection has been released.
@@ -107,30 +127,6 @@ export abstract class BaseSqliteDriver implements Driver {
107127 await connection . executeQuery ( CompiledQuery . raw ( 'rollback' ) )
108128 }
109129
110- async savepoint (
111- connection : DatabaseConnection ,
112- savepointName : string ,
113- compileQuery : QueryCompiler [ 'compileQuery' ] ,
114- ) : Promise < void > {
115- await connection . executeQuery ( compileQuery ( parseSavepointCommand ( 'savepoint' , savepointName ) , createQueryId ( ) ) )
116- }
117-
118- async rollbackToSavepoint (
119- connection : DatabaseConnection ,
120- savepointName : string ,
121- compileQuery : QueryCompiler [ 'compileQuery' ] ,
122- ) : Promise < void > {
123- await connection . executeQuery ( compileQuery ( parseSavepointCommand ( 'rollback to' , savepointName ) , createQueryId ( ) ) )
124- }
125-
126- async releaseSavepoint (
127- connection : DatabaseConnection ,
128- savepointName : string ,
129- compileQuery : QueryCompiler [ 'compileQuery' ] ,
130- ) : Promise < void > {
131- await connection . executeQuery ( compileQuery ( parseSavepointCommand ( 'release' , savepointName ) , createQueryId ( ) ) )
132- }
133-
134130 async releaseConnection ( ) : Promise < void > {
135131 this . mutex . unlock ( )
136132 }
0 commit comments