Skip to content

Commit 046f9cb

Browse files
Merge pull request #390 from ujjwalguptaofficial/ug-fix-update-wait-for-tx
Ug fix update wait for tx
2 parents 6a5b3e9 + 1d8357f commit 046f9cb

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

src/common/interfaces.ts

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export interface IUpdateQuery {
5252
set: { [columnName: string]: any };
5353
where?: IWhereQuery | IWhereQuery[];
5454
mapSet?: string | Function;
55+
returnImmediate?: boolean;
5556
}
5657

5758
export interface IAlterQuery {

src/worker/executors/transaction/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ export class Transaction extends Base {
164164
const query = request.query;
165165

166166
const callAPI = (api: typeof Select) => {
167+
query.returnImmediate = true;
167168
requestObj = new api(
168169
query, this.util
169170
);
@@ -233,4 +234,4 @@ export class Transaction extends Base {
233234
});
234235
return invalidTable;
235236
}
236-
}
237+
}

src/worker/executors/update/index.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { IUpdateQuery, ISelectQuery, QUERY_OPTION, API, IWhereQuery, DATA_TYPE, ERROR_TYPE } from "@/common";
1+
import { IUpdateQuery, ISelectQuery, QUERY_OPTION, API, IWhereQuery, DATA_TYPE, ERROR_TYPE, promiseAll } from "@/common";
22
import { IDBUtil } from "@/worker/idbutil";
3-
import { DbMeta } from "@worker/model";
43
import { QueryHelper } from "../query_helper";
54
import { promiseReject, isArray, getDataType, variableFromPath, LogHelper } from "@worker/utils";
65
import { BaseFetch } from "@executors/base_fetch";
@@ -15,6 +14,7 @@ export class Update extends BaseFetch {
1514

1615
constructor(query: IUpdateQuery, util: IDBUtil) {
1716
super();
17+
query.returnImmediate = query.returnImmediate == null ? false : query.returnImmediate;
1818
this.query = query as any;
1919
this.util = util;
2020
this.tableName = query.in;
@@ -37,7 +37,7 @@ export class Update extends BaseFetch {
3737
const err = queryHelper.validate(API.Update, query);
3838
if (err) return promiseReject(err);
3939
return beforeExecute().then(_ => {
40-
this.initTransaction();
40+
const txPromise = this.initTransaction();
4141
let pResult: Promise<void>;
4242
if (query.where != null) {
4343
if ((query.where as IWhereQuery).or || isArray(query.where)) {
@@ -50,7 +50,11 @@ export class Update extends BaseFetch {
5050
else {
5151
pResult = this.executeWhereUndefinedLogic();
5252
}
53-
return pResult.then(() => {
53+
const promiseToUse = [pResult];
54+
if (query.returnImmediate === false && txPromise) {
55+
promiseToUse.push(txPromise);
56+
}
57+
return promiseAll(promiseToUse).then(() => {
5458
return this.rowAffected;
5559
})
5660
})
@@ -84,10 +88,12 @@ export class Update extends BaseFetch {
8488

8589
private initTransaction() {
8690
const tableName = (this.query as any).in;
91+
let promise: Promise<void>;
8792
if (!this.isTxQuery) {
88-
this.util.createTransaction([tableName]);
93+
promise = this.util.createTransaction([tableName]);
8994
}
9095
this.objectStore = this.util.objectStore(tableName);
96+
return promise;
9197
}
9298
}
9399

src/worker/idbutil/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class IDBUtil {
2727

2828
createTransaction(tables: string[], mode = IDB_MODE.ReadWrite) {
2929
this.tx = this.con.transaction(tables, mode);
30-
return promise((res, rej) => {
30+
return promise<void>((res, rej) => {
3131
this.tx.oncomplete = res;
3232
this.tx.onabort = res;
3333
this.tx.onerror = rej;

0 commit comments

Comments
 (0)