Notes/src/services/attributes.js

123 lines
3.1 KiB
JavaScript
Raw Normal View History

"use strict";
const searchService = require('./search/services/search');
2024-02-16 22:44:12 +02:00
const sql = require('./sql');
const becca = require('../becca/becca');
const BAttribute = require('../becca/entities/battribute');
2024-02-17 19:15:50 +02:00
const {formatAttrForSearch} = require('./attribute_formatter');
2024-02-17 19:02:14 +02:00
const BUILTIN_ATTRIBUTES = require('./builtin_attributes');
2023-04-08 19:54:59 +08:00
const ATTRIBUTE_TYPES = ['label', 'relation'];
2019-02-11 23:45:58 +01:00
/** @returns {BNote[]} */
function getNotesWithLabel(name, value = undefined) {
const query = 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
/** @returns {BNote|null} */
2022-02-20 12:33:50 +01:00
function getNoteWithLabel(name, value = undefined) {
2023-06-30 11:18:34 +02:00
// 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;
}
2020-06-20 12:31:38 +02:00
function createLabel(noteId, name, value = "") {
return createAttribute({
noteId: noteId,
type: 'label',
name: name,
value: value
});
}
2020-06-20 12:31:38 +02:00
function createRelation(noteId, name, targetNoteId) {
return createAttribute({
noteId: noteId,
type: 'relation',
name: name,
value: targetNoteId
});
}
2020-06-20 12:31:38 +02:00
function createAttribute(attribute) {
return new BAttribute(attribute).save();
}
2020-06-20 12:31:38 +02:00
function getAttributeNames(type, nameLike) {
nameLike = nameLike.toLowerCase();
2022-01-15 22:09:51 +01:00
let names = sql.getColumn(
`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);
}
}
2022-01-15 22:09:51 +01:00
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;
}
2019-02-11 23:45:58 +01:00
function isAttributeType(type) {
return ATTRIBUTE_TYPES.includes(type);
}
function isAttributeDangerous(type, name) {
2020-06-07 10:20:48 +02:00
return BUILTIN_ATTRIBUTES.some(attr =>
2023-04-08 19:54:59 +08:00
attr.type === type &&
2019-02-11 23:45:58 +01:00
attr.name.toLowerCase() === name.trim().toLowerCase() &&
attr.isDangerous
);
}
module.exports = {
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
};