Skip to content

Build your personal knowledge base with Trilium Notes

smaller refactorings continued

azivneracc82f39c42e
diff --git a/src/entities/entity.js b/src/entities/entity.js
index 7906488153..41fbfd93c5 100644
--- a/src/entities/entity.js
+++ b/src/entities/entity.js
@@ -4,7 +4,7 @@ const utils = require('../services/utils');
const repository = require('../services/repository');
class Entity {
- constructor(row) {
+ constructor(row = {}) {
utils.assertArguments(row);
for (const key in row) {
diff --git a/src/entities/entity_constructor.js b/src/entities/entity_constructor.js
index b439c854a0..1fbf02622d 100644
--- a/src/entities/entity_constructor.js
+++ b/src/entities/entity_constructor.js
@@ -1,5 +1,6 @@
const Note = require('../entities/note');
const NoteRevision = require('../entities/note_revision');
+const Image = require('../entities/image');
const NoteImage = require('../entities/note_image');
const Branch = require('../entities/branch');
const Label = require('../entities/label');
@@ -17,6 +18,9 @@ function createEntityFromRow(row) {
else if (row.noteImageId) {
entity = new NoteImage(row);
}
+ else if (row.imageId) {
+ entity = new Image(row);
+ }
else if (row.branchId) {
entity = new Branch(row);
}
diff --git a/src/public/javascripts/dialogs/labels.js b/src/public/javascripts/dialogs/labels.js
index de1d49187e..32f0666b68 100644
--- a/src/public/javascripts/dialogs/labels.js
+++ b/src/public/javascripts/dialogs/labels.js
@@ -15,6 +15,18 @@ function LabelsModel() {
this.labels = ko.observableArray();
+ this.updateLabelPositions = function() {
+ let position = 0;
+
+ // we need to update positions by searching in the DOM, because order of the
+ // labels in the viewmodel (self.labels()) stays the same
+ $labelsBody.find('input[name="position"]').each(function() {
+ const label = self.getTargetLabel(this);
+
+ label().position = position++;
+ });
+ };
+
this.loadLabels = async function() {
const noteId = noteDetailService.getCurrentNoteId();
@@ -32,17 +44,7 @@ function LabelsModel() {
$labelsBody.sortable({
handle: '.handle',
containment: $labelsBody,
- update: function() {
- let position = 0;
-
- // we need to update positions by searching in the DOM, because order of the
- // labels in the viewmodel (self.labels()) stays the same
- $labelsBody.find('input[name="position"]').each(function() {
- const label = self.getTargetLabel(this);
-
- label().position = position++;
- });
- }
+ update: this.updateLabelPositions
});
};
@@ -80,6 +82,8 @@ function LabelsModel() {
return;
}
+ self.updateLabelPositions();
+
const noteId = noteDetailService.getCurrentNoteId();
const labelsToSave = self.labels()
diff --git a/src/public/javascripts/services/tree.js b/src/public/javascripts/services/tree.js
index 7e3a4e15c5..a32a50acee 100644
--- a/src/public/javascripts/services/tree.js
+++ b/src/public/javascripts/services/tree.js
@@ -438,35 +438,28 @@ async function createNote(node, parentNoteId, target, isProtected) {
const newNoteName = "new note";
- const result = await server.post('notes/' + parentNoteId + '/children', {
+ const {note, branch} = await server.post('notes/' + parentNoteId + '/children', {
title: newNoteName,
target: target,
target_branchId: node.data.branchId,
isProtected: isProtected
});
- const note = new NoteShort(treeCache, {
- noteId: result.noteId,
- title: result.title,
- isProtected: result.isProtected,
- type: result.type,
- mime: result.mime
- });
-
- const branch = new Branch(treeCache, result);
+ const noteEntity = new NoteShort(treeCache, note);
+ const branchEntity = new Branch(treeCache, branch);
- treeCache.add(note, branch);
+ treeCache.add(noteEntity, branchEntity);
noteDetailService.newNoteCreated();
const newNode = {
title: newNoteName,
- noteId: result.noteId,
+ noteId: branchEntity.noteId,
parentNoteId: parentNoteId,
- refKey: result.noteId,
- branchId: result.branchId,
+ refKey: branchEntity.noteId,
+ branchId: branchEntity.branchId,
isProtected: isProtected,
- extraClasses: await treeBuilder.getExtraClasses(note)
+ extraClasses: await treeBuilder.getExtraClasses(noteEntity)
};
if (target === 'after') {
diff --git a/src/routes/api/file_upload.js b/src/routes/api/file_upload.js
index bc77d966c7..b1ac17afe2 100644
--- a/src/routes/api/file_upload.js
+++ b/src/routes/api/file_upload.js
@@ -1,9 +1,9 @@
"use strict";
-const sql = require('../../services/sql');
const notes = require('../../services/notes');
const labels = require('../../services/labels');
const protected_session = require('../../services/protected_session');
+const repository = require('../../services/repository');
async function uploadFile(req) {
const parentNoteId = req.params.parentNoteId;
@@ -11,50 +11,46 @@ async function uploadFile(req) {
const originalName = file.originalname;
const size = file.size;
- const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [parentNoteId]);
+ const parentNote = await repository.getNote(parentNoteId);
- if (!note) {
+ if (!parentNote) {
return [404, `Note ${parentNoteId} doesn't exist.`];
}
- const {noteId} = await notes.createNewNote(parentNoteId, {
+ const {note} = await notes.createNewNote(parentNoteId, {
title: originalName,
content: file.buffer,
target: 'into',
isProtected: false,
type: 'file',
mime: file.mimetype
- }, req);
+ });
- await labels.createLabel(noteId, "original_file_name", originalName);
- await labels.createLabel(noteId, "file_size", size);
+ await labels.createLabel(note.noteId, "original_file_name", originalName);
+ await labels.createLabel(note.noteId, "file_size", size);
return {
- noteId: noteId
+ noteId: note.noteId
};
}
async function downloadFile(req, res) {
const noteId = req.params.noteId;
- const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
+ const note = await repository.getNote(noteId);
if (!note) {
return res.status(404).send(`Note ${noteId} doesn't exist.`);
}
- if (note.isProtected) {
- if (!protected_session.isProtectedSessionAvailable()) {
- res.status(401).send("Protected session not available");
- return;
- }
-
- protected_session.decryptNote(note);
+ if (note.isProtected && !protected_session.isProtectedSessionAvailable()) {
+ res.status(401).send("Protected session not available");
+ return;
}
- const labelMap = await labels.getNoteLabelMap(noteId);
+ const labelMap = await note.getLabelMap();
const fileName = labelMap.original_file_name ? labelMap.original_file_name : note.title;
- res.setHeader('Content-Disposition', 'file; filename=' + fileName);
+ res.setHeader('Content-Disposition', 'file; filename="' + fileName + '"');
res.setHeader('Content-Type', note.mime);
res.send(note.content);
diff --git a/src/routes/api/image.js b/src/routes/api/image.js
index 056bdd5f15..1e39a5bf5e 100644
--- a/src/routes/api/image.js
+++ b/src/routes/api/image.js
@@ -1,12 +1,12 @@
"use strict";
-const sql = require('../../services/sql');
const image = require('../../services/image');
+const repository = require('../../services/repository');
const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR;
const fs = require('fs');
async function returnImage(req, res) {
- const image = await sql.getRow("SELECT * FROM images WHERE imageId = ?", [req.params.imageId]);
+ const image = await repository.getImage(req.params.imageId);
if (!image) {
return res.sendStatus(404);
@@ -25,7 +25,7 @@ async function uploadImage(req) {
const noteId = req.query.noteId;
const file = req.file;
- const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
+ const note = await repository.getNote(noteId);
if (!note) {
return [404, `Note ${noteId} doesn't exist.`];
diff --git a/src/routes/api/import.js b/src/routes/api/import.js
index 5d13b27aa8..ca5c1b64c1 100644
--- a/src/routes/api/import.js
+++ b/src/routes/api/import.js
@@ -1,6 +1,6 @@
"use strict";
-const sql = require('../../services/sql');
+const repository = require('../../services/repository');
const labels = require('../../services/labels');
const notes = require('../../services/notes');
const tar = require('tar-stream');
@@ -89,9 +89,9 @@ async function importTar(req) {
const parentNoteId = req.params.parentNoteId;
const file = req.file;
- const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [parentNoteId]);
+ const parentNote = await repository.getNote(parentNoteId);
- if (!note) {
+ if (!parentNote) {
return [404, `Note ${parentNoteId} doesn't exist.`];
}
diff --git a/src/routes/api/labels.js b/src/routes/api/labels.js
index fb5b7c11c5..68e4a042ff 100644
--- a/src/routes/api/labels.js
+++ b/src/routes/api/labels.js
@@ -1,25 +1,26 @@
"use strict";
const sql = require('../../services/sql');
-const sync_table = require('../../services/sync_table');
const utils = require('../../services/utils');
const labels = require('../../services/labels');
+const repository = require('../../services/repository');
+const Label = require('../../entities/label');
async function getNoteLabels(req) {
const noteId = req.params.noteId;
- return await sql.getRows("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
+ return await repository.getEntities("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
}
-async function updateNoteLabels(req, res, next) {
+async function updateNoteLabels(req) {
const noteId = req.params.noteId;
const labels = req.body;
- const now = utils.nowDate();
for (const label of labels) {
+ let labelEntity;
+
if (label.labelId) {
- await sql.execute("UPDATE labels SET name = ?, value = ?, dateModified = ?, isDeleted = ?, position = ? WHERE labelId = ?",
- [label.name, label.value, now, label.isDeleted, label.position, label.labelId]);
+ labelEntity = await repository.getLabel(label.labelId);
}
else {
// if it was "created" and then immediatelly deleted, we just don't create it at all
@@ -27,27 +28,23 @@ async function updateNoteLabels(req, res, next) {
continue;
}
- label.labelId = utils.newLabelId();
-
- await sql.insert("labels", {
- labelId: label.labelId,
- noteId: noteId,
- name: label.name,
- value: label.value,
- position: label.position,
- dateCreated: now,
- dateModified: now,
- isDeleted: false
- });
+ labelEntity = new Label();
+ labelEntity.labelId = utils.newLabelId();
+ labelEntity.noteId = noteId;
}
- await sync_table.addLabelSync(label.labelId);
+ labelEntity.name = label.name;
+ labelEntity.value = label.value;
+ labelEntity.position = label.position;
+ labelEntity.isDeleted = label.isDeleted;
+
+ await labelEntity.save();
}
- return await sql.getRows("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
+ return await repository.getEntities("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
}
-async function getAllLabelNames(req) {
+async function getAllLabelNames() {
const names = await sql.getColumn("SELECT DISTINCT name FROM labels WHERE isDeleted = 0");
for (const label of labels.BUILTIN_LABELS) {
diff --git a/src/routes/api/notes.js b/src/routes/api/notes.js
index 612b86f17c..1c5e03e0f9 100644
--- a/src/routes/api/notes.js
+++ b/src/routes/api/notes.js
@@ -3,22 +3,18 @@
const sql = require('../../services/sql');
const notes = require('../../services/notes');
const utils = require('../../services/utils');
-const protected_session = require('../../services/protected_session');
const tree = require('../../services/tree');
const sync_table = require('../../services/sync_table');
const repository = require('../../services/repository');
async function getNote(req) {
const noteId = req.params.noteId;
-
- const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
+ const note = await repository.getNote(noteId);
if (!note) {
return [404, "Note " + noteId + " has not been found."];
}
- protected_session.decryptNote(note);
-
if (note.type === 'file') {
// no need to transfer (potentially large) file payload for this request
note.content = null;
@@ -31,12 +27,11 @@ async function createNote(req) {
const parentNoteId = req.params.parentNoteId;
const newNote = req.body;
- const { noteId, branchId, note } = await notes.createNewNote(parentNoteId, newNote, req);
+ const { note, branch } = await notes.createNewNote(parentNoteId, newNote, req);
return {
- 'noteId': noteId,
- 'branchId': branchId,
- 'note': note
+ note,
+ branch
};
}
diff --git a/src/routes/api/sender.js b/src/routes/api/sender.js
index 704e9ab71f..fe345ac35d 100644
--- a/src/routes/api/sender.js
+++ b/src/routes/api/sender.js
@@ -47,7 +47,7 @@ async function uploadImage(req) {
const parentNoteId = await date_notes.getDateNoteId(req.headers['x-local-date']);
- const {noteId} = await notes.createNewNote(parentNoteId, {
+ const {note} = await notes.createNewNote(parentNoteId, {
title: "Sender image",
content: "",
target: 'into',
@@ -56,13 +56,13 @@ async function uploadImage(req) {
mime: 'text/html'
});
- const {fileName, imageId} = await image.saveImage(file, null, noteId);
+ const {fileName, imageId} = await image.saveImage(file, null, note.noteId);
const url = `/api/images/${imageId}/${fileName}`;
const content = `<img src="${url}"/>`;
- await sql.execute("UPDATE notes SET content = ? WHERE noteId = ?", [content, noteId]);
+ await sql.execute("UPDATE notes SET content = ? WHERE noteId = ?", [content, note.noteId]);
}
async function saveNote(req) {
diff --git a/src/services/date_notes.js b/src/services/date_notes.js
index ad75e8becd..26effc8b61 100644
--- a/src/services/date_notes.js
+++ b/src/services/date_notes.js
@@ -14,12 +14,14 @@ const DAYS = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Satur
const MONTHS = ['January','February','March','April','May','June','July','August','September','October','November','December'];
async function createNote(parentNoteId, noteTitle, noteText) {
- return (await notes.createNewNote(parentNoteId, {
+ const {note} = await notes.createNewNote(parentNoteId, {
title: noteTitle,
content: noteText,
target: 'into',
isProtected: false
- })).noteId;
+ });
+
+ return note.noteId;
}
async function getNoteStartingWith(parentNoteId, startsWith) {
@@ -34,11 +36,13 @@ async function getRootCalendarNoteId() {
WHERE labels.name = '${CALENDAR_ROOT_LABEL}' AND notes.isDeleted = 0`);
if (!rootNoteId) {
- rootNoteId = (await notes.createNewNote('root', {
+ const {rootNote} = await notes.createNewNote('root', {
title: 'Calendar',
target: 'into',
isProtected: false
- })).noteId;
+ });
+
+ const rootNoteId = rootNote.noteId;
await labels.createLabel(rootNoteId, CALENDAR_ROOT_LABEL);
}
diff --git a/src/services/notes.js b/src/services/notes.js
index b78a8d5236..bd38daaf07 100644
--- a/src/services/notes.js
+++ b/src/services/notes.js
@@ -72,9 +72,7 @@ async function createNewNote(parentNoteId, noteOpts) {
await branch.save();
return {
- noteId: note.noteId,
note,
- branchId: branch.branchId,
branch
};
}
@@ -83,7 +81,7 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {})
if (!parentNoteId) throw new Error("Empty parentNoteId");
if (!title) throw new Error("Empty title");
- const note = {
+ const noteData = {
title: title,
content: extraOptions.json ? JSON.stringify(content, null, '\t') : content,
target: 'into',
@@ -92,25 +90,25 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {})
mime: extraOptions.mime
};
- if (extraOptions.json && !note.type) {
- note.type = "code";
- note.mime = "application/json";
+ if (extraOptions.json && !noteData.type) {
+ noteData.type = "code";
+ noteData.mime = "application/json";
}
- if (!note.type) {
- note.type = "text";
- note.mime = "text/html";
+ if (!noteData.type) {
+ noteData.type = "text";
+ noteData.mime = "text/html";
}
- const {noteId} = await createNewNote(parentNoteId, note);
+ const {note} = await createNewNote(parentNoteId, noteData);
if (extraOptions.labels) {
for (const labelName in extraOptions.labels) {
- await labels.createLabel(noteId, labelName, extraOptions.labels[labelName]);
+ await labels.createLabel(note.noteId, labelName, extraOptions.labels[labelName]);
}
}
- return noteId;
+ return note.noteId;
}
async function protectNoteRecursively(note, protect) {
diff --git a/src/services/repository.js b/src/services/repository.js
index 810ba58411..e20b05979c 100644
--- a/src/services/repository.js
+++ b/src/services/repository.js
@@ -37,6 +37,10 @@ async function getImage(imageId) {
return await getEntity("SELECT * FROM images WHERE imageId = ?", [imageId]);
}
+async function getLabel(labelId) {
+ return await getEntity("SELECT * FROM labels WHERE labelId = ?", [labelId]);
+}
+
async function updateEntity(entity) {
if (entity.beforeSaving) {
entity.beforeSaving();
@@ -59,6 +63,7 @@ module.exports = {
getNote,
getBranch,
getImage,
+ getLabel,
updateEntity,
setEntityConstructor
};
\ No newline at end of file