Skip to content

Build your personal knowledge base with Trilium Notes

fix setNoteToParent and deprecate it

zadamdea519522373
diff --git a/src/services/backend_script_api.js b/src/services/backend_script_api.js
index 620f6480fd..c430c9f88e 100644
--- a/src/services/backend_script_api.js
+++ b/src/services/backend_script_api.js
@@ -282,9 +282,10 @@ function BackendScriptApi(currentNote, apiParams) {
* This method looks similar to toggleNoteInParent() but differs because we're looking up branch by prefix.
*
* @method
+ * @deprecated - this method is pretty confusing and serves specialized purpose only
* @param {string} noteId
* @param {string} prefix
- * @param {string} [parentNoteId]
+ * @param {string|null} parentNoteId
*/
this.setNoteToParent = treeService.setNoteToParent;
diff --git a/src/services/consistency_checks.js b/src/services/consistency_checks.js
index 9393b62247..5d0a286dd1 100644
--- a/src/services/consistency_checks.js
+++ b/src/services/consistency_checks.js
@@ -195,7 +195,7 @@ async function findExistencyIssues() {
HAVING
COUNT(*) > 1`,
async ({noteId, parentNoteId}) => {
- const branches = await repository.getEntities(`SELECT * FROM branches WHERE noteId = ? and parentNoteId = ? and isDeleted = 1`, [noteId, parentNoteId]);
+ const branches = await repository.getEntities(`SELECT * FROM branches WHERE noteId = ? and parentNoteId = ? and isDeleted = 0`, [noteId, parentNoteId]);
// it's not necessarily "original" branch, it's just the only one which will survive
const origBranch = branches[0];
diff --git a/src/services/tree.js b/src/services/tree.js
index ca2459ac78..5a38c97a97 100644
--- a/src/services/tree.js
+++ b/src/services/tree.js
@@ -36,7 +36,7 @@ async function validateParentChild(parentNoteId, childNoteId, branchId = null) {
}
async function getExistingBranch(parentNoteId, childNoteId) {
- return await sql.getRow('SELECT * FROM branches WHERE noteId = ? AND parentNoteId = ? AND isDeleted = 0', [childNoteId, parentNoteId]);
+ return await repository.getEntity('SELECT * FROM branches WHERE noteId = ? AND parentNoteId = ? AND isDeleted = 0', [childNoteId, parentNoteId]);
}
/**
@@ -123,6 +123,9 @@ async function sortNotesAlphabetically(parentNoteId, directoriesFirst = false) {
});
}
+/**
+ * @deprecated - this will be removed in the future
+ */
async function setNoteToParent(noteId, prefix, parentNoteId) {
const parentNote = await repository.getNote(parentNoteId);
@@ -151,11 +154,19 @@ async function setNoteToParent(noteId, prefix, parentNoteId) {
throw new Error(`Cannot create a branch for ${noteId} which is deleted.`);
}
- await new Branch({
- noteId: noteId,
- parentNoteId: parentNoteId,
- prefix: prefix
- }).save();
+ const branch = await repository.getEntity('SELECT * FROM branches WHERE isDeleted = 0 AND noteId = ? AND parentNoteId = ?', [noteId, parentNoteId]);
+
+ if (branch) {
+ branch.prefix = prefix;
+ await branch.save();
+ }
+ else {
+ await new Branch({
+ noteId: noteId,
+ parentNoteId: parentNoteId,
+ prefix: prefix
+ }).save();
+ }
}
}