trilium/services/sync_table.js

110 lines
3.6 KiB
JavaScript
Raw Normal View History

const sql = require('./sql');
2018-01-29 08:30:14 +08:00
const sourceId = require('./sourceId');
const utils = require('./utils');
const sync_setup = require('./sync_setup');
const log = require('./log');
async function addNoteSync(noteId, sourceId) {
await addEntitySync("notes", noteId, sourceId)
}
async function addNoteTreeSync(noteTreeId, sourceId) {
await addEntitySync("note_tree", noteTreeId, sourceId)
}
async function addNoteReorderingSync(parentNoteTreeId, sourceId) {
await addEntitySync("notes_reordering", parentNoteTreeId, sourceId)
}
async function addNoteHistorySync(noteRevisionId, sourceId) {
await addEntitySync("note_revisions", noteRevisionId, sourceId);
}
2018-01-29 08:30:14 +08:00
async function addOptionsSync(name, sourceId) {
await addEntitySync("options", name, sourceId);
}
async function addRecentNoteSync(noteTreeId, sourceId) {
await addEntitySync("recent_notes", noteTreeId, sourceId);
}
2018-01-07 04:56:00 +08:00
async function addImageSync(imageId, sourceId) {
await addEntitySync("images", imageId, sourceId);
}
2018-01-07 11:38:53 +08:00
async function addNoteImageSync(noteImageId, sourceId) {
await addEntitySync("note_images", noteImageId, sourceId);
2018-01-07 10:49:02 +08:00
}
async function addAttributeSync(attributeId, sourceId) {
await addEntitySync("attributes", attributeId, sourceId);
}
async function addEntitySync(entityName, entityId, sourceId) {
await sql.replace("sync", {
2018-01-29 08:30:14 +08:00
entityName: entityName,
entityId: entityId,
syncDate: utils.nowDate(),
sourceId: sourceId || sourceId.getCurrentSourceId()
});
if (!sync_setup.isSyncSetup) {
// this is because the "server" instances shouldn't have outstanding pushes
// useful when you fork the DB for new "client" instance, it won't try to sync the whole DB
2018-01-29 08:30:14 +08:00
await sql.execute("UPDATE options SET value = (SELECT MAX(id) FROM sync) WHERE name IN('last_synced_push', 'last_synced_pull')");
}
}
async function cleanupSyncRowsForMissingEntities(entityName, entityKey) {
await sql.execute(`
DELETE
FROM sync
2018-01-29 08:30:14 +08:00
WHERE sync.entityName = '${entityName}'
AND sync.entityId NOT IN (SELECT ${entityKey} FROM ${entityName})`);
}
async function fillSyncRows(entityName, entityKey) {
await cleanupSyncRowsForMissingEntities(entityName, entityKey);
const entityIds = await sql.getFirstColumn(`SELECT ${entityKey} FROM ${entityName}`);
for (const entityId of entityIds) {
2018-01-29 08:30:14 +08:00
const existingRows = await sql.getFirstValue("SELECT COUNT(id) FROM sync WHERE entityName = ? AND entityId = ?", [entityName, entityId]);
// we don't want to replace existing entities (which would effectively cause full resync)
if (existingRows === 0) {
log.info(`Creating missing sync record for ${entityName} ${entityId}`);
await sql.insert("sync", {
2018-01-29 08:30:14 +08:00
entityName: entityName,
entityId: entityId,
sourceId: "SYNC_FILL",
syncDate: utils.nowDate()
});
}
}
}
async function fillAllSyncRows() {
2018-01-29 08:30:14 +08:00
await fillSyncRows("notes", "noteId");
await fillSyncRows("note_tree", "noteTreeId");
await fillSyncRows("note_revisions", "noteRevisionId");
2018-01-29 08:30:14 +08:00
await fillSyncRows("recent_notes", "noteTreeId");
await fillSyncRows("images", "imageId");
await fillSyncRows("note_images", "noteImageId");
2018-01-29 08:30:14 +08:00
await fillSyncRows("attributes", "attributeId");
}
module.exports = {
addNoteSync,
addNoteTreeSync,
addNoteReorderingSync,
addNoteHistorySync,
addOptionsSync,
addRecentNoteSync,
2018-01-07 04:56:00 +08:00
addImageSync,
2018-01-07 10:49:02 +08:00
addNoteImageSync,
addAttributeSync,
cleanupSyncRowsForMissingEntities,
fillAllSyncRows
};