trilium/docs/backend_api/entities_note.js.html

986 lines
33 KiB
HTML
Raw Normal View History

2018-08-30 02:44:35 +08:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: entities/note.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: entities/note.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>"use strict";
const Entity = require('./entity');
const Attribute = require('./attribute');
const protectedSessionService = require('../services/protected_session');
2018-12-23 05:28:49 +08:00
const sql = require('../services/sql');
2019-03-28 04:04:25 +08:00
const utils = require('../services/utils');
2018-08-30 02:44:35 +08:00
const dateUtils = require('../services/date_utils');
2020-10-01 04:48:30 +08:00
const entityChangesService = require('../services/entity_changes.js');
2018-08-30 02:44:35 +08:00
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 {boolean} isProtected - true if note is protected
* @property {boolean} isDeleted - true if note is deleted
2020-02-02 18:44:08 +08:00
* @property {string|null} deleteId - ID identifying delete transaction
2019-11-09 05:34:30 +08:00
* @property {boolean} isErased - true if note's content is erased after it has been deleted
* @property {string} dateCreated - local date time (with offset)
* @property {string} dateModified - local date time (with offset)
* @property {string} utcDateCreated
* @property {string} utcDateModified
2018-08-30 02:44:35 +08:00
*
* @extends Entity
*/
class Note extends Entity {
static get entityName() { return "notes"; }
static get primaryKeyName() { return "noteId"; }
2020-03-08 18:41:42 +08:00
static get hashedProperties() { return ["noteId", "title", "type", "mime", "isProtected", "isDeleted", "deleteId"]; }
2018-08-30 02:44:35 +08:00
/**
* @param row - object containing database row from "notes" table
*/
constructor(row) {
super(row);
this.isProtected = !!this.isProtected;
2019-11-09 05:34:30 +08:00
/* true if content is either not encrypted
* or encrypted, but with available protected session (so effectively decrypted) */
this.isContentAvailable = true;
2018-08-30 02:44:35 +08:00
// check if there's noteId, otherwise this is a new entity which wasn't encrypted yet
if (this.isProtected &amp;&amp; this.noteId) {
this.isContentAvailable = protectedSessionService.isProtectedSessionAvailable();
2019-02-21 05:24:51 +08:00
if (this.isContentAvailable) {
2019-11-09 05:34:30 +08:00
this.title = protectedSessionService.decryptString(this.title);
2019-02-21 05:24:51 +08:00
}
else {
this.title = "[protected]";
}
}
}
/*
* Note content has quite special handling - it's not a separate entity, but a lazily loaded
* part of Note entity with it's own sync. Reasons behind this hybrid design has been:
*
* - content can be quite large and it's not necessary to load it / fill memory for any note access even if we don't need a content, especially for bulk operations like search
2020-10-01 04:48:30 +08:00
* - changes in the note metadata or title should not trigger note content sync (so we keep separate utcDateModified and entity changes records)
* - but to the user note content and title changes are one and the same - single dateModified (so all changes must go through Note and content is not a separate entity)
*/
2020-10-01 04:48:30 +08:00
/** @returns {*} */
getContent(silentNotFoundError = false) {
2019-03-28 04:04:25 +08:00
if (this.content === undefined) {
2020-10-01 04:48:30 +08:00
const res = sql.getRow(`SELECT content, hash FROM note_contents WHERE noteId = ?`, [this.noteId]);
if (!res) {
if (silentNotFoundError) {
return undefined;
}
else {
throw new Error("Cannot find note content for noteId=" + this.noteId);
}
}
this.content = res.content;
2019-02-21 05:24:51 +08:00
2019-03-28 04:04:25 +08:00
if (this.isProtected) {
if (this.isContentAvailable) {
2019-11-09 05:34:30 +08:00
this.content = this.content === null ? null : protectedSessionService.decrypt(this.content);
2019-03-28 04:04:25 +08:00
}
else {
this.content = "";
}
2019-02-21 05:24:51 +08:00
}
2018-08-30 02:44:35 +08:00
}
2020-04-08 01:19:20 +08:00
if (this.isStringNote()) {
return this.content === null
? ""
: this.content.toString("UTF-8");
}
else {
return this.content;
}
2019-02-21 05:24:51 +08:00
}
2020-10-01 04:48:30 +08:00
/** @returns {{contentLength, dateModified, utcDateModified}} */
getContentMetadata() {
return sql.getRow(`
SELECT
LENGTH(content) AS contentLength,
dateModified,
utcDateModified
FROM note_contents
WHERE noteId = ?`, [this.noteId]);
}
/** @returns {*} */
getJsonContent() {
const content = this.getContent();
2019-02-21 05:24:51 +08:00
2020-02-02 18:44:08 +08:00
if (!content || !content.trim()) {
return null;
}
2019-02-21 05:24:51 +08:00
return JSON.parse(content);
}
2020-10-01 04:48:30 +08:00
setContent(content) {
2019-11-19 06:01:31 +08:00
if (content === null || content === undefined) {
throw new Error(`Cannot set null content to note ${this.noteId}`);
}
2020-10-01 04:48:30 +08:00
if (this.isStringNote()) {
content = content.toString();
}
else {
content = Buffer.isBuffer(content) ? content : Buffer.from(content);
}
2019-03-28 04:04:25 +08:00
this.content = content;
const pojo = {
noteId: this.noteId,
content: content,
2020-10-01 04:48:30 +08:00
dateModified: dateUtils.localNowDateTime(),
2019-03-28 04:04:25 +08:00
utcDateModified: dateUtils.utcNowDateTime(),
2020-02-02 18:44:08 +08:00
hash: utils.hash(this.noteId + "|" + content.toString())
2019-03-28 04:04:25 +08:00
};
if (this.isProtected) {
if (this.isContentAvailable) {
2019-11-09 05:34:30 +08:00
pojo.content = protectedSessionService.encrypt(pojo.content);
2019-03-28 04:04:25 +08:00
}
else {
throw new Error(`Cannot update content of noteId=${this.noteId} since we're out of protected session.`);
}
2018-08-30 02:44:35 +08:00
}
2019-02-21 05:24:51 +08:00
2020-10-01 04:48:30 +08:00
sql.upsert("note_contents", "noteId", pojo);
2019-03-28 04:04:25 +08:00
2020-10-01 04:48:30 +08:00
entityChangesService.addNoteContentEntityChange(this.noteId);
2019-02-21 05:24:51 +08:00
}
2020-10-01 04:48:30 +08:00
setJsonContent(content) {
this.setContent(JSON.stringify(content, null, '\t'));
2018-08-30 02:44:35 +08:00
}
/** @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")
2018-12-23 05:28:49 +08:00
&amp;&amp; (this.mime.startsWith("application/javascript")
|| this.mime === "application/x-javascript"
|| this.mime === "text/javascript");
2018-08-30 02:44:35 +08:00
}
/** @returns {boolean} true if this note is HTML */
isHtml() {
return (this.type === "code" || this.type === "file" || this.type === "render") &amp;&amp; this.mime === "text/html";
}
2019-02-21 05:24:51 +08:00
/** @returns {boolean} true if the note has string content (not binary) */
isStringNote() {
2019-11-09 05:34:30 +08:00
return utils.isStringNote(this.type, this.mime);
2019-02-21 05:24:51 +08:00
}
2018-08-30 02:44:35 +08:00
/** @returns {string} JS script environment - either "frontend" or "backend" */
getScriptEnv() {
if (this.isHtml() || (this.isJavaScript() &amp;&amp; this.mime.endsWith('env=frontend'))) {
return "frontend";
}
if (this.type === 'render') {
return "frontend";
}
if (this.isJavaScript() &amp;&amp; this.mime.endsWith('env=backend')) {
return "backend";
}
return null;
}
2020-10-01 04:48:30 +08:00
loadOwnedAttributesToCache() {
this.__ownedAttributeCache = this.repository.getEntities(`SELECT * FROM attributes WHERE isDeleted = 0 AND noteId = ?`, [this.noteId]);
2019-12-04 05:53:17 +08:00
return this.__ownedAttributeCache;
}
2018-08-30 02:44:35 +08:00
/**
2019-12-04 05:53:17 +08:00
* This method is a faster variant of getAttributes() which looks for only owned attributes.
* Use when inheritance is not needed and/or in batch/performance sensitive operations.
*
2019-12-04 05:53:17 +08:00
* @param {string} [type] - (optional) attribute type to filter
* @param {string} [name] - (optional) attribute name to filter
2020-10-01 04:48:30 +08:00
* @returns {Attribute[]} note's "owned" attributes - excluding inherited ones
2018-08-30 02:44:35 +08:00
*/
2020-10-01 04:48:30 +08:00
getOwnedAttributes(type, name) {
2019-12-04 05:53:17 +08:00
if (!this.__ownedAttributeCache) {
2020-10-01 04:48:30 +08:00
this.loadOwnedAttributesToCache();
}
2019-12-04 05:53:17 +08:00
if (type &amp;&amp; name) {
return this.__ownedAttributeCache.filter(attr => attr.type === type &amp;&amp; attr.name === name);
}
else if (type) {
return this.__ownedAttributeCache.filter(attr => attr.type === type);
}
else if (name) {
return this.__ownedAttributeCache.filter(attr => attr.name === name);
}
else {
return this.__ownedAttributeCache.slice();
}
}
/**
2020-10-01 04:48:30 +08:00
* @returns {Attribute} attribute belonging to this specific note (excludes inherited attributes)
*
* This method can be significantly faster than the getAttribute()
*/
2020-10-01 04:48:30 +08:00
getOwnedAttribute(type, name) {
const attrs = this.getOwnedAttributes(type, name);
return attrs.length > 0 ? attrs[0] : null;
2018-08-30 02:44:35 +08:00
}
/**
2020-10-01 04:48:30 +08:00
* @returns {Attribute[]} relations targetting this specific note
*/
2020-10-01 04:48:30 +08:00
getTargetRelations() {
return this.repository.getEntities("SELECT * FROM attributes WHERE type = 'relation' AND isDeleted = 0 AND value = ?", [this.noteId]);
}
/**
2019-12-04 05:53:17 +08:00
* @param {string} [type] - (optional) attribute type to filter
* @param {string} [name] - (optional) attribute name to filter
2020-10-01 04:48:30 +08:00
* @returns {Attribute[]} all note's attributes, including inherited ones
*/
2020-10-01 04:48:30 +08:00
getAttributes(type, name) {
2018-08-30 02:44:35 +08:00
if (!this.__attributeCache) {
2020-10-01 04:48:30 +08:00
this.loadAttributesToCache();
2018-08-30 02:44:35 +08:00
}
2019-12-04 05:53:17 +08:00
if (type &amp;&amp; name) {
return this.__attributeCache.filter(attr => attr.type === type &amp;&amp; attr.name === name);
}
else if (type) {
return this.__attributeCache.filter(attr => attr.type === type);
}
else if (name) {
return this.__attributeCache.filter(attr => attr.name === name);
}
else {
2019-12-04 05:53:17 +08:00
return this.__attributeCache.slice();
}
2018-08-30 02:44:35 +08:00
}
/**
* @param {string} [name] - label name to filter
2020-10-01 04:48:30 +08:00
* @returns {Attribute[]} all note's labels (attributes with type label), including inherited ones
*/
2020-10-01 04:48:30 +08:00
getLabels(name) {
return this.getAttributes(LABEL, name);
2019-12-04 05:53:17 +08:00
}
/**
* @param {string} [name] - label name to filter
2020-10-01 04:48:30 +08:00
* @returns {Attribute[]} all note's labels (attributes with type label), excluding inherited ones
2019-12-04 05:53:17 +08:00
*/
2020-10-01 04:48:30 +08:00
getOwnedLabels(name) {
return this.getOwnedAttributes(LABEL, name);
}
/**
* @param {string} [name] - relation name to filter
2020-10-01 04:48:30 +08:00
* @returns {Attribute[]} all note's relations (attributes with type relation), including inherited ones
*/
2020-10-01 04:48:30 +08:00
getRelations(name) {
return this.getAttributes(RELATION, name);
2019-12-04 05:53:17 +08:00
}
/**
* @param {string} [name] - relation name to filter
2020-10-01 04:48:30 +08:00
* @returns {Attribute[]} all note's relations (attributes with type relation), excluding inherited ones
2019-12-04 05:53:17 +08:00
*/
2020-10-01 04:48:30 +08:00
getOwnedRelations(name) {
return this.getOwnedAttributes(RELATION, name);
2018-08-30 02:44:35 +08:00
}
/**
* @param {string} [name] - relation name to filter
2020-10-01 04:48:30 +08:00
* @returns {Note[]}
*/
2020-10-01 04:48:30 +08:00
getRelationTargets(name) {
const relations = this.getRelations(name);
const targets = [];
for (const relation of relations) {
2020-10-01 04:48:30 +08:00
targets.push(relation.getTargetNote());
}
return targets;
}
2018-08-30 02:44:35 +08:00
/**
* Clear note's attributes cache to force fresh reload for next attribute request.
* Cache is note instance scoped.
*/
invalidateAttributeCache() {
this.__attributeCache = null;
2019-12-04 05:53:17 +08:00
this.__ownedAttributeCache = null;
2018-08-30 02:44:35 +08:00
}
2020-10-01 04:48:30 +08:00
loadAttributesToCache() {
const attributes = this.repository.getEntities(`
2018-08-30 02:44:35 +08:00
WITH RECURSIVE
tree(noteId, level) AS (
SELECT ?, 0
UNION
2019-12-04 05:53:17 +08:00
SELECT branches.parentNoteId, tree.level + 1
FROM branches
2018-08-30 02:44:35 +08:00
JOIN tree ON branches.noteId = tree.noteId
2019-12-04 05:53:17 +08:00
WHERE branches.isDeleted = 0
2018-08-30 02:44:35 +08:00
),
treeWithAttrs(noteId, level) AS (
SELECT * FROM tree
UNION
SELECT attributes.value, treeWithAttrs.level FROM attributes
2018-08-30 02:44:35 +08:00
JOIN treeWithAttrs ON treeWithAttrs.noteId = attributes.noteId
WHERE attributes.isDeleted = 0
AND attributes.type = 'relation'
AND attributes.name = 'template'
AND (treeWithAttrs.level = 0 OR attributes.isInheritable = 1)
2018-08-30 02:44:35 +08:00
)
SELECT attributes.* FROM attributes JOIN treeWithAttrs ON attributes.noteId = treeWithAttrs.noteId
WHERE attributes.isDeleted = 0 AND (attributes.isInheritable = 1 OR treeWithAttrs.level = 0)
ORDER BY level, noteId, position`, [this.noteId]);
2018-08-30 02:44:35 +08:00
// attributes are ordered so that "closest" attributes are first
// we order by noteId so that attributes from same note stay together. Actual noteId ordering doesn't matter.
const filteredAttributes = attributes.filter((attr, index) => {
2019-12-04 05:53:17 +08:00
// if this exact attribute already appears then don't include it (can happen via cloning)
if (attributes.findIndex(it => it.attributeId === attr.attributeId) !== index) {
return false;
}
2020-10-01 04:48:30 +08:00
// FIXME: this code is quite questionable, one problem is that other caches (TreeCache, NoteCache) have nothing like that
2018-08-30 02:44:35 +08:00
if (attr.isDefinition()) {
const firstDefinitionIndex = attributes.findIndex(el => el.type === attr.type &amp;&amp; el.name === attr.name);
// keep only if this element is the first definition for this type &amp; name
return firstDefinitionIndex === index;
}
else {
2020-10-01 04:48:30 +08:00
const definitionAttr = attributes.find(el => el.type === 'label' &amp;&amp; el.name === attr.type + ':' + attr.name);
2018-08-30 02:44:35 +08:00
if (!definitionAttr) {
return true;
}
2020-10-01 04:48:30 +08:00
const definition = definitionAttr.getDefinition();
2018-08-30 02:44:35 +08:00
2020-10-01 04:48:30 +08:00
if (definition.multiplicity === 'multi') {
2018-08-30 02:44:35 +08:00
return true;
}
else {
const firstAttrIndex = attributes.findIndex(el => el.type === attr.type &amp;&amp; el.name === attr.name);
// in case of single-valued attribute we'll keep it only if it's first (closest)
return firstAttrIndex === index;
}
}
});
this.__attributeCache = filteredAttributes;
}
/**
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
2020-10-01 04:48:30 +08:00
* @returns {boolean} true if note has an attribute with given type and name (including inherited)
2018-08-30 02:44:35 +08:00
*/
2020-10-01 04:48:30 +08:00
hasAttribute(type, name) {
return !!this.getAttribute(type, name);
2018-08-30 02:44:35 +08:00
}
2019-12-04 05:53:17 +08:00
/**
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
2020-10-01 04:48:30 +08:00
* @returns {boolean} true if note has an attribute with given type and name (excluding inherited)
2019-12-04 05:53:17 +08:00
*/
2020-10-01 04:48:30 +08:00
hasOwnedAttribute(type, name) {
return !!this.getOwnedAttribute(type, name);
2019-12-04 05:53:17 +08:00
}
2018-08-30 02:44:35 +08:00
/**
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
2020-10-01 04:48:30 +08:00
* @returns {Attribute} 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.
2018-08-30 02:44:35 +08:00
*/
2020-10-01 04:48:30 +08:00
getAttribute(type, name) {
const attributes = this.getAttributes();
2018-08-30 02:44:35 +08:00
return attributes.find(attr => attr.type === type &amp;&amp; attr.name === name);
}
/**
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
2020-10-01 04:48:30 +08:00
* @returns {string|null} attribute value of given type and name or null if no such attribute exists.
2018-08-30 02:44:35 +08:00
*/
2020-10-01 04:48:30 +08:00
getAttributeValue(type, name) {
const attr = this.getAttribute(type, name);
2018-08-30 02:44:35 +08:00
return attr ? attr.value : null;
}
2019-12-04 05:53:17 +08:00
/**
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
2020-10-01 04:48:30 +08:00
* @returns {string|null} attribute value of given type and name or null if no such attribute exists.
2019-12-04 05:53:17 +08:00
*/
2020-10-01 04:48:30 +08:00
getOwnedAttributeValue(type, name) {
const attr = this.getOwnedAttribute(type, name);
2019-12-04 05:53:17 +08:00
return attr ? attr.value : null;
}
2018-08-30 02:44:35 +08:00
/**
* 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)
*/
2020-10-01 04:48:30 +08:00
toggleAttribute(type, enabled, name, value) {
2018-08-30 02:44:35 +08:00
if (enabled) {
2020-10-01 04:48:30 +08:00
this.setAttribute(type, name, value);
2018-08-30 02:44:35 +08:00
}
else {
2020-10-01 04:48:30 +08:00
this.removeAttribute(type, name, value);
2018-08-30 02:44:35 +08:00
}
}
/**
2019-11-09 05:34:30 +08:00
* Update's given attribute's value or creates it if it doesn't exist
2018-08-30 02:44:35 +08:00
*
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
* @param {string} [value] - attribute value (optional)
*/
2020-10-01 04:48:30 +08:00
setAttribute(type, name, value) {
const attributes = this.loadOwnedAttributesToCache();
2019-11-09 05:34:30 +08:00
let attr = attributes.find(attr => attr.type === type &amp;&amp; attr.name === name);
if (attr) {
if (attr.value !== value) {
attr.value = value;
2020-10-01 04:48:30 +08:00
attr.save();
2018-08-30 02:44:35 +08:00
2019-11-09 05:34:30 +08:00
this.invalidateAttributeCache();
}
}
else {
2018-08-30 02:44:35 +08:00
attr = new Attribute({
noteId: this.noteId,
type: type,
name: name,
value: value !== undefined ? value : ""
});
2020-10-01 04:48:30 +08:00
attr.save();
2018-08-30 02:44:35 +08:00
this.invalidateAttributeCache();
}
}
/**
* 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)
*/
2020-10-01 04:48:30 +08:00
removeAttribute(type, name, value) {
const attributes = this.loadOwnedAttributesToCache();
2018-08-30 02:44:35 +08:00
for (const attribute of attributes) {
if (attribute.type === type &amp;&amp; attribute.name === name &amp;&amp; (value === undefined || value === attribute.value)) {
2018-08-30 02:44:35 +08:00
attribute.isDeleted = true;
2020-10-01 04:48:30 +08:00
attribute.save();
2018-08-30 02:44:35 +08:00
this.invalidateAttributeCache();
}
}
}
2019-11-19 06:01:31 +08:00
/**
2020-10-01 04:48:30 +08:00
* @return {Attribute}
2019-11-19 06:01:31 +08:00
*/
2020-10-01 04:48:30 +08:00
addAttribute(type, name, value = "", isInheritable = false, position = 1000) {
2019-11-19 06:01:31 +08:00
const attr = new Attribute({
noteId: this.noteId,
type: type,
name: name,
2020-10-01 04:48:30 +08:00
value: value,
isInheritable: isInheritable,
position: position
2019-11-19 06:01:31 +08:00
});
2020-10-01 04:48:30 +08:00
attr.save();
2019-11-19 06:01:31 +08:00
this.invalidateAttributeCache();
return attr;
}
2020-10-01 04:48:30 +08:00
addLabel(name, value = "", isInheritable = false) {
return this.addAttribute(LABEL, name, value, isInheritable);
2019-11-19 06:01:31 +08:00
}
2020-10-01 04:48:30 +08:00
addRelation(name, targetNoteId, isInheritable = false) {
return this.addAttribute(RELATION, name, targetNoteId, isInheritable);
2019-11-19 06:01:31 +08:00
}
2018-08-30 02:44:35 +08:00
/**
* @param {string} name - label name
2020-10-01 04:48:30 +08:00
* @returns {boolean} true if label exists (including inherited)
2018-08-30 02:44:35 +08:00
*/
2020-10-01 04:48:30 +08:00
hasLabel(name) { return this.hasAttribute(LABEL, name); }
2018-08-30 02:44:35 +08:00
2019-12-04 05:53:17 +08:00
/**
* @param {string} name - label name
2020-10-01 04:48:30 +08:00
* @returns {boolean} true if label exists (excluding inherited)
2019-12-04 05:53:17 +08:00
*/
2020-10-01 04:48:30 +08:00
hasOwnedLabel(name) { return this.hasOwnedAttribute(LABEL, name); }
2019-12-04 05:53:17 +08:00
2018-08-30 02:44:35 +08:00
/**
* @param {string} name - relation name
2020-10-01 04:48:30 +08:00
* @returns {boolean} true if relation exists (including inherited)
2018-08-30 02:44:35 +08:00
*/
2020-10-01 04:48:30 +08:00
hasRelation(name) { return this.hasAttribute(RELATION, name); }
2018-08-30 02:44:35 +08:00
2019-12-04 05:53:17 +08:00
/**
* @param {string} name - relation name
2020-10-01 04:48:30 +08:00
* @returns {boolean} true if relation exists (excluding inherited)
2019-12-04 05:53:17 +08:00
*/
2020-10-01 04:48:30 +08:00
hasOwnedRelation(name) { return this.hasOwnedAttribute(RELATION, name); }
2019-12-04 05:53:17 +08:00
2018-08-30 02:44:35 +08:00
/**
* @param {string} name - label name
2020-10-01 04:48:30 +08:00
* @returns {Attribute|null} label if it exists, null otherwise
2018-08-30 02:44:35 +08:00
*/
2020-10-01 04:48:30 +08:00
getLabel(name) { return this.getAttribute(LABEL, name); }
2018-08-30 02:44:35 +08:00
2019-12-04 05:53:17 +08:00
/**
* @param {string} name - label name
2020-10-01 04:48:30 +08:00
* @returns {Attribute|null} label if it exists, null otherwise
2019-12-04 05:53:17 +08:00
*/
2020-10-01 04:48:30 +08:00
getOwnedLabel(name) { return this.getOwnedAttribute(LABEL, name); }
2019-12-04 05:53:17 +08:00
2018-08-30 02:44:35 +08:00
/**
* @param {string} name - relation name
2020-10-01 04:48:30 +08:00
* @returns {Attribute|null} relation if it exists, null otherwise
2018-08-30 02:44:35 +08:00
*/
2020-10-01 04:48:30 +08:00
getRelation(name) { return this.getAttribute(RELATION, name); }
2018-08-30 02:44:35 +08:00
2019-12-04 05:53:17 +08:00
/**
* @param {string} name - relation name
2020-10-01 04:48:30 +08:00
* @returns {Attribute|null} relation if it exists, null otherwise
2019-12-04 05:53:17 +08:00
*/
2020-10-01 04:48:30 +08:00
getOwnedRelation(name) { return this.getOwnedAttribute(RELATION, name); }
2019-12-04 05:53:17 +08:00
2018-08-30 02:44:35 +08:00
/**
* @param {string} name - label name
2020-10-01 04:48:30 +08:00
* @returns {string|null} label value if label exists, null otherwise
2018-08-30 02:44:35 +08:00
*/
2020-10-01 04:48:30 +08:00
getLabelValue(name) { return this.getAttributeValue(LABEL, name); }
2018-08-30 02:44:35 +08:00
2019-12-04 05:53:17 +08:00
/**
* @param {string} name - label name
2020-10-01 04:48:30 +08:00
* @returns {string|null} label value if label exists, null otherwise
2019-12-04 05:53:17 +08:00
*/
2020-10-01 04:48:30 +08:00
getOwnedLabelValue(name) { return this.getOwnedAttributeValue(LABEL, name); }
2019-12-04 05:53:17 +08:00
2018-08-30 02:44:35 +08:00
/**
* @param {string} name - relation name
2020-10-01 04:48:30 +08:00
* @returns {string|null} relation value if relation exists, null otherwise
2018-08-30 02:44:35 +08:00
*/
2020-10-01 04:48:30 +08:00
getRelationValue(name) { return this.getAttributeValue(RELATION, name); }
2018-08-30 02:44:35 +08:00
2019-12-04 05:53:17 +08:00
/**
* @param {string} name - relation name
2020-10-01 04:48:30 +08:00
* @returns {string|null} relation value if relation exists, null otherwise
2019-12-04 05:53:17 +08:00
*/
2020-10-01 04:48:30 +08:00
getOwnedRelationValue(name) { return this.getOwnedAttributeValue(RELATION, name); }
2019-12-04 05:53:17 +08:00
2018-12-23 05:28:49 +08:00
/**
* @param {string} name
2020-10-01 04:48:30 +08:00
* @returns {Note|null} target note of the relation or null (if target is empty or note was not found)
2018-12-23 05:28:49 +08:00
*/
2020-10-01 04:48:30 +08:00
getRelationTarget(name) {
const relation = this.getRelation(name);
2018-12-23 05:28:49 +08:00
2020-10-01 04:48:30 +08:00
return relation ? this.repository.getNote(relation.value) : null;
2018-12-23 05:28:49 +08:00
}
2019-12-04 05:53:17 +08:00
/**
* @param {string} name
2020-10-01 04:48:30 +08:00
* @returns {Note|null} target note of the relation or null (if target is empty or note was not found)
2019-12-04 05:53:17 +08:00
*/
2020-10-01 04:48:30 +08:00
getOwnedRelationTarget(name) {
const relation = this.getOwnedRelation(name);
2019-12-04 05:53:17 +08:00
2020-10-01 04:48:30 +08:00
return relation ? this.repository.getNote(relation.value) : null;
2019-12-04 05:53:17 +08:00
}
2018-08-30 02:44:35 +08:00
/**
* 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)
*/
2020-10-01 04:48:30 +08:00
toggleLabel(enabled, name, value) { return this.toggleAttribute(LABEL, enabled, name, value); }
2018-08-30 02:44:35 +08:00
/**
* 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)
*/
2020-10-01 04:48:30 +08:00
toggleRelation(enabled, name, value) { return this.toggleAttribute(RELATION, enabled, name, value); }
2018-08-30 02:44:35 +08:00
/**
2019-11-09 05:34:30 +08:00
* Update's given label's value or creates it if it doesn't exist
2018-08-30 02:44:35 +08:00
*
* @param {string} name - label name
* @param {string} [value] - label value
*/
2020-10-01 04:48:30 +08:00
setLabel(name, value) { return this.setAttribute(LABEL, name, value); }
2018-08-30 02:44:35 +08:00
/**
2019-11-09 05:34:30 +08:00
* Update's given relation's value or creates it if it doesn't exist
2018-08-30 02:44:35 +08:00
*
* @param {string} name - relation name
* @param {string} [value] - relation value (noteId)
*/
2020-10-01 04:48:30 +08:00
setRelation(name, value) { return this.setAttribute(RELATION, name, value); }
2018-08-30 02:44:35 +08:00
/**
* Remove label name-value pair, if it exists.
*
* @param {string} name - label name
* @param {string} [value] - label value
*/
2020-10-01 04:48:30 +08:00
removeLabel(name, value) { return this.removeAttribute(LABEL, name, value); }
2018-08-30 02:44:35 +08:00
/**
* Remove relation name-value pair, if it exists.
*
* @param {string} name - relation name
* @param {string} [value] - relation value (noteId)
*/
2020-10-01 04:48:30 +08:00
removeRelation(name, value) { return this.removeAttribute(RELATION, name, value); }
2018-08-30 02:44:35 +08:00
/**
2020-10-01 04:48:30 +08:00
* @return {string[]} return list of all descendant noteIds of this note. Returning just noteIds because number of notes can be huge. Includes also this note's noteId
2018-08-30 02:44:35 +08:00
*/
2020-10-01 04:48:30 +08:00
getDescendantNoteIds() {
return sql.getColumn(`
2018-12-23 05:28:49 +08:00
WITH RECURSIVE
tree(noteId) AS (
SELECT ?
UNION
SELECT branches.noteId FROM branches
JOIN tree ON branches.parentNoteId = tree.noteId
JOIN notes ON notes.noteId = branches.noteId
WHERE notes.isDeleted = 0
AND branches.isDeleted = 0
)
SELECT noteId FROM tree`, [this.noteId]);
2018-08-30 02:44:35 +08:00
}
/**
2018-12-23 05:28:49 +08:00
* Finds descendant notes with given attribute name and value. Only own attributes are considered, not inherited ones
2018-08-30 02:44:35 +08:00
*
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
* @param {string} [value] - attribute value
2020-10-01 04:48:30 +08:00
* @returns {Note[]}
2018-08-30 02:44:35 +08:00
*/
2020-10-01 04:48:30 +08:00
getDescendantNotesWithAttribute(type, name, value) {
2018-08-30 02:44:35 +08:00
const params = [this.noteId, name];
let valueCondition = "";
if (value !== undefined) {
params.push(value);
valueCondition = " AND attributes.value = ?";
}
2020-10-01 04:48:30 +08:00
const notes = this.repository.getEntities(`
2018-08-30 02:44:35 +08:00
WITH RECURSIVE
tree(noteId) AS (
SELECT ?
UNION
SELECT branches.noteId FROM branches
JOIN tree ON branches.parentNoteId = tree.noteId
JOIN notes ON notes.noteId = branches.noteId
WHERE notes.isDeleted = 0
AND branches.isDeleted = 0
)
SELECT notes.* FROM notes
JOIN tree ON tree.noteId = notes.noteId
JOIN attributes ON attributes.noteId = notes.noteId
WHERE attributes.isDeleted = 0
AND attributes.name = ?
${valueCondition}
ORDER BY noteId, position`, params);
return notes;
}
/**
2018-12-23 05:28:49 +08:00
* Finds descendant notes with given label name and value. Only own labels are considered, not inherited ones
2018-08-30 02:44:35 +08:00
*
* @param {string} name - label name
* @param {string} [value] - label value
2020-10-01 04:48:30 +08:00
* @returns {Note[]}
2018-08-30 02:44:35 +08:00
*/
2020-10-01 04:48:30 +08:00
getDescendantNotesWithLabel(name, value) { return this.getDescendantNotesWithAttribute(LABEL, name, value); }
2018-08-30 02:44:35 +08:00
/**
2018-12-23 05:28:49 +08:00
* Finds descendant notes with given relation name and value. Only own relations are considered, not inherited ones
2018-08-30 02:44:35 +08:00
*
* @param {string} name - relation name
* @param {string} [value] - relation value
2020-10-01 04:48:30 +08:00
* @returns {Note[]}
2018-08-30 02:44:35 +08:00
*/
2020-10-01 04:48:30 +08:00
getDescendantNotesWithRelation(name, value) { return this.getDescendantNotesWithAttribute(RELATION, name, value); }
2018-08-30 02:44:35 +08:00
/**
* Returns note revisions of this note.
*
2020-10-01 04:48:30 +08:00
* @returns {NoteRevision[]}
2018-08-30 02:44:35 +08:00
*/
2020-10-01 04:48:30 +08:00
getRevisions() {
return this.repository.getEntities("SELECT * FROM note_revisions WHERE noteId = ?", [this.noteId]);
2018-08-30 02:44:35 +08:00
}
/**
* Get list of links coming out of this note.
*
* @deprecated - not intended for general use
2020-10-01 04:48:30 +08:00
* @returns {Attribute[]}
*/
2020-10-01 04:48:30 +08:00
getLinks() {
return this.repository.getEntities(`
SELECT *
FROM attributes
WHERE noteId = ? AND
2020-04-08 01:19:20 +08:00
isDeleted = 0 AND
type = 'relation' AND
name IN ('internalLink', 'imageLink', 'relationMapLink', 'includeNoteLink')`, [this.noteId]);
}
2018-08-30 02:44:35 +08:00
/**
2020-10-01 04:48:30 +08:00
* @returns {Branch[]}
2018-08-30 02:44:35 +08:00
*/
2020-10-01 04:48:30 +08:00
getBranches() {
return this.repository.getEntities("SELECT * FROM branches WHERE isDeleted = 0 AND noteId = ?", [this.noteId]);
2018-08-30 02:44:35 +08:00
}
/**
* @returns {boolean} - true if note has children
*/
2020-10-01 04:48:30 +08:00
hasChildren() {
return (this.getChildNotes()).length > 0;
}
2018-08-30 02:44:35 +08:00
/**
2020-10-01 04:48:30 +08:00
* @returns {Note[]} child notes of this note
2018-08-30 02:44:35 +08:00
*/
2020-10-01 04:48:30 +08:00
getChildNotes() {
return this.repository.getEntities(`
2018-08-30 02:44:35 +08:00
SELECT notes.*
FROM branches
JOIN notes USING(noteId)
WHERE notes.isDeleted = 0
AND branches.isDeleted = 0
AND branches.parentNoteId = ?
ORDER BY branches.notePosition`, [this.noteId]);
}
/**
2020-10-01 04:48:30 +08:00
* @returns {Branch[]} child branches of this note
2018-08-30 02:44:35 +08:00
*/
2020-10-01 04:48:30 +08:00
getChildBranches() {
return this.repository.getEntities(`
2018-08-30 02:44:35 +08:00
SELECT branches.*
FROM branches
WHERE branches.isDeleted = 0
AND branches.parentNoteId = ?
ORDER BY branches.notePosition`, [this.noteId]);
}
/**
2020-10-01 04:48:30 +08:00
* @returns {Note[]} parent notes of this note (note can have multiple parents because of cloning)
2018-08-30 02:44:35 +08:00
*/
2020-10-01 04:48:30 +08:00
getParentNotes() {
return this.repository.getEntities(`
2018-08-30 02:44:35 +08:00
SELECT parent_notes.*
FROM
branches AS child_tree
JOIN notes AS parent_notes ON parent_notes.noteId = child_tree.parentNoteId
WHERE child_tree.noteId = ?
AND child_tree.isDeleted = 0
AND parent_notes.isDeleted = 0`, [this.noteId]);
}
2019-11-17 02:07:32 +08:00
/**
2020-10-01 04:48:30 +08:00
* @return {string[][]} - array of notePaths (each represented by array of noteIds constituting the particular note path)
2019-11-17 02:07:32 +08:00
*/
2020-10-01 04:48:30 +08:00
getAllNotePaths() {
2019-11-17 02:07:32 +08:00
if (this.noteId === 'root') {
return [['root']];
}
const notePaths = [];
2020-10-01 04:48:30 +08:00
for (const parentNote of this.getParentNotes()) {
for (const parentPath of parentNote.getAllNotePaths()) {
2019-11-17 02:07:32 +08:00
parentPath.push(this.noteId);
notePaths.push(parentPath);
}
}
return notePaths;
}
2020-10-01 04:48:30 +08:00
getRelationDefinitions() {
return this.getLabels()
.filter(l => l.name.startsWith("relation:"));
}
getLabelDefinitions() {
return this.getLabels()
.filter(l => l.name.startsWith("relation:"));
}
2019-11-28 06:07:10 +08:00
/**
* @param ancestorNoteId
2020-10-01 04:48:30 +08:00
* @return {boolean} - true if ancestorNoteId occurs in at least one of the note's paths
2019-11-28 06:07:10 +08:00
*/
2020-10-01 04:48:30 +08:00
isDescendantOfNote(ancestorNoteId) {
const notePaths = this.getAllNotePaths();
2019-11-28 06:07:10 +08:00
return notePaths.some(path => path.includes(ancestorNoteId));
}
2018-08-30 02:44:35 +08:00
beforeSaving() {
if (!this.isDeleted) {
this.isDeleted = false;
}
if (!this.dateCreated) {
this.dateCreated = dateUtils.localNowDateTime();
}
if (!this.utcDateCreated) {
this.utcDateCreated = dateUtils.utcNowDateTime();
2018-08-30 02:44:35 +08:00
}
super.beforeSaving();
if (this.isChanged) {
this.dateModified = dateUtils.localNowDateTime();
this.utcDateModified = dateUtils.utcNowDateTime();
2018-08-30 02:44:35 +08:00
}
}
2018-12-23 05:28:49 +08:00
// cannot be static!
updatePojo(pojo) {
if (pojo.isProtected) {
2019-02-21 05:24:51 +08:00
if (this.isContentAvailable) {
2019-11-09 05:34:30 +08:00
pojo.title = protectedSessionService.encrypt(pojo.title);
2019-02-21 05:24:51 +08:00
}
else {
// updating protected note outside of protected session means we will keep original ciphertexts
2019-03-28 04:04:25 +08:00
delete pojo.title;
2019-02-21 05:24:51 +08:00
}
2018-12-23 05:28:49 +08:00
}
delete pojo.isContentAvailable;
delete pojo.__attributeCache;
delete pojo.__ownedAttributeCache;
2019-03-28 04:04:25 +08:00
delete pojo.content;
2019-11-28 06:07:10 +08:00
/** zero references to contentHash, probably can be removed */
delete pojo.contentHash;
2018-12-23 05:28:49 +08:00
}
2018-08-30 02:44:35 +08:00
}
2020-10-01 04:48:30 +08:00
module.exports = Note;
</code></pre>
2018-08-30 02:44:35 +08:00
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="ApiToken.html">ApiToken</a></li><li><a href="Attribute.html">Attribute</a></li><li><a href="BackendScriptApi.html">BackendScriptApi</a></li><li><a href="Branch.html">Branch</a></li><li><a href="Entity.html">Entity</a></li><li><a href="Note.html">Note</a></li><li><a href="NoteRevision.html">NoteRevision</a></li><li><a href="Option.html">Option</a></li><li><a href="RecentNote.html">RecentNote</a></li></ul><h3><a href="global.html">Global</a></h3>
2018-08-30 02:44:35 +08:00
</nav>
<br class="clear">
<footer>
2020-10-01 04:48:30 +08:00
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.6</a>
2018-08-30 02:44:35 +08:00
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>