Skip to content

Build your personal knowledge base with Trilium Notes

server-ts: Fix most type errors in becca

Elian Doran2f96dc2d9def
diff --git a/src/becca/entities/abstract_becca_entity.ts b/src/becca/entities/abstract_becca_entity.ts
index 506e66564b..53999be682 100644
--- a/src/becca/entities/abstract_becca_entity.ts
+++ b/src/becca/entities/abstract_becca_entity.ts
@@ -88,11 +88,17 @@ abstract class AbstractBeccaEntity<T extends AbstractBeccaEntity<T>> {
return this.getPojo();
}
- abstract hasStringContent(): boolean;
+ hasStringContent(): boolean {
+ // FIXME: Not sure why some entities don't implement it.
+ return true;
+ }
abstract getPojo(): {};
- abstract get isDeleted(): boolean;
+ get isDeleted(): boolean {
+ // FIXME: Not sure why some entities don't implement it.
+ return false;
+ }
/**
* Saves entity - executes SQL, but doesn't commit the transaction on its own
diff --git a/src/becca/entities/battachment.ts b/src/becca/entities/battachment.ts
index 9c46d00b7b..0d301494e0 100644
--- a/src/becca/entities/battachment.ts
+++ b/src/becca/entities/battachment.ts
@@ -36,7 +36,7 @@ class BAttachment extends AbstractBeccaEntity<BAttachment> {
noteId?: number;
attachmentId?: string;
/** either noteId or revisionId to which this attachment belongs */
- ownerId?: string;
+ ownerId: string;
role: string;
mime: string;
title: string;
diff --git a/src/becca/entities/bblob.ts b/src/becca/entities/bblob.ts
index 2ea67f0448..27d5595159 100644
--- a/src/becca/entities/bblob.ts
+++ b/src/becca/entities/bblob.ts
@@ -1,5 +1,6 @@
import { BlobRow } from "./rows";
+// FIXME: Why this does not extend the abstract becca?
class BBlob {
static get entityName() { return "blobs"; }
static get primaryKeyName() { return "blobId"; }
diff --git a/src/becca/entities/bbranch.ts b/src/becca/entities/bbranch.ts
index 2086fc8a00..bf64d2ac6f 100644
--- a/src/becca/entities/bbranch.ts
+++ b/src/becca/entities/bbranch.ts
@@ -180,7 +180,9 @@ class BBranch extends AbstractBeccaEntity<BBranch> {
}
for (const childBranch of note.getChildBranches()) {
- childBranch.deleteBranch(deleteId, taskContext);
+ if (childBranch) {
+ childBranch.deleteBranch(deleteId, taskContext);
+ }
}
// first delete children and then parent - this will show up better in recent changes
@@ -220,11 +222,17 @@ class BBranch extends AbstractBeccaEntity<BBranch> {
if (this.notePosition === undefined || this.notePosition === null) {
let maxNotePos = 0;
- for (const childBranch of this.parentNote.getChildBranches()) {
- if (maxNotePos < childBranch.notePosition
- && childBranch.noteId !== '_hidden' // hidden has a very large notePosition to always stay last
- ) {
- maxNotePos = childBranch.notePosition;
+ if (this.parentNote) {
+ for (const childBranch of this.parentNote.getChildBranches()) {
+ if (!childBranch) {
+ continue;
+ }
+
+ if (maxNotePos < childBranch.notePosition
+ && childBranch.noteId !== '_hidden' // hidden has a very large notePosition to always stay last
+ ) {
+ maxNotePos = childBranch.notePosition;
+ }
}
}
diff --git a/src/becca/entities/betapi_token.ts b/src/becca/entities/betapi_token.ts
index e4711c5fa7..d128c1beb2 100644
--- a/src/becca/entities/betapi_token.ts
+++ b/src/becca/entities/betapi_token.ts
@@ -22,7 +22,7 @@ class BEtapiToken extends AbstractBeccaEntity<BEtapiToken> {
etapiTokenId!: string;
name!: string;
tokenHash!: string;
- isDeleted!: boolean;
+ private _isDeleted!: boolean;
constructor(row: EtapiTokenRow) {
super();
@@ -35,13 +35,17 @@ class BEtapiToken extends AbstractBeccaEntity<BEtapiToken> {
this.init();
}
+ get isDeleted() {
+ return this._isDeleted;
+ }
+
updateFromRow(row: EtapiTokenRow) {
this.etapiTokenId = row.etapiTokenId;
this.name = row.name;
this.tokenHash = row.tokenHash;
this.utcDateCreated = row.utcDateCreated || dateUtils.utcNowDateTime();
this.utcDateModified = row.utcDateModified || this.utcDateCreated;
- this.isDeleted = !!row.isDeleted;
+ this._isDeleted = !!row.isDeleted;
if (this.etapiTokenId) {
this.becca.etapiTokens[this.etapiTokenId] = this;
diff --git a/src/becca/entities/bnote.ts b/src/becca/entities/bnote.ts
index 12c8ec5933..a6ae852842 100644
--- a/src/becca/entities/bnote.ts
+++ b/src/becca/entities/bnote.ts
@@ -203,8 +203,7 @@ class BNote extends AbstractBeccaEntity<BNote> {
return this.children && this.children.length > 0;
}
- /** @returns {BBranch[]} */
- getChildBranches() {
+ getChildBranches(): (BBranch | null)[] {
return this.children.map(childNote => this.becca.getBranchFromChildAndParent(childNote.noteId, this.noteId));
}
@@ -757,7 +756,7 @@ class BNote extends AbstractBeccaEntity<BNote> {
this.parents = this.parentBranches
.map(branch => branch.parentNote)
- .filter(note => !!note);
+ .filter(note => !!note) as BNote[];
}
sortChildren() {
@@ -771,7 +770,7 @@ class BNote extends AbstractBeccaEntity<BNote> {
const aBranch = becca.getBranchFromChildAndParent(a.noteId, this.noteId);
const bBranch = becca.getBranchFromChildAndParent(b.noteId, this.noteId);
- return (aBranch?.notePosition - bBranch?.notePosition) || 0;
+ return ((aBranch?.notePosition || 0) - (bBranch?.notePosition || 0)) || 0;
});
}
@@ -900,7 +899,7 @@ class BNote extends AbstractBeccaEntity<BNote> {
const {searchResultNoteIds} = searchService.searchFromNote(this);
const becca = this.becca;
- return searchResultNoteIds
+ return (searchResultNoteIds as string[]) // FIXME: remove cast once search is converted
.map(resultNoteId => becca.notes[resultNoteId])
.filter(note => !!note);
}
@@ -1445,9 +1444,9 @@ class BNote extends AbstractBeccaEntity<BNote> {
cloneTo(parentNoteId: string) {
const cloningService = require('../../services/cloning');
- const branch = this.becca.getNote(parentNoteId).getParentBranches()[0];
+ const branch = this.becca.getNote(parentNoteId)?.getParentBranches()[0];
- return cloningService.cloneNoteToBranch(this.noteId, branch.branchId);
+ return cloningService.cloneNoteToBranch(this.noteId, branch?.branchId);
}
isEligibleForConversionToAttachment(opts: ConvertOpts = { autoConversion: false }) {
@@ -1603,6 +1602,11 @@ class BNote extends AbstractBeccaEntity<BNote> {
for (const noteAttachment of this.getAttachments()) {
const revisionAttachment = noteAttachment.copy();
+
+ if (!revision.revisionId) {
+ throw new Error("Revision ID is missing.");
+ }
+
revisionAttachment.ownerId = revision.revisionId;
revisionAttachment.setContent(noteAttachment.getContent(), { forceSave: true });