Skip to content

Build your personal knowledge base with Trilium Notes

chore(core): port attribute route

Elian Doran969860c344ad
diff --git a/apps/server/src/routes/routes.ts b/apps/server/src/routes/routes.ts
index ab378fd18b..2f209795cc 100644
--- a/apps/server/src/routes/routes.ts
+++ b/apps/server/src/routes/routes.ts
@@ -19,7 +19,6 @@ import openID from '../services/open_id.js';
import { isElectron } from "../services/utils.js";
import shareRoutes from "../share/routes.js";
import anthropicRoute from "./api/anthropic.js";
-import attributesRoute from "./api/attributes.js";
import autocompleteApiRoute from "./api/autocomplete.js";
import backendLogRoute from "./api/backend_log.js";
import bulkActionRoute from "./api/bulk_action.js";
@@ -152,17 +151,6 @@ function register(app: express.Application) {
asyncRoute(PST, "/api/notes/:parentNoteId/notes-import", [auth.checkApiAuthOrElectron, uploadMiddlewareWithErrorHandling, csrfMiddleware], importRoute.importNotesToBranch, apiResultHandler);
route(PST, "/api/notes/:parentNoteId/attachments-import", [auth.checkApiAuthOrElectron, uploadMiddlewareWithErrorHandling, csrfMiddleware], importRoute.importAttachmentsToNote, apiResultHandler);
- apiRoute(GET, "/api/notes/:noteId/attributes", attributesRoute.getEffectiveNoteAttributes);
- apiRoute(PST, "/api/notes/:noteId/attributes", attributesRoute.addNoteAttribute);
- apiRoute(PUT, "/api/notes/:noteId/attributes", attributesRoute.updateNoteAttributes);
- apiRoute(PUT, "/api/notes/:noteId/attribute", attributesRoute.updateNoteAttribute);
- apiRoute(PUT, "/api/notes/:noteId/set-attribute", attributesRoute.setNoteAttribute);
- apiRoute(PUT, "/api/notes/:noteId/relations/:name/to/:targetNoteId", attributesRoute.createRelation);
- apiRoute(DEL, "/api/notes/:noteId/relations/:name/to/:targetNoteId", attributesRoute.deleteRelation);
- apiRoute(DEL, "/api/notes/:noteId/attributes/:attributeId", attributesRoute.deleteNoteAttribute);
- apiRoute(GET, "/api/attribute-names", attributesRoute.getAttributeNames);
- apiRoute(GET, "/api/attribute-values/:attributeName", attributesRoute.getValuesForAttribute);
-
// :filename is not used by trilium, but instead used for "save as" to assign a human-readable filename
route(GET, "/api/images/:noteId/:filename", [auth.checkApiAuthOrElectron], imageRoute.returnImageFromNote);
route(PUT, "/api/images/:noteId", [auth.checkApiAuthOrElectron, uploadMiddlewareWithErrorHandling, csrfMiddleware], imageRoute.updateImage, apiResultHandler);
diff --git a/apps/server/src/routes/api/attributes.ts b/packages/trilium-core/src/routes/api/attributes.ts
similarity index 87%
rename from apps/server/src/routes/api/attributes.ts
rename to packages/trilium-core/src/routes/api/attributes.ts
index 3b0ac83269..7c97e3024c 100644
--- a/apps/server/src/routes/api/attributes.ts
+++ b/packages/trilium-core/src/routes/api/attributes.ts
@@ -1,12 +1,12 @@
import { UpdateAttributeResponse } from "@triliumnext/commons";
-import { ValidationError } from "@triliumnext/core";
import type { Request } from "express";
import becca from "../../becca/becca.js";
import BAttribute from "../../becca/entities/battribute.js";
import attributeService from "../../services/attributes.js";
-import log from "../../services/log.js";
-import sql from "../../services/sql.js";
+import { getLog } from "../../services/log.js";
+import { ValidationError } from "../../errors.js";
+import { getSql } from "../../services/sql/index.js";
function getEffectiveNoteAttributes(req: Request) {
const note = becca.getNote(req.params.noteId);
@@ -71,7 +71,7 @@ function setNoteAttribute(req: Request) {
const noteId = req.params.noteId;
const body = req.body;
- const attributeId = sql.getValue<string | null>(/*sql*/`SELECT attributeId FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = ? AND name = ?`, [noteId, body.type, body.name]);
+ const attributeId = getSql().getValue<string | null>(/*sql*/`SELECT attributeId FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = ? AND name = ?`, [noteId, body.type, body.name]);
if (attributeId) {
const attr = becca.getAttribute(attributeId);
@@ -145,7 +145,7 @@ function updateNoteAttributes(req: Request) {
const targetNote = becca.getNote(incAttr.value);
if (!targetNote) {
- log.error(`Target note of relation ${JSON.stringify(incAttr)} does not exist or is deleted`);
+ getLog().error(`Target note of relation ${JSON.stringify(incAttr)} does not exist or is deleted`);
continue;
}
}
@@ -189,7 +189,7 @@ function getAttributeNames(req: Request) {
function getValuesForAttribute(req: Request) {
const attributeName = req.params.attributeName;
- return sql.getColumn("SELECT DISTINCT value FROM attributes WHERE isDeleted = 0 AND name = ? AND type = 'label' AND value != '' ORDER BY value", [attributeName]);
+ return getSql().getColumn("SELECT DISTINCT value FROM attributes WHERE isDeleted = 0 AND name = ? AND type = 'label' AND value != '' ORDER BY value", [attributeName]);
}
function createRelation(req: Request) {
@@ -197,7 +197,7 @@ function createRelation(req: Request) {
const targetNoteId = req.params.targetNoteId;
const name = req.params.name;
- const attributeId = sql.getValue<string>(/*sql*/`SELECT attributeId FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = 'relation' AND name = ? AND value = ?`, [
+ const attributeId = getSql().getValue<string>(/*sql*/`SELECT attributeId FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = 'relation' AND name = ? AND value = ?`, [
sourceNoteId,
name,
targetNoteId
@@ -221,7 +221,7 @@ function deleteRelation(req: Request) {
const targetNoteId = req.params.targetNoteId;
const name = req.params.name;
- const attributeId = sql.getValue<string | null>(/*sql*/`SELECT attributeId FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = 'relation' AND name = ? AND value = ?`, [
+ const attributeId = getSql().getValue<string | null>(/*sql*/`SELECT attributeId FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = 'relation' AND name = ? AND value = ?`, [
sourceNoteId,
name,
targetNoteId
diff --git a/packages/trilium-core/src/routes/index.ts b/packages/trilium-core/src/routes/index.ts
index 4cb2befa39..2aca61bfe5 100644
--- a/packages/trilium-core/src/routes/index.ts
+++ b/packages/trilium-core/src/routes/index.ts
@@ -12,6 +12,7 @@ import statsRoute from "./api/stats";
import AbstractBeccaEntity from "../becca/entities/abstract_becca_entity";
import cloningApiRoute from "./api/cloning";
import sqlRoute from "./api/sql";
+import attributesRoute from "./api/attributes";
// TODO: Deduplicate with routes.ts
const GET = "get",
@@ -58,6 +59,17 @@ export function buildSharedApiRoutes(apiRoute: any) {
apiRoute(PUT, "/api/attachments/:attachmentId/rename", attachmentsApiRoute.renameAttachment);
apiRoute(GET, "/api/attachments/:attachmentId/blob", attachmentsApiRoute.getAttachmentBlob);
+ apiRoute(GET, "/api/notes/:noteId/attributes", attributesRoute.getEffectiveNoteAttributes);
+ apiRoute(PST, "/api/notes/:noteId/attributes", attributesRoute.addNoteAttribute);
+ apiRoute(PUT, "/api/notes/:noteId/attributes", attributesRoute.updateNoteAttributes);
+ apiRoute(PUT, "/api/notes/:noteId/attribute", attributesRoute.updateNoteAttribute);
+ apiRoute(PUT, "/api/notes/:noteId/set-attribute", attributesRoute.setNoteAttribute);
+ apiRoute(PUT, "/api/notes/:noteId/relations/:name/to/:targetNoteId", attributesRoute.createRelation);
+ apiRoute(DEL, "/api/notes/:noteId/relations/:name/to/:targetNoteId", attributesRoute.deleteRelation);
+ apiRoute(DEL, "/api/notes/:noteId/attributes/:attributeId", attributesRoute.deleteNoteAttribute);
+ apiRoute(GET, "/api/attribute-names/", attributesRoute.getAttributeNames);
+ apiRoute(GET, "/api/attribute-values/:attributeName", attributesRoute.getValuesForAttribute);
+
apiRoute(PUT, "/api/branches/:branchId/move-to/:parentBranchId", branchesApiRoute.moveBranchToParent);
apiRoute(PUT, "/api/branches/:branchId/move-before/:beforeBranchId", branchesApiRoute.moveBranchBeforeNote);
apiRoute(PUT, "/api/branches/:branchId/move-after/:afterBranchId", branchesApiRoute.moveBranchAfterNote);
diff --git a/apps/server/src/services/attributes.ts b/packages/trilium-core/src/services/attributes.ts
similarity index 97%
rename from apps/server/src/services/attributes.ts
rename to packages/trilium-core/src/services/attributes.ts
index c1fec6808f..2faf916375 100644
--- a/apps/server/src/services/attributes.ts
+++ b/packages/trilium-core/src/services/attributes.ts
@@ -1,13 +1,13 @@
"use strict";
import searchService from "./search/services/search.js";
-import sql from "./sql.js";
import becca from "../becca/becca.js";
import BAttribute from "../becca/entities/battribute.js";
import attributeFormatter from "./attribute_formatter.js";
import BUILTIN_ATTRIBUTES from "./builtin_attributes.js";
import type BNote from "../becca/entities/bnote.js";
import type { AttributeRow } from "@triliumnext/commons";
+import { getSql } from "./sql/index.js";
const ATTRIBUTE_TYPES = new Set(["label", "relation"]);
@@ -64,7 +64,7 @@ function createAttribute(attribute: AttributeRow) {
function getAttributeNames(type: string, nameLike: string) {
nameLike = nameLike.toLowerCase();
- let names = sql.getColumn<string>(
+ let names = getSql().getColumn<string>(
/*sql*/`SELECT DISTINCT name
FROM attributes
WHERE isDeleted = 0