Skip to content

Build your personal knowledge base with Trilium Notes

smaller refactorings

azivner42dd8d4754d2
diff --git a/src/entities/entity.js b/src/entities/entity.js
index e3ef102b51..897b8dea42 100644
--- a/src/entities/entity.js
+++ b/src/entities/entity.js
@@ -18,6 +18,8 @@ class Entity {
async save() {
await repository.updateEntity(this);
+
+ return this;
}
}
diff --git a/src/routes/api/cloning.js b/src/routes/api/cloning.js
index 99e4c4b2d9..9c9626bb2c 100644
--- a/src/routes/api/cloning.js
+++ b/src/routes/api/cloning.js
@@ -19,15 +19,13 @@ async function cloneNoteToParent(req) {
const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
- const branch = new Branch({
+ const branch = await new Branch({
noteId: noteId,
parentNoteId: parentNoteId,
prefix: prefix,
notePosition: newNotePos,
isExpanded: 0
- });
-
- await branch.save();
+ }).save();
await sql.execute("UPDATE branches SET isExpanded = 1 WHERE noteId = ?", [parentNoteId]);
@@ -53,14 +51,12 @@ async function cloneNoteAfter(req) {
await syncTable.addNoteReorderingSync(afterNote.parentNoteId);
- const branch = new Branch({
+ const branch = await new Branch({
noteId: noteId,
parentNoteId: afterNote.parentNoteId,
notePosition: afterNote.notePosition + 1,
isExpanded: 0
- });
-
- await branch.save();
+ }).save();
return { success: true };
}
diff --git a/src/routes/api/recent_notes.js b/src/routes/api/recent_notes.js
index 7700724c96..a5b0c0cf37 100644
--- a/src/routes/api/recent_notes.js
+++ b/src/routes/api/recent_notes.js
@@ -24,14 +24,12 @@ async function addRecentNote(req) {
const branchId = req.params.branchId;
const notePath = req.params.notePath;
- const recentNote = new RecentNote({
+ await new RecentNote({
branchId: branchId,
notePath: notePath,
dateAccessed: dateUtils.nowDate(),
isDeleted: 0
- });
-
- await recentNote.save();
+ }).save();
await optionService.setOption('startNotePath', notePath);
diff --git a/src/routes/api/sender.js b/src/routes/api/sender.js
index 313d51db31..06ab61cb9f 100644
--- a/src/routes/api/sender.js
+++ b/src/routes/api/sender.js
@@ -20,8 +20,9 @@ async function login(req) {
return [401, "Incorrect username/password"];
}
- const apiToken = new ApiToken({ token: utils.randomSecureToken() });
- await apiToken.save();
+ const apiToken = await new ApiToken({
+ token: utils.randomSecureToken()
+ }).save();
return {
token: apiToken.token
diff --git a/src/routes/api/tree.js b/src/routes/api/tree.js
index f0e251e15c..0bd0372d4b 100644
--- a/src/routes/api/tree.js
+++ b/src/routes/api/tree.js
@@ -2,7 +2,6 @@
const sql = require('../../services/sql');
const optionService = require('../../services/options');
-const config = require('../../services/config');
const protectedSessionService = require('../../services/protected_session');
async function getTree() {
diff --git a/src/services/anonymization.js b/src/services/anonymization.js
index aafb8f0f8b..8db63aba8f 100644
--- a/src/services/anonymization.js
+++ b/src/services/anonymization.js
@@ -10,7 +10,7 @@ async function anonymize() {
fs.mkdirSync(dataDir.ANONYMIZED_DB_DIR, 0o700);
}
- const anonymizedFile = dataDir.ANONYMIZED_DB_DIR + "/" + "backup-" + dateUtils.getDateTimeForFile() + ".db";
+ const anonymizedFile = dataDir.ANONYMIZED_DB_DIR + "/" + "anonymized-" + dateUtils.getDateTimeForFile() + ".db";
fs.copySync(dataDir.DOCUMENT_PATH, anonymizedFile);
diff --git a/src/services/image.js b/src/services/image.js
index 1196f753c2..7edd4c1304 100644
--- a/src/services/image.js
+++ b/src/services/image.js
@@ -20,21 +20,17 @@ async function saveImage(file, noteId) {
const fileNameWithouExtension = file.originalname.replace(/\.[^/.]+$/, "");
const fileName = sanitizeFilename(fileNameWithouExtension + "." + imageFormat.ext);
- const image = new Image({
+ const image = await new Image({
format: imageFormat.ext,
name: fileName,
checksum: utils.hash(optimizedImage),
data: optimizedImage
- });
-
- await image.save();
+ }).save();
- const noteImage = new NoteImage({
+ await new NoteImage({
noteId: noteId,
imageId: image.imageId
- });
-
- await noteImage.save();
+ }).save();
return {fileName, imageId: image.imageId};
}
diff --git a/src/services/labels.js b/src/services/labels.js
index 97b3c00de7..896f61697f 100644
--- a/src/services/labels.js
+++ b/src/services/labels.js
@@ -15,10 +15,6 @@ const BUILTIN_LABELS = [
'appCss'
];
-async function getNoteLabelMap(noteId) {
- return await sql.getMap(`SELECT name, value FROM labels WHERE noteId = ? AND isDeleted = 0`, [noteId]);
-}
-
async function getNoteIdWithLabel(name, value) {
return await sql.getValue(`SELECT notes.noteId FROM notes JOIN labels USING(noteId)
WHERE notes.isDeleted = 0
@@ -54,19 +50,14 @@ async function getNoteIdsWithLabel(name) {
}
async function createLabel(noteId, name, value = "") {
- const label = new Label({
+ return await new Label({
noteId: noteId,
name: name,
value: value
- });
-
- await label.save();
-
- return label;
+ }).save();
}
module.exports = {
- getNoteLabelMap,
getNoteIdWithLabel,
getNotesWithLabel,
getNoteWithLabel,
diff --git a/src/services/notes.js b/src/services/notes.js
index e602e064c8..c4b2a3d80b 100644
--- a/src/services/notes.js
+++ b/src/services/notes.js
@@ -44,24 +44,20 @@ async function createNewNote(parentNoteId, noteData) {
noteData.mime = noteData.mime || parent.mime;
}
- const note = new Note({
+ const note = await new Note({
title: noteData.title,
content: noteData.content || '',
isProtected: noteData.isProtected,
type: noteData.type || 'text',
mime: noteData.mime || 'text/html'
- });
+ }).save();
- await note.save();
-
- const branch = new Branch({
+ const branch = await new Branch({
noteId: note.noteId,
parentNoteId: parentNoteId,
notePosition: newNotePos,
isExpanded: 0
- });
-
- await branch.save();
+ }).save();
return {
note,
@@ -141,10 +137,10 @@ async function saveNoteImages(note) {
const existingNoteImage = existingNoteImages.find(ni => ni.imageId === imageId);
if (!existingNoteImage) {
- await (new NoteImage({
+ await new NoteImage({
noteId: note.noteId,
imageId: imageId
- })).save();
+ }).save();
}
// else we don't need to do anything
@@ -179,7 +175,7 @@ async function saveNoteRevision(note) {
&& !existingnoteRevisionId
&& msSinceDateCreated >= noteRevisionSnapshotTimeInterval * 1000) {
- await (new NoteRevision({
+ await new NoteRevision({
noteId: note.noteId,
// title and text should be decrypted now
title: note.title,
@@ -187,7 +183,7 @@ async function saveNoteRevision(note) {
isProtected: 0, // will be fixed in the protectNoteRevisions() call
dateModifiedFrom: note.dateModified,
dateModifiedTo: dateUtils.nowDate()
- })).save();
+ }).save();
}
}
diff --git a/src/services/options.js b/src/services/options.js
index 7676e7f013..d4435b4735 100644
--- a/src/services/options.js
+++ b/src/services/options.js
@@ -4,12 +4,8 @@ const dateUtils = require('./date_utils');
const syncTableService = require('./sync_table');
const appInfo = require('./app_info');
-async function getOptionOrNull(name) {
- return await sql.getRowOrNull("SELECT value FROM options WHERE name = ?", [name]);
-}
-
async function getOption(name) {
- const row = await getOptionOrNull(name);
+ const row = await await sql.getRowOrNull("SELECT value FROM options WHERE name = ?", [name]);
if (!row) {
throw new Error("Option " + name + " doesn't exist");
@@ -69,8 +65,6 @@ async function initOptions(startNotePath) {
module.exports = {
getOption,
- getOptionOrNull,
setOption,
- initOptions,
- createOption
+ initOptions
};
\ No newline at end of file
diff --git a/src/services/script_context.js b/src/services/script_context.js
index fb16b113c9..0236f8efea 100644
--- a/src/services/script_context.js
+++ b/src/services/script_context.js
@@ -48,21 +48,11 @@ function ScriptApi(startNote, currentNote) {
this.getEntity = repository.getEntity;
this.getEntities = repository.getEntities;
- this.getNotesWithLabel = async function (labelName, labelValue) {
- return await labelService.getNotesWithLabel(labelName, labelValue);
- };
-
- this.getNoteWithLabel = async function (labelName, labelValue) {
- const notes = await this.getNotesWithLabel(labelName, labelValue);
-
- return notes.length > 0 ? notes[0] : null;
- };
-
- this.createNote = async function(parentNoteId, title, content = "", extraOptions = {}) {
- return await noteService.createNote(parentNoteId, title, content, extraOptions);
- };
-
this.createLabel = labelService.createLabel;
+ this.getNotesWithLabel = labelService.getNotesWithLabel;
+ this.getNoteWithLabel = labelService.getNoteWithLabel;
+
+ this.createNote = noteService.createNote;
this.log = message => log.info(`Script ${currentNote.noteId}: ${message}`);