Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
smaller refactorings (mostly entitization)
azivnerfad0ec757b6f
diff --git a/src/entities/image.js b/src/entities/image.jsnew file mode 100644index 0000000000..5116ce2b87--- /dev/null+++ b/src/entities/image.js@@ -0,0 +1,19 @@+"use strict";++const Entity = require('./entity');+const utils = require('../services/utils');++class Image extends Entity {+ static get tableName() { return "images"; }+ static get primaryKeyName() { return "imageId"; }++ beforeSaving() {+ if (!this.dateCreated) {+ this.dateCreated = utils.nowDate();+ }++ this.dateModified = utils.nowDate();+ }+}++module.exports = Image;\ No newline at end of filediff --git a/src/entities/note_image.js b/src/entities/note_image.jsindex 434a27070a..2a4edefa5e 100644--- a/src/entities/note_image.js+++ b/src/entities/note_image.js@@ -2,6 +2,7 @@const Entity = require('./entity');const repository = require('../services/repository');+const utils = require('../services/utils');class NoteImage extends Entity {static get tableName() { return "note_images"; }@@ -10,6 +11,18 @@ class NoteImage extends Entity {async getNote() {return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]);}++ async getImage() {+ return await repository.getEntity("SELECT * FROM images WHERE imageId = ?", [this.imageId]);+ }++ beforeSaving() {+ if (!this.dateCreated) {+ this.dateCreated = utils.nowDate();+ }++ this.dateModified = utils.nowDate();+ }}module.exports = NoteImage;\ No newline at end of filediff --git a/src/routes/api/cleanup.js b/src/routes/api/cleanup.jsindex 16da38bd0a..02968799f0 100644--- a/src/routes/api/cleanup.js+++ b/src/routes/api/cleanup.js@@ -4,6 +4,7 @@ const sql = require('../../services/sql');const utils = require('../../services/utils');const sync_table = require('../../services/sync_table');const log = require('../../services/log');+const repository = require('../../services/repository');async function cleanupSoftDeletedItems() {const noteIdsToDelete = await sql.getColumn("SELECT noteId FROM notes WHERE isDeleted = 1");@@ -33,6 +34,9 @@ async function cleanupSoftDeletedItems() {await sync_table.cleanupSyncRowsForMissingEntities("branches", "branchId");await sync_table.cleanupSyncRowsForMissingEntities("note_revisions", "noteRevisionId");await sync_table.cleanupSyncRowsForMissingEntities("recent_notes", "branchId");+ await sync_table.cleanupSyncRowsForMissingEntities("images", "imageId");+ await sync_table.cleanupSyncRowsForMissingEntities("note_images", "noteImageId");+ await sync_table.cleanupSyncRowsForMissingEntities("labels", "labelId");log.info("Following notes has been completely cleaned from database: " + noteIdsSql);}@@ -51,10 +55,10 @@ async function cleanupUnusedImages() {for (const imageId of unusedImageIds) {log.info(`Deleting unused image: ${imageId}`);- await sql.execute("UPDATE images SET isDeleted = 1, data = null, dateModified = ? WHERE imageId = ?",- [now, imageId]);-- await sync_table.addImageSync(imageId);+ const image = await repository.getImage(imageId);+ image.isDeleted = true;+ image.data = null;+ await image.save();}}diff --git a/src/routes/api/cloning.js b/src/routes/api/cloning.jsindex fdf39cfcaa..b32f35d990 100644--- a/src/routes/api/cloning.js+++ b/src/routes/api/cloning.js@@ -4,6 +4,7 @@ const sql = require('../../services/sql');const utils = require('../../services/utils');const sync_table = require('../../services/sync_table');const tree = require('../../services/tree');+const Branch = require('../../entities/branch');async function cloneNoteToParent(req) {const parentNoteId = req.params.parentNoteId;@@ -19,7 +20,7 @@ async function cloneNoteToParent(req) {const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;- const branch = {+ const branch = new Branch({branchId: utils.newBranchId(),noteId: childNoteId,parentNoteId: parentNoteId,@@ -28,11 +29,9 @@ async function cloneNoteToParent(req) {isExpanded: 0,dateModified: utils.nowDate(),isDeleted: 0- };+ });- await sql.replace("branches", branch);-- await sync_table.addBranchSync(branch.branchId);+ await branch.save();await sql.execute("UPDATE branches SET isExpanded = 1 WHERE noteId = ?", [parentNoteId]);@@ -58,7 +57,7 @@ async function cloneNoteAfter(req) {await sync_table.addNoteReorderingSync(afterNote.parentNoteId);- const branch = {+ const branch = new Branch({branchId: utils.newBranchId(),noteId: noteId,parentNoteId: afterNote.parentNoteId,@@ -66,11 +65,9 @@ async function cloneNoteAfter(req) {isExpanded: 0,dateModified: utils.nowDate(),isDeleted: 0- };-- await sql.replace("branches", branch);+ });- await sync_table.addBranchSync(branch.branchId);+ await branch.save();return { success: true };}diff --git a/src/routes/api/event_log.js b/src/routes/api/event_log.jsindex 9644d4a7a0..42b14752d2 100644--- a/src/routes/api/event_log.js+++ b/src/routes/api/event_log.js@@ -12,9 +12,7 @@ async function deleteOld() {const cutoffId = await sql.getValue("SELECT id FROM event_log ORDER BY id DESC LIMIT 1000, 1");if (cutoffId) {- await sql.doInTransaction(async () => {- await sql.execute("DELETE FROM event_log WHERE id < ?", [cutoffId]);- });+ await sql.execute("DELETE FROM event_log WHERE id < ?", [cutoffId]);}}diff --git a/src/routes/api/export.js b/src/routes/api/export.jsindex 2186b877fc..a988e09048 100644--- a/src/routes/api/export.js+++ b/src/routes/api/export.js@@ -24,8 +24,8 @@ async function exportNote(req, res) {}async function exportNoteInner(branchId, directory, pack) {- const branch = await sql.getRow("SELECT * FROM branches WHERE branchId = ?", [branchId]);- const note = await repository.getEntity("SELECT notes.* FROM notes WHERE noteId = ?", [branch.noteId]);+ const branch = await repository.getBranch(branchId);+ const note = await branch.getNote();if (note.isProtected) {return;@@ -46,12 +46,8 @@ async function exportNoteInner(branchId, directory, pack) {pack.entry({ name: childFileName + ".dat", size: content.length }, content);- const children = await sql.getRows("SELECT * FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [note.noteId]);-- if (children.length > 0) {- for (const child of children) {- await exportNoteInner(child.branchId, childFileName + "/", pack);- }+ for (const child of await note.getChildBranches()) {+ await exportNoteInner(child.branchId, childFileName + "/", pack);}return childFileName;diff --git a/src/routes/api/note_revisions.js b/src/routes/api/note_revisions.jsindex d72f19282e..e35f509e7d 100644--- a/src/routes/api/note_revisions.js+++ b/src/routes/api/note_revisions.js@@ -1,14 +1,10 @@"use strict";-const sql = require('../../services/sql');-const protected_session = require('../../services/protected_session');+const repository = require('../../services/repository');async function getNoteRevisions(req) {const noteId = req.params.noteId;- const revisions = await sql.getRows("SELECT * FROM note_revisions WHERE noteId = ? order by dateModifiedTo desc", [noteId]);- protected_session.decryptNoteRevisions(revisions);-- return revisions;+ return await repository.getEntities("SELECT * FROM note_revisions WHERE noteId = ? order by dateModifiedTo desc", [noteId]);}module.exports = {diff --git a/src/routes/api/tree.js b/src/routes/api/tree.jsindex 1041b6ad08..656d889bf3 100644--- a/src/routes/api/tree.js+++ b/src/routes/api/tree.js@@ -5,9 +5,9 @@ const options = require('../../services/options');const utils = require('../../services/utils');const config = require('../../services/config');const protected_session = require('../../services/protected_session');-const sync_table = require('../../services/sync_table');+const repository = require('../../services/repository');-async function getTree(req) {+async function getTree() {const branches = await sql.getRows(`SELECTbranchId,@@ -64,13 +64,9 @@ async function setPrefix(req) {const branchId = req.params.branchId;const prefix = utils.isEmptyOrWhitespace(req.body.prefix) ? null : req.body.prefix;- await sql.doInTransaction(async () => {- await sql.execute("UPDATE branches SET prefix = ?, dateModified = ? WHERE branchId = ?", [prefix, utils.nowDate(), branchId]);-- await sync_table.addBranchSync(branchId);- });-- return {};+ const branch = await repository.getBranch(branchId);+ branch.prefix = prefix;+ await branch.save();}module.exports = {diff --git a/src/routes/routes.js b/src/routes/routes.jsindex 00793b8ea2..441089f76b 100644--- a/src/routes/routes.js+++ b/src/routes/routes.js@@ -52,7 +52,7 @@ function apiResultHandler(res, result) {}}else if (result === undefined) {- res.status(200).send();+ res.status(204).send();}else {res.status(200).send(result);diff --git a/src/services/notes.js b/src/services/notes.jsindex 831e0a208b..b78a8d5236 100644--- a/src/services/notes.js+++ b/src/services/notes.js@@ -58,7 +58,7 @@ async function createNewNote(parentNoteId, noteOpts) {mime: noteOpts.mime ? noteOpts.mime : 'text/html'});- await repository.updateEntity(note);+ await note.save();const branch = new Branch({branchId: utils.newBranchId(),@@ -69,12 +69,13 @@ async function createNewNote(parentNoteId, noteOpts) {isDeleted: 0});- await repository.updateEntity(branch);+ await branch.save();return {noteId: note.noteId,+ note,branchId: branch.branchId,- note+ branch};}diff --git a/src/services/protected_session.js b/src/services/protected_session.jsindex 069ad51df4..c0fff7f0b0 100644--- a/src/services/protected_session.js+++ b/src/services/protected_session.js@@ -27,10 +27,6 @@ function getDataKey() {return dataKeyMap[protectedSessionId];}-function getDataKeyForProtectedSessionId(protectedSessionId) {- return dataKeyMap[protectedSessionId];-}-function isProtectedSessionAvailable(req) {const protectedSessionId = getProtectedSessionId(req);@@ -84,12 +80,6 @@ function decryptNoteRevision(hist) {}}-function decryptNoteRevisions(noteRevisions) {- for (const revision of noteRevisions) {- decryptNoteRevision(revision);- }-}-function encryptNote(note) {const dataKey = getDataKey();@@ -107,12 +97,10 @@ function encryptNoteRevision(revision) {module.exports = {setDataKey,getDataKey,- getDataKeyForProtectedSessionId,isProtectedSessionAvailable,decryptNote,decryptNotes,decryptNoteRevision,- decryptNoteRevisions,encryptNote,encryptNoteRevision,setProtectedSessionIddiff --git a/src/services/repository.js b/src/services/repository.jsindex e337df724a..810ba58411 100644--- a/src/services/repository.js+++ b/src/services/repository.js@@ -33,6 +33,10 @@ async function getBranch(branchId) {return await getEntity("SELECT * FROM branches WHERE branchId = ?", [branchId]);}+async function getImage(imageId) {+ return await getEntity("SELECT * FROM images WHERE imageId = ?", [imageId]);+}+async function updateEntity(entity) {if (entity.beforeSaving) {entity.beforeSaving();@@ -54,6 +58,7 @@ module.exports = {getEntity,getNote,getBranch,+ getImage,updateEntity,setEntityConstructor};\ No newline at end of file