From 7c76d28f75f2082dfda64c8360228487468d800c Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Tue, 9 Apr 2024 22:49:05 +0300 Subject: [PATCH] server-ts: Port share/shaca/shaca_loader --- src/share/routes.js | 2 +- src/share/shaca/entities/rows.ts | 4 +++ src/share/shaca/entities/sattachment.ts | 4 +-- src/share/shaca/entities/sattribute.ts | 4 +-- src/share/shaca/entities/sbranch.ts | 4 +-- src/share/shaca/entities/snote.ts | 4 +-- src/share/shaca/shaca-interface.ts | 2 +- .../{shaca_loader.js => shaca_loader.ts} | 30 +++++++++---------- src/share/sql.ts | 2 +- 9 files changed, 25 insertions(+), 31 deletions(-) create mode 100644 src/share/shaca/entities/rows.ts rename src/share/shaca/{shaca_loader.js => shaca_loader.ts} (77%) diff --git a/src/share/routes.js b/src/share/routes.js index 7c36d4b28..8bbb106c4 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 000000000..d8b8ec84e --- /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 352d34749..cb86f1106 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 e12a9d121..390a85b8c 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 4d906d750..a4cf57c51 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 f91949aef..50b03ec42 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 751412880..495741fcc 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 6b8e59e5e..c6a3fc4da 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(` 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(` 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(` 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(` 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 3d07df65f..a5465f4c2 100644 --- a/src/share/sql.ts +++ b/src/share/sql.ts @@ -23,7 +23,7 @@ function getRow(query: string, params: string[] = []): T { return dbConnection.prepare(query).get(params) as T; } -function getColumn(query: string, params = []): T[] { +function getColumn(query: string, params: string[] = []): T[] { return dbConnection.prepare(query).pluck().all(params) as T[]; }