Skip to content

Commit

Permalink
[#76] Fix multiple queries transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
ifirmawan committed Jul 27, 2023
1 parent d64ca13 commit f2ba212
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions app/src/database/conn.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import { Platform } from 'react-native';
import { Asset } from 'expo-asset';
import * as FileSystem from 'expo-file-system';
import * as SQLite from 'expo-sqlite';

const openDatabase = () => {
if (Platform.OS === 'web') {
return {
transaction: () => {
return {
executeSql: () => {},
};
},
};
}
const db = SQLite.openDatabase('db.db');
return db;
};
Expand All @@ -24,13 +14,29 @@ const tx = (db, query, params = []) => {
db.transaction(
(transaction) => {
if (Array.isArray(query)) {
const results = [];
query.forEach(async (q) => {
transaction.executeSql(q, params, (_, resultSet) => {
results.push(resultSet); // Store the result set in the array
const promises = query.map((q) => {
return new Promise((innerResolve, innerReject) => {
transaction.executeSql(
q,
params,
(_, resultSet) => {
innerResolve(resultSet);
},
(_, error) => {
innerReject(error);
return false; // Rollback the transaction
},
);
});
});
resolve(results);

Promise.all(promises)
.then((results) => {
resolve(results);
})
.catch((error) => {
reject(error);
});
} else {
transaction.executeSql(
query,
Expand Down

0 comments on commit f2ba212

Please sign in to comment.