server-ts: Port battribute

This commit is contained in:
Elian Doran 2024-02-17 01:19:49 +02:00
parent eef8297ce1
commit 06287da9d8
No known key found for this signature in database
5 changed files with 54 additions and 37 deletions

View File

@ -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 = {};

View File

@ -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;

View File

@ -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;
}

View File

@ -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
};

View File

@ -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;