mirror of
https://github.com/streetwriters/notesnook.git
synced 2026-09-01 19:49:54 +02:00
core: remove lazy access of pendingsyncitems table
Signed-off-by: 01zulfi <85733202+01zulfi@users.noreply.github.com>
This commit is contained in:
@@ -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
|
||||
);
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>;
|
||||
|
||||
Reference in New Issue
Block a user