From 15ca19ddd005cb60edfd9e696985230b7a1dddac Mon Sep 17 00:00:00 2001 From: 01zulfi <85733202+01zulfi@users.noreply.github.com> Date: Thu, 9 Jul 2026 14:36:39 +0500 Subject: [PATCH] core: remove lazy access of pendingsyncitems table Signed-off-by: 01zulfi <85733202+01zulfi@users.noreply.github.com> --- packages/core/src/api/index.ts | 6 ++-- packages/core/src/api/sync/index.ts | 14 ++++----- .../core/src/database/pending-sync-items.ts | 30 ++++++++----------- packages/core/src/interfaces.ts | 2 -- 4 files changed, 22 insertions(+), 30 deletions(-) diff --git a/packages/core/src/api/index.ts b/packages/core/src/api/index.ts index 272997403..d1e63c18c 100644 --- a/packages/core/src/api/index.ts +++ b/packages/core/src/api/index.ts @@ -58,7 +58,6 @@ import { IFileStorage, IStorage, KVStorageAccessor, - PendingSyncItemsAccessor, StorageAccessor } from "../interfaces.js"; import TokenManager from "./token-manager.js"; @@ -166,8 +165,9 @@ class Database { private _kv = new KVStorage(this.databaseReady.promise); kv: KVStorageAccessor = () => this._kv; - private _pendingSyncItems = new PendingSyncItems(this.databaseReady.promise); - pendingSyncItems: PendingSyncItemsAccessor = () => this._pendingSyncItems; + pendingSyncItems = new PendingSyncItems( + this.sql as unknown as DatabaseAccessor + ); private _config: ConfigStorage = new ConfigStorage( this.databaseReady.promise ); diff --git a/packages/core/src/api/sync/index.ts b/packages/core/src/api/sync/index.ts index abced08c2..cbc12e0b0 100644 --- a/packages/core/src/api/sync/index.ts +++ b/packages/core/src/api/sync/index.ts @@ -306,17 +306,17 @@ export class Sync { } async stop(options: SyncOptions) { - const pendingInboxItems = await this.db - .pendingSyncItems() - .getByType("inbox-item"); + const pendingInboxItems = await this.db.pendingSyncItems.getByType( + "inbox-item" + ); if (pendingInboxItems.length > 0) { const items = pendingInboxItems.map( (item) => JSON.parse(item.data) as SyncInboxItem ); await handleInboxItems(items, this.db); - await this.db - .pendingSyncItems() - .remove(pendingInboxItems.map((item) => item.id)); + await this.db.pendingSyncItems.remove( + pendingInboxItems.map((item) => item.id) + ); } if ( @@ -602,7 +602,7 @@ export class Sync { * TODO: do this for other items as well */ for (const item of inboxItems) { - await this.db.pendingSyncItems().add({ + await this.db.pendingSyncItems.add({ id: item.id, type: "inbox-item", data: JSON.stringify(item), diff --git a/packages/core/src/database/pending-sync-items.ts b/packages/core/src/database/pending-sync-items.ts index 5fd4d8f1b..5ad25afe1 100644 --- a/packages/core/src/database/pending-sync-items.ts +++ b/packages/core/src/database/pending-sync-items.ts @@ -17,37 +17,31 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -import { LazyDatabaseAccessor, RawDatabaseSchema } from "./index.js"; +import { DatabaseAccessor, RawDatabaseSchema } from "./index.js"; import { PendingSyncItem } from "../types.js"; export class PendingSyncItems { - private readonly db: LazyDatabaseAccessor; - constructor(db: LazyDatabaseAccessor) { - this.db = db as unknown as LazyDatabaseAccessor; - } + constructor(private readonly db: DatabaseAccessor) {} async add(item: PendingSyncItem) { - await this.db.then((db) => - db.replaceInto("pendingsyncitems").values(item).execute() - ); + await this.db().replaceInto("pendingsyncitems").values(item).execute(); } async getByType(type: PendingSyncItem["type"]) { - const result = await this.db.then((db) => - db - .selectFrom("pendingsyncitems") - .where("type", "==", type) - .selectAll() - .execute() - ); + const result = await this.db() + .selectFrom("pendingsyncitems") + .where("type", "==", type) + .selectAll() + .execute(); return result as PendingSyncItem[]; } async remove(ids: string[]) { if (ids.length === 0) return; - await this.db.then((db) => - db.deleteFrom("pendingsyncitems").where("id", "in", ids).execute() - ); + await this.db() + .deleteFrom("pendingsyncitems") + .where("id", "in", ids) + .execute(); } } diff --git a/packages/core/src/interfaces.ts b/packages/core/src/interfaces.ts index 33efd460a..616040e0c 100644 --- a/packages/core/src/interfaces.ts +++ b/packages/core/src/interfaces.ts @@ -25,7 +25,6 @@ import { } from "@notesnook/crypto"; import { KVStorage } from "./database/kv.js"; import { ConfigStorage } from "./database/config.js"; -import { PendingSyncItems } from "./database/pending-sync-items.js"; export type Output = TOutputFormat extends "uint8array" ? Uint8Array : string; @@ -146,5 +145,4 @@ export interface IFileStorage { export type StorageAccessor = () => IStorage; export type KVStorageAccessor = () => KVStorage; export type ConfigStorageAccessor = () => ConfigStorage; -export type PendingSyncItemsAccessor = () => PendingSyncItems; export type CompressorAccessor = () => Promise;