core: remove lazy access of pendingsyncitems table

Signed-off-by: 01zulfi <85733202+01zulfi@users.noreply.github.com>
This commit is contained in:
01zulfi
2026-07-09 14:36:39 +05:00
parent a54e3dfebd
commit 15ca19ddd0
4 changed files with 22 additions and 30 deletions

View File

@@ -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<RawDatabaseSchema>
);
private _config: ConfigStorage = new ConfigStorage(
this.databaseReady.promise
);

View File

@@ -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),

View File

@@ -17,37 +17,31 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { LazyDatabaseAccessor, RawDatabaseSchema } from "./index.js";
import { DatabaseAccessor, RawDatabaseSchema } from "./index.js";
import { PendingSyncItem } from "../types.js";
export class PendingSyncItems {
private readonly db: LazyDatabaseAccessor<RawDatabaseSchema>;
constructor(db: LazyDatabaseAccessor) {
this.db = db as unknown as LazyDatabaseAccessor<RawDatabaseSchema>;
}
constructor(private readonly db: DatabaseAccessor<RawDatabaseSchema>) {}
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();
}
}

View File

@@ -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 DataFormat> =
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<ICompressor>;