Skip to content

Build your personal knowledge base with Trilium Notes

server-ts: Port services/erase

Elian Doran77fb7bc6e8db
diff --git a/src/routes/api/branches.js b/src/routes/api/branches.js
index cbc951f1e6..ae06efddaa 100644
--- a/src/routes/api/branches.js
+++ b/src/routes/api/branches.js
@@ -4,7 +4,7 @@ const sql = require('../../services/sql');
const utils = require('../../services/utils');
const entityChangesService = require('../../services/entity_changes');
const treeService = require('../../services/tree.js');
-const eraseService = require('../../services/erase.js');
+const eraseService = require('../../services/erase');
const becca = require('../../becca/becca');
const TaskContext = require('../../services/task_context');
const branchService = require('../../services/branches.js');
diff --git a/src/routes/api/notes.js b/src/routes/api/notes.js
index 467a089f71..7c8ccf7730 100644
--- a/src/routes/api/notes.js
+++ b/src/routes/api/notes.js
@@ -1,7 +1,7 @@
"use strict";
const noteService = require('../../services/notes.js');
-const eraseService = require('../../services/erase.js');
+const eraseService = require('../../services/erase');
const treeService = require('../../services/tree.js');
const sql = require('../../services/sql');
const utils = require('../../services/utils');
diff --git a/src/routes/api/revisions.js b/src/routes/api/revisions.js
index f553b18bcc..e317fec951 100644
--- a/src/routes/api/revisions.js
+++ b/src/routes/api/revisions.js
@@ -8,7 +8,7 @@ const cls = require('../../services/cls');
const path = require('path');
const becca = require('../../becca/becca');
const blobService = require('../../services/blob');
-const eraseService = require("../../services/erase.js");
+const eraseService = require("../../services/erase");
function getRevisionBlob(req) {
const preview = req.query.preview === 'true';
diff --git a/src/services/bulk_actions.js b/src/services/bulk_actions.js
index ab8bed7398..136d1cccfc 100644
--- a/src/services/bulk_actions.js
+++ b/src/services/bulk_actions.js
@@ -4,7 +4,7 @@ const becca = require('../becca/becca');
const cloningService = require('./cloning.js');
const branchService = require('./branches.js');
const utils = require('./utils');
-const eraseService = require("./erase.js");
+const eraseService = require("./erase");
const ACTION_HANDLERS = {
addLabel: (action, note) => {
diff --git a/src/services/consistency_checks.js b/src/services/consistency_checks.js
index 9db5994c5e..3fd40a0406 100644
--- a/src/services/consistency_checks.js
+++ b/src/services/consistency_checks.js
@@ -12,7 +12,7 @@ const BBranch = require('../becca/entities/bbranch');
const revisionService = require('./revisions');
const becca = require('../becca/becca');
const utils = require('../services/utils');
-const eraseService = require('../services/erase.js');
+const eraseService = require('../services/erase');
const {sanitizeAttributeName} = require('./sanitize_attribute_name');
const noteTypes = require('../services/note_types.js').getNoteTypeNames();
diff --git a/src/services/content_hash.js b/src/services/content_hash.js
index a42c16503e..24fbfcfaa2 100644
--- a/src/services/content_hash.js
+++ b/src/services/content_hash.js
@@ -3,7 +3,7 @@
const sql = require('./sql');
const utils = require('./utils');
const log = require('./log');
-const eraseService = require('./erase.js');
+const eraseService = require('./erase');
function getEntityHashes() {
// blob erasure is not synced, we should check before each sync if there's some blob to erase
diff --git a/src/services/erase.js b/src/services/erase.ts
similarity index 67%
rename from src/services/erase.js
rename to src/services/erase.ts
index 99814cf424..6e6804f3d2 100644
--- a/src/services/erase.js
+++ b/src/services/erase.ts
@@ -1,13 +1,14 @@
-const sql = require("./sql");
-const revisionService = require("./revisions");
-const log = require("./log.ts");
-const entityChangesService = require("./entity_changes");
-const optionService = require("./options");
-const dateUtils = require("./date_utils");
-const sqlInit = require("./sql_init");
-const cls = require("./cls");
-
-function eraseNotes(noteIdsToErase) {
+import sql = require("./sql");
+import revisionService = require("./revisions");
+import log = require("./log");
+import entityChangesService = require("./entity_changes");
+import optionService = require("./options");
+import dateUtils = require("./date_utils");
+import sqlInit = require("./sql_init");
+import cls = require("./cls");
+import { EntityChange } from "./entity_changes_interface";
+
+function eraseNotes(noteIdsToErase: string[]) {
if (noteIdsToErase.length === 0) {
return;
}
@@ -16,17 +17,17 @@ function eraseNotes(noteIdsToErase) {
setEntityChangesAsErased(sql.getManyRows(`SELECT * FROM entity_changes WHERE entityName = 'notes' AND entityId IN (???)`, noteIdsToErase));
// we also need to erase all "dependent" entities of the erased notes
- const branchIdsToErase = sql.getManyRows(`SELECT branchId FROM branches WHERE noteId IN (???)`, noteIdsToErase)
+ const branchIdsToErase = sql.getManyRows<{ branchId: string }>(`SELECT branchId FROM branches WHERE noteId IN (???)`, noteIdsToErase)
.map(row => row.branchId);
eraseBranches(branchIdsToErase);
- const attributeIdsToErase = sql.getManyRows(`SELECT attributeId FROM attributes WHERE noteId IN (???)`, noteIdsToErase)
+ const attributeIdsToErase = sql.getManyRows<{ attributeId: string }>(`SELECT attributeId FROM attributes WHERE noteId IN (???)`, noteIdsToErase)
.map(row => row.attributeId);
eraseAttributes(attributeIdsToErase);
- const revisionIdsToErase = sql.getManyRows(`SELECT revisionId FROM revisions WHERE noteId IN (???)`, noteIdsToErase)
+ const revisionIdsToErase = sql.getManyRows<{ revisionId: string }>(`SELECT revisionId FROM revisions WHERE noteId IN (???)`, noteIdsToErase)
.map(row => row.revisionId);
eraseRevisions(revisionIdsToErase);
@@ -34,7 +35,7 @@ function eraseNotes(noteIdsToErase) {
log.info(`Erased notes: ${JSON.stringify(noteIdsToErase)}`);
}
-function setEntityChangesAsErased(entityChanges) {
+function setEntityChangesAsErased(entityChanges: EntityChange[]) {
for (const ec of entityChanges) {
ec.isErased = true;
// we're not changing hash here, not sure if good or not
@@ -45,7 +46,7 @@ function setEntityChangesAsErased(entityChanges) {
}
}
-function eraseBranches(branchIdsToErase) {
+function eraseBranches(branchIdsToErase: string[]) {
if (branchIdsToErase.length === 0) {
return;
}
@@ -57,7 +58,7 @@ function eraseBranches(branchIdsToErase) {
log.info(`Erased branches: ${JSON.stringify(branchIdsToErase)}`);
}
-function eraseAttributes(attributeIdsToErase) {
+function eraseAttributes(attributeIdsToErase: string[]) {
if (attributeIdsToErase.length === 0) {
return;
}
@@ -69,7 +70,7 @@ function eraseAttributes(attributeIdsToErase) {
log.info(`Erased attributes: ${JSON.stringify(attributeIdsToErase)}`);
}
-function eraseAttachments(attachmentIdsToErase) {
+function eraseAttachments(attachmentIdsToErase: string[]) {
if (attachmentIdsToErase.length === 0) {
return;
}
@@ -81,7 +82,7 @@ function eraseAttachments(attachmentIdsToErase) {
log.info(`Erased attachments: ${JSON.stringify(attachmentIdsToErase)}`);
}
-function eraseRevisions(revisionIdsToErase) {
+function eraseRevisions(revisionIdsToErase: string[]) {
if (revisionIdsToErase.length === 0) {
return;
}
@@ -116,7 +117,7 @@ function eraseUnusedBlobs() {
log.info(`Erased unused blobs: ${JSON.stringify(unusedBlobIds)}`);
}
-function eraseDeletedEntities(eraseEntitiesAfterTimeInSeconds = null) {
+function eraseDeletedEntities(eraseEntitiesAfterTimeInSeconds: number | null = null) {
// this is important also so that the erased entity changes are sent to the connected clients
sql.transactional(() => {
if (eraseEntitiesAfterTimeInSeconds === null) {
@@ -125,41 +126,33 @@ function eraseDeletedEntities(eraseEntitiesAfterTimeInSeconds = null) {
const cutoffDate = new Date(Date.now() - eraseEntitiesAfterTimeInSeconds * 1000);
- const noteIdsToErase = sql.getColumn("SELECT noteId FROM notes WHERE isDeleted = 1 AND utcDateModified <= ?", [dateUtils.utcDateTimeStr(cutoffDate)]);
-
+ const noteIdsToErase = sql.getColumn<string>("SELECT noteId FROM notes WHERE isDeleted = 1 AND utcDateModified <= ?", [dateUtils.utcDateTimeStr(cutoffDate)]);
eraseNotes(noteIdsToErase);
- const branchIdsToErase = sql.getColumn("SELECT branchId FROM branches WHERE isDeleted = 1 AND utcDateModified <= ?", [dateUtils.utcDateTimeStr(cutoffDate)]);
-
+ const branchIdsToErase = sql.getColumn<string>("SELECT branchId FROM branches WHERE isDeleted = 1 AND utcDateModified <= ?", [dateUtils.utcDateTimeStr(cutoffDate)]);
eraseBranches(branchIdsToErase);
- const attributeIdsToErase = sql.getColumn("SELECT attributeId FROM attributes WHERE isDeleted = 1 AND utcDateModified <= ?", [dateUtils.utcDateTimeStr(cutoffDate)]);
-
+ const attributeIdsToErase = sql.getColumn<string>("SELECT attributeId FROM attributes WHERE isDeleted = 1 AND utcDateModified <= ?", [dateUtils.utcDateTimeStr(cutoffDate)]);
eraseAttributes(attributeIdsToErase);
- const attachmentIdsToErase = sql.getColumn("SELECT attachmentId FROM attachments WHERE isDeleted = 1 AND utcDateModified <= ?", [dateUtils.utcDateTimeStr(cutoffDate)]);
-
+ const attachmentIdsToErase = sql.getColumn<string>("SELECT attachmentId FROM attachments WHERE isDeleted = 1 AND utcDateModified <= ?", [dateUtils.utcDateTimeStr(cutoffDate)]);
eraseAttachments(attachmentIdsToErase);
eraseUnusedBlobs();
});
}
-function eraseNotesWithDeleteId(deleteId) {
- const noteIdsToErase = sql.getColumn("SELECT noteId FROM notes WHERE isDeleted = 1 AND deleteId = ?", [deleteId]);
-
+function eraseNotesWithDeleteId(deleteId: string) {
+ const noteIdsToErase = sql.getColumn<string>("SELECT noteId FROM notes WHERE isDeleted = 1 AND deleteId = ?", [deleteId]);
eraseNotes(noteIdsToErase);
- const branchIdsToErase = sql.getColumn("SELECT branchId FROM branches WHERE isDeleted = 1 AND deleteId = ?", [deleteId]);
-
+ const branchIdsToErase = sql.getColumn<string>("SELECT branchId FROM branches WHERE isDeleted = 1 AND deleteId = ?", [deleteId]);
eraseBranches(branchIdsToErase);
- const attributeIdsToErase = sql.getColumn("SELECT attributeId FROM attributes WHERE isDeleted = 1 AND deleteId = ?", [deleteId]);
-
+ const attributeIdsToErase = sql.getColumn<string>("SELECT attributeId FROM attributes WHERE isDeleted = 1 AND deleteId = ?", [deleteId]);
eraseAttributes(attributeIdsToErase);
- const attachmentIdsToErase = sql.getColumn("SELECT attachmentId FROM attachments WHERE isDeleted = 1 AND deleteId = ?", [deleteId]);
-
+ const attachmentIdsToErase = sql.getColumn<string>("SELECT attachmentId FROM attachments WHERE isDeleted = 1 AND deleteId = ?", [deleteId]);
eraseAttachments(attachmentIdsToErase);
eraseUnusedBlobs();
@@ -173,13 +166,13 @@ function eraseUnusedAttachmentsNow() {
eraseScheduledAttachments(0);
}
-function eraseScheduledAttachments(eraseUnusedAttachmentsAfterSeconds = null) {
+function eraseScheduledAttachments(eraseUnusedAttachmentsAfterSeconds: number | null = null) {
if (eraseUnusedAttachmentsAfterSeconds === null) {
eraseUnusedAttachmentsAfterSeconds = optionService.getOptionInt('eraseUnusedAttachmentsAfterSeconds');
}
const cutOffDate = dateUtils.utcDateTimeStr(new Date(Date.now() - (eraseUnusedAttachmentsAfterSeconds * 1000)));
- const attachmentIdsToErase = sql.getColumn('SELECT attachmentId FROM attachments WHERE utcDateScheduledForErasureSince < ?', [cutOffDate]);
+ const attachmentIdsToErase = sql.getColumn<string>('SELECT attachmentId FROM attachments WHERE utcDateScheduledForErasureSince < ?', [cutOffDate]);
eraseAttachments(attachmentIdsToErase);
}
@@ -193,7 +186,7 @@ sqlInit.dbReady.then(() => {
setInterval(cls.wrap(() => eraseScheduledAttachments()), 3600 * 1000);
});
-module.exports = {
+export = {
eraseDeletedNotesNow,
eraseUnusedAttachmentsNow,
eraseNotesWithDeleteId,