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
added document_id for sync identification
azivner724f4b43b798
diff --git a/app.js b/app.jsindex 520f26e72f..fa50b982b0 100644--- a/app.js+++ b/app.js@@ -95,6 +95,11 @@ app.use((req, res, next) => {// error handlerapp.use((err, req, res, next) => {log.error(err.message);++ res.status(err.status || 500);+ res.send({+ message: err.message+ });});// triggers sync timerdiff --git a/migrations/0017__document_id.sql b/migrations/0017__document_id.sqlnew file mode 100644index 0000000000..9bced5a9b7--- /dev/null+++ b/migrations/0017__document_id.sql@@ -0,0 +1 @@+INSERT INTO options (opt_name, opt_value) VALUES ('document_id', '');\ No newline at end of filediff --git a/services/migration.js b/services/migration.jsindex db3f565496..86ebb49db9 100644--- a/services/migration.js+++ b/services/migration.js@@ -3,7 +3,7 @@ const sql = require('./sql');const fs = require('fs-extra');const log = require('./log');-const APP_DB_VERSION = 16;+const APP_DB_VERSION = 17;const MIGRATIONS_DIR = "./migrations";async function migrate() {diff --git a/services/sql.js b/services/sql.jsindex 0b8d0c07d4..e1870f2361 100644--- a/services/sql.js+++ b/services/sql.js@@ -34,9 +34,14 @@ async function rollback() {}async function getOption(optName) {- const row = await getSingleResult("SELECT opt_value FROM options WHERE opt_name = ?", [optName]);+ try {+ const row = await getSingleResult("SELECT opt_value FROM options WHERE opt_name = ?", [optName]);- return row['opt_value'];+ return row['opt_value'];+ }+ catch (e) {+ throw new Error("Option " + optName + " doesn't exist");+ }}async function setOption(optName, optValue) {diff --git a/services/sync.js b/services/sync.jsindex c54efc49ec..97c9d686c1 100644--- a/services/sync.js+++ b/services/sync.js@@ -112,6 +112,7 @@ async function sync() {async function getChangedSince(since) {return {+ 'documentId': await getDocumentId(),'syncTimestamp': utils.nowTimestamp(),'tree': await sql.getResults("select * from notes_tree where date_modified >= ?", [since]),'notes': await sql.getFlattenedResults('note_id', "select note_id from notes where date_modified >= ?", [since]),@@ -167,6 +168,22 @@ async function putNote(note) {log.info("Update/sync note " + note.detail.note_id);}+let documentIdCache = null;++async function getDocumentId() {+ if (!documentIdCache) {+ documentIdCache = await sql.getOption('document_id');++ if (!documentIdCache) {+ documentIdCache = utils.randomString(16);++ await sql.setOption('document_id', documentIdCache);+ }+ }++ return documentIdCache;+}+if (SYNC_SERVER) {log.info("Setting up sync");diff --git a/services/utils.js b/services/utils.jsindex 1c3ef3253d..ba4548e779 100644--- a/services/utils.js+++ b/services/utils.js@@ -6,7 +6,7 @@ function newNoteId() {const ALPHA_NUMERIC = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';-function randomString(length, chars) {+function randomString(length) {let result = '';for (let i = length; i > 0; --i) {