server-ts: Port services/attributes

This commit is contained in:
Elian Doran
2024-02-18 11:26:05 +02:00
parent 2fbd2e3c29
commit d8d729342d
15 changed files with 35 additions and 35 deletions

View File

@@ -1,8 +1,8 @@
"use strict";
import BAttribute = require("../becca/entities/battribute");
import { AttributeRow } from "../becca/entities/rows";
function formatAttrForSearch(attr: BAttribute, searchWithValue: string) {
function formatAttrForSearch(attr: AttributeRow, searchWithValue: boolean) {
let searchStr = '';
if (attr.type === 'label') {

View File

@@ -1,17 +1,18 @@
"use strict";
const searchService = require('./search/services/search');
const sql = require('./sql');
const becca = require('../becca/becca');
const BAttribute = require('../becca/entities/battribute');
const {formatAttrForSearch} = require('./attribute_formatter');
const BUILTIN_ATTRIBUTES = require('./builtin_attributes');
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'];
/** @returns {BNote[]} */
function getNotesWithLabel(name, value = undefined) {
const query = formatAttrForSearch({type: 'label', name, value}, value !== undefined);
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
@@ -19,8 +20,7 @@ function getNotesWithLabel(name, value = undefined) {
}
// TODO: should be in search service
/** @returns {BNote|null} */
function getNoteWithLabel(name, value = undefined) {
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);
@@ -39,7 +39,7 @@ function getNoteWithLabel(name, value = undefined) {
return null;
}
function createLabel(noteId, name, value = "") {
function createLabel(noteId: string, name: string, value: string = "") {
return createAttribute({
noteId: noteId,
type: 'label',
@@ -48,7 +48,7 @@ function createLabel(noteId, name, value = "") {
});
}
function createRelation(noteId, name, targetNoteId) {
function createRelation(noteId: string, name: string, targetNoteId: string) {
return createAttribute({
noteId: noteId,
type: 'relation',
@@ -57,14 +57,14 @@ function createRelation(noteId, name, targetNoteId) {
});
}
function createAttribute(attribute) {
function createAttribute(attribute: AttributeRow) {
return new BAttribute(attribute).save();
}
function getAttributeNames(type, nameLike) {
function getAttributeNames(type: string, nameLike: string) {
nameLike = nameLike.toLowerCase();
let names = sql.getColumn(
let names = sql.getColumn<string>(
`SELECT DISTINCT name
FROM attributes
WHERE isDeleted = 0
@@ -98,11 +98,11 @@ function getAttributeNames(type, nameLike) {
return names;
}
function isAttributeType(type) {
function isAttributeType(type: string): boolean {
return ATTRIBUTE_TYPES.includes(type);
}
function isAttributeDangerous(type, name) {
function isAttributeDangerous(type: string, name: string): boolean {
return BUILTIN_ATTRIBUTES.some(attr =>
attr.type === type &&
attr.name.toLowerCase() === name.trim().toLowerCase() &&
@@ -110,7 +110,7 @@ function isAttributeDangerous(type, name) {
);
}
module.exports = {
export = {
getNotesWithLabel,
getNoteWithLabel,
createLabel,

View File

@@ -2,7 +2,7 @@ const log = require('./log');
const noteService = require('./notes');
const sql = require('./sql');
const utils = require('./utils');
const attributeService = require('./attributes.js');
const attributeService = require('./attributes');
const dateNoteService = require('./date_notes.js');
const treeService = require('./tree.js');
const config = require('./config');

View File

@@ -1,7 +1,7 @@
"use strict";
const noteService = require('./notes');
const attributeService = require('./attributes.js');
const attributeService = require('./attributes');
const dateUtils = require('./date_utils');
const sql = require('./sql');
const protectedSessionService = require('./protected_session');

View File

@@ -4,7 +4,7 @@ const BAttribute = require('../../becca/entities/battribute');
const utils = require('../../services/utils');
const log = require('../../services/log');
const noteService = require('../../services/notes');
const attributeService = require('../../services/attributes.js');
const attributeService = require('../../services/attributes');
const BBranch = require('../../becca/entities/bbranch');
const path = require('path');
const protectedSessionService = require('../protected_session');

View File

@@ -3,7 +3,7 @@ const cls = require('./cls');
const sqlInit = require('./sql_init');
const config = require('./config');
const log = require('./log');
const attributeService = require('../services/attributes.js');
const attributeService = require('../services/attributes');
const protectedSessionService = require('../services/protected_session');
const hiddenSubtreeService = require('./hidden_subtree');

View File

@@ -1,4 +1,4 @@
const attributeService = require('./attributes.js');
const attributeService = require('./attributes');
const dateNoteService = require('./date_notes.js');
const becca = require('../becca/becca');
const noteService = require('./notes');