trilium/src/services/sync_table.js

128 lines
3.9 KiB
JavaScript
Raw Normal View History

const sql = require('./sql');
const sourceIdService = require('./source_id');
2018-04-03 08:46:46 +08:00
const dateUtils = require('./date_utils');
2018-07-25 15:46:57 +08:00
const syncOptions = require('./sync_options');
const log = require('./log');
const cls = require('./cls');
const eventService = require('./events');
async function addNoteSync(noteId, sourceId) {
await addEntitySync("notes", noteId, sourceId)
}
2018-03-25 09:39:15 +08:00
async function addBranchSync(branchId, sourceId) {
await addEntitySync("branches", branchId, sourceId)
}
2018-01-29 10:57:46 +08:00
async function addNoteReorderingSync(parentNoteId, sourceId) {
await addEntitySync("note_reordering", parentNoteId, sourceId)
}
async function addNoteRevisionSync(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);
}
2018-03-25 09:39:15 +08:00
async function addRecentNoteSync(branchId, sourceId) {
await addEntitySync("recent_notes", branchId, 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 addLabelSync(labelId, sourceId) {
await addEntitySync("labels", labelId, sourceId);
}
async function addRelationSync(relationId, sourceId) {
await addEntitySync("relations", relationId, sourceId);
}
2018-02-11 13:18:59 +08:00
async function addApiTokenSync(apiTokenId, sourceId) {
await addEntitySync("api_tokens", apiTokenId, sourceId);
}
async function addEntitySync(entityName, entityId, sourceId) {
await sql.replace("sync", {
2018-01-29 08:30:14 +08:00
entityName: entityName,
entityId: entityId,
2018-04-03 08:46:46 +08:00
syncDate: dateUtils.nowDate(),
sourceId: sourceId || cls.getSourceId() || sourceIdService.getCurrentSourceId()
});
eventService.emit(eventService.ENTITY_CHANGED, {
entityName,
entityId
});
}
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, condition = '') {
await cleanupSyncRowsForMissingEntities(entityName, entityKey);
const entityIds = await sql.getColumn(`SELECT ${entityKey} FROM ${entityName}`
+ (condition ? ` WHERE ${condition}` : ''));
for (const entityId of entityIds) {
const existingRows = await sql.getValue("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",
2018-04-03 08:46:46 +08:00
syncDate: dateUtils.nowDate()
});
}
}
}
async function fillAllSyncRows() {
2018-04-08 10:25:28 +08:00
await sql.execute("DELETE FROM sync");
2018-01-29 08:30:14 +08:00
await fillSyncRows("notes", "noteId");
2018-03-25 09:39:15 +08:00
await fillSyncRows("branches", "branchId");
await fillSyncRows("note_revisions", "noteRevisionId");
2018-03-25 09:39:15 +08:00
await fillSyncRows("recent_notes", "branchId");
2018-01-29 08:30:14 +08:00
await fillSyncRows("images", "imageId");
await fillSyncRows("note_images", "noteImageId");
await fillSyncRows("labels", "labelId");
await fillSyncRows("relations", "relationId");
2018-02-11 13:18:59 +08:00
await fillSyncRows("api_tokens", "apiTokenId");
await fillSyncRows("options", "name", 'isSynced = 1');
}
module.exports = {
addNoteSync,
2018-03-25 09:39:15 +08:00
addBranchSync,
addNoteReorderingSync,
addNoteRevisionSync,
addOptionsSync,
addRecentNoteSync,
2018-01-07 04:56:00 +08:00
addImageSync,
2018-01-07 10:49:02 +08:00
addNoteImageSync,
addLabelSync,
addRelationSync,
2018-02-11 13:18:59 +08:00
addApiTokenSync,
2018-01-31 09:12:19 +08:00
addEntitySync,
cleanupSyncRowsForMissingEntities,
fillAllSyncRows
};