mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
server-ts: Convert routes/api/sync
This commit is contained in:
parent
6bbb1f8404
commit
7a98718e64
@ -1,16 +1,19 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const syncService = require('../../services/sync');
|
import syncService = require('../../services/sync');
|
||||||
const syncUpdateService = require('../../services/sync_update');
|
import syncUpdateService = require('../../services/sync_update');
|
||||||
const entityChangesService = require('../../services/entity_changes');
|
import entityChangesService = require('../../services/entity_changes');
|
||||||
const sql = require('../../services/sql');
|
import sql = require('../../services/sql');
|
||||||
const sqlInit = require('../../services/sql_init');
|
import sqlInit = require('../../services/sql_init');
|
||||||
const optionService = require('../../services/options');
|
import optionService = require('../../services/options');
|
||||||
const contentHashService = require('../../services/content_hash');
|
import contentHashService = require('../../services/content_hash');
|
||||||
const log = require('../../services/log');
|
import log = require('../../services/log');
|
||||||
const syncOptions = require('../../services/sync_options');
|
import syncOptions = require('../../services/sync_options');
|
||||||
const utils = require('../../services/utils');
|
import utils = require('../../services/utils');
|
||||||
const ws = require('../../services/ws');
|
import ws = require('../../services/ws');
|
||||||
|
import { Request } from 'express';
|
||||||
|
import { EntityChange, EntityChangeRecord } from '../../services/entity_changes_interface';
|
||||||
|
import ValidationError = require('../../errors/validation_error');
|
||||||
|
|
||||||
async function testSync() {
|
async function testSync() {
|
||||||
try {
|
try {
|
||||||
@ -26,7 +29,7 @@ async function testSync() {
|
|||||||
|
|
||||||
return { success: true, message: "Sync server handshake has been successful, sync has been started." };
|
return { success: true, message: "Sync server handshake has been successful, sync has been started." };
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e: any) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
message: e.message
|
message: e.message
|
||||||
@ -82,15 +85,19 @@ function forceFullSync() {
|
|||||||
syncService.sync();
|
syncService.sync();
|
||||||
}
|
}
|
||||||
|
|
||||||
function getChanged(req) {
|
function getChanged(req: Request) {
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
|
|
||||||
let lastEntityChangeId = parseInt(req.query.lastEntityChangeId);
|
if (typeof req.query.lastEntityChangeId !== "string") {
|
||||||
|
throw new ValidationError("Missing or invalid last entity change ID.");
|
||||||
|
}
|
||||||
|
|
||||||
|
let lastEntityChangeId: number | null | undefined = parseInt(req.query.lastEntityChangeId);
|
||||||
const clientInstanceId = req.query.instanceId;
|
const clientInstanceId = req.query.instanceId;
|
||||||
let filteredEntityChanges = [];
|
let filteredEntityChanges: EntityChange[] = [];
|
||||||
|
|
||||||
do {
|
do {
|
||||||
const entityChanges = sql.getRows(`
|
const entityChanges: EntityChange[] = sql.getRows<EntityChange>(`
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM entity_changes
|
FROM entity_changes
|
||||||
WHERE isSynced = 1
|
WHERE isSynced = 1
|
||||||
@ -129,16 +136,22 @@ function getChanged(req) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const partialRequests = {};
|
const partialRequests: Record<string, {
|
||||||
|
createdAt: number,
|
||||||
|
payload: string
|
||||||
|
}> = {};
|
||||||
|
|
||||||
function update(req) {
|
function update(req: Request) {
|
||||||
let { body } = req;
|
let { body } = req;
|
||||||
|
|
||||||
const pageCount = parseInt(req.get('pageCount'));
|
const pageCount = parseInt(req.get('pageCount') as string);
|
||||||
const pageIndex = parseInt(req.get('pageIndex'));
|
const pageIndex = parseInt(req.get('pageIndex') as string);
|
||||||
|
|
||||||
if (pageCount !== 1) {
|
if (pageCount !== 1) {
|
||||||
const requestId = req.get('requestId');
|
const requestId = req.get('requestId');
|
||||||
|
if (!requestId) {
|
||||||
|
throw new Error("Missing request ID.");
|
||||||
|
}
|
||||||
|
|
||||||
if (pageIndex === 0) {
|
if (pageIndex === 0) {
|
||||||
partialRequests[requestId] = {
|
partialRequests[requestId] = {
|
||||||
@ -185,7 +198,7 @@ function syncFinished() {
|
|||||||
sqlInit.setDbAsInitialized();
|
sqlInit.setDbAsInitialized();
|
||||||
}
|
}
|
||||||
|
|
||||||
function queueSector(req) {
|
function queueSector(req: Request) {
|
||||||
const entityName = utils.sanitizeSqlIdentifier(req.params.entityName);
|
const entityName = utils.sanitizeSqlIdentifier(req.params.entityName);
|
||||||
const sector = utils.sanitizeSqlIdentifier(req.params.sector);
|
const sector = utils.sanitizeSqlIdentifier(req.params.sector);
|
||||||
|
|
||||||
@ -196,7 +209,7 @@ function checkEntityChanges() {
|
|||||||
require('../../services/consistency_checks').runEntityChangesChecks();
|
require('../../services/consistency_checks').runEntityChangesChecks();
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
export = {
|
||||||
testSync,
|
testSync,
|
||||||
checkSync,
|
checkSync,
|
||||||
syncNow,
|
syncNow,
|
Loading…
x
Reference in New Issue
Block a user