diff --git a/src/entities/api_token.js b/src/entities/api_token.js index e4051a4dd..31a083af8 100644 --- a/src/entities/api_token.js +++ b/src/entities/api_token.js @@ -3,6 +3,16 @@ const Entity = require('./entity'); const dateUtils = require('../services/date_utils'); +/** + * ApiToken is an entity representing token used to authenticate against Trilium API from client applications. Currently used only by Trilium Sender. + * + * @param {string} apiTokenId - primary key + * @param {string} token + * @param {boolean} isDeleted - true if API token is deleted + * @param {string} dateCreated + * + * @extends Entity + */ class ApiToken extends Entity { static get entityName() { return "api_tokens"; } static get primaryKeyName() { return "apiTokenId"; } diff --git a/src/entities/attribute.js b/src/entities/attribute.js index 21bba9eef..c12f39a20 100644 --- a/src/entities/attribute.js +++ b/src/entities/attribute.js @@ -5,6 +5,22 @@ const repository = require('../services/repository'); const dateUtils = require('../services/date_utils'); const sql = require('../services/sql'); +/** + * Attribute is key value pair owned by a note. + * + * @param {string} attributeId + * @param {string} noteId + * @param {string} type + * @param {string} name + * @param {string} value + * @param {int} position + * @param {boolean} isInheritable + * @param {boolean} isDeleted + * @param {string} dateCreated + * @param {string} dateModified + * + * @extends Entity + */ class Attribute extends Entity { static get entityName() { return "attributes"; } static get primaryKeyName() { return "attributeId"; } diff --git a/src/entities/branch.js b/src/entities/branch.js index c8da8ede5..c24698a8a 100644 --- a/src/entities/branch.js +++ b/src/entities/branch.js @@ -5,6 +5,22 @@ const dateUtils = require('../services/date_utils'); const repository = require('../services/repository'); const sql = require('../services/sql'); +/** + * Branch represents note's placement in the tree - it's essentially pair of noteId and parentNoteId. + * Each note can have multiple (at least one) branches, meaning it can be placed into multiple places in the tree. + * + * @param {string} branchId - primary key + * @param {string} noteId + * @param {string} parentNoteId + * @param {int} notePosition + * @param {string} prefix + * @param {boolean} isExpanded + * @param {boolean} isDeleted + * @param {string} dateModified + * @param {string} dateCreated + * + * @extends Entity + */ class Branch extends Entity { static get entityName() { return "branches"; } static get primaryKeyName() { return "branchId"; } diff --git a/src/entities/entity.js b/src/entities/entity.js index 74da23cc6..7c7d8a961 100644 --- a/src/entities/entity.js +++ b/src/entities/entity.js @@ -3,6 +3,9 @@ const utils = require('../services/utils'); class Entity { + /** + * @param {object} [row] - database row representing given entity + */ constructor(row = {}) { for (const key in row) { this[key] = row[key]; diff --git a/src/entities/image.js b/src/entities/image.js index 9e42ab143..1de18164d 100644 --- a/src/entities/image.js +++ b/src/entities/image.js @@ -3,6 +3,20 @@ const Entity = require('./entity'); const dateUtils = require('../services/date_utils'); +/** + * This class represents image data. + * + * @param {string} imageId + * @param {string} format + * @param {string} checksum + * @param {string} name + * @param {blob} data + * @param {boolean} isDeleted + * @param {string} dateModified + * @param {string} dateCreated + * + * @extends Entity + */ class Image extends Entity { static get entityName() { return "images"; } static get primaryKeyName() { return "imageId"; } diff --git a/src/entities/note.js b/src/entities/note.js index 2c1f89e84..c7a0a5c59 100644 --- a/src/entities/note.js +++ b/src/entities/note.js @@ -9,11 +9,29 @@ const dateUtils = require('../services/date_utils'); const LABEL = 'label'; const RELATION = 'relation'; +/** + * This represents a Note which is a central object in the Trilium Notes project. + * + * @property {string} noteId - primary key + * @property {string} type - one of "text", "code", "file" or "render" + * @property {string} mime - MIME type, e.g. "text/html" + * @property {string} title - note title + * @property {string} content - note content - e.g. HTML text for text notes, file payload for files + * @property {boolean} isProtected - true if note is protected + * @property {boolean} isDeleted - true if note is deleted + * @property {string} dateCreated + * @property {string} dateModified + * + * @extends Entity + */ class Note extends Entity { static get entityName() { return "notes"; } static get primaryKeyName() { return "noteId"; } static get hashedProperties() { return ["noteId", "title", "content", "type", "isProtected", "isDeleted"]; } + /** + * @param row - object containing database row from "notes" table + */ constructor(row) { super(row); @@ -36,23 +54,28 @@ class Note extends Entity { catch(e) {} } + /** @returns {boolean} true if this note is the root of the note tree. Root note has "root" noteId */ isRoot() { return this.noteId === 'root'; } + /** @returns {boolean} true if this note is of application/json content type */ isJson() { return this.mime === "application/json"; } + /** @returns {boolean} true if this note is JavaScript (code or attachment) */ isJavaScript() { return (this.type === "code" || this.type === "file") && (this.mime.startsWith("application/javascript") || this.mime === "application/x-javascript"); } + /** @returns {boolean} true if this note is HTML */ isHtml() { return (this.type === "code" || this.type === "file" || this.type === "render") && this.mime === "text/html"; } + /** @returns {string} JS script environment - either "frontend" or "backend" */ getScriptEnv() { if (this.isHtml() || (this.isJavaScript() && this.mime.endsWith('env=frontend'))) { return "frontend"; @@ -69,10 +92,14 @@ class Note extends Entity { return null; } + /** + * @returns {Promise>} attributes belonging to this specific note (excludes inherited attributes) + */ async getOwnedAttributes() { return await repository.getEntities(`SELECT * FROM attributes WHERE isDeleted = 0 AND noteId = ?`, [this.noteId]); } + /** @returns {Promise>} all note's attributes, including inherited ones */ async getAttributes() { if (!this.__attributeCache) { await this.loadAttributesToCache(); @@ -81,18 +108,25 @@ class Note extends Entity { return this.__attributeCache; } + /** @returns {Promise>} all note's labels (attributes with type label), including inherited ones */ async getLabels() { return (await this.getAttributes()).filter(attr => attr.type === LABEL); } + /** @returns {Promise>} all note's relations (attributes with type relation), including inherited ones */ async getRelations() { return (await this.getAttributes()).filter(attr => attr.type === RELATION); } + /** + * Clear note's attributes cache to force fresh reload for next attribute request. + * Cache is note instance scoped. + */ invalidateAttributeCache() { this.__attributeCache = null; } + /** @returns {Promise} */ async loadAttributesToCache() { const attributes = await repository.getEntities(` WITH RECURSIVE @@ -156,24 +190,47 @@ class Note extends Entity { this.__attributeCache = filteredAttributes; } + /** + * @param {string} type - attribute type (label, relation, etc.) + * @param {string} name - attribute name + * @returns {Promise} true if note has an attribute with given type and name (including inherited) + */ async hasAttribute(type, name) { return !!await this.getAttribute(type, name); } - // WARNING: this doesn't take into account the possibility to have multi-valued labels! + /** + * @param {string} type - attribute type (label, relation, etc.) + * @param {string} name - attribute name + * @returns {Promise} attribute of given type and name. If there's more such attributes, first is returned. Returns null if there's no such attribute belonging to this note. + */ async getAttribute(type, name) { const attributes = await this.getAttributes(); return attributes.find(attr => attr.type === type && attr.name === name); } + /** + * @param {string} type - attribute type (label, relation, etc.) + * @param {string} name - attribute name + * @returns {Promise} attribute value of given type and name or null if no such attribute exists. + */ async getAttributeValue(type, name) { const attr = await this.getAttribute(type, name); return attr ? attr.value : null; } - async toggleAttribute(type, enabled, name, value = "") { + /** + * Based on enabled, attribute is either set or removed. + * + * @param {string} type - attribute type ('relation', 'label' etc.) + * @param {boolean} enabled - toggle On or Off + * @param {string} name - attribute name + * @param {string} [value] - attribute value (optional) + * @returns {Promise} + */ + async toggleAttribute(type, enabled, name, value) { if (enabled) { await this.setAttribute(type, name, value); } @@ -182,16 +239,24 @@ class Note extends Entity { } } - async setAttribute(type, name, value = "") { + /** + * Creates given attribute name-value pair if it doesn't exist. + * + * @param {string} type - attribute type (label, relation, etc.) + * @param {string} name - attribute name + * @param {string} [value] - attribute value (optional) + * @returns {Promise} + */ + async setAttribute(type, name, value) { const attributes = await this.getOwnedAttributes(); - let attr = attributes.find(attr => attr.type === type && attr.value === value); + let attr = attributes.find(attr => attr.type === type && (value === undefined || attr.value === value)); if (!attr) { attr = new Attribute({ noteId: this.noteId, type: type, name: name, - value: value + value: value !== undefined ? value : "" }); await attr.save(); @@ -200,11 +265,19 @@ class Note extends Entity { } } - async removeAttribute(type, name, value = "") { + /** + * Removes given attribute name-value pair if it exists. + * + * @param {string} type - attribute type (label, relation, etc.) + * @param {string} name - attribute name + * @param {string} [value] - attribute value (optional) + * @returns {Promise} + */ + async removeAttribute(type, name, value) { const attributes = await this.getOwnedAttributes(); for (const attribute of attributes) { - if (attribute.type === type && (!value || value === attribute.value)) { + if (attribute.type === type && (value === undefined || value === attribute.value)) { attribute.isDeleted = true; await attribute.save(); @@ -213,30 +286,116 @@ class Note extends Entity { } } + /** + * @param {string} name - label name + * @returns {Promise} true if label exists (including inherited) + */ async hasLabel(name) { return await this.hasAttribute(LABEL, name); } + + /** + * @param {string} name - relation name + * @returns {Promise} true if relation exists (including inherited) + */ async hasRelation(name) { return await this.hasAttribute(RELATION, name); } + /** + * @param {string} name - label name + * @returns {Promise} label if it exists, null otherwise + */ async getLabel(name) { return await this.getAttribute(LABEL, name); } + + /** + * @param {string} name - relation name + * @returns {Promise} relation if it exists, null otherwise + */ async getRelation(name) { return await this.getAttribute(RELATION, name); } + /** + * @param {string} name - label name + * @returns {Promise} label value if label exists, null otherwise + */ async getLabelValue(name) { return await this.getAttributeValue(LABEL, name); } + + /** + * @param {string} name - relation name + * @returns {Promise} relation value if relation exists, null otherwise + */ async getRelationValue(name) { return await this.getAttributeValue(RELATION, name); } - async toggleLabel(enabled, name, value = "") { return await this.toggleAttribute(LABEL, enabled, name, value); } - async toggleRelation(enabled, name, value = "") { return await this.toggleAttribute(RELATION, enabled, name, value); } + /** + * Based on enabled, label is either set or removed. + * + * @param {boolean} enabled - toggle On or Off + * @param {string} name - label name + * @param {string} [value] - label value (optional) + * @returns {Promise} + */ + async toggleLabel(enabled, name, value) { return await this.toggleAttribute(LABEL, enabled, name, value); } - async setLabel(name, value = "") { return await this.setAttribute(LABEL, name, value); } - async setRelation(name, value = "") { return await this.setAttribute(RELATION, name, value); } + /** + * Based on enabled, relation is either set or removed. + * + * @param {boolean} enabled - toggle On or Off + * @param {string} name - relation name + * @param {string} [value] - relation value (noteId) + * @returns {Promise} + */ + async toggleRelation(enabled, name, value) { return await this.toggleAttribute(RELATION, enabled, name, value); } - async removeLabel(name, value = "") { return await this.removeAttribute(LABEL, name, value); } - async removeRelation(name, value = "") { return await this.removeAttribute(RELATION, name, value); } + /** + * Create label name-value pair if it doesn't exist yet. + * + * @param {string} name - label name + * @param {string} [value] - label value + * @returns {Promise} + */ + async setLabel(name, value) { return await this.setAttribute(LABEL, name, value); } + /** + * Create relation name-value pair if it doesn't exist yet. + * + * @param {string} name - relation name + * @param {string} [value] - relation value (noteId) + * @returns {Promise} + */ + async setRelation(name, value) { return await this.setAttribute(RELATION, name, value); } + + /** + * Remove label name-value pair, if it exists. + * + * @param {string} name - label name + * @param {string} [value] - label value + * @returns {Promise} + */ + async removeLabel(name, value) { return await this.removeAttribute(LABEL, name, value); } + + /** + * Remove relation name-value pair, if it exists. + * + * @param {string} name - relation name + * @param {string} [value] - relation value (noteId) + * @returns {Promise} + */ + async removeRelation(name, value) { return await this.removeAttribute(RELATION, name, value); } + + /** + * @param {string} name + * @returns {Promise|null} target note of the relation or null (if target is empty or note was not found) + */ async getRelationTarget(name) { const relation = await this.getRelation(name); return relation ? await repository.getNote(relation.value) : null; } + /** + * Finds notes with given attribute name and value. Only own attributes are considered, not inherited ones + * + * @param {string} type - attribute type (label, relation, etc.) + * @param {string} name - attribute name + * @param {string} [value] - attribute value + * @returns {Promise>} + */ async findNotesWithAttribute(type, name, value) { const params = [this.noteId, name]; let valueCondition = ""; @@ -268,32 +427,50 @@ class Note extends Entity { return notes; } + /** + * Finds notes with given label name and value. Only own labels are considered, not inherited ones + * + * @param {string} name - label name + * @param {string} [value] - label value + * @returns {Promise>} + */ async findNotesWithLabel(name, value) { return await this.findNotesWithAttribute(LABEL, name, value); } + + /** + * Finds notes with given relation name and value. Only own relations are considered, not inherited ones + * + * @param {string} name - relation name + * @param {string} [value] - relation value + * @returns {Promise>} + */ async findNotesWithRelation(name, value) { return await this.findNotesWithAttribute(RELATION, name, value); } + /** + * Returns note revisions of this note. + * + * @returns {Promise>} + */ async getRevisions() { return await repository.getEntities("SELECT * FROM note_revisions WHERE noteId = ?", [this.noteId]); } + /** + * @returns {Promise>} + */ async getNoteImages() { return await repository.getEntities("SELECT * FROM note_images WHERE noteId = ? AND isDeleted = 0", [this.noteId]); } + /** + * @returns {Promise>} + */ async getBranches() { return await repository.getEntities("SELECT * FROM branches WHERE isDeleted = 0 AND noteId = ?", [this.noteId]); } - async getChildNote(name) { - return await repository.getEntity(` - SELECT notes.* - FROM branches - JOIN notes USING(noteId) - WHERE notes.isDeleted = 0 - AND branches.isDeleted = 0 - AND branches.parentNoteId = ? - AND notes.title = ?`, [this.noteId, name]); - } - + /** + * @returns {Promise>} child notes of this note + */ async getChildNotes() { return await repository.getEntities(` SELECT notes.* @@ -305,6 +482,9 @@ class Note extends Entity { ORDER BY branches.notePosition`, [this.noteId]); } + /** + * @returns {Promise>} child branches of this note + */ async getChildBranches() { return await repository.getEntities(` SELECT branches.* @@ -314,6 +494,9 @@ class Note extends Entity { ORDER BY branches.notePosition`, [this.noteId]); } + /** + * @returns {Promise>} parent notes of this note (note can have multiple parents because of cloning) + */ async getParentNotes() { return await repository.getEntities(` SELECT parent_notes.* diff --git a/src/entities/note_image.js b/src/entities/note_image.js index cd271acfe..584a9241a 100644 --- a/src/entities/note_image.js +++ b/src/entities/note_image.js @@ -4,6 +4,18 @@ const Entity = require('./entity'); const repository = require('../services/repository'); const dateUtils = require('../services/date_utils'); +/** + * This class represents image's placement in the note(s). One image may be placed into several notes. + * + * @param {string} noteImageId + * @param {string} noteId + * @param {string} imageId + * @param {boolean} isDeleted + * @param {string} dateModified + * @param {string} dateCreated + * + * @extends Entity + */ class NoteImage extends Entity { static get entityName() { return "note_images"; } static get primaryKeyName() { return "noteImageId"; } diff --git a/src/entities/note_revision.js b/src/entities/note_revision.js index f4ff7a7ae..fb2213d93 100644 --- a/src/entities/note_revision.js +++ b/src/entities/note_revision.js @@ -4,6 +4,21 @@ const Entity = require('./entity'); const protectedSessionService = require('../services/protected_session'); const repository = require('../services/repository'); +/** + * NoteRevision represents snapshot of note's title and content at some point in the past. It's used for seamless note versioning. + * + * @param {string} noteRevisionId + * @param {string} noteId + * @param {string} type + * @param {string} mime + * @param {string} title + * @param {string} content + * @param {string} isProtected + * @param {string} dateModifiedFrom + * @param {string} dateModifiedTo + * + * @extends Entity + */ class NoteRevision extends Entity { static get entityName() { return "note_revisions"; } static get primaryKeyName() { return "noteRevisionId"; } diff --git a/src/entities/option.js b/src/entities/option.js index 896e053f1..b029a1bb1 100644 --- a/src/entities/option.js +++ b/src/entities/option.js @@ -3,6 +3,17 @@ const Entity = require('./entity'); const dateUtils = require('../services/date_utils'); +/** + * Option represents name-value pair, either directly configurable by the user or some system property. + * + * @param {string} name + * @param {string} value + * @param {boolean} isSynced + * @param {string} dateModified + * @param {string} dateCreated + * + * @extends Entity + */ class Option extends Entity { static get entityName() { return "options"; } static get primaryKeyName() { return "name"; } diff --git a/src/entities/recent_note.js b/src/entities/recent_note.js index 7baca564c..32d8515be 100644 --- a/src/entities/recent_note.js +++ b/src/entities/recent_note.js @@ -3,6 +3,16 @@ const Entity = require('./entity'); const dateUtils = require('../services/date_utils'); +/** + * RecentNote represents recently visited note. + * + * @param {string} branchId + * @param {string} notePath + * @param {boolean} isDeleted + * @param {string} dateModified + * + * @extends Entity + */ class RecentNote extends Entity { static get entityName() { return "recent_notes"; } static get primaryKeyName() { return "branchId"; }