From f1e0d5558c1af7d94fe4e3d5b0adfd70d55853c8 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Tue, 6 Jan 2026 16:16:54 +0200 Subject: [PATCH] chore(core): integrate erase --- apps/server/src/services/cls.ts | 14 ++------- packages/trilium-core/src/services/context.ts | 11 ++++++- .../trilium-core}/src/services/erase.ts | 31 ++++++++++++------- 3 files changed, 31 insertions(+), 25 deletions(-) rename {apps/server => packages/trilium-core}/src/services/erase.ts (88%) diff --git a/apps/server/src/services/cls.ts b/apps/server/src/services/cls.ts index f5bb25ef1..a9c91ede9 100644 --- a/apps/server/src/services/cls.ts +++ b/apps/server/src/services/cls.ts @@ -1,22 +1,10 @@ import type { EntityChange } from "@triliumnext/commons"; import { cls } from "@triliumnext/core"; -type Callback = (...args: any[]) => any; - function init(callback: () => T) { return cls.getContext().init(callback); } -function wrap(callback: Callback) { - return () => { - try { - init(callback); - } catch (e: any) { - console.log(`Error occurred: ${e.message}: ${e.stack}`); - } - }; -} - function getHoistedNoteId() { return cls.getHoistedNoteId(); } @@ -77,6 +65,8 @@ function reset() { cls.getContext().reset(); } +export const wrap = cls.wrap; + export default { init, wrap, diff --git a/packages/trilium-core/src/services/context.ts b/packages/trilium-core/src/services/context.ts index 374eb1296..91db0e836 100644 --- a/packages/trilium-core/src/services/context.ts +++ b/packages/trilium-core/src/services/context.ts @@ -19,11 +19,20 @@ export function getContext(): ExecutionContext { return ctx; } +export function wrap(callback: (...args: any[]) => any) { + return () => { + try { + getContext().init(callback); + } catch (e: any) { + console.log(`Error occurred: ${e.message}: ${e.stack}`); + } + }; +} + export function getHoistedNoteId() { return getContext().get("hoistedNoteId") || "root"; } - export function getComponentId() { return getContext().get("componentId"); } diff --git a/apps/server/src/services/erase.ts b/packages/trilium-core/src/services/erase.ts similarity index 88% rename from apps/server/src/services/erase.ts rename to packages/trilium-core/src/services/erase.ts index 92b28e573..481687726 100644 --- a/apps/server/src/services/erase.ts +++ b/packages/trilium-core/src/services/erase.ts @@ -1,17 +1,18 @@ -import sql from "./sql.js"; -import log from "./log.js"; +import { getLog } from "./log.js"; import entityChangesService from "./entity_changes.js"; import optionService from "./options.js"; -import dateUtils from "./date_utils.js"; +import dateUtils from "./utils/date.js"; import sqlInit from "./sql_init.js"; -import cls from "./cls.js"; +import * as cls from "./context.js"; import type { EntityChange } from "@triliumnext/commons"; +import { getSql } from "./sql/index.js"; function eraseNotes(noteIdsToErase: string[]) { if (noteIdsToErase.length === 0) { return; } + const sql = getSql(); sql.executeMany(/*sql*/`DELETE FROM notes WHERE noteId IN (???)`, noteIdsToErase); setEntityChangesAsErased(sql.getManyRows(/*sql*/`SELECT * FROM entity_changes WHERE entityName = 'notes' AND entityId IN (???)`, noteIdsToErase)); @@ -28,8 +29,7 @@ function eraseNotes(noteIdsToErase: string[]) { eraseRevisions(revisionIdsToErase); - - log.info(`Erased notes: ${JSON.stringify(noteIdsToErase)}`); + getLog().info(`Erased notes: ${JSON.stringify(noteIdsToErase)}`); } function setEntityChangesAsErased(entityChanges: EntityChange[]) { @@ -48,11 +48,12 @@ function eraseBranches(branchIdsToErase: string[]) { return; } + const sql = getSql(); sql.executeMany(/*sql*/`DELETE FROM branches WHERE branchId IN (???)`, branchIdsToErase); setEntityChangesAsErased(sql.getManyRows(/*sql*/`SELECT * FROM entity_changes WHERE entityName = 'branches' AND entityId IN (???)`, branchIdsToErase)); - log.info(`Erased branches: ${JSON.stringify(branchIdsToErase)}`); + getLog().info(`Erased branches: ${JSON.stringify(branchIdsToErase)}`); } function eraseAttributes(attributeIdsToErase: string[]) { @@ -60,11 +61,12 @@ function eraseAttributes(attributeIdsToErase: string[]) { return; } + const sql = getSql(); sql.executeMany(/*sql*/`DELETE FROM attributes WHERE attributeId IN (???)`, attributeIdsToErase); setEntityChangesAsErased(sql.getManyRows(/*sql*/`SELECT * FROM entity_changes WHERE entityName = 'attributes' AND entityId IN (???)`, attributeIdsToErase)); - log.info(`Erased attributes: ${JSON.stringify(attributeIdsToErase)}`); + getLog().info(`Erased attributes: ${JSON.stringify(attributeIdsToErase)}`); } function eraseAttachments(attachmentIdsToErase: string[]) { @@ -72,11 +74,12 @@ function eraseAttachments(attachmentIdsToErase: string[]) { return; } + const sql = getSql(); sql.executeMany(/*sql*/`DELETE FROM attachments WHERE attachmentId IN (???)`, attachmentIdsToErase); setEntityChangesAsErased(sql.getManyRows(/*sql*/`SELECT * FROM entity_changes WHERE entityName = 'attachments' AND entityId IN (???)`, attachmentIdsToErase)); - log.info(`Erased attachments: ${JSON.stringify(attachmentIdsToErase)}`); + getLog().info(`Erased attachments: ${JSON.stringify(attachmentIdsToErase)}`); } function eraseRevisions(revisionIdsToErase: string[]) { @@ -84,14 +87,16 @@ function eraseRevisions(revisionIdsToErase: string[]) { return; } + const sql = getSql(); sql.executeMany(/*sql*/`DELETE FROM revisions WHERE revisionId IN (???)`, revisionIdsToErase); setEntityChangesAsErased(sql.getManyRows(/*sql*/`SELECT * FROM entity_changes WHERE entityName = 'revisions' AND entityId IN (???)`, revisionIdsToErase)); - log.info(`Removed revisions: ${JSON.stringify(revisionIdsToErase)}`); + getLog().info(`Removed revisions: ${JSON.stringify(revisionIdsToErase)}`); } function eraseUnusedBlobs() { + const sql = getSql(); const unusedBlobIds = sql.getColumn(` SELECT blobs.blobId FROM blobs @@ -111,10 +116,11 @@ function eraseUnusedBlobs() { // this is because technically every keystroke can create a new blob and there would be just too many sql.executeMany(/*sql*/`DELETE FROM entity_changes WHERE entityName = 'blobs' AND entityId IN (???)`, unusedBlobIds); - log.info(`Erased unused blobs: ${JSON.stringify(unusedBlobIds)}`); + getLog().info(`Erased unused blobs: ${JSON.stringify(unusedBlobIds)}`); } function eraseDeletedEntities(eraseEntitiesAfterTimeInSeconds: number | null = null) { + const sql = getSql(); // this is important also so that the erased entity changes are sent to the connected clients sql.transactional(() => { if (eraseEntitiesAfterTimeInSeconds === null) { @@ -140,6 +146,7 @@ function eraseDeletedEntities(eraseEntitiesAfterTimeInSeconds: number | null = n } function eraseNotesWithDeleteId(deleteId: string) { + const sql = getSql(); const noteIdsToErase = sql.getColumn("SELECT noteId FROM notes WHERE isDeleted = 1 AND deleteId = ?", [deleteId]); eraseNotes(noteIdsToErase); @@ -170,7 +177,7 @@ function eraseScheduledAttachments(eraseUnusedAttachmentsAfterSeconds: number | } const cutOffDate = dateUtils.utcDateTimeStr(new Date(Date.now() - eraseUnusedAttachmentsAfterSeconds * 1000)); - const attachmentIdsToErase = sql.getColumn("SELECT attachmentId FROM attachments WHERE utcDateScheduledForErasureSince < ?", [cutOffDate]); + const attachmentIdsToErase = getSql().getColumn("SELECT attachmentId FROM attachments WHERE utcDateScheduledForErasureSince < ?", [cutOffDate]); eraseAttachments(attachmentIdsToErase); }