Skip to content

Commit 3ff9882

Browse files
author
Oscar Franco
committed
Add an async version of transactions
1 parent 1590c9d commit 3ff9882

1 file changed

Lines changed: 59 additions & 5 deletions

File tree

src/index.ts

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable no-shadow */
2-
/* eslint-disable no-undef */
31
import { NativeModules } from 'react-native';
42

53
declare global {
@@ -129,6 +127,15 @@ export interface Transaction {
129127
executeSql: (query: string, params?: any[]) => QueryResult;
130128
}
131129

130+
export interface AsyncTransaction {
131+
executeSql: (query: string, params?: any[]) => QueryResult;
132+
asyncExecuteSql: (
133+
query: string,
134+
params: any[] | undefined,
135+
cb: (res: QueryResult) => void
136+
) => void;
137+
}
138+
132139
export interface PendingTransaction {
133140
start: () => void;
134141
}
@@ -141,9 +148,7 @@ interface ISQLite {
141148
status: 0 | 1;
142149
message?: string;
143150
};
144-
close: (
145-
dbName: string
146-
) => {
151+
close: (dbName: string) => {
147152
status: 0 | 1;
148153
message?: string;
149154
};
@@ -156,6 +161,10 @@ interface ISQLite {
156161
) => StatementResult;
157162
detach: (mainDbName: string, alias: string) => StatementResult;
158163
transaction: (dbName: string, fn: (tx: Transaction) => boolean) => void;
164+
asyncTransaction: (
165+
dbName: string,
166+
fn: (tx: AsyncTransaction) => Promise<boolean>
167+
) => void;
159168
executeSql: (
160169
dbName: string,
161170
query: string,
@@ -298,6 +307,51 @@ QuickSQLite.transaction = (
298307
startNextTransaction(dbName);
299308
};
300309

310+
QuickSQLite.asyncTransaction = (
311+
dbName: string,
312+
callback: (tx: AsyncTransaction) => Promise<boolean>
313+
) => {
314+
if (!locks[dbName]) {
315+
throw Error(`Quick SQLite Error: No lock found on db: ${dbName}`);
316+
}
317+
318+
// Local transaction context object implementation
319+
const executeSql = (query: string, params?: any[]): QueryResult => {
320+
return QuickSQLite.executeSql(dbName, query, params);
321+
};
322+
323+
const asyncExecuteSql = (
324+
query: string,
325+
params: any[] | undefined,
326+
cb: (res: QueryResult) => void
327+
) => {
328+
return QuickSQLite.asyncExecuteSql(dbName, query, params, cb);
329+
};
330+
331+
const tx: PendingTransaction = {
332+
start: async () => {
333+
try {
334+
QuickSQLite.executeSql(dbName, 'BEGIN TRANSACTION', null);
335+
const result = await callback({ executeSql, asyncExecuteSql });
336+
if (result === true) {
337+
QuickSQLite.executeSql(dbName, 'COMMIT', null);
338+
} else {
339+
QuickSQLite.executeSql(dbName, 'ROLLBACK', null);
340+
}
341+
} catch (e: any) {
342+
QuickSQLite.executeSql(dbName, 'ROLLBACK', null);
343+
throw e;
344+
} finally {
345+
locks[dbName].inProgress = false;
346+
startNextTransaction(dbName);
347+
}
348+
},
349+
};
350+
351+
locks[dbName].queue.push(tx);
352+
startNextTransaction(dbName);
353+
};
354+
301355
const startNextTransaction = (dbName: string) => {
302356
if (locks[dbName].inProgress) {
303357
// Transaction is already in process bail out

0 commit comments

Comments
 (0)