mirror of
https://github.com/zadam/trilium.git
synced 2025-11-02 11:26:15 +01:00
separate sync for pull (implemented) and push (not yet)
This commit is contained in:
147
services/sync.js
147
services/sync.js
@@ -10,78 +10,92 @@ const SYNC_SERVER = 'http://localhost:3000';
|
||||
|
||||
let syncInProgress = false;
|
||||
|
||||
async function sync() {
|
||||
try {
|
||||
syncInProgress = true;
|
||||
async function pullSync() {
|
||||
const lastSynced = parseInt(await sql.getOption('last_synced_pull'));
|
||||
|
||||
const resp = await rp({
|
||||
uri: SYNC_SERVER + '/api/sync/changed/' + lastSynced,
|
||||
headers: {
|
||||
auth: 'sync'
|
||||
},
|
||||
json: true
|
||||
});
|
||||
|
||||
try {
|
||||
await sql.beginTransaction();
|
||||
|
||||
for (const treeItem of resp.tree) {
|
||||
delete treeItem['id'];
|
||||
|
||||
await sql.insert("notes_tree", treeItem, true);
|
||||
|
||||
log.info("Syncing notes_tree " + treeItem.note_id);
|
||||
}
|
||||
|
||||
for (const audit of resp.audit_log) {
|
||||
delete audit['id'];
|
||||
|
||||
await sql.insert("audit_log", audit, true);
|
||||
|
||||
log.info("Syncing audit_log for noteId=" + audit.note_id);
|
||||
}
|
||||
|
||||
for (const noteId of resp.notes) {
|
||||
const note = await rp({
|
||||
uri: SYNC_SERVER + "/api/sync/note/" + noteId + "/" + lastSynced,
|
||||
headers: {
|
||||
auth: 'sync'
|
||||
},
|
||||
json: true
|
||||
});
|
||||
|
||||
console.log(noteId);
|
||||
|
||||
await sql.insert("notes", note.detail, true);
|
||||
|
||||
await sql.remove("images", noteId);
|
||||
|
||||
for (const image of note.images) {
|
||||
await sql.insert("images", image);
|
||||
}
|
||||
|
||||
for (const history of note.history) {
|
||||
delete history['id'];
|
||||
|
||||
await sql.insert("notes_history", history);
|
||||
}
|
||||
}
|
||||
|
||||
await sql.setOption('last_synced_pull', syncTimestamp);
|
||||
|
||||
await sql.commit();
|
||||
}
|
||||
catch (e) {
|
||||
await sql.rollback();
|
||||
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
async function pushSync() {
|
||||
|
||||
}
|
||||
|
||||
async function sync() {
|
||||
if (syncInProgress) {
|
||||
return;
|
||||
}
|
||||
|
||||
syncInProgress = true;
|
||||
|
||||
try {
|
||||
if (!await migration.isDbUpToDate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const lastSynced = parseInt(await sql.getOption('last_synced'));
|
||||
await pushSync();
|
||||
|
||||
const resp = await rp({
|
||||
uri: SYNC_SERVER + '/api/sync/changed/' + lastSynced,
|
||||
headers: {
|
||||
auth: 'sync'
|
||||
},
|
||||
json: true
|
||||
});
|
||||
|
||||
try {
|
||||
await sql.beginTransaction();
|
||||
|
||||
for (const treeItem of resp.tree) {
|
||||
delete treeItem['id'];
|
||||
|
||||
await sql.insert("notes_tree", treeItem, true);
|
||||
|
||||
log.info("Syncing notes_tree " + treeItem.note_id);
|
||||
}
|
||||
|
||||
for (const audit of resp.audit_log) {
|
||||
delete audit['id'];
|
||||
|
||||
await sql.insert("audit_log", audit, true);
|
||||
|
||||
log.info("Syncing audit_log for noteId=" + audit.note_id);
|
||||
}
|
||||
|
||||
for (const noteId of resp.notes) {
|
||||
const note = await rp({
|
||||
uri: SYNC_SERVER + "/api/sync/note/" + noteId + "/" + lastSynced,
|
||||
headers: {
|
||||
auth: 'sync'
|
||||
},
|
||||
json: true
|
||||
});
|
||||
|
||||
console.log(noteId);
|
||||
|
||||
await sql.insert("notes", note.detail, true);
|
||||
|
||||
await sql.remove("images", noteId);
|
||||
|
||||
for (const image of note.images) {
|
||||
await sql.insert("images", image);
|
||||
}
|
||||
|
||||
for (const history of note.history) {
|
||||
delete history['id'];
|
||||
|
||||
await sql.insert("notes_history", history);
|
||||
}
|
||||
}
|
||||
|
||||
await sql.setOption('last_synced', syncTimestamp);
|
||||
|
||||
await sql.commit();
|
||||
}
|
||||
catch (e) {
|
||||
await sql.rollback();
|
||||
|
||||
throw e;
|
||||
}
|
||||
await pullSync();
|
||||
}
|
||||
catch (e) {
|
||||
log.error("sync failed: " + e.stack);
|
||||
@@ -93,4 +107,5 @@ async function sync() {
|
||||
|
||||
setInterval(sync, 60000);
|
||||
|
||||
// kickoff initial sync immediately
|
||||
setTimeout(sync, 1000);
|
||||
Reference in New Issue
Block a user