File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -195,6 +195,20 @@ QuickSQLite.transaction('myDatabase', (tx) => {
195195});
196196```
197197
198+ Async transactions are also possible, but the API is based on promises and/or a boolean response:
199+
200+ ``` ts
201+ QuickSQLite .asyncTransaction (' myDatabase' , async (tx ) => {
202+ // If the function throws (rejects) the transaction will be rolled back
203+ const res = tx .promiseExecuteSql (
204+ ' UPDATE sometable SET somecolumn = ? where somekey = ?' ,
205+ [0 , 1 ]
206+ );
207+ // You must also return true to signal a correct transaction
208+ return true ;
209+ });
210+ ```
211+
198212### Batch operation
199213
200214Batch execution allows transactional execution of a set of commands
Original file line number Diff line number Diff line change @@ -134,6 +134,10 @@ export interface AsyncTransaction {
134134 params : any [ ] | undefined ,
135135 cb : ( res : QueryResult ) => void
136136 ) => void ;
137+ promiseExecuteSql : (
138+ query : string ,
139+ params : any [ ] | undefined
140+ ) => Promise < QueryResult > ;
137141}
138142
139143export interface PendingTransaction {
@@ -328,11 +332,30 @@ QuickSQLite.asyncTransaction = (
328332 return QuickSQLite . asyncExecuteSql ( dbName , query , params , cb ) ;
329333 } ;
330334
335+ const promiseExecuteSql = (
336+ query : string ,
337+ params : any [ ] | undefined
338+ ) : Promise < QueryResult > => {
339+ return new Promise ( ( resolve , reject ) => {
340+ QuickSQLite . asyncExecuteSql ( dbName , query , params , ( res ) => {
341+ if ( res . status ) {
342+ reject ( res ) ;
343+ } else {
344+ resolve ( res ) ;
345+ }
346+ } ) ;
347+ } ) ;
348+ } ;
349+
331350 const tx : PendingTransaction = {
332351 start : async ( ) => {
333352 try {
334353 QuickSQLite . executeSql ( dbName , 'BEGIN TRANSACTION' , null ) ;
335- const result = await callback ( { executeSql, asyncExecuteSql } ) ;
354+ const result = await callback ( {
355+ executeSql,
356+ asyncExecuteSql,
357+ promiseExecuteSql,
358+ } ) ;
336359 if ( result === true ) {
337360 QuickSQLite . executeSql ( dbName , 'COMMIT' , null ) ;
338361 } else {
You can’t perform that action at this time.
0 commit comments