Skip to content

Build your personal knowledge base with Trilium Notes

converted option operations to repository

azivnercd45bcfd03d8
diff --git a/src/entities/entity_constructor.js b/src/entities/entity_constructor.js
index 7fa3158daa..5db7c09635 100644
--- a/src/entities/entity_constructor.js
+++ b/src/entities/entity_constructor.js
@@ -6,6 +6,7 @@ const Branch = require('../entities/branch');
const Label = require('../entities/label');
const RecentNote = require('../entities/recent_note');
const ApiToken = require('../entities/api_token');
+const Option = require('../entities/option');
const repository = require('../services/repository');
function createEntityFromRow(row) {
@@ -35,6 +36,9 @@ function createEntityFromRow(row) {
else if (row.noteId) {
entity = new Note(row);
}
+ else if (row.name) {
+ entity = new Option(row);
+ }
else {
throw new Error('Unknown entity type for row: ' + JSON.stringify(row));
}
diff --git a/src/entities/option.js b/src/entities/option.js
index 68b3dc2123..994fa77055 100644
--- a/src/entities/option.js
+++ b/src/entities/option.js
@@ -1,11 +1,18 @@
"use strict";
const Entity = require('./entity');
+const dateUtils = require('../services/date_utils');
class Option extends Entity {
static get tableName() { return "options"; }
static get primaryKeyName() { return "name"; }
static get syncedProperties() { return ["name", "value"]; }
+
+ beforeSaving() {
+ super.beforeSaving();
+
+ this.dateModified = dateUtils.nowDate();
+ }
}
module.exports = Option;
\ No newline at end of file
diff --git a/src/services/options.js b/src/services/options.js
index d4435b4735..7841e39558 100644
--- a/src/services/options.js
+++ b/src/services/options.js
@@ -1,41 +1,42 @@
-const sql = require('./sql');
+const repository = require('./repository');
const utils = require('./utils');
const dateUtils = require('./date_utils');
const syncTableService = require('./sync_table');
const appInfo = require('./app_info');
+const Option = require('../entities/option');
async function getOption(name) {
- const row = await await sql.getRowOrNull("SELECT value FROM options WHERE name = ?", [name]);
+ const option = await repository.getOption(name);
- if (!row) {
+ if (!option) {
throw new Error("Option " + name + " doesn't exist");
}
- return row.value;
+ return option.value;
}
async function setOption(name, value) {
- const opt = await sql.getRow("SELECT * FROM options WHERE name = ?", [name]);
+ const option = await repository.getOption(name);
- if (!opt) {
+ if (!option) {
throw new Error(`Option ${name} doesn't exist`);
}
- if (opt.isSynced) {
+ if (option.isSynced) {
await syncTableService.addOptionsSync(name);
}
- await sql.execute("UPDATE options SET value = ?, dateModified = ? WHERE name = ?",
- [value, dateUtils.nowDate(), name]);
+ option.value = value;
+
+ await option.save();
}
async function createOption(name, value, isSynced) {
- await sql.insert("options", {
+ await new Option({
name: name,
value: value,
- isSynced: isSynced,
- dateModified: dateUtils.nowDate()
- });
+ isSynced: isSynced
+ }).save();
if (isSynced) {
await syncTableService.addOptionsSync(name);
diff --git a/src/services/repository.js b/src/services/repository.js
index 7057153273..e0287ad874 100644
--- a/src/services/repository.js
+++ b/src/services/repository.js
@@ -41,6 +41,10 @@ async function getLabel(labelId) {
return await getEntity("SELECT * FROM labels WHERE labelId = ?", [labelId]);
}
+async function getOption(name) {
+ return await getEntity("SELECT * FROM options WHERE name = ?", [name]);
+}
+
async function updateEntity(entity) {
if (entity.beforeSaving) {
await entity.beforeSaving();
@@ -66,6 +70,7 @@ module.exports = {
getBranch,
getImage,
getLabel,
+ getOption,
updateEntity,
setEntityConstructor
};
\ No newline at end of file