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: init notes with database
Abdullah Attaa967482c4f32
diff --git a/apps/web/src/common/db.ts b/apps/web/src/common/db.tsindex 00b21a003..930beb842 100644--- a/apps/web/src/common/db.ts+++ b/apps/web/src/common/db.ts@@ -64,9 +64,6 @@ async function initializeDatabase(persistence: DatabasePersistence) {// });// }- // db.eventManager.subscribe(EVENTS.databaseMigrating, async ({ from, to }) => {-- // });await db.init();diff --git a/apps/web/src/components/diff-viewer/index.jsx b/apps/web/src/components/diff-viewer/index.jsxindex f696fcdde..41227f1f4 100644--- a/apps/web/src/components/diff-viewer/index.jsx+++ b/apps/web/src/components/diff-viewer/index.jsx@@ -92,7 +92,6 @@ function DiffViewer(props) {useEffect(() => {(async function () {- await db.notes.init();let note = db.notes.note(noteId);if (!note) {hashNavigate(`/notes/create`, { replace: true });diff --git a/apps/web/src/dialogs/settings/backup-export-settings.ts b/apps/web/src/dialogs/settings/backup-export-settings.tsindex 100a456a2..e95ff23cf 100644--- a/apps/web/src/dialogs/settings/backup-export-settings.ts+++ b/apps/web/src/dialogs/settings/backup-export-settings.ts@@ -165,7 +165,6 @@ export const BackupExportSettings: SettingsGroup[] = [onSelectionChanged: async (value) => {if (!db.notes || value === "-") return;- await db.notes.init();await exportNotes(value as "txt" | "md" | "html",db.notes.all.map((n) => n.id)diff --git a/apps/web/src/stores/editor-store.js b/apps/web/src/stores/editor-store.jsindex ef3481900..b78521994 100644--- a/apps/web/src/stores/editor-store.js+++ b/apps/web/src/stores/editor-store.js@@ -134,8 +134,6 @@ class EditorStore extends BaseStore {};openSession = async (noteId, force) => {- await db.notes.init();-const session = this.get().session;if (session.id) await db.fs.cancel(session.id);diff --git a/apps/web/src/stores/note-store.js b/apps/web/src/stores/note-store.jsindex 65c899633..ad6b3a29b 100644--- a/apps/web/src/stores/note-store.js+++ b/apps/web/src/stores/note-store.js@@ -89,10 +89,8 @@ class NoteStore extends BaseStore {};setContext = (context) => {- db.notes.init().then(() => {- this.get().context = { ...context, notes: notesFromContext(context) };- this._forceUpdate();- });+ this.get().context = { ...context, notes: notesFromContext(context) };+ this._forceUpdate();};delete = async (...ids) => {diff --git a/apps/web/src/utils/web-extension-server.ts b/apps/web/src/utils/web-extension-server.tsindex 187c57904..2c6fbc68c 100644--- a/apps/web/src/utils/web-extension-server.ts+++ b/apps/web/src/utils/web-extension-server.ts@@ -41,8 +41,6 @@ export class WebExtensionServer implements Server {}async getNotes(): Promise<ItemReference[] | undefined> {- await db.notes?.init();-return db.notes?.all.filter((n) => !n.locked).map((note) => ({ id: note.id, title: note.title }));diff --git a/apps/web/src/views/home.jsx b/apps/web/src/views/home.jsxindex c07e0fee3..01eab7fab 100644--- a/apps/web/src/views/home.jsx+++ b/apps/web/src/views/home.jsx@@ -31,18 +31,14 @@ function Home() {const isCompact = useStore((store) => store.viewMode === "compact");const refresh = useStore((store) => store.refresh);const clearContext = useStore((store) => store.clearContext);- const [isLoading, setIsLoading] = useState(true);- useNavigate("home", () => {- clearContext();- });++ useNavigate("home", clearContext);useEffect(() => {(async function () {const initialized = db.notes.initialized;if (!initialized || !notes.length) {- await db.notes.init();store.refresh();- setIsLoading(false);}// const note = db.notes.note("62bc3f28a1a1a10000707077").data;// const data = await db.content.raw(note.contentId);@@ -64,7 +60,6 @@ function Home() {groupType="home"compact={isCompact}refresh={refresh}- isLoading={isLoading}items={notes}placeholder={<Placeholder context="notes" />}button={{diff --git a/apps/web/src/views/search.jsx b/apps/web/src/views/search.jsxindex a2f48627d..7aac093eb 100644--- a/apps/web/src/views/search.jsx+++ b/apps/web/src/views/search.jsx@@ -37,7 +37,6 @@ async function typeToItems(type, context) {return ["notes", notes];}case "notes": {- await db.notes.init();if (!context) return ["notes", db.notes.all];const notes = context.notes;let topicNotes = [];diff --git a/apps/web/src/views/trash.jsx b/apps/web/src/views/trash.jsxindex ffd40243d..faef92349 100644--- a/apps/web/src/views/trash.jsx+++ b/apps/web/src/views/trash.jsx@@ -23,14 +23,10 @@ import { confirm } from "../common/dialog-controller";import { useStore, store } from "../stores/trash-store";import { showToast } from "../utils/toast";import useNavigate from "../hooks/use-navigate";-import { db } from "../common/db";import Placeholder from "../components/placeholders";function Trash() {- useNavigate("trash", async () => {- await db.notes.init();- store.refresh();- });+ useNavigate("trash", store.refresh);const items = useStore((store) => store.trash);const refresh = useStore((store) => store.refresh);const clearTrash = useStore((store) => store.clear);diff --git a/packages/core/api/index.js b/packages/core/api/index.jsindex ce4f6d0bb..0bd93be71 100644--- a/packages/core/api/index.js+++ b/packages/core/api/index.js@@ -80,6 +80,23 @@ class Database {this.storage = storage ? new Storage(storage) : null;this.fs = fs && storage ? new FileStorage(fs, storage) : null;NNEventSource = eventsource;++ this.session = new Session(this.storage);+ this.user = new UserManager(this.storage, this);+ this.mfa = new MFAManager(this.storage, this);+ this.syncer = new Sync(this);+ this.vault = new Vault(this);+ this.lookup = new Lookup(this);+ this.backup = new Backup(this);+ this.settings = new Settings(this);+ this.migrations = new Migrations(this);+ this.outbox = new Outbox(this);+ this.monographs = new Monographs(this);+ this.offers = new Offers();+ this.debug = new Debug();+ this.pricing = new Pricing();+ this.subscriptions = new Subscriptions(this.user.tokenManager);+ this.trash = new Trash(this);}async _validate() {@@ -105,40 +122,12 @@ class Database {await this.fs.clear();this.disconnectSSE();});- EV.subscribe(EVENTS.databaseCollectionInitiated, async (collectionName) => {- switch (collectionName) {- case "notes": {- await this.monographs.init();- await this.trash.init();- await this.relations.cleanup();- break;- }- }- });- this.session = new Session(this.storage);await this._validate();- this.user = new UserManager(this.storage, this);- this.mfa = new MFAManager(this.storage, this);- this.syncer = new Sync(this);- this.vault = new Vault(this);- this.lookup = new Lookup(this);- this.backup = new Backup(this);- this.settings = new Settings(this);- this.migrations = new Migrations(this);- this.outbox = new Outbox(this);- this.monographs = new Monographs(this);- this.offers = new Offers();- this.debug = new Debug();- this.pricing = new Pricing();- this.subscriptions = new Subscriptions(this.user.tokenManager);-await this.initCollections();- await this.settings.init();await this.outbox.init();- await this.user.init();await this.migrations.init();this.isInitialized = true;if (this.migrations.required()) {@@ -147,9 +136,8 @@ class Database {}async initCollections() {+ await this.settings.init();// collections- /** @type {Notes} */- this.notes = await Notes.new(this, "notes", true, true);/** @type {Notebooks} */this.notebooks = await Notebooks.new(this, "notebooks");/** @type {Tags} */@@ -168,8 +156,13 @@ class Database {this.reminders = await Reminders.new(this, "reminders");/**@type {Relations} */this.relations = await Relations.new(this, "relations");+ /** @type {Notes} */+ this.notes = await Notes.new(this, "notes");- this.trash = new Trash(this);+ await this.trash.init();+ await this.relations.cleanup();++ this.monographs.init().catch(console.error);}disconnectSSE() {diff --git a/packages/core/collections/notes.js b/packages/core/collections/notes.jsindex 9dd26e05d..596a6ada0 100644--- a/packages/core/collections/notes.js+++ b/packages/core/collections/notes.js@@ -32,7 +32,7 @@ import { NEWLINE_STRIP_REGEX, formatTitle } from "../utils/title-format";export default class Notes extends Collection {constructor(db, name, cached) {super(db, name, cached);- this.topicReferences = new NoteIdCache(this._db);+ this.topicReferences = new NoteIdCache(this);}async init() {@@ -440,16 +440,16 @@ function getNoteHeadline(note, content) {class NoteIdCache {/***- * @param {import("../api/index").default} db+ * @param {Notes} notes*/- constructor(db) {- this._db = db;+ constructor(notes) {+ this.notes = notes;this.cache = new Map();}rebuild() {this.cache = new Map();- const notes = this._db.notes.all;+ const notes = this.notes.all;for (const note of notes) {const { notebooks } = note;diff --git a/packages/core/collections/trash.js b/packages/core/collections/trash.jsindex fb4324aa5..b4e4144af 100644--- a/packages/core/collections/trash.js+++ b/packages/core/collections/trash.js@@ -26,10 +26,7 @@ export default class Trash {*/constructor(db) {this._db = db;- this.collections = {- notes: db.notes,- notebooks: db.notebooks- };+ this.collections = ["notes", "notebooks"];}async init() {@@ -48,16 +45,16 @@ export default class Trash {get all() {let trashItems = [];- for (let key in this.collections) {- const collection = this.collections[key];+ for (const key of this.collections) {+ const collection = this._db[key];trashItems.push(...collection.deleted);}return trashItems;}_getItem(id) {- for (let key in this.collections) {- const collection = this.collections[key]._collection;+ for (const key of this.collections) {+ const collection = this._db[key]._collection;if (collection.has(id)) return [collection.getItem(id), collection];}return [];@@ -67,7 +64,7 @@ export default class Trash {const collection = collectionNameFromItem(item);if (!item || !item.type || !collection) return;- await this.collections[collection]._collection.updateItem({+ await this._db[collection]._collection.updateItem({...item,id: item.itemId || item.id,type: "trash",@@ -104,9 +101,9 @@ export default class Trash {delete item.itemType;if (item.type === "note") {- await this.collections.notes.add(item);+ await this._db.notes.add(item);} else if (item.type === "notebook") {- await this.collections.notebooks.add(item);+ await this._db.notebooks.add(item);}}this._db.notes.topicReferences.rebuild();diff --git a/packages/core/database/cached-collection.js b/packages/core/database/cached-collection.jsindex 2922ab046..1f67e0cb5 100644--- a/packages/core/database/cached-collection.js+++ b/packages/core/database/cached-collection.js@@ -48,7 +48,6 @@ export default class CachedCollection extends IndexedCollection {// }this.map = new MapStub.Map(data, this.type);- this.invalidateCache();}async clear() {