Skip to content

Build your personal knowledge base with Trilium Notes

refactor(script): move runScript to executeScript at BNote level

Elian Doran0f1d39565177
diff --git a/apps/server/src/becca/entities/bnote.ts b/apps/server/src/becca/entities/bnote.ts
index 112543a603..fe0cca706b 100644
--- a/apps/server/src/becca/entities/bnote.ts
+++ b/apps/server/src/becca/entities/bnote.ts
@@ -316,6 +316,18 @@ class BNote extends AbstractBeccaEntity<BNote> {
return null;
}
+ /**
+ * Executes this note as a script. The note must be of type "Code: JS backend".
+ *
+ * @returns the return value of the executed script
+ */
+ executeScript() {
+ // Lazy require to avoid circular dependency (script.ts imports BNote as a type).
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
+ const scriptService = require("../../services/script.js").default;
+ return scriptService.executeNote(this, { originEntity: this });
+ }
+
/**
* Beware that the method must not create a copy of the array, but actually returns its internal array
* (for performance reasons)
diff --git a/apps/server/src/services/backend_script_api.ts b/apps/server/src/services/backend_script_api.ts
index c19669e776..d84b21fb39 100644
--- a/apps/server/src/services/backend_script_api.ts
+++ b/apps/server/src/services/backend_script_api.ts
@@ -28,7 +28,6 @@ import log from "./log.js";
import type { NoteParams } from "./note-interface.js";
import noteService from "./notes.js";
import optionsService from "./options.js";
-import scriptService from "./script.js";
import SearchContext from "./search/search_context.js";
import searchService from "./search/services/search.js";
import SpacedUpdate from "./spaced_update.js";
@@ -111,14 +110,6 @@ export interface Api {
getInstanceName(): string | null;
getNote(noteId: string): BNote | null;
-
- /**
- * Executes a backend JS note with the given noteId. The note must be of type "Code: JS backend".
- *
- * @returns the return value of the executed note
- */
- runNote(noteId: string): unknown;
-
getBranch(branchId: string): BBranch | null;
getAttribute(attachmentId: string): BAttribute | null;
getAttachment(attachmentId: string): BAttachment | null;
@@ -456,10 +447,6 @@ function BackendScriptApi(this: Api, currentNote: BNote, apiParams: ApiParams) {
this.cheerio = cheerio;
this.getInstanceName = () => (config.General ? config.General.instanceName : null);
this.getNote = (noteId) => becca.getNote(noteId);
- this.runNote = (noteId) => {
- const note = becca.getNoteOrThrow(noteId);
- return scriptService.executeNote(note, { originEntity: note });
- };
this.getBranch = (branchId) => becca.getBranch(branchId);
this.getAttribute = (attributeId) => becca.getAttribute(attributeId);
this.getAttachment = (attachmentId) => becca.getAttachment(attachmentId);