From 06287da9d8c2104f084005c7394ef107ffacdd62 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 17 Feb 2024 01:19:49 +0200 Subject: [PATCH] server-ts: Port battribute --- src/becca/becca-interface.ts | 8 ++-- .../entities/{battribute.js => battribute.ts} | 45 +++++++++---------- src/becca/entities/rows.ts | 13 ++++++ ...> promoted_attribute_definition_parser.ts} | 17 +++++-- ...ute_name.js => sanitize_attribute_name.ts} | 8 ++-- 5 files changed, 54 insertions(+), 37 deletions(-) rename src/becca/entities/{battribute.js => battribute.ts} (86%) rename src/services/{promoted_attribute_definition_parser.js => promoted_attribute_definition_parser.ts} (75%) rename src/services/{sanitize_attribute_name.js => sanitize_attribute_name.ts} (69%) diff --git a/src/becca/becca-interface.ts b/src/becca/becca-interface.ts index 954c34966..6b36892b3 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; + attributes!: Record; + /** Points from attribute type-name to list of attributes */ + attributeIndex!: Record; options!: Record; etapiTokens!: Record; @@ -25,9 +29,7 @@ class Becca { this.branches = {}; /** @type {Object.} */ this.childParentToBranch = {}; - /** @type {Object.} */ - this.attributes = {}; - /** @type {Object.} 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 54afd3587..71d575193 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 54c0e8b04..98bb0ec8a 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 937dae1de..cd3dc18db 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 = {}; 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 3a7c9ff67..ecdc35dc0 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;