Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
smaller refactorings continued
azivner15d951b04e6c
diff --git a/src/entities/label.js b/src/entities/label.jsindex da6dab27a4..5c346c511a 100644--- a/src/entities/label.js+++ b/src/entities/label.js@@ -3,6 +3,7 @@const Entity = require('./entity');const repository = require('../services/repository');const utils = require('../services/utils');+const sql = require('../services/sql');class Label extends Entity {static get tableName() { return "labels"; }@@ -12,7 +13,24 @@ class Label extends Entity {return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]);}- beforeSaving() {+ async beforeSaving() {+ if (!this.labelId) {+ this.labelId = utils.newLabelId();+ }++ if (this.value) {+ // null value isn't allowed+ this.value = "";+ }++ if (this.position === undefined) {+ this.position = 1 + await sql.getValue(`SELECT COALESCE(MAX(position), 0) FROM labels WHERE noteId = ?`, [noteId]);+ }++ if (!this.isDeleted) {+ this.isDeleted = false;+ }+if (!this.dateCreated) {this.dateCreated = utils.nowDate();}diff --git a/src/services/labels.js b/src/services/labels.jsindex 6612948590..407e94e657 100644--- a/src/services/labels.js+++ b/src/services/labels.js@@ -2,7 +2,6 @@const sql = require('./sql');const utils = require('./utils');-const sync_table = require('./sync_table');const repository = require('./repository');const Label = require('../entities/label');@@ -58,21 +57,15 @@ async function getNoteIdsWithLabel(name) {}async function createLabel(noteId, name, value = "") {- if (value === null || value === undefined) {- value = "";- }-- const labelId = utils.newLabelId();- const position = 1 + await sql.getValue(`SELECT COALESCE(MAX(position), 0) FROM labels WHERE noteId = ?`, [noteId]);-- await (new Label({- labelId: labelId,+ const label = new Label({noteId: noteId,name: name,- value: value,- position: position,- isDeleted: false- })).save();+ value: value+ });++ await label.save();++ return label;}module.exports = {diff --git a/src/services/repository.js b/src/services/repository.jsindex e20b05979c..504e6c6488 100644--- a/src/services/repository.js+++ b/src/services/repository.js@@ -43,18 +43,20 @@ async function getLabel(labelId) {async function updateEntity(entity) {if (entity.beforeSaving) {- entity.beforeSaving();+ await entity.beforeSaving();}const clone = Object.assign({}, entity);delete clone.jsonContent;- await sql.replace(entity.constructor.tableName, clone);+ await sql.doInTransaction(async () => {+ await sql.replace(entity.constructor.tableName, clone);- const primaryKey = entity[entity.constructor.primaryKeyName];+ const primaryKey = entity[entity.constructor.primaryKeyName];- await sync_table.addEntitySync(entity.constructor.tableName, primaryKey);+ await sync_table.addEntitySync(entity.constructor.tableName, primaryKey);+ });}module.exports = {diff --git a/src/services/script_context.js b/src/services/script_context.jsindex 963fae670d..cf38410b23 100644--- a/src/services/script_context.js+++ b/src/services/script_context.js@@ -40,9 +40,12 @@ function ScriptApi(startNote, currentNote) {this.getInstanceName = () => config.General ? config.General.instanceName : null;- this.getNoteById = async function(noteId) {- return await repository.getNote(noteId);- };+ this.getNote = repository.getNote;+ this.getBranch = repository.getBranch;+ this.getLabel = repository.getLabel;+ this.getImage = repository.getImage;+ this.getEntity = repository.getEntity;+ this.getEntities = repository.getEntities;this.getNotesWithLabel = async function (labelName, labelValue) {return await labels.getNotesWithLabel(labelName, labelValue);@@ -60,8 +63,6 @@ function ScriptApi(startNote, currentNote) {this.createLabel = labels.createLabel;- this.updateEntity = repository.updateEntity;-this.log = message => log.info(`Script ${currentNote.noteId}: ${message}`);this.getRootCalendarNoteId = date_notes.getRootCalendarNoteId;diff --git a/src/services/source_id.js b/src/services/source_id.jsindex f5b89f4f18..4862ddf04d 100644--- a/src/services/source_id.js+++ b/src/services/source_id.js@@ -4,11 +4,9 @@ const sql = require('./sql');const cls = require('./cls');async function saveSourceId(sourceId) {- await sql.doInTransaction(async () => {- await sql.insert("source_ids", {- sourceId: sourceId,- dateCreated: utils.nowDate()- });+ await sql.insert("source_ids", {+ sourceId: sourceId,+ dateCreated: utils.nowDate()});await refreshSourceIds();diff --git a/src/services/sync.js b/src/services/sync.jsindex 8c7fc9d1d3..239beb3674 100644--- a/src/services/sync.js+++ b/src/services/sync.js@@ -95,9 +95,7 @@ async function getLastSyncedPull() {}async function setLastSyncedPull(syncId) {- await sql.doInTransaction(async () => {- await options.setOption('last_synced_pull', syncId);- });+ await options.setOption('last_synced_pull', syncId);}async function pullSync(syncContext) {@@ -168,9 +166,7 @@ async function getLastSyncedPush() {}async function setLastSyncedPush(lastSyncedPush) {- await sql.doInTransaction(async () => {- await options.setOption('last_synced_push', lastSyncedPush);- });+ await options.setOption('last_synced_push', lastSyncedPush);}async function pushSync(syncContext) {