@@ -110,12 +110,6 @@ export interface FileLoadResult extends BatchQueryResult {
110110}
111111
112112export interface Transaction {
113- commit : ( ) => QueryResult ;
114- execute : ( query : string , params ?: any [ ] ) => QueryResult ;
115- rollback : ( ) => QueryResult ;
116- }
117-
118- export interface TransactionAsync {
119113 commit : ( ) => QueryResult ;
120114 execute : ( query : string , params ?: any [ ] ) => QueryResult ;
121115 executeAsync : (
@@ -149,11 +143,10 @@ interface ISQLite {
149143 location ?: string
150144 ) => void ;
151145 detach : ( mainDbName : string , alias : string ) => void ;
152- transactionAsync : (
146+ transaction : (
153147 dbName : string ,
154- fn : ( tx : TransactionAsync ) => Promise < any >
148+ fn : ( tx : Transaction ) => Promise < void > | void
155149 ) => Promise < void > ;
156- transaction : ( dbName : string , fn : ( tx : Transaction ) => void ) => Promise < void > ;
157150 execute : ( dbName : string , query : string , params ?: any [ ] ) => QueryResult ;
158151 executeAsync : (
159152 dbName : string ,
@@ -203,9 +196,7 @@ QuickSQLite.open = (dbName: string, location?: string) => {
203196const _close = QuickSQLite . close ;
204197QuickSQLite . close = ( dbName : string ) => {
205198 _close ( dbName ) ;
206- // setImmediate(() => {
207199 delete locks [ dbName ] ;
208- // });
209200} ;
210201
211202const _execute = QuickSQLite . execute ;
@@ -230,91 +221,10 @@ QuickSQLite.executeAsync = async (
230221 return res ;
231222} ;
232223
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 (
315225 dbName : string ,
316- callback : ( tx : TransactionAsync ) => Promise < any >
317- ) => {
226+ fn : ( tx : Transaction ) => Promise < void >
227+ ) : Promise < void > => {
318228 if ( ! locks [ dbName ] ) {
319229 throw Error ( `Quick SQLite Error: No lock found on db: ${ dbName } ` ) ;
320230 }
@@ -366,7 +276,7 @@ QuickSQLite.transactionAsync = async (
366276 try {
367277 await QuickSQLite . executeAsync ( dbName , 'BEGIN TRANSACTION' ) ;
368278
369- await callback ( {
279+ await fn ( {
370280 commit,
371281 execute,
372282 executeAsync,
@@ -469,7 +379,7 @@ export const typeORMDriver = {
469379 transaction : (
470380 fn : ( tx : Transaction ) => Promise < void >
471381 ) : Promise < void > => {
472- return QuickSQLite . transactionAsync ( options . name , fn ) ;
382+ return QuickSQLite . transaction ( options . name , fn ) ;
473383 } ,
474384 close : ( ok : any , fail : any ) => {
475385 try {
@@ -510,8 +420,7 @@ export type QuickSQLiteConnection = {
510420 delete : ( ) => void ;
511421 attach : ( dbNameToAttach : string , alias : string , location ?: string ) => void ;
512422 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 > ;
515424 execute : ( query : string , params ?: any [ ] ) => QueryResult ;
516425 executeAsync : ( query : string , params ?: any [ ] ) => Promise < QueryResult > ;
517426 executeBatch : ( commands : SQLBatchTuple [ ] ) => BatchQueryResult ;
@@ -532,9 +441,7 @@ export const open = (options: {
532441 attach : ( dbNameToAttach : string , alias : string , location ?: string ) =>
533442 QuickSQLite . attach ( options . name , dbNameToAttach , alias , location ) ,
534443 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 ) =>
538445 QuickSQLite . transaction ( options . name , fn ) ,
539446 execute : ( query : string , params ?: any [ ] | undefined ) : QueryResult =>
540447 QuickSQLite . execute ( options . name , query , params ) ,
0 commit comments