Skip to content

Build your personal knowledge base with Trilium Notes

server-ts: Port share/shaca/shaca_loader

Elian Doran7c76d28f75f2
diff --git a/src/share/routes.js b/src/share/routes.js
index 7c36d4b289..8bbb106c42 100644
--- a/src/share/routes.js
+++ b/src/share/routes.js
@@ -4,7 +4,7 @@ const safeCompare = require('safe-compare');
const ejs = require("ejs");
const shaca = require('./shaca/shaca');
-const shacaLoader = require('./shaca/shaca_loader.js');
+const shacaLoader = require('./shaca/shaca_loader');
const shareRoot = require('./share_root');
const contentRenderer = require('./content_renderer.js');
const assetPath = require('../services/asset_path');
diff --git a/src/share/shaca/entities/rows.ts b/src/share/shaca/entities/rows.ts
new file mode 100644
index 0000000000..d8b8ec84e4
--- /dev/null
+++ b/src/share/shaca/entities/rows.ts
@@ -0,0 +1,4 @@
+type SNoteRow = [ string, string, string, string, string, string, boolean ];
+type SBranchRow = [ string, string, string, string, string, boolean ];
+type SAttributeRow = [ string, string, string, string, string, boolean, number ];
+type SAttachmentRow = [ string, string, string, string, string, string, string ];
diff --git a/src/share/shaca/entities/sattachment.ts b/src/share/shaca/entities/sattachment.ts
index 352d347490..cb86f11069 100644
--- a/src/share/shaca/entities/sattachment.ts
+++ b/src/share/shaca/entities/sattachment.ts
@@ -6,8 +6,6 @@ import AbstractShacaEntity = require('./abstract_shaca_entity');
import SNote = require('./snote');
import { Blob } from '../../../services/blob-interface';
-type AttachmentRow = [ string, string, string, string, string, string, string ];
-
class SAttachment extends AbstractShacaEntity {
private attachmentId: string;
private ownerId: string;
@@ -18,7 +16,7 @@ class SAttachment extends AbstractShacaEntity {
/** used for caching of images */
private utcDateModified: string;
- constructor([attachmentId, ownerId, role, mime, title, blobId, utcDateModified]: AttachmentRow) {
+ constructor([attachmentId, ownerId, role, mime, title, blobId, utcDateModified]: SAttachmentRow) {
super();
this.attachmentId = attachmentId;
diff --git a/src/share/shaca/entities/sattribute.ts b/src/share/shaca/entities/sattribute.ts
index e12a9d1216..390a85b8cc 100644
--- a/src/share/shaca/entities/sattribute.ts
+++ b/src/share/shaca/entities/sattribute.ts
@@ -4,8 +4,6 @@ import SNote = require("./snote");
const AbstractShacaEntity = require('./abstract_shaca_entity');
-type AttributeRow = [ string, string, string, string, string, boolean, number ];
-
class SAttribute extends AbstractShacaEntity {
attributeId: string;
@@ -16,7 +14,7 @@ class SAttribute extends AbstractShacaEntity {
value: string;
isInheritable: boolean;
- constructor([attributeId, noteId, type, name, value, isInheritable, position]: AttributeRow) {
+ constructor([attributeId, noteId, type, name, value, isInheritable, position]: SAttributeRow) {
super();
this.attributeId = attributeId;
diff --git a/src/share/shaca/entities/sbranch.ts b/src/share/shaca/entities/sbranch.ts
index 4d906d750e..a4cf57c511 100644
--- a/src/share/shaca/entities/sbranch.ts
+++ b/src/share/shaca/entities/sbranch.ts
@@ -3,8 +3,6 @@
import AbstractShacaEntity = require('./abstract_shaca_entity');
import SNote = require('./snote');
-type BranchRow = [ string, string, string, string, string, boolean ];
-
class SBranch extends AbstractShacaEntity {
private branchId: string;
@@ -14,7 +12,7 @@ class SBranch extends AbstractShacaEntity {
private isExpanded: boolean;
isHidden: boolean;
- constructor([branchId, noteId, parentNoteId, prefix, isExpanded]: BranchRow) {
+ constructor([branchId, noteId, parentNoteId, prefix, isExpanded]: SBranchRow) {
super();
this.branchId = branchId;
diff --git a/src/share/shaca/entities/snote.ts b/src/share/shaca/entities/snote.ts
index f91949aef2..50b03ec421 100644
--- a/src/share/shaca/entities/snote.ts
+++ b/src/share/shaca/entities/snote.ts
@@ -15,8 +15,6 @@ const CREDENTIALS = 'shareCredentials';
const isCredentials = (attr: SAttribute) => attr.type === 'label' && attr.name === CREDENTIALS;
-type NoteRow = [ string, string, string, string, string, string, boolean ];
-
class SNote extends AbstractShacaEntity {
noteId: string;
private title: string;
@@ -34,7 +32,7 @@ class SNote extends AbstractShacaEntity {
targetRelations: SAttribute[];
attachments: SAttachment[];
- constructor([noteId, title, type, mime, blobId, utcDateModified, isProtected]: NoteRow) {
+ constructor([noteId, title, type, mime, blobId, utcDateModified, isProtected]: SNoteRow) {
super();
this.noteId = noteId;
diff --git a/src/share/shaca/shaca-interface.ts b/src/share/shaca/shaca-interface.ts
index 751412880b..495741fcc5 100644
--- a/src/share/shaca/shaca-interface.ts
+++ b/src/share/shaca/shaca-interface.ts
@@ -14,7 +14,7 @@ export default class Shaca {
private shareRootNote!: SNote | null;
/** true if the index of all shared subtrees is enabled */
private shareIndexEnabled!: boolean;
- private loaded!: boolean;
+ loaded!: boolean;
constructor() {
this.reset();
diff --git a/src/share/shaca/shaca_loader.js b/src/share/shaca/shaca_loader.ts
similarity index 77%
rename from src/share/shaca/shaca_loader.js
rename to src/share/shaca/shaca_loader.ts
index 6b8e59e5e7..c6a3fc4daa 100644
--- a/src/share/shaca/shaca_loader.js
+++ b/src/share/shaca/shaca_loader.ts
@@ -1,14 +1,14 @@
"use strict";
-const sql = require('../sql');
-const shaca = require('./shaca');
-const log = require('../../services/log');
-const SNote = require('./entities/snote');
-const SBranch = require('./entities/sbranch');
-const SAttribute = require('./entities/sattribute');
-const SAttachment = require('./entities/sattachment');
-const shareRoot = require('../share_root');
-const eventService = require('../../services/events');
+import sql = require('../sql');
+import shaca = require('./shaca');
+import log = require('../../services/log');
+import SNote = require('./entities/snote');
+import SBranch = require('./entities/sbranch');
+import SAttribute = require('./entities/sattribute');
+import SAttachment = require('./entities/sattachment');
+import shareRoot = require('../share_root');
+import eventService = require('../../services/events');
function load() {
const start = Date.now();
@@ -35,7 +35,7 @@ function load() {
const noteIdStr = noteIds.map(noteId => `'${noteId}'`).join(",");
- const rawNoteRows = sql.getRawRows(`
+ const rawNoteRows = sql.getRawRows<SNoteRow>(`
SELECT noteId, title, type, mime, blobId, utcDateModified, isProtected
FROM notes
WHERE isDeleted = 0
@@ -45,7 +45,7 @@ function load() {
new SNote(row);
}
- const rawBranchRows = sql.getRawRows(`
+ const rawBranchRows = sql.getRawRows<SBranchRow>(`
SELECT branchId, noteId, parentNoteId, prefix, isExpanded, utcDateModified
FROM branches
WHERE isDeleted = 0
@@ -56,7 +56,7 @@ function load() {
new SBranch(row);
}
- const rawAttributeRows = sql.getRawRows(`
+ const rawAttributeRows = sql.getRawRows<SAttributeRow>(`
SELECT attributeId, noteId, type, name, value, isInheritable, position, utcDateModified
FROM attributes
WHERE isDeleted = 0
@@ -66,14 +66,12 @@ function load() {
new SAttribute(row);
}
- const rawAttachmentRows = sql.getRawRows(`
+ const rawAttachmentRows = sql.getRawRows<SAttachmentRow>(`
SELECT attachmentId, ownerId, role, mime, title, blobId, utcDateModified
FROM attachments
WHERE isDeleted = 0
AND ownerId IN (${noteIdStr})`);
- rawAttachmentRows.sort((a, b) => a.position < b.position ? -1 : 1);
-
for (const row of rawAttachmentRows) {
new SAttachment(row);
}
@@ -93,7 +91,7 @@ eventService.subscribe([eventService.ENTITY_CREATED, eventService.ENTITY_CHANGED
shaca.reset();
});
-module.exports = {
+export = {
load,
ensureLoad
};
diff --git a/src/share/sql.ts b/src/share/sql.ts
index 3d07df65ff..a5465f4c26 100644
--- a/src/share/sql.ts
+++ b/src/share/sql.ts
@@ -23,7 +23,7 @@ function getRow<T>(query: string, params: string[] = []): T {
return dbConnection.prepare(query).get(params) as T;
}
-function getColumn<T>(query: string, params = []): T[] {
+function getColumn<T>(query: string, params: string[] = []): T[] {
return dbConnection.prepare(query).pluck().all(params) as T[];
}