A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
core: clear database on logout
Abdullah Attae127b8f4ab9e
diff --git a/packages/core/src/api/index.ts b/packages/core/src/api/index.tsindex 1306980d5..4a7733f65 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.tsindex fb7c90596..3391390df 100644--- a/packages/core/src/api/sync/index.ts+++ b/packages/core/src/api/sync/index.ts@@ -155,10 +155,10 @@ class Sync {this.logger.info("Starting sync", options);- 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.tsindex a42709889..04d9da4ea 100644--- a/packages/core/src/api/user-manager.ts+++ b/packages/core/src/api/user-manager.ts@@ -269,7 +269,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.tsindex 3b212a2a3..951b3c646 100644--- a/packages/core/src/database/index.ts+++ b/packages/core/src/database/index.ts@@ -197,7 +197,7 @@ const DataMappers: Partial<Record<ItemType, (row: any) => 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<DatabaseSchema>({- dialect: options.dialect,+ dialect: options.dialect(name),plugins: [new SqliteBooleanPlugin()]});