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: add localStorage like ConfigStorage based on SQLite
Abdullah Attaca0a4cb02c47
diff --git a/packages/core/src/api/index.ts b/packages/core/src/api/index.tsindex d4a2f34e8..964c2cbe2 100644--- a/packages/core/src/api/index.ts+++ b/packages/core/src/api/index.ts@@ -51,6 +51,7 @@ import { Relations } from "../collections/relations";import Subscriptions from "./subscriptions";import {CompressorAccessor,+ ConfigStorageAccessor,ICompressor,IFileStorage,IStorage,@@ -77,6 +78,7 @@ import { QueueValue } from "../utils/queue-value";import { Sanitizer } from "../database/sanitizer";import { createTriggers, dropTriggers } from "../database/triggers";import { NNMigrationProvider } from "../database/migrations";+import { ConfigStorage } from "../database/config";type EventSourceConstructor = new (uri: string,@@ -147,6 +149,9 @@ class Database {private _kv?: KVStorage;kv: KVStorageAccessor = () => this._kv || new KVStorage(this.sql);+ private _config?: ConfigStorage;+ config: ConfigStorageAccessor = () =>+ this._config || new ConfigStorage(this.sql);private _transaction?: QueueValue<Transaction<DatabaseSchema>>;transaction = async (diff --git a/packages/core/src/database/config.ts b/packages/core/src/database/config.tsnew file mode 100644index 000000000..1c51821f8--- /dev/null+++ b/packages/core/src/database/config.ts@@ -0,0 +1,57 @@+/*+This file is part of the Notesnook project (https://notesnook.com/)++Copyright (C) 2023 Streetwriters (Private) Limited++This program is free software: you can redistribute it and/or modify+it under the terms of the GNU General Public License as published by+the Free Software Foundation, either version 3 of the License, or+(at your option) any later version.++This program is distributed in the hope that it will be useful,+but WITHOUT ANY WARRANTY; without even the implied warranty of+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+GNU General Public License for more details.++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 { DatabaseAccessor, RawDatabaseSchema } from ".";++export class ConfigStorage {+ private readonly db: DatabaseAccessor<RawDatabaseSchema>;+ constructor(db: DatabaseAccessor) {+ this.db = db as unknown as DatabaseAccessor<RawDatabaseSchema>;+ }++ async getItem(name: string): Promise<unknown | undefined> {+ const result = await this.db()+ .selectFrom("config")+ .where("name", "==", name)+ .select("value")+ .limit(1)+ .executeTakeFirst();+ if (!result?.value) return;+ return JSON.parse(result.value);+ }++ async setItem(name: string, value: unknown) {+ await this.db()+ .replaceInto("config")+ .values({+ name,+ value: JSON.stringify(value),+ dateModified: Date.now()+ })+ .execute();+ }++ async removeItem(name: string) {+ await this.db().deleteFrom("config").where("name", "==", name).execute();+ }++ async clear() {+ await this.db().deleteFrom("config").execute();+ }+}diff --git a/packages/core/src/database/index.ts b/packages/core/src/database/index.tsindex 460710776..36993bd17 100644--- a/packages/core/src/database/index.ts+++ b/packages/core/src/database/index.ts@@ -97,6 +97,11 @@ export type RawDatabaseSchema = DatabaseSchema & {value?: string | null;dateModified?: number | null;};+ config: {+ name: string;+ value?: string | null;+ dateModified?: number | null;+ };notes_fts: SQLiteItemWithRowID<{notes_fts: string;diff --git a/packages/core/src/database/migrations.ts b/packages/core/src/database/migrations.tsindex 970bc3662..6a11f6a79 100644--- a/packages/core/src/database/migrations.ts+++ b/packages/core/src/database/migrations.ts@@ -309,6 +309,17 @@ export class NNMigrationProvider implements MigrationProvider {.set({ conflicted: false }).execute();}+ },+ "4": {+ async up(db) {+ await db.schema+ .createTable("config")+ .modifyEnd(sql`without rowid`)+ .addColumn("name", "text", (c) => c.primaryKey().unique().notNull())+ .addColumn("value", "text")+ .addColumn("dateModified", "integer")+ .execute();+ }}};}diff --git a/packages/core/src/interfaces.ts b/packages/core/src/interfaces.tsindex d5f55f2bf..1f2440981 100644--- a/packages/core/src/interfaces.ts+++ b/packages/core/src/interfaces.ts@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.import { Cipher, DataFormat, SerializedKey } from "@notesnook/crypto";import { KVStorage } from "./database/kv";+import { ConfigStorage } from "./database/config";export type Output<TOutputFormat extends DataFormat> =TOutputFormat extends "uint8array" ? Uint8Array : string;@@ -113,4 +114,5 @@ export interface IFileStorage {export type StorageAccessor = () => IStorage;export type KVStorageAccessor = () => KVStorage;+export type ConfigStorageAccessor = () => ConfigStorage;export type CompressorAccessor = () => ICompressor;