@@ -110,12 +110,6 @@ export interface FileLoadResult extends BatchQueryResult {
110
110
}
111
111
112
112
export interface Transaction {
113
- commit : ( ) => QueryResult ;
114
- execute : ( query : string , params ?: any [ ] ) => QueryResult ;
115
- rollback : ( ) => QueryResult ;
116
- }
117
-
118
- export interface TransactionAsync {
119
113
commit : ( ) => QueryResult ;
120
114
execute : ( query : string , params ?: any [ ] ) => QueryResult ;
121
115
executeAsync : (
@@ -149,11 +143,10 @@ interface ISQLite {
149
143
location ?: string
150
144
) => void ;
151
145
detach : ( mainDbName : string , alias : string ) => void ;
152
- transactionAsync : (
146
+ transaction : (
153
147
dbName : string ,
154
- fn : ( tx : TransactionAsync ) => Promise < any >
148
+ fn : ( tx : Transaction ) => Promise < void > | void
155
149
) => Promise < void > ;
156
- transaction : ( dbName : string , fn : ( tx : Transaction ) => void ) => Promise < void > ;
157
150
execute : ( dbName : string , query : string , params ?: any [ ] ) => QueryResult ;
158
151
executeAsync : (
159
152
dbName : string ,
@@ -203,9 +196,7 @@ QuickSQLite.open = (dbName: string, location?: string) => {
203
196
const _close = QuickSQLite . close ;
204
197
QuickSQLite . close = ( dbName : string ) => {
205
198
_close ( dbName ) ;
206
- // setImmediate(() => {
207
199
delete locks [ dbName ] ;
208
- // });
209
200
} ;
210
201
211
202
const _execute = QuickSQLite . execute ;
@@ -230,91 +221,10 @@ QuickSQLite.executeAsync = async (
230
221
return res ;
231
222
} ;
232
223
233
- QuickSQLite . transaction = (
234
- dbName : string ,
235
- callback : ( tx : Transaction ) => void
236
- ) => {
237
- if ( ! locks [ dbName ] ) {
238
- throw Error ( `No lock found on db: ${ dbName } ` ) ;
239
- }
240
-
241
- let isFinalized = false ;
242
-
243
- // Local transaction context object implementation
244
- const execute = ( query : string , params ?: any [ ] ) : QueryResult => {
245
- if ( isFinalized ) {
246
- throw Error (
247
- `Quick SQLite Error: Cannot execute query on finalized transaction: ${ dbName } `
248
- ) ;
249
- }
250
-
251
- return QuickSQLite . execute ( dbName , query , params ) ;
252
- } ;
253
-
254
- const commit = ( ) => {
255
- if ( isFinalized ) {
256
- throw Error (
257
- `Quick SQLite Error: Cannot execute commit on finalized transaction: ${ dbName } `
258
- ) ;
259
- }
260
- const result = QuickSQLite . execute ( dbName , 'COMMIT' ) ;
261
- isFinalized = true ;
262
- return result ;
263
- } ;
264
-
265
- const rollback = ( ) => {
266
- if ( isFinalized ) {
267
- throw Error (
268
- `Quick SQLite Error: Cannot execute rollback on finalized transaction: ${ dbName } `
269
- ) ;
270
- }
271
- const result = QuickSQLite . execute ( dbName , 'ROLLBACK' ) ;
272
- isFinalized = true ;
273
- return result ;
274
- } ;
275
-
276
- async function run ( ) {
277
- try {
278
- QuickSQLite . execute ( dbName , 'BEGIN TRANSACTION' ) ;
279
-
280
- // Handle possible async callbacks
281
- await callback ( { commit, execute, rollback } ) ;
282
-
283
- if ( ! isFinalized ) {
284
- commit ( ) ;
285
- }
286
- } catch ( executionError ) {
287
- if ( ! isFinalized ) {
288
- try {
289
- rollback ( ) ;
290
- } catch ( rollbackError ) {
291
- throw rollbackError ;
292
- }
293
- }
294
-
295
- throw executionError ;
296
- } finally {
297
- locks [ dbName ] . inProgress = false ;
298
- startNextTransaction ( dbName ) ;
299
- }
300
- }
301
-
302
- return new Promise ( ( resolve , reject ) => {
303
- const tx : PendingTransaction = {
304
- start : ( ) => {
305
- run ( ) . then ( resolve ) . catch ( reject ) ;
306
- } ,
307
- } ;
308
-
309
- locks [ dbName ] . queue . push ( tx ) ;
310
- startNextTransaction ( dbName ) ;
311
- } ) ;
312
- } ;
313
-
314
- QuickSQLite . transactionAsync = async (
224
+ QuickSQLite . transaction = async (
315
225
dbName : string ,
316
- callback : ( tx : TransactionAsync ) => Promise < any >
317
- ) => {
226
+ fn : ( tx : Transaction ) => Promise < void >
227
+ ) : Promise < void > => {
318
228
if ( ! locks [ dbName ] ) {
319
229
throw Error ( `Quick SQLite Error: No lock found on db: ${ dbName } ` ) ;
320
230
}
@@ -366,7 +276,7 @@ QuickSQLite.transactionAsync = async (
366
276
try {
367
277
await QuickSQLite . executeAsync ( dbName , 'BEGIN TRANSACTION' ) ;
368
278
369
- await callback ( {
279
+ await fn ( {
370
280
commit,
371
281
execute,
372
282
executeAsync,
@@ -469,7 +379,7 @@ export const typeORMDriver = {
469
379
transaction : (
470
380
fn : ( tx : Transaction ) => Promise < void >
471
381
) : Promise < void > => {
472
- return QuickSQLite . transactionAsync ( options . name , fn ) ;
382
+ return QuickSQLite . transaction ( options . name , fn ) ;
473
383
} ,
474
384
close : ( ok : any , fail : any ) => {
475
385
try {
@@ -510,8 +420,7 @@ export type QuickSQLiteConnection = {
510
420
delete : ( ) => void ;
511
421
attach : ( dbNameToAttach : string , alias : string , location ?: string ) => void ;
512
422
detach : ( alias : string ) => void ;
513
- transactionAsync : ( fn : ( tx : TransactionAsync ) => Promise < any > ) => void ;
514
- transaction : ( fn : ( tx : Transaction ) => void ) => Promise < void > ;
423
+ transaction : ( fn : ( tx : Transaction ) => Promise < void > | void ) => Promise < void > ;
515
424
execute : ( query : string , params ?: any [ ] ) => QueryResult ;
516
425
executeAsync : ( query : string , params ?: any [ ] ) => Promise < QueryResult > ;
517
426
executeBatch : ( commands : SQLBatchTuple [ ] ) => BatchQueryResult ;
@@ -532,9 +441,7 @@ export const open = (options: {
532
441
attach : ( dbNameToAttach : string , alias : string , location ?: string ) =>
533
442
QuickSQLite . attach ( options . name , dbNameToAttach , alias , location ) ,
534
443
detach : ( alias : string ) => QuickSQLite . detach ( options . name , alias ) ,
535
- transactionAsync : ( fn : ( tx : TransactionAsync ) => Promise < any > ) =>
536
- QuickSQLite . transactionAsync ( options . name , fn ) ,
537
- transaction : ( fn : ( tx : Transaction ) => void ) =>
444
+ transaction : ( fn : ( tx : Transaction ) => Promise < void > | void ) =>
538
445
QuickSQLite . transaction ( options . name , fn ) ,
539
446
execute : ( query : string , params ?: any [ ] | undefined ) : QueryResult =>
540
447
QuickSQLite . execute ( options . name , query , params ) ,
0 commit comments