diff --git a/web/core/local-db/utils/load-issues.ts b/web/core/local-db/utils/load-issues.ts index 4a1986f1e19..d374d2184f6 100644 --- a/web/core/local-db/utils/load-issues.ts +++ b/web/core/local-db/utils/load-issues.ts @@ -40,13 +40,14 @@ export const addIssuesBulk = async (issues: any, batchSize = 100) => { export const deleteIssueFromLocal = async (issue_id: any) => { if (!rootStore.user.localDBEnabled || !persistence.db) return; - const deleteQuery = `delete from issues where id='${issue_id}'`; + const deleteQuery = `DELETE from issues where id='${issue_id}'`; const deleteMetaQuery = `delete from issue_meta where issue_id='${issue_id}'`; - persistence.db.exec("BEGIN;"); - persistence.db.exec(deleteQuery); - persistence.db.exec(deleteMetaQuery); - persistence.db.exec("COMMIT;"); + await persistence.db.exec("BEGIN;"); + + await persistence.db.exec(deleteQuery); + await persistence.db.exec(deleteMetaQuery); + await persistence.db.exec("COMMIT;"); }; // @todo: Update deletes the issue description from local. Implement a separate update. export const updateIssue = async (issue: TIssue & { is_local_update: number }) => { @@ -55,7 +56,7 @@ export const updateIssue = async (issue: TIssue & { is_local_update: number }) = const issue_id = issue.id; // delete the issue and its meta data await deleteIssueFromLocal(issue_id); - addIssue(issue); + await addIssue(issue); }; export const syncDeletesToLocal = async (workspaceId: string, projectId: string, queries: any) => { @@ -98,27 +99,33 @@ const stageIssueInserts = async (issue: any) => { .join(", "); const query = `INSERT OR REPLACE INTO issues (${columns}) VALUES (${values});`; - persistence.db.exec(query); + await persistence.db.exec(query); await persistence.db.exec({ sql: `DELETE from issue_meta where issue_id='${issue_id}'`, }); + const metaPromises: Promise[] = []; + ARRAY_FIELDS.forEach((field) => { const values = issue[field]; if (values && values.length) { values.forEach((val: any) => { - persistence.db.exec({ + const p = persistence.db.exec({ sql: `INSERT OR REPLACE into issue_meta(issue_id,key,value) values (?,?,?) `, bind: [issue_id, field, val], }); + metaPromises.push(p); }); } else { // Added for empty fields? - persistence.db.exec({ + const p = persistence.db.exec({ sql: `INSERT OR REPLACE into issue_meta(issue_id,key,value) values (?,?,?) `, bind: [issue_id, field, ""], }); + metaPromises.push(p); } }); + + await Promise.all(metaPromises); }; diff --git a/web/core/local-db/utils/utils.ts b/web/core/local-db/utils/utils.ts index 9fb81d4c250..bb42d3531f2 100644 --- a/web/core/local-db/utils/utils.ts +++ b/web/core/local-db/utils/utils.ts @@ -59,7 +59,7 @@ export const updatePersistentLayer = async (issueIds: string | string[]) => { "type_id", "description_html", ]); - updateIssue({ ...issuePartial, is_local_update: 1 }); + await updateIssue({ ...issuePartial, is_local_update: 1 }); } }); }; diff --git a/web/core/local-db/worker/db.ts b/web/core/local-db/worker/db.ts index 17e326b7877..b14347456b7 100644 --- a/web/core/local-db/worker/db.ts +++ b/web/core/local-db/worker/db.ts @@ -24,8 +24,8 @@ interface SQLiteInstance { export class DBClass { private instance: SQLiteInstance = {} as SQLiteInstance; private sqlite3: any; - private tp: Promise | null = null; - private tpResolver: any; + private tp: Promise[] = []; + private tpResolver: any = []; async init(dbName: string) { if (!dbName || typeof dbName !== "string") { throw new Error("Invalid database name"); @@ -57,8 +57,19 @@ export class DBClass { } async exec(props: string | TQueryProps) { - if (this.tp && props === "BEGIN;") { - await this.tp; + if (props === "BEGIN;") { + let promiseToAwait; + if (this.tp.length > 0) { + promiseToAwait = this.tp.shift(); + } + const p = new Promise((resolve, reject) => { + this.tpResolver.push({ resolve, reject }); + }); + this.tp.push(p); + + if (promiseToAwait) { + await promiseToAwait; + } } let sql: string, bind: any[]; if (typeof props === "string") { @@ -84,16 +95,12 @@ export class DBClass { } } - if (sql === "BEGIN;") { - this.tp = new Promise((resolve, reject) => { - this.tpResolver = { resolve, reject }; - }); - } - if (sql === "COMMIT;" && this.tp) { await this.instance.exec(sql); - this.tpResolver.resolve(); - this.tp = null; + if (this.tp.length > 0) { + const { resolve } = this.tpResolver.shift(); + resolve(); + } return; } return await this.instance.exec(sql);