mirror of
https://github.com/zadam/trilium.git
synced 2025-11-17 18:50:41 +01:00
server-ts: Port services/attributes
This commit is contained in:
122
src/services/attributes.ts
Normal file
122
src/services/attributes.ts
Normal file
@@ -0,0 +1,122 @@
|
||||
"use strict";
|
||||
|
||||
import searchService = require('./search/services/search');
|
||||
import sql = require('./sql');
|
||||
import becca = require('../becca/becca');
|
||||
import BAttribute = require('../becca/entities/battribute');
|
||||
import attributeFormatter = require('./attribute_formatter');
|
||||
import BUILTIN_ATTRIBUTES = require('./builtin_attributes');
|
||||
import BNote = require('../becca/entities/bnote');
|
||||
import { AttributeRow } from '../becca/entities/rows';
|
||||
|
||||
const ATTRIBUTE_TYPES = ['label', 'relation'];
|
||||
|
||||
function getNotesWithLabel(name: string, value?: string): BNote[] {
|
||||
const query = attributeFormatter.formatAttrForSearch({type: 'label', name, value}, value !== undefined);
|
||||
return searchService.searchNotes(query, {
|
||||
includeArchivedNotes: true,
|
||||
ignoreHoistedNote: true
|
||||
});
|
||||
}
|
||||
|
||||
// TODO: should be in search service
|
||||
function getNoteWithLabel(name: string, value?: string): BNote | null {
|
||||
// optimized version (~20 times faster) without using normal search, useful for e.g., finding date notes
|
||||
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;
|
||||
}
|
||||
|
||||
function createLabel(noteId: string, name: string, value: string = "") {
|
||||
return createAttribute({
|
||||
noteId: noteId,
|
||||
type: 'label',
|
||||
name: name,
|
||||
value: value
|
||||
});
|
||||
}
|
||||
|
||||
function createRelation(noteId: string, name: string, targetNoteId: string) {
|
||||
return createAttribute({
|
||||
noteId: noteId,
|
||||
type: 'relation',
|
||||
name: name,
|
||||
value: targetNoteId
|
||||
});
|
||||
}
|
||||
|
||||
function createAttribute(attribute: AttributeRow) {
|
||||
return new BAttribute(attribute).save();
|
||||
}
|
||||
|
||||
function getAttributeNames(type: string, nameLike: string) {
|
||||
nameLike = nameLike.toLowerCase();
|
||||
|
||||
let names = sql.getColumn<string>(
|
||||
`SELECT DISTINCT name
|
||||
FROM attributes
|
||||
WHERE isDeleted = 0
|
||||
AND type = ?
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
names = names.filter(name => ![
|
||||
'internalLink',
|
||||
'imageLink',
|
||||
'includeNoteLink',
|
||||
'relationMapLink'
|
||||
].includes(name));
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
function isAttributeType(type: string): boolean {
|
||||
return ATTRIBUTE_TYPES.includes(type);
|
||||
}
|
||||
|
||||
function isAttributeDangerous(type: string, name: string): boolean {
|
||||
return BUILTIN_ATTRIBUTES.some(attr =>
|
||||
attr.type === type &&
|
||||
attr.name.toLowerCase() === name.trim().toLowerCase() &&
|
||||
attr.isDangerous
|
||||
);
|
||||
}
|
||||
|
||||
export = {
|
||||
getNotesWithLabel,
|
||||
getNoteWithLabel,
|
||||
createLabel,
|
||||
createRelation,
|
||||
createAttribute,
|
||||
getAttributeNames,
|
||||
isAttributeType,
|
||||
isAttributeDangerous
|
||||
};
|
||||
Reference in New Issue
Block a user