1- import { HybridNitroSQLite } from '../nitro'
21import { queueOperationAsync , throwIfDatabaseIsNotOpen } from '../DatabaseQueue'
32import type {
43 QueryResult ,
@@ -9,20 +8,19 @@ import type {
98import { execute , executeAsync } from './execute'
109import NitroSQLiteError from '../NitroSQLiteError'
1110
12- export const transaction = (
11+ export const transaction = async < Result = void > (
1312 dbName : string ,
14- fn : ( tx : Transaction ) => Promise < void > | void ,
13+ transactionCallback : ( tx : Transaction ) => Promise < Result > ,
1514 isExclusive = false ,
16- ) : Promise < void > => {
15+ ) => {
1716 throwIfDatabaseIsNotOpen ( dbName )
1817
1918 let isFinalized = false
2019
21- // Local transaction context object implementation
22- const executeOnTransaction = < Data extends QueryResultRow = never > (
20+ const executeOnTransaction = < Row extends QueryResultRow = never > (
2321 query : string ,
2422 params ?: SQLiteQueryParams ,
25- ) : QueryResult < Data > => {
23+ ) : QueryResult < Row > => {
2624 if ( isFinalized ) {
2725 throw new NitroSQLiteError (
2826 `Cannot execute query on finalized transaction: ${ dbName } ` ,
@@ -31,10 +29,10 @@ export const transaction = (
3129 return execute ( dbName , query , params )
3230 }
3331
34- const executeAsyncOnTransaction = < Data extends QueryResultRow = never > (
32+ const executeAsyncOnTransaction = < Row extends QueryResultRow = never > (
3533 query : string ,
3634 params ?: SQLiteQueryParams ,
37- ) : Promise < QueryResult < Data > > => {
35+ ) : Promise < QueryResult < Row > > => {
3836 if ( isFinalized ) {
3937 throw new NitroSQLiteError (
4038 `Cannot execute query on finalized transaction: ${ dbName } ` ,
@@ -49,9 +47,8 @@ export const transaction = (
4947 `Cannot execute commit on finalized transaction: ${ dbName } ` ,
5048 )
5149 }
52- const result = HybridNitroSQLite . execute ( dbName , 'COMMIT' )
5350 isFinalized = true
54- return result
51+ return execute ( dbName , 'COMMIT' )
5552 }
5653
5754 const rollback = ( ) => {
@@ -60,27 +57,28 @@ export const transaction = (
6057 `Cannot execute rollback on finalized transaction: ${ dbName } ` ,
6158 )
6259 }
63- const result = HybridNitroSQLite . execute ( dbName , 'ROLLBACK' )
6460 isFinalized = true
65- return result
61+ return execute ( dbName , 'ROLLBACK' )
6662 }
6763
6864 try {
69- return queueOperationAsync ( dbName , async ( ) => {
65+ return await queueOperationAsync ( dbName , async ( ) => {
7066 try {
71- await HybridNitroSQLite . executeAsync (
67+ await executeAsync (
7268 dbName ,
7369 isExclusive ? 'BEGIN EXCLUSIVE TRANSACTION' : 'BEGIN TRANSACTION' ,
7470 )
7571
76- await fn ( {
72+ const result = await transactionCallback ( {
7773 commit,
7874 execute : executeOnTransaction ,
7975 executeAsync : executeAsyncOnTransaction ,
8076 rollback,
8177 } )
8278
8379 if ( ! isFinalized ) commit ( )
80+
81+ return result
8482 } catch ( executionError ) {
8583 if ( ! isFinalized ) {
8684 try {
@@ -91,8 +89,6 @@ export const transaction = (
9189 }
9290
9391 throw executionError
94- } finally {
95- isFinalized = false
9692 }
9793 } )
9894 } catch ( error ) {
0 commit comments