mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	removed notes_parent, instead using notes_tree
This commit is contained in:
		
							
								
								
									
										6
									
								
								migrations/0040__fix_note_pid.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								migrations/0040__fix_note_pid.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | ||||
| UPDATE | ||||
|     notes_tree | ||||
| SET | ||||
|     note_pid = (SELECT parent.note_id FROM notes_tree parent WHERE notes_tree.note_pid = parent.note_tree_id) | ||||
| WHERE | ||||
|     note_pid != 'root' | ||||
| @@ -1,28 +0,0 @@ | ||||
| CREATE TABLE [notes_parent] ( | ||||
|     [parent_id] VARCHAR(30) NOT NULL, | ||||
|     [child_id] VARCHAR(30) NOT NULL, | ||||
|     date_created INTEGER NOT NULL DEFAULT 0, | ||||
|     PRIMARY KEY (parent_id, child_id) | ||||
| ); | ||||
|  | ||||
| INSERT INTO notes_parent (parent_id, child_id, date_created) | ||||
|     SELECT note_pid, note_tree_id, date_modified FROM notes_tree; | ||||
|  | ||||
| CREATE TABLE [notes_tree_mig] ( | ||||
|     [note_tree_id] VARCHAR(30) PRIMARY KEY NOT NULL, | ||||
|     [note_id] VARCHAR(30) UNIQUE NOT NULL, | ||||
|     [note_pos] INTEGER NOT NULL, | ||||
|     [is_expanded] BOOLEAN NULL , | ||||
|     date_modified INTEGER NOT NULL DEFAULT 0, | ||||
|     is_deleted INTEGER NOT NULL DEFAULT 0 | ||||
| ); | ||||
|  | ||||
| INSERT INTO notes_tree_mig (note_tree_id, note_id, note_pos, is_expanded, date_modified, is_deleted) | ||||
|     SELECT note_tree_id, note_id, note_pos, is_expanded, date_modified, is_deleted FROM notes_tree; | ||||
|  | ||||
| DROP TABLE notes_tree; | ||||
| ALTER TABLE notes_tree_mig RENAME TO notes_tree; | ||||
|  | ||||
| CREATE INDEX `IDX_notes_tree_note_id` ON `notes_tree` ( | ||||
| 	`note_id` | ||||
| ); | ||||
| @@ -11,6 +11,7 @@ const noteTree = (function() { | ||||
|     let childToParents = {}; | ||||
|     let counter = 1; | ||||
|     let noteTreeIdToKey = {}; | ||||
|     let parentChildToNoteTreeId = {}; | ||||
|  | ||||
|     function getNoteTreeIdFromKey(key) { | ||||
|         const node = treeUtils.getNodeByKey(key); | ||||
| @@ -34,38 +35,54 @@ const noteTree = (function() { | ||||
|         clipboardNoteTreeId = cbNoteId; | ||||
|     } | ||||
|  | ||||
|     function prepareNoteTree(notes, notesParent) { | ||||
|     function getNoteTreeId(parentNoteId, childNoteId) { | ||||
|         const key = parentNoteId + "-" + childNoteId; | ||||
|  | ||||
|         const noteTreeId = parentChildToNoteTreeId[key]; | ||||
|  | ||||
|         if (!noteTreeId) { | ||||
|             throw new Error("Can't find note tree id for parent=" + parentNoteId + ", child=" + childNoteId); | ||||
|         } | ||||
|  | ||||
|         return noteTreeId; | ||||
|     } | ||||
|  | ||||
|     function prepareNoteTree(notes) { | ||||
|         parentToChildren = {}; | ||||
|         childToParents = {}; | ||||
|         notesMap = {}; | ||||
|  | ||||
|         for (const note of notes) { | ||||
|             notesMap[note.note_tree_id] = note; | ||||
|  | ||||
|             const key = note.note_pid + "-" + note.note_id; | ||||
|  | ||||
|             parentChildToNoteTreeId[key] = note.note_tree_id; | ||||
|  | ||||
|             if (!parentToChildren[note.note_pid]) { | ||||
|                 parentToChildren[note.note_pid] = []; | ||||
|             } | ||||
|  | ||||
|         for (const np of notesParent) { | ||||
|             if (!parentToChildren[np.parent_id]) { | ||||
|                 parentToChildren[np.parent_id] = []; | ||||
|             parentToChildren[note.note_pid].push(note.note_id); | ||||
|  | ||||
|             if (!childToParents[note.note_id]) { | ||||
|                 childToParents[note.note_id] = []; | ||||
|             } | ||||
|  | ||||
|             parentToChildren[np.parent_id].push(np.child_id); | ||||
|  | ||||
|             if (!childToParents[np.child_id]) { | ||||
|                 childToParents[np.child_id] = []; | ||||
|             } | ||||
|  | ||||
|             childToParents[np.child_id].push(np.parent_id); | ||||
|             childToParents[note.note_id].push(note.note_pid); | ||||
|         } | ||||
|  | ||||
|         glob.allNoteIds = Object.keys(notesMap); | ||||
|  | ||||
|         return prepareNoteTreeInner(parentToChildren['root']); | ||||
|         return prepareNoteTreeInner('root'); | ||||
|     } | ||||
|  | ||||
|     function prepareNoteTreeInner(noteTreeIds) { | ||||
|     function prepareNoteTreeInner(parentNoteId) { | ||||
|         const childNoteIds = parentToChildren[parentNoteId]; | ||||
|         const noteList = []; | ||||
|  | ||||
|         for (const noteTreeId of noteTreeIds) { | ||||
|         for (const childNoteId of childNoteIds) { | ||||
|             const noteTreeId = getNoteTreeId(parentNoteId, childNoteId); | ||||
|             const note = notesMap[noteTreeId]; | ||||
|  | ||||
|             note.title = note.note_title; | ||||
| @@ -79,11 +96,11 @@ const noteTree = (function() { | ||||
|  | ||||
|             noteTreeIdToKey[noteTreeId] = note.key; | ||||
|  | ||||
|             if (parentToChildren[noteTreeId] && parentToChildren[noteTreeId].length > 0) { | ||||
|             if (parentToChildren[note.note_id] && parentToChildren[note.note_id].length > 0) { | ||||
|                 note.folder = true; | ||||
|  | ||||
|                 if (note.expanded) { | ||||
|                     note.children = prepareNoteTreeInner(parentToChildren[noteTreeId], notesMap, parentToChildren); | ||||
|                     note.children = prepareNoteTreeInner(note.note_id); | ||||
|                 } | ||||
|                 else { | ||||
|                     note.lazy = true; | ||||
| @@ -99,21 +116,40 @@ const noteTree = (function() { | ||||
|     async function activateNode(notePath) { | ||||
|         const path = notePath.split("/").reverse(); | ||||
|  | ||||
|         if (!notesMap[path[0]]) { | ||||
|             console.log("Requested note doesn't exist."); | ||||
|         const effectivePath = []; | ||||
|         let childNoteId = null; | ||||
|  | ||||
|         for (const parentNoteId of path) { | ||||
|             if (childNoteId !== null) { | ||||
|                 const parents = childToParents[childNoteId]; | ||||
|  | ||||
|                 if (!parents.includes(parentNoteId)) { | ||||
|                     console.log("Did not find parent " + parentNoteId + " for child " + childNoteId); | ||||
|  | ||||
|                     if (parents.length > 0) { | ||||
|                         childNoteId = parents[0]; | ||||
|                         effectivePath.push(childNoteId); | ||||
|  | ||||
|                         console.log("Choosing parent " + childNoteId + " instead."); | ||||
|                         continue; | ||||
|                     } | ||||
|                     else { | ||||
|                         console.log("No parents, can't activate node."); | ||||
|                         return; | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|         const effectivePath = []; | ||||
|  | ||||
|         for (const noteTreeId of path) { | ||||
|             effectivePath.push(noteTreeId); | ||||
|             effectivePath.push(parentNoteId); | ||||
|             childNoteId = parentNoteId; | ||||
|         } | ||||
|  | ||||
|         const runPath = effectivePath.reverse(); | ||||
|         let parentNoteId = 'root'; | ||||
|  | ||||
|         for (let i = 0; i < runPath.length; i++) { | ||||
|             const noteTreeId = runPath[i]; | ||||
|             const childNoteId = runPath[i]; | ||||
|             const noteTreeId = getNoteTreeId(parentNoteId, childNoteId); | ||||
|  | ||||
|             const node = treeUtils.getNodeByNoteTreeId(noteTreeId); | ||||
|  | ||||
| @@ -123,6 +159,8 @@ const noteTree = (function() { | ||||
|             else { | ||||
|                 await node.setActive(); | ||||
|             } | ||||
|  | ||||
|             parentNoteId = childNoteId; | ||||
|         } | ||||
|     } | ||||
|  | ||||
| @@ -201,6 +239,7 @@ const noteTree = (function() { | ||||
|                 setExpandedToServer(getNoteTreeIdFromKey(data.node.key), false); | ||||
|             }, | ||||
|             init: (event, data) => { | ||||
|                 showAppIfHidden(); | ||||
|                 if (startNoteTreeId) { | ||||
|                     activateNode(startNoteTreeId); | ||||
|                 } | ||||
|   | ||||
| @@ -58,8 +58,8 @@ const treeUtils = (function() { | ||||
|         const path = []; | ||||
|  | ||||
|         while (node) { | ||||
|             if (node.data.note_tree_id) { | ||||
|                 path.push(node.data.note_tree_id); | ||||
|             if (node.data.note_id) { | ||||
|                 path.push(node.data.note_id); | ||||
|             } | ||||
|  | ||||
|             node = node.getParent(); | ||||
|   | ||||
| @@ -21,13 +21,6 @@ router.get('/', auth.checkApiAuth, async (req, res, next) => { | ||||
|         + "where notes.is_deleted = 0 and notes_tree.is_deleted = 0 " | ||||
|         + "order by note_pos"); | ||||
|  | ||||
|     const notes_parent = await sql.getResults("" + | ||||
|         "select parent_id, child_id " + | ||||
|         "from notes_parent " + | ||||
|         "join notes_tree as child on child.note_tree_id = notes_parent.child_id " + | ||||
|         "left join notes_tree as parent on parent.note_tree_id = notes_parent.parent_id " + | ||||
|         "where child.is_deleted = 0 and (parent.is_deleted = 0 or notes_parent.parent_id = 'root')"); | ||||
|  | ||||
|     const dataKey = protected_session.getDataKey(req); | ||||
|  | ||||
|     for (const note of notes) { | ||||
| @@ -38,7 +31,6 @@ router.get('/', auth.checkApiAuth, async (req, res, next) => { | ||||
|  | ||||
|     res.send({ | ||||
|         notes: notes, | ||||
|         notes_parent: notes_parent, | ||||
|         start_note_tree_id: await options.getOption('start_note_tree_id'), | ||||
|         tree_load_time: utils.nowTimestamp() | ||||
|     }); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user