Skip to content

Commit 88ff3b6

Browse files
author
Oscar Franco
committed
Remove transaction async
1 parent 0ce3258 commit 88ff3b6

File tree

5 files changed

+30
-130
lines changed

5 files changed

+30
-130
lines changed

README.md

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ This library is sponsored by:
2626

2727
[<img src="https://raw.githubusercontent.com/ospfranco/react-native-quick-sqlite/main/sponsors/stream.png">](https://getstream.io/try-for-free/?utm_source=ospfranco&utm_medium=Github_Repo_Content_Ad&utm_content=Developer&utm_campaign=ospfranco_December2022_Trial_klmh22)
2828

29-
*Build cross-platform messaging experiences with Stream Chat API. Sign up for Stream's 30 day trial for free!*
29+
_Build cross-platform messaging experiences with Stream Chat API. Sign up for Stream's 30 day trial for free!_
3030

31-
[*Try the React Native Chat Tutorial 💬*](https://getstream.io/chat/sdk/react-native/?utm_source=ospfranco&utm_medium=Github_Repo_Content_Ad&utm_content=Developer&utm_campaign=ospfranco_December2022_Trial_klmh22)
31+
[_Try the React Native Chat Tutorial 💬_](https://getstream.io/chat/sdk/react-native/?utm_source=ospfranco&utm_medium=Github_Repo_Content_Ad&utm_content=Developer&utm_campaign=ospfranco_December2022_Trial_klmh22)
3232

3333
If you want to sponsor the development of this library, [get in touch](mailto:[email protected]).
34+
3435
## API
3536

3637
```typescript
@@ -45,7 +46,6 @@ db = {
4546
delete: () => void,
4647
attach: (dbNameToAttach: string, alias: string, location?: string) => void,
4748
detach: (alias: string) => void,
48-
transactionAsync: (fn: (tx: TransactionAsync) => Promise<void>) => Promise<void>,
4949
transaction: (fn: (tx: Transaction) => void) => Promise<void>,
5050
execute: (query: string, params?: any[]) => QueryResult,
5151
executeAsync: (
@@ -101,6 +101,12 @@ await QuickSQLite.transaction('myDatabase', (tx) => {
101101
[0, 1]
102102
);
103103

104+
// offload from JS thread
105+
await tx.executeAsync = tx.executeAsync(
106+
'UPDATE sometable SET somecolumn = ? where somekey = ?',
107+
[0, 1]
108+
);
109+
104110
// Any uncatched error ROLLBACK transaction
105111
throw new Error('Random Error!');
106112

@@ -111,19 +117,6 @@ await QuickSQLite.transaction('myDatabase', (tx) => {
111117
});
112118
```
113119

114-
Async transactions are also possible:
115-
116-
```ts
117-
await QuickSQLite.transactionAsync('myDatabase', async (tx) => {
118-
tx.execute('UPDATE sometable SET somecolumn = ? where somekey = ?', [0, 1]);
119-
120-
await tx.executeAsync(
121-
'UPDATE sometable SET somecolumn = ? where somekey = ?',
122-
[0, 1]
123-
);
124-
});
125-
```
126-
127120
### Batch operation
128121

129122
Batch execution allows transactional execution of a set of commands

example/src/tests/rawQueries.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ export function registerBaseTests() {
386386
const age = chance.integer();
387387
const networth = chance.floating();
388388

389-
await db.transactionAsync(async tx => {
389+
await db.transaction(async tx => {
390390
const res = await tx.executeAsync(
391391
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
392392
[id, name, age, networth],
@@ -418,7 +418,7 @@ export function registerBaseTests() {
418418
const networth = chance.floating();
419419

420420
try {
421-
await db.transactionAsync(async tx => {
421+
await db.transaction(async tx => {
422422
await tx.executeAsync(
423423
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
424424
[id, name, age, networth],
@@ -441,7 +441,7 @@ export function registerBaseTests() {
441441
const age = chance.integer();
442442
const networth = chance.floating();
443443

444-
await db.transactionAsync(async tx => {
444+
await db.transaction(async tx => {
445445
await tx.executeAsync(
446446
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
447447
[id, name, age, networth],
@@ -466,7 +466,7 @@ export function registerBaseTests() {
466466
const age = chance.integer();
467467
const networth = chance.floating();
468468

469-
await db.transactionAsync(async tx => {
469+
await db.transaction(async tx => {
470470
await tx.executeAsync(
471471
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
472472
[id, name, age, networth],
@@ -491,7 +491,7 @@ export function registerBaseTests() {
491491
// ACT: Start multiple async transactions to upsert and select the same record
492492
const promises = [];
493493
for (let iteration = 1; iteration <= iterations; iteration++) {
494-
const promised = db.transactionAsync(async tx => {
494+
const promised = db.transaction(async tx => {
495495
// ACT: Upsert statement to create record / increment the value
496496
await tx.executeAsync(
497497
`
@@ -532,7 +532,7 @@ export function registerBaseTests() {
532532
});
533533

534534
it('Async transaction, rejects on callback error', async () => {
535-
const promised = db.transactionAsync(async tx => {
535+
const promised = db.transaction(async tx => {
536536
throw new Error('Error from callback');
537537
});
538538

@@ -548,7 +548,7 @@ export function registerBaseTests() {
548548
});
549549

550550
it('Async transaction, rejects on invalid query', async () => {
551-
const promised = db.transactionAsync(async tx => {
551+
const promised = db.transaction(async tx => {
552552
await tx.executeAsync('SELECT * FROM [tableThatDoesNotExist];');
553553
});
554554

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"@types/react": "18.0.26",
5454
"@types/react-native": "0.70.7",
5555
"husky": "^6.0.0",
56-
"prettier": "^2.7.1",
56+
"prettier": "^2.8.3",
5757
"react": "18.1.0",
5858
"react-native": "0.70.6",
5959
"react-native-builder-bob": "^0.18.2",

src/index.ts

Lines changed: 9 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,6 @@ export interface FileLoadResult extends BatchQueryResult {
110110
}
111111

112112
export 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) => {
203196
const _close = QuickSQLite.close;
204197
QuickSQLite.close = (dbName: string) => {
205198
_close(dbName);
206-
// setImmediate(() => {
207199
delete locks[dbName];
208-
// });
209200
};
210201

211202
const _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),

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4653,10 +4653,10 @@ posix-character-classes@^0.1.0:
46534653
resolved "https://registry.yarnpkg.com/posix-character-classes/-/posix-character-classes-0.1.1.tgz#01eac0fe3b5af71a2a6c02feabb8c1fef7e00eab"
46544654
integrity sha1-AerA/jta9xoqbAL+q7jB/vfgDqs=
46554655

4656-
prettier@^2.7.1:
4657-
version "2.7.1"
4658-
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.7.1.tgz#e235806850d057f97bb08368a4f7d899f7760c64"
4659-
integrity sha512-ujppO+MkdPqoVINuDFDRLClm7D78qbDt0/NR+wp5FqEZOoTNAjPHWj17QRhu7geIHJfcNhRk1XVQmF8Bp3ye+g==
4656+
prettier@^2.8.3:
4657+
version "2.8.3"
4658+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.3.tgz#ab697b1d3dd46fb4626fbe2f543afe0cc98d8632"
4659+
integrity sha512-tJ/oJ4amDihPoufT5sM0Z1SKEuKay8LfVAMlbbhnnkvt6BUserZylqo2PN+p9KeljLr0OHa2rXHU1T8reeoTrw==
46604660

46614661
pretty-format@^26.0.0, pretty-format@^26.5.2, pretty-format@^26.6.2:
46624662
version "26.6.2"

0 commit comments

Comments
 (0)