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
sync cleanup
azivner72905a9854c4
diff --git a/routes/api/sync.js b/routes/api/sync.jsindex 1f068a4afb..cea4e96652 100644--- a/routes/api/sync.js+++ b/routes/api/sync.js@@ -19,9 +19,7 @@ router.get('/changed', auth.checkApiAuth, async (req, res, next) => {const lastSyncId = parseInt(req.query.lastSyncId);const sourceId = req.query.sourceId;- const result = await sync.getChanged(lastSyncId, sourceId);-- res.send(result);+ res.send(await sql.getResults("SELECT * FROM sync WHERE id > ? AND source_id != ?", [lastSyncId, sourceId]));});router.get('/notes/:noteId', auth.checkApiAuth, async (req, res, next) => {diff --git a/services/sync.js b/services/sync.jsindex 6dd9bc3e3f..52f62710d8 100644--- a/services/sync.js+++ b/services/sync.js@@ -48,8 +48,6 @@ async function pullSync(cookieJar, syncLog) {logSyncError("Can't pull " + sync.entity_name + " " + sync.entity_id, e, syncLog);}- // TODO: ideally this should be in transaction-if (sync.entity_name === 'notes') {await updateNote(resp.entity, resp.links, sync.source_id, syncLog);}@@ -69,7 +67,7 @@ async function pullSync(cookieJar, syncLog) {logSync("Finished pull");}-async function syncEntity(entity, entityName, cookieJar, syncLog) {+async function pushEntity(entity, entityName, cookieJar, syncLog) {try {const payload = {sourceId: SOURCE_ID,@@ -108,28 +106,26 @@ async function pushSync(cookieJar, syncLog) {break;}- await sql.doInTransaction(async () => {- let entity;+ let entity;- if (sync.entity_name === 'notes') {- entity = await sql.getSingleResult('SELECT * FROM notes WHERE note_id = ?', [sync.entity_id]);- }- else if (sync.entity_name === 'notes_tree') {- entity = await sql.getSingleResult('SELECT * FROM notes_tree WHERE note_id = ?', [sync.entity_id]);- }- else if (sync.entity_name === 'notes_history') {- entity = await sql.getSingleResult('SELECT * FROM notes_history WHERE note_history_id = ?', [sync.entity_id]);- }- else {- logSyncError("Unrecognized entity type " + sync.entity_name, null, syncLog);- }+ if (sync.entity_name === 'notes') {+ entity = await sql.getSingleResult('SELECT * FROM notes WHERE note_id = ?', [sync.entity_id]);+ }+ else if (sync.entity_name === 'notes_tree') {+ entity = await sql.getSingleResult('SELECT * FROM notes_tree WHERE note_id = ?', [sync.entity_id]);+ }+ else if (sync.entity_name === 'notes_history') {+ entity = await sql.getSingleResult('SELECT * FROM notes_history WHERE note_history_id = ?', [sync.entity_id]);+ }+ else {+ logSyncError("Unrecognized entity type " + sync.entity_name, null, syncLog);+ }- await syncEntity(entity, sync.entity_name, cookieJar, syncLog);+ await pushEntity(entity, sync.entity_name, cookieJar, syncLog);- lastSyncedPush = sync.id;+ lastSyncedPush = sync.id;- await sql.setOption('last_synced_push', lastSyncedPush);- });+ await sql.setOption('last_synced_push', lastSyncedPush);}}@@ -220,12 +216,6 @@ function logSyncError(message, e, syncLog) {throw new Error(completeMessage);}-async function getChanged(lastSyncId, sourceId) {- console.log([lastSyncId, sourceId]);-- return await sql.getResults("SELECT * FROM sync WHERE id > ? AND source_id != ?", [lastSyncId, sourceId]);-}-async function updateNote(entity, links, sourceId, syncLog) {const origNote = await sql.getSingleResult("select * from notes where note_id = ?", [entity.note_id]);@@ -301,7 +291,6 @@ else {module.exports = {sync,- getChanged,updateNote,updateNoteTree,updateNoteHistory