Skip to content

Build your personal knowledge base with Trilium Notes

parent-child relationship is now stored in notes_parent table

azivnercc3c9d64285f
diff --git a/migrations/0040__notes_parent.sql b/migrations/0040__notes_parent.sql
new file mode 100644
index 0000000000..21b5c7b55a
--- /dev/null
+++ b/migrations/0040__notes_parent.sql
@@ -0,0 +1,28 @@
+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`
+);
\ No newline at end of file
diff --git a/public/javascripts/note_tree.js b/public/javascripts/note_tree.js
index 835215e743..7da0503a74 100644
--- a/public/javascripts/note_tree.js
+++ b/public/javascripts/note_tree.js
@@ -33,17 +33,20 @@ const noteTree = (function() {
clipboardNoteTreeId = cbNoteId;
}
- function prepareNoteTree(notes) { showAppIfHidden();
+ function prepareNoteTree(notes, notesParent) {
parentToNotes = {};
notesMap = {};
for (const note of notes) {
- if (!parentToNotes[note.note_pid]) {
- parentToNotes[note.note_pid] = [];
+ notesMap[note.note_tree_id] = note;
+ }
+
+ for (const np of notesParent) {
+ if (!parentToNotes[np.parent_id]) {
+ parentToNotes[np.parent_id] = [];
}
- notesMap[note.note_tree_id] = note;
- parentToNotes[note.note_pid].push(note.note_tree_id);
+ parentToNotes[np.parent_id].push(np.child_id);
}
glob.allNoteIds = Object.keys(notesMap);
@@ -160,9 +163,13 @@ const noteTree = (function() {
setExpandedToServer(getNoteTreeIdFromKey(data.node.key), false);
},
init: (event, data) => {
- if (startNoteTreeId) {
- treeUtils.activateNode(startNoteTreeId);
- }
+ // if (startNoteTreeId) {
+ // treeUtils.activateNode(startNoteTreeId);
+ // }
+
+
+
+ showAppIfHidden();
},
hotkeys: {
keydown: keybindings
@@ -258,7 +265,7 @@ const noteTree = (function() {
startNoteTreeId = document.location.hash.substr(1); // strip initial #
}
- return prepareNoteTree(resp.notes);
+ return prepareNoteTree(resp.notes, resp.notes_parent);
});
}
diff --git a/routes/api/tree.js b/routes/api/tree.js
index 3fa72f577e..d5a78f37c5 100644
--- a/routes/api/tree.js
+++ b/routes/api/tree.js
@@ -19,7 +19,14 @@ router.get('/', auth.checkApiAuth, async (req, res, next) => {
+ "from notes_tree "
+ "join notes on notes.note_id = notes_tree.note_id "
+ "where notes.is_deleted = 0 and notes_tree.is_deleted = 0 "
- + "order by note_pid, note_pos");
+ + "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);
@@ -31,6 +38,7 @@ 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()
});
diff --git a/services/migration.js b/services/migration.js
index 9e07e1edab..25f2dd3523 100644
--- a/services/migration.js
+++ b/services/migration.js
@@ -4,7 +4,7 @@ const options = require('./options');
const fs = require('fs-extra');
const log = require('./log');
-const APP_DB_VERSION = 39;
+const APP_DB_VERSION = 40;
const MIGRATIONS_DIR = "migrations";
async function migrate() {
diff --git a/services/sql.js b/services/sql.js
index 34e3c91ad8..5c882d1967 100644
--- a/services/sql.js
+++ b/services/sql.js
@@ -128,9 +128,11 @@ async function wrap(func) {
return await func(db);
}
catch (e) {
- log.error("Error executing query. Inner exception: " + e.stack + error.stack);
+ const thisError = new Error();
- throw new Error();
+ log.error("Error executing query. Inner exception: " + e.stack + thisError.stack);
+
+ throw thisError;
}
}