Notes/src/services/attributes.ts

116 lines
3.2 KiB
TypeScript
Raw Normal View History

"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 "../becca/entities/rows.js";
2025-01-09 18:07:02 +02:00
const ATTRIBUTE_TYPES = new Set(["label", "relation"]);
2019-02-11 23:45:58 +01:00
2024-02-18 11:26:05 +02:00
function getNotesWithLabel(name: string, value?: string): BNote[] {
2025-01-09 18:07:02 +02:00
const query = attributeFormatter.formatAttrForSearch({ type: "label", name, value }, value !== undefined);
2021-07-04 21:05:47 +02:00
return searchService.searchNotes(query, {
includeArchivedNotes: true,
ignoreHoistedNote: true
});
}
2021-06-06 11:01:10 +02:00
// TODO: should be in search service
2024-02-18 11:26:05 +02:00
function getNoteWithLabel(name: string, value?: string): BNote | null {
2023-06-30 11:18:34 +02:00
// optimized version (~20 times faster) without using normal search, useful for e.g., finding date notes
2025-01-09 18:07:02 +02:00
const attrs = becca.findAttributes("label", name);
if (value === undefined) {
return attrs[0]?.getNote();
}
value = value?.toLowerCase();
for (const attr of attrs) {
if (attr.value.toLowerCase() === value) {
return attr.getNote();
}
}
return null;
}
2024-02-18 11:26:05 +02:00
function createLabel(noteId: string, name: string, value: string = "") {
2020-06-20 12:31:38 +02:00
return createAttribute({
noteId: noteId,
2025-01-09 18:07:02 +02:00
type: "label",
name: name,
value: value
});
}
2024-02-18 11:26:05 +02:00
function createRelation(noteId: string, name: string, targetNoteId: string) {
2020-06-20 12:31:38 +02:00
return createAttribute({
noteId: noteId,
2025-01-09 18:07:02 +02:00
type: "relation",
name: name,
value: targetNoteId
});
}
2024-02-18 11:26:05 +02:00
function createAttribute(attribute: AttributeRow) {
return new BAttribute(attribute).save();
}
2024-02-18 11:26:05 +02:00
function getAttributeNames(type: string, nameLike: string) {
nameLike = nameLike.toLowerCase();
2024-02-18 11:26:05 +02:00
let names = sql.getColumn<string>(
`SELECT DISTINCT name
FROM attributes
WHERE isDeleted = 0
AND type = ?
2025-01-09 18:07:02 +02:00
AND name LIKE ?`,
[type, `%${nameLike}%`]
);
for (const attr of BUILTIN_ATTRIBUTES) {
if (attr.type === type && attr.name.toLowerCase().includes(nameLike) && !names.includes(attr.name)) {
names.push(attr.name);
}
}
2025-01-09 18:07:02 +02:00
names = names.filter((name) => !["internalLink", "imageLink", "includeNoteLink", "relationMapLink"].includes(name));
2022-01-15 22:09:51 +01:00
names.sort((a, b) => {
const aPrefix = a.toLowerCase().startsWith(nameLike);
const bPrefix = b.toLowerCase().startsWith(nameLike);
if (aPrefix !== bPrefix) {
return aPrefix ? -1 : 1;
}
return a < b ? -1 : 1;
});
return names;
}
2024-02-18 11:26:05 +02:00
function isAttributeType(type: string): boolean {
return ATTRIBUTE_TYPES.has(type);
2019-02-11 23:45:58 +01:00
}
2024-02-18 11:26:05 +02:00
function isAttributeDangerous(type: string, name: string): boolean {
2025-01-09 18:07:02 +02:00
return BUILTIN_ATTRIBUTES.some((attr) => attr.type === type && attr.name.toLowerCase() === name.trim().toLowerCase() && attr.isDangerous);
2019-02-11 23:45:58 +01:00
}
export default {
getNotesWithLabel,
getNoteWithLabel,
createLabel,
createRelation,
createAttribute,
2019-02-11 23:45:58 +01:00
getAttributeNames,
isAttributeType,
2022-12-23 14:18:40 +01:00
isAttributeDangerous
2020-06-07 10:20:48 +02:00
};