mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +01:00 
			
		
		
		
	refactoring of note deletion
This commit is contained in:
		| @@ -2,11 +2,16 @@ | |||||||
|  |  | ||||||
| const Entity = require('./entity'); | const Entity = require('./entity'); | ||||||
| const utils = require('../services/utils'); | const utils = require('../services/utils'); | ||||||
|  | const repository = require('../services/repository'); | ||||||
|  |  | ||||||
| class Branch extends Entity { | class Branch extends Entity { | ||||||
|     static get tableName() { return "branches"; } |     static get tableName() { return "branches"; } | ||||||
|     static get primaryKeyName() { return "branchId"; } |     static get primaryKeyName() { return "branchId"; } | ||||||
|  |  | ||||||
|  |     async getNote() { | ||||||
|  |         return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     beforeSaving() { |     beforeSaving() { | ||||||
|         this.dateModified = utils.nowDate() |         this.dateModified = utils.nowDate() | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -1,6 +1,7 @@ | |||||||
| "use strict"; | "use strict"; | ||||||
|  |  | ||||||
| const utils = require('../services/utils'); | const utils = require('../services/utils'); | ||||||
|  | const repository = require('../services/repository'); | ||||||
|  |  | ||||||
| class Entity { | class Entity { | ||||||
|     constructor(row) { |     constructor(row) { | ||||||
| @@ -10,6 +11,10 @@ class Entity { | |||||||
|             this[key] = row[key]; |             this[key] = row[key]; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     async save() { | ||||||
|  |         await repository.updateEntity(this); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| module.exports = Entity; | module.exports = Entity; | ||||||
| @@ -84,11 +84,11 @@ class Note extends Entity { | |||||||
|         return await repository.getEntities("SELECT * FROM note_images WHERE noteId = ? AND isDeleted = 0", [this.noteId]); |         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]); |         return await repository.getEntities("SELECT * FROM branches WHERE isDeleted = 0 AND noteId = ?", [this.noteId]); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async getChild(name) { |     async getChildNote(name) { | ||||||
|         return await repository.getEntity(` |         return await repository.getEntity(` | ||||||
|           SELECT notes.*  |           SELECT notes.*  | ||||||
|           FROM branches  |           FROM branches  | ||||||
| @@ -99,7 +99,7 @@ class Note extends Entity { | |||||||
|                 AND notes.title = ?`, [this.noteId, name]); |                 AND notes.title = ?`, [this.noteId, name]); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async getChildren() { |     async getChildNotes() { | ||||||
|         return await repository.getEntities(` |         return await repository.getEntities(` | ||||||
|           SELECT notes.*  |           SELECT notes.*  | ||||||
|           FROM branches  |           FROM branches  | ||||||
| @@ -110,7 +110,16 @@ class Note extends Entity { | |||||||
|           ORDER BY branches.notePosition`, [this.noteId]); |           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(` |         return await repository.getEntities(` | ||||||
|           SELECT parent_notes.*  |           SELECT parent_notes.*  | ||||||
|           FROM  |           FROM  | ||||||
| @@ -121,16 +130,6 @@ class Note extends Entity { | |||||||
|                 AND parent_notes.isDeleted = 0`, [this.noteId]); |                 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() { |     beforeSaving() { | ||||||
|         if (this.isJson()) { |         if (this.isJson()) { | ||||||
|             this.content = JSON.stringify(this.jsonContent, null, '\t'); |             this.content = JSON.stringify(this.jsonContent, null, '\t'); | ||||||
|   | |||||||
| @@ -17,7 +17,7 @@ class NoteShort { | |||||||
|         const branches = []; |         const branches = []; | ||||||
|  |  | ||||||
|         for (const parent of this.treeCache.parents[this.noteId]) { |         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; |         return branches; | ||||||
|   | |||||||
| @@ -7,7 +7,7 @@ import editTreePrefixDialog from "../dialogs/edit_tree_prefix.js"; | |||||||
|  |  | ||||||
| const keyBindings = { | const keyBindings = { | ||||||
|     "del": node => { |     "del": node => { | ||||||
|         treeChangesService.deleteNodes(getSelectedNodes(true)); |         treeChangesService.deleteNodes(treeService.getSelectedNodes(true)); | ||||||
|     }, |     }, | ||||||
|     "ctrl+up": node => { |     "ctrl+up": node => { | ||||||
|         const beforeNode = node.getPrevSibling(); |         const beforeNode = node.getPrevSibling(); | ||||||
|   | |||||||
| @@ -5,6 +5,7 @@ const utils = require('../../services/utils'); | |||||||
| const sync_table = require('../../services/sync_table'); | const sync_table = require('../../services/sync_table'); | ||||||
| const tree = require('../../services/tree'); | const tree = require('../../services/tree'); | ||||||
| const notes = require('../../services/notes'); | 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 |  * 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) { | async function deleteBranch(req) { | ||||||
|     await notes.deleteNote(req.params.branchId); |     const branch = await repository.getBranch(req.params.branchId); | ||||||
|  |  | ||||||
|  |     await notes.deleteNote(branch); | ||||||
| } | } | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|   | |||||||
| @@ -115,7 +115,7 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {}) | |||||||
| async function protectNoteRecursively(note, protect) { | async function protectNoteRecursively(note, protect) { | ||||||
|     await protectNote(note, protect); |     await protectNote(note, protect); | ||||||
|  |  | ||||||
|     for (const child of await note.getChildren()) { |     for (const child of await note.getChildNotes()) { | ||||||
|         await protectNoteRecursively(child, protect); |         await protectNoteRecursively(child, protect); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @@ -229,30 +229,23 @@ async function updateNote(noteId, noteUpdates) { | |||||||
|     await protectNoteRevisions(note); |     await protectNoteRevisions(note); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function deleteNote(branchId) { | async function deleteNote(branch) { | ||||||
|     const branch = await sql.getRowOrNull("SELECT * FROM branches WHERE branchId = ?", [branchId]); |  | ||||||
|  |  | ||||||
|     if (!branch || branch.isDeleted === 1) { |     if (!branch || branch.isDeleted === 1) { | ||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     const now = utils.nowDate(); |     branch.isDeleted = true; | ||||||
|  |     await branch.save(); | ||||||
|  |  | ||||||
|     await sql.execute("UPDATE branches SET isDeleted = 1, dateModified = ? WHERE branchId = ?", [now, branchId]); |     const note = await branch.getNote(); | ||||||
|     await sync_table.addBranchSync(branchId); |     const notDeletedBranches = await note.getBranches(); | ||||||
|  |  | ||||||
|     const noteId = await sql.getValue("SELECT noteId FROM branches WHERE branchId = ?", [branchId]); |     if (notDeletedBranches.length === 0) { | ||||||
|  |         note.isDeleted = true; | ||||||
|  |         await note.save(); | ||||||
|  |  | ||||||
|     const notDeletedBranchsCount = await sql.getValue("SELECT COUNT(*) FROM branches WHERE noteId = ? AND isDeleted = 0", [noteId]); |         for (const childBranch of await note.getChildBranches()) { | ||||||
|  |             await deleteNote(childBranch); | ||||||
|     if (!notDeletedBranchsCount) { |  | ||||||
|         await sql.execute("UPDATE notes SET isDeleted = 1, dateModified = ? WHERE noteId = ?", [now, noteId]); |  | ||||||
|         await sync_table.addNoteSync(noteId); |  | ||||||
|  |  | ||||||
|         const children = await sql.getRows("SELECT branchId FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [noteId]); |  | ||||||
|  |  | ||||||
|         for (const child of children) { |  | ||||||
|             await deleteNote(child.branchId); |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -29,6 +29,10 @@ async function getNote(noteId) { | |||||||
|     return await getEntity("SELECT * FROM notes WHERE noteId = ?", [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) { | async function updateEntity(entity) { | ||||||
|     if (entity.beforeSaving) { |     if (entity.beforeSaving) { | ||||||
|         entity.beforeSaving(); |         entity.beforeSaving(); | ||||||
| @@ -49,6 +53,7 @@ module.exports = { | |||||||
|     getEntities, |     getEntities, | ||||||
|     getEntity, |     getEntity, | ||||||
|     getNote, |     getNote, | ||||||
|  |     getBranch, | ||||||
|     updateEntity, |     updateEntity, | ||||||
|     setEntityConstructor |     setEntityConstructor | ||||||
| }; | }; | ||||||
| @@ -99,7 +99,7 @@ async function getScriptBundle(note, root = true, scriptEnv = null, includedNote | |||||||
|  |  | ||||||
|     const modules = []; |     const modules = []; | ||||||
|  |  | ||||||
|     for (const child of await note.getChildren()) { |     for (const child of await note.getChildNotes()) { | ||||||
|         const childBundle = await getScriptBundle(child, false, scriptEnv, includedNoteIds); |         const childBundle = await getScriptBundle(child, false, scriptEnv, includedNoteIds); | ||||||
|  |  | ||||||
|         if (childBundle) { |         if (childBundle) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user