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
refactoring of note deletion
azivner12439d876196
diff --git a/src/entities/branch.js b/src/entities/branch.jsindex 61be42e534..3331b045ac 100644--- a/src/entities/branch.js+++ b/src/entities/branch.js@@ -2,11 +2,16 @@const Entity = require('./entity');const utils = require('../services/utils');+const repository = require('../services/repository');class Branch extends Entity {static get tableName() { return "branches"; }static get primaryKeyName() { return "branchId"; }+ async getNote() {+ return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]);+ }+beforeSaving() {this.dateModified = utils.nowDate()}diff --git a/src/entities/entity.js b/src/entities/entity.jsindex 1842c15a8b..7906488153 100644--- a/src/entities/entity.js+++ b/src/entities/entity.js@@ -1,6 +1,7 @@"use strict";const utils = require('../services/utils');+const repository = require('../services/repository');class Entity {constructor(row) {@@ -10,6 +11,10 @@ class Entity {this[key] = row[key];}}++ async save() {+ await repository.updateEntity(this);+ }}module.exports = Entity;\ No newline at end of filediff --git a/src/entities/note.js b/src/entities/note.jsindex 1d4896f3cb..ba09368586 100644--- a/src/entities/note.js+++ b/src/entities/note.js@@ -84,11 +84,11 @@ class Note extends Entity {return await repository.getEntities("SELECT * FROM note_images WHERE noteId = ? AND isDeleted = 0", [this.noteId]);}- async getTrees() {+ async getBranches() {return await repository.getEntities("SELECT * FROM branches WHERE isDeleted = 0 AND noteId = ?", [this.noteId]);}- async getChild(name) {+ async getChildNote(name) {return await repository.getEntity(`SELECT notes.*FROM branches@@ -99,7 +99,7 @@ class Note extends Entity {AND notes.title = ?`, [this.noteId, name]);}- async getChildren() {+ async getChildNotes() {return await repository.getEntities(`SELECT notes.*FROM branches@@ -110,7 +110,16 @@ class Note extends Entity {ORDER BY branches.notePosition`, [this.noteId]);}- async getParents() {+ async getChildBranches() {+ return await repository.getEntities(`+ SELECT branches.*+ FROM branches+ WHERE branches.isDeleted = 0+ AND branches.parentNoteId = ?+ ORDER BY branches.notePosition`, [this.noteId]);+ }++ async getParentNotes() {return await repository.getEntities(`SELECT parent_notes.*FROM@@ -121,16 +130,6 @@ class Note extends Entity {AND parent_notes.isDeleted = 0`, [this.noteId]);}- async getBranch() {- return await repository.getEntities(`- SELECT branches.*- FROM branches- JOIN notes USING(noteId)- WHERE notes.isDeleted = 0- AND branches.isDeleted = 0- AND branches.noteId = ?`, [this.noteId]);- }-beforeSaving() {if (this.isJson()) {this.content = JSON.stringify(this.jsonContent, null, '\t');diff --git a/src/public/javascripts/entities/note_short.js b/src/public/javascripts/entities/note_short.jsindex 9ffdb01c5d..8d10a7c9b0 100644--- a/src/public/javascripts/entities/note_short.js+++ b/src/public/javascripts/entities/note_short.js@@ -17,7 +17,7 @@ class NoteShort {const branches = [];for (const parent of this.treeCache.parents[this.noteId]) {- branches.push(await this.treeCache.getBranchByChildParent(this.noteId, p.noteId));+ branches.push(await this.treeCache.getBranchByChildParent(this.noteId, parent.noteId));}return branches;diff --git a/src/public/javascripts/services/tree_keybindings.js b/src/public/javascripts/services/tree_keybindings.jsindex 2e298355fa..71b5eb8b22 100644--- a/src/public/javascripts/services/tree_keybindings.js+++ b/src/public/javascripts/services/tree_keybindings.js@@ -7,7 +7,7 @@ import editTreePrefixDialog from "../dialogs/edit_tree_prefix.js";const keyBindings = {"del": node => {- treeChangesService.deleteNodes(getSelectedNodes(true));+ treeChangesService.deleteNodes(treeService.getSelectedNodes(true));},"ctrl+up": node => {const beforeNode = node.getPrevSibling();diff --git a/src/routes/api/tree_changes.js b/src/routes/api/tree_changes.jsindex f3bd261980..103612f908 100644--- a/src/routes/api/tree_changes.js+++ b/src/routes/api/tree_changes.js@@ -5,6 +5,7 @@ const utils = require('../../services/utils');const sync_table = require('../../services/sync_table');const tree = require('../../services/tree');const notes = require('../../services/notes');+const repository = require('../../services/repository');/*** Code in this file deals with moving and cloning note tree rows. Relationship between note and parent note is unique@@ -101,7 +102,9 @@ async function setExpanded(req) {}async function deleteBranch(req) {- await notes.deleteNote(req.params.branchId);+ const branch = await repository.getBranch(req.params.branchId);++ await notes.deleteNote(branch);}module.exports = {diff --git a/src/services/notes.js b/src/services/notes.jsindex eae5652868..648d639099 100644--- a/src/services/notes.js+++ b/src/services/notes.js@@ -115,7 +115,7 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {})async function protectNoteRecursively(note, protect) {await protectNote(note, protect);- for (const child of await note.getChildren()) {+ for (const child of await note.getChildNotes()) {await protectNoteRecursively(child, protect);}}@@ -229,30 +229,23 @@ async function updateNote(noteId, noteUpdates) {await protectNoteRevisions(note);}-async function deleteNote(branchId) {- const branch = await sql.getRowOrNull("SELECT * FROM branches WHERE branchId = ?", [branchId]);-+async function deleteNote(branch) {if (!branch || branch.isDeleted === 1) {return;}- const now = utils.nowDate();-- await sql.execute("UPDATE branches SET isDeleted = 1, dateModified = ? WHERE branchId = ?", [now, branchId]);- await sync_table.addBranchSync(branchId);-- const noteId = await sql.getValue("SELECT noteId FROM branches WHERE branchId = ?", [branchId]);-- const notDeletedBranchsCount = await sql.getValue("SELECT COUNT(*) FROM branches WHERE noteId = ? AND isDeleted = 0", [noteId]);+ branch.isDeleted = true;+ await branch.save();- if (!notDeletedBranchsCount) {- await sql.execute("UPDATE notes SET isDeleted = 1, dateModified = ? WHERE noteId = ?", [now, noteId]);- await sync_table.addNoteSync(noteId);+ const note = await branch.getNote();+ const notDeletedBranches = await note.getBranches();- const children = await sql.getRows("SELECT branchId FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [noteId]);+ if (notDeletedBranches.length === 0) {+ note.isDeleted = true;+ await note.save();- for (const child of children) {- await deleteNote(child.branchId);+ for (const childBranch of await note.getChildBranches()) {+ await deleteNote(childBranch);}}}diff --git a/src/services/repository.js b/src/services/repository.jsindex ef0bf0e199..e337df724a 100644--- a/src/services/repository.js+++ b/src/services/repository.js@@ -29,6 +29,10 @@ async function getNote(noteId) {return await getEntity("SELECT * FROM notes WHERE noteId = ?", [noteId]);}+async function getBranch(branchId) {+ return await getEntity("SELECT * FROM branches WHERE branchId = ?", [branchId]);+}+async function updateEntity(entity) {if (entity.beforeSaving) {entity.beforeSaving();@@ -49,6 +53,7 @@ module.exports = {getEntities,getEntity,getNote,+ getBranch,updateEntity,setEntityConstructor};\ No newline at end of filediff --git a/src/services/script.js b/src/services/script.jsindex d4f58fe90d..51f973eff4 100644--- a/src/services/script.js+++ b/src/services/script.js@@ -99,7 +99,7 @@ async function getScriptBundle(note, root = true, scriptEnv = null, includedNoteconst modules = [];- for (const child of await note.getChildren()) {+ for (const child of await note.getChildNotes()) {const childBundle = await getScriptBundle(child, false, scriptEnv, includedNoteIds);if (childBundle) {