Skip to content

Build your personal knowledge base with Trilium Notes

server-ts: Port battribute

Elian Doran06287da9d8c2
diff --git a/src/becca/becca-interface.ts b/src/becca/becca-interface.ts
index 954c349664..6b36892b3f 100644
--- a/src/becca/becca-interface.ts
+++ b/src/becca/becca-interface.ts
@@ -4,6 +4,7 @@ import NotFoundError = require('../errors/not_found_error');
import BOption = require('./entities/boption');
import BNote = require('./entities/bnote');
import BEtapiToken = require('./entities/betapi_token');
+import BAttribute = require('./entities/battribute');
/**
* Becca is a backend cache of all notes, branches, and attributes.
@@ -11,6 +12,9 @@ import BEtapiToken = require('./entities/betapi_token');
*/
class Becca {
notes!: Record<string, BNote>;
+ attributes!: Record<string, BAttribute>;
+ /** Points from attribute type-name to list of attributes */
+ attributeIndex!: Record<string, BAttribute[]>;
options!: Record<string, BOption>;
etapiTokens!: Record<string, BEtapiToken>;
@@ -25,9 +29,7 @@ class Becca {
this.branches = {};
/** @type {Object.<String, BBranch>} */
this.childParentToBranch = {};
- /** @type {Object.<String, BAttribute>} */
- this.attributes = {};
- /** @type {Object.<String, BAttribute[]>} Points from attribute type-name to list of attributes */
+ this.attributes = {};
this.attributeIndex = {};
this.options = {};
this.etapiTokens = {};
diff --git a/src/becca/entities/battribute.js b/src/becca/entities/battribute.ts
similarity index 86%
rename from src/becca/entities/battribute.js
rename to src/becca/entities/battribute.ts
index 54afd35873..71d5751937 100644
--- a/src/becca/entities/battribute.js
+++ b/src/becca/entities/battribute.ts
@@ -1,17 +1,11 @@
"use strict";
-const BNote = require('./bnote.js');
-const AbstractBeccaEntity = require('./abstract_becca_entity.js');
-const sql = require('../../services/sql');
-const dateUtils = require('../../services/date_utils');
-const promotedAttributeDefinitionParser = require('../../services/promoted_attribute_definition_parser.js');
-const {sanitizeAttributeName} = require('../../services/sanitize_attribute_name.js');
-
-
-/**
- * There are currently only two types of attributes, labels or relations.
- * @typedef {"label" | "relation"} AttributeType
- */
+import BNote = require('./bnote.js');
+import AbstractBeccaEntity = require('./abstract_becca_entity.js');
+import dateUtils = require('../../services/date_utils');
+import promotedAttributeDefinitionParser = require('../../services/promoted_attribute_definition_parser');
+import sanitizeAttributeName = require('../../services/sanitize_attribute_name');
+import { AttributeRow, AttributeType } from './rows.js';
/**
* Attribute is an abstract concept which has two real uses - label (key - value pair)
@@ -24,7 +18,16 @@ class BAttribute extends AbstractBeccaEntity {
static get primaryKeyName() { return "attributeId"; }
static get hashedProperties() { return ["attributeId", "noteId", "type", "name", "value", "isInheritable"]; }
- constructor(row) {
+ attributeId!: string;
+ noteId!: string;
+ type!: AttributeType;
+ name!: string;
+ position!: number;
+ value!: string;
+ isInheritable!: boolean;
+ utcDateModified!: string;
+
+ constructor(row: AttributeRow) {
super();
if (!row) {
@@ -35,7 +38,7 @@ class BAttribute extends AbstractBeccaEntity {
this.init();
}
- updateFromRow(row) {
+ updateFromRow(row: AttributeRow) {
this.update([
row.attributeId,
row.noteId,
@@ -48,22 +51,14 @@ class BAttribute extends AbstractBeccaEntity {
]);
}
- update([attributeId, noteId, type, name, value, isInheritable, position, utcDateModified]) {
- /** @type {string} */
+ update([attributeId, noteId, type, name, value, isInheritable, position, utcDateModified]: any[]) {
this.attributeId = attributeId;
- /** @type {string} */
this.noteId = noteId;
- /** @type {AttributeType} */
this.type = type;
- /** @type {string} */
this.name = name;
- /** @type {int} */
this.position = position;
- /** @type {string} */
this.value = value || "";
- /** @type {boolean} */
this.isInheritable = !!isInheritable;
- /** @type {string} */
this.utcDateModified = utcDateModified;
return this;
@@ -226,7 +221,7 @@ class BAttribute extends AbstractBeccaEntity {
};
}
- createClone(type, name, value, isInheritable) {
+ createClone(type: AttributeType, name: string, value: string, isInheritable: boolean) {
return new BAttribute({
noteId: this.noteId,
type: type,
@@ -239,4 +234,4 @@ class BAttribute extends AbstractBeccaEntity {
}
}
-module.exports = BAttribute;
+export = BAttribute;
diff --git a/src/becca/entities/rows.ts b/src/becca/entities/rows.ts
index 54c0e8b04c..98bb0ec8a8 100644
--- a/src/becca/entities/rows.ts
+++ b/src/becca/entities/rows.ts
@@ -59,4 +59,17 @@ export interface BlobRow {
contentLength: number;
dateModified: string;
utcDateModified: string;
+}
+
+export type AttributeType = "label" | "relation";
+
+export interface AttributeRow {
+ attributeId?: string;
+ noteId: string;
+ type: AttributeType;
+ name: string;
+ position: number;
+ value: string;
+ isInheritable: boolean;
+ utcDateModified: string;
}
\ No newline at end of file
diff --git a/src/services/promoted_attribute_definition_parser.js b/src/services/promoted_attribute_definition_parser.ts
similarity index 75%
rename from src/services/promoted_attribute_definition_parser.js
rename to src/services/promoted_attribute_definition_parser.ts
index 937dae1de2..cd3dc18db2 100644
--- a/src/services/promoted_attribute_definition_parser.js
+++ b/src/services/promoted_attribute_definition_parser.ts
@@ -1,6 +1,15 @@
-function parse(value) {
+interface DefinitionObject {
+ isPromoted: boolean;
+ labelType: string;
+ multiplicity: string;
+ numberPrecision: number;
+ promotedAlias: string;
+ inverseRelation: string;
+}
+
+function parse(value: string): DefinitionObject {
const tokens = value.split(',').map(t => t.trim());
- const defObj = {};
+ const defObj: Partial<DefinitionObject> = {};
for (const token of tokens) {
if (token === 'promoted') {
@@ -32,9 +41,9 @@ function parse(value) {
}
}
- return defObj;
+ return defObj as DefinitionObject;
}
-module.exports = {
+export = {
parse
};
diff --git a/src/services/sanitize_attribute_name.js b/src/services/sanitize_attribute_name.ts
similarity index 69%
rename from src/services/sanitize_attribute_name.js
rename to src/services/sanitize_attribute_name.ts
index 3a7c9ff67b..ecdc35dc0d 100644
--- a/src/services/sanitize_attribute_name.js
+++ b/src/services/sanitize_attribute_name.ts
@@ -1,5 +1,5 @@
-function sanitizeAttributeName(origName) {
- let fixedName;
+function sanitizeAttributeName(origName: string) {
+ let fixedName: string;
if (origName === '') {
fixedName = "unnamed";
@@ -13,6 +13,4 @@ function sanitizeAttributeName(origName) {
}
-module.exports = {
- sanitizeAttributeName
-};
+export = sanitizeAttributeName;