diff --git a/packages/core/src/api/index.ts b/packages/core/src/api/index.ts index 9832214b0..904515490 100644 --- a/packages/core/src/api/index.ts +++ b/packages/core/src/api/index.ts @@ -66,7 +66,7 @@ import { SQLiteOptions, createDatabase } from "../database"; -import { Kysely, Transaction } from "kysely"; +import { Kysely, Transaction, sql } from "kysely"; import { CachedCollection } from "../database/cached-collection"; type EventSourceConstructor = new ( @@ -223,6 +223,22 @@ class Database { await this.session.set(); } + async reset() { + await this.storage().clear(); + + for (const statement of [ + "PRAGMA writable_schema = 1", + "DELETE FROM sqlite_master", + "PRAGMA writable_schema = 0", + "VACUUM", + "PRAGMA integrity_check" + ]) { + await sql.raw(statement).execute(this.sql()); + } + await this.sql().destroy(); + this._sql = await createDatabase("notesnook", this.options.sqliteOptions); + } + async init() { if (!this.options) throw new Error( @@ -244,7 +260,7 @@ class Database { this.disconnectSSE(); }); - this._sql = await createDatabase(this.options.sqliteOptions); + this._sql = await createDatabase("notesnook", this.options.sqliteOptions); await this._validate(); diff --git a/packages/core/src/api/sync/index.ts b/packages/core/src/api/sync/index.ts index 1ca071be8..7357a94cf 100644 --- a/packages/core/src/api/sync/index.ts +++ b/packages/core/src/api/sync/index.ts @@ -151,10 +151,10 @@ class Sync { this.logger.info("Starting sync", { full, force }); - this.connection.onclose((error) => { + this.connection.onclose((error = new Error("Connection closed.")) => { this.db.eventManager.publish(EVENTS.syncAborted); console.error(error); - this.logger.error(error || new Error("Connection closed.")); + this.logger.error(error); throw new Error("Connection closed."); }); diff --git a/packages/core/src/api/user-manager.ts b/packages/core/src/api/user-manager.ts index aa584b1e4..d12c9b492 100644 --- a/packages/core/src/api/user-manager.ts +++ b/packages/core/src/api/user-manager.ts @@ -265,7 +265,7 @@ class UserManager { } catch (e) { console.error(e); } finally { - await this.db.storage().clear(); + await this.db.reset(); EV.publish(EVENTS.userLoggedOut, reason); EV.publish(EVENTS.appRefreshRequested); } diff --git a/packages/core/src/database/index.ts b/packages/core/src/database/index.ts index 3b212a2a3..951b3c646 100644 --- a/packages/core/src/database/index.ts +++ b/packages/core/src/database/index.ts @@ -197,7 +197,7 @@ const DataMappers: Partial void>> = { }; export type SQLiteOptions = { - dialect: Dialect; + dialect: (name: string) => Dialect; journalMode?: "WAL" | "MEMORY" | "OFF" | "PERSIST" | "TRUNCATE" | "DELETE"; synchronous?: "normal" | "extra" | "full" | "off"; lockingMode?: "normal" | "exclusive"; @@ -205,9 +205,9 @@ export type SQLiteOptions = { cacheSize?: number; pageSize?: number; }; -export async function createDatabase(options: SQLiteOptions) { +export async function createDatabase(name: string, options: SQLiteOptions) { const db = new Kysely({ - dialect: options.dialect, + dialect: options.dialect(name), plugins: [new SqliteBooleanPlugin()] });