@@ -11,7 +11,6 @@ import type {
11
11
12
12
import {
13
13
CompiledQuery ,
14
- createQueryId ,
15
14
IdentifierNode ,
16
15
RawNode ,
17
16
SqliteAdapter ,
@@ -67,27 +66,48 @@ class ConnectionMutex {
67
66
}
68
67
}
69
68
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
+ )
75
85
}
76
86
77
87
export abstract class BaseSqliteDriver implements Driver {
78
88
private mutex = new ConnectionMutex ( )
79
89
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 >
80
94
/**
81
95
* Base abstract class that implements {@link Driver}
82
96
*
83
97
* You **MUST** assign `this.conn` in `init` and implement `destroy` method
84
98
*/
85
99
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 )
87
109
}
88
110
89
- init : ( ) => Promise < void >
90
-
91
111
async acquireConnection ( ) : Promise < DatabaseConnection > {
92
112
// SQLite only has one single connection. We use a mutex here to wait
93
113
// until the single connection has been released.
@@ -107,30 +127,6 @@ export abstract class BaseSqliteDriver implements Driver {
107
127
await connection . executeQuery ( CompiledQuery . raw ( 'rollback' ) )
108
128
}
109
129
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
-
134
130
async releaseConnection ( ) : Promise < void > {
135
131
this . mutex . unlock ( )
136
132
}
0 commit comments