mirror of
https://github.com/zadam/trilium.git
synced 2025-03-02 01:53:11 +08:00
API docs updates
This commit is contained in:
parent
aa5ede5039
commit
af10f0f52a
6 changed files with 2720 additions and 131 deletions
File diff suppressed because it is too large
Load diff
|
@ -224,26 +224,36 @@ class Note extends Entity {
|
|||
return null;
|
||||
}
|
||||
|
||||
async loadOwnedAttributesToCache() {
|
||||
this.__ownedAttributeCache = await repository.getEntities(`SELECT * FROM attributes WHERE isDeleted = 0 AND noteId = ?`, [this.noteId]);
|
||||
return this.__ownedAttributeCache;
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {Promise<Attribute[]>} attributes belonging to this specific note (excludes inherited attributes)
|
||||
* 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.
|
||||
*
|
||||
* This method can be significantly faster than the getAttributes()
|
||||
* @param {string} [type] - (optional) attribute type to filter
|
||||
* @param {string} [name] - (optional) attribute name to filter
|
||||
* @returns {Promise<Attribute[]>} note's "owned" attributes - excluding inherited ones
|
||||
*/
|
||||
async getOwnedAttributes(type, name) {
|
||||
let query = `SELECT * FROM attributes WHERE isDeleted = 0 AND noteId = ?`;
|
||||
const params = [this.noteId];
|
||||
|
||||
if (type) {
|
||||
query += ` AND type = ?`;
|
||||
params.push(type);
|
||||
if (!this.__ownedAttributeCache) {
|
||||
await this.loadOwnedAttributesToCache();
|
||||
}
|
||||
|
||||
if (name) {
|
||||
query += ` AND name = ?`;
|
||||
params.push(name);
|
||||
if (type && name) {
|
||||
return this.__ownedAttributeCache.filter(attr => attr.type === type && 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();
|
||||
}
|
||||
|
||||
return await repository.getEntities(query, params);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -265,19 +275,26 @@ class Note extends Entity {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param {string} [name] - attribute name to filter
|
||||
* @param {string} [type] - (optional) attribute type to filter
|
||||
* @param {string} [name] - (optional) attribute name to filter
|
||||
* @returns {Promise<Attribute[]>} all note's attributes, including inherited ones
|
||||
*/
|
||||
async getAttributes(name) {
|
||||
async getAttributes(type, name) {
|
||||
if (!this.__attributeCache) {
|
||||
await this.loadAttributesToCache();
|
||||
}
|
||||
|
||||
if (name) {
|
||||
if (type && name) {
|
||||
return this.__attributeCache.filter(attr => attr.type === type && 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 {
|
||||
return this.__attributeCache;
|
||||
return this.__attributeCache.slice();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -286,7 +303,15 @@ class Note extends Entity {
|
|||
* @returns {Promise<Attribute[]>} all note's labels (attributes with type label), including inherited ones
|
||||
*/
|
||||
async getLabels(name) {
|
||||
return (await this.getAttributes(name)).filter(attr => attr.type === LABEL);
|
||||
return await this.getAttributes(LABEL, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} [name] - label name to filter
|
||||
* @returns {Promise<Attribute[]>} all note's labels (attributes with type label), excluding inherited ones
|
||||
*/
|
||||
async getOwnedLabels(name) {
|
||||
return await this.getOwnedAttributes(LABEL, name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -294,7 +319,7 @@ class Note extends Entity {
|
|||
* @returns {Promise<Attribute[]>} all note's label definitions, including inherited ones
|
||||
*/
|
||||
async getLabelDefinitions(name) {
|
||||
return (await this.getAttributes(name)).filter(attr => attr.type === LABEL_DEFINITION);
|
||||
return await this.getAttributes(LABEL_DEFINITION, name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -302,7 +327,15 @@ class Note extends Entity {
|
|||
* @returns {Promise<Attribute[]>} all note's relations (attributes with type relation), including inherited ones
|
||||
*/
|
||||
async getRelations(name) {
|
||||
return (await this.getAttributes(name)).filter(attr => attr.type === RELATION);
|
||||
return await this.getAttributes(RELATION, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} [name] - relation name to filter
|
||||
* @returns {Promise<Attribute[]>} all note's relations (attributes with type relation), excluding inherited ones
|
||||
*/
|
||||
async getOwnedRelations(name) {
|
||||
return await this.getOwnedAttributes(RELATION, name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -325,7 +358,7 @@ class Note extends Entity {
|
|||
* @returns {Promise<Attribute[]>} all note's relation definitions including inherited ones
|
||||
*/
|
||||
async getRelationDefinitions(name) {
|
||||
return (await this.getAttributes(name)).filter(attr => attr.type === RELATION_DEFINITION);
|
||||
return await this.getAttributes(RELATION_DEFINITION, name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -334,6 +367,7 @@ class Note extends Entity {
|
|||
*/
|
||||
invalidateAttributeCache() {
|
||||
this.__attributeCache = null;
|
||||
this.__ownedAttributeCache = null;
|
||||
}
|
||||
|
||||
/** @returns {Promise<void>} */
|
||||
|
@ -343,11 +377,10 @@ class Note extends Entity {
|
|||
tree(noteId, level) AS (
|
||||
SELECT ?, 0
|
||||
UNION
|
||||
SELECT branches.parentNoteId, tree.level + 1 FROM branches
|
||||
SELECT branches.parentNoteId, tree.level + 1
|
||||
FROM branches
|
||||
JOIN tree ON branches.noteId = tree.noteId
|
||||
JOIN notes ON notes.noteId = branches.parentNoteId
|
||||
WHERE notes.isDeleted = 0
|
||||
AND branches.isDeleted = 0
|
||||
WHERE branches.isDeleted = 0
|
||||
),
|
||||
treeWithAttrs(noteId, level) AS (
|
||||
SELECT * FROM tree
|
||||
|
@ -366,6 +399,11 @@ class Note extends Entity {
|
|||
// we order by noteId so that attributes from same note stay together. Actual noteId ordering doesn't matter.
|
||||
|
||||
const filteredAttributes = attributes.filter((attr, index) => {
|
||||
// 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;
|
||||
}
|
||||
|
||||
if (attr.isDefinition()) {
|
||||
const firstDefinitionIndex = attributes.findIndex(el => el.type === attr.type && el.name === attr.name);
|
||||
|
||||
|
@ -409,6 +447,15 @@ class Note extends Entity {
|
|||
return !!await this.getAttribute(type, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} type - attribute type (label, relation, etc.)
|
||||
* @param {string} name - attribute name
|
||||
* @returns {Promise<boolean>} true if note has an attribute with given type and name (excluding inherited)
|
||||
*/
|
||||
async hasOwnedAttribute(type, name) {
|
||||
return !!await this.getOwnedAttribute(type, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} type - attribute type (label, relation, etc.)
|
||||
* @param {string} name - attribute name
|
||||
|
@ -423,7 +470,7 @@ class Note extends Entity {
|
|||
/**
|
||||
* @param {string} type - attribute type (label, relation, etc.)
|
||||
* @param {string} name - attribute name
|
||||
* @returns {Promise<string>} attribute value of given type and name or null if no such attribute exists.
|
||||
* @returns {Promise<string|null>} 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);
|
||||
|
@ -431,6 +478,17 @@ class Note extends Entity {
|
|||
return attr ? attr.value : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} type - attribute type (label, relation, etc.)
|
||||
* @param {string} name - attribute name
|
||||
* @returns {Promise<string|null>} attribute value of given type and name or null if no such attribute exists.
|
||||
*/
|
||||
async getOwnedAttributeValue(type, name) {
|
||||
const attr = await this.getOwnedAttribute(type, name);
|
||||
|
||||
return attr ? attr.value : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Based on enabled, attribute is either set or removed.
|
||||
*
|
||||
|
@ -458,7 +516,7 @@ class Note extends Entity {
|
|||
* @returns {Promise<void>}
|
||||
*/
|
||||
async setAttribute(type, name, value) {
|
||||
const attributes = await this.getOwnedAttributes();
|
||||
const attributes = await this.loadOwnedAttributesToCache();
|
||||
let attr = attributes.find(attr => attr.type === type && attr.name === name);
|
||||
|
||||
if (attr) {
|
||||
|
@ -492,7 +550,7 @@ class Note extends Entity {
|
|||
* @returns {Promise<void>}
|
||||
*/
|
||||
async removeAttribute(type, name, value) {
|
||||
const attributes = await this.getOwnedAttributes();
|
||||
const attributes = await this.loadOwnedAttributesToCache();
|
||||
|
||||
for (const attribute of attributes) {
|
||||
if (attribute.type === type && (value === undefined || value === attribute.value)) {
|
||||
|
@ -536,36 +594,72 @@ class Note extends Entity {
|
|||
*/
|
||||
async hasLabel(name) { return await this.hasAttribute(LABEL, name); }
|
||||
|
||||
/**
|
||||
* @param {string} name - label name
|
||||
* @returns {Promise<boolean>} true if label exists (excluding inherited)
|
||||
*/
|
||||
async hasOwnedLabel(name) { return await this.hasOwnedAttribute(LABEL, name); }
|
||||
|
||||
/**
|
||||
* @param {string} name - relation name
|
||||
* @returns {Promise<boolean>} true if relation exists (including inherited)
|
||||
*/
|
||||
async hasRelation(name) { return await this.hasAttribute(RELATION, name); }
|
||||
|
||||
/**
|
||||
* @param {string} name - relation name
|
||||
* @returns {Promise<boolean>} true if relation exists (excluding inherited)
|
||||
*/
|
||||
async hasOwnedRelation(name) { return await this.hasOwnedAttribute(RELATION, name); }
|
||||
|
||||
/**
|
||||
* @param {string} name - label name
|
||||
* @returns {Promise<Attribute>} label if it exists, null otherwise
|
||||
* @returns {Promise<Attribute|null>} label if it exists, null otherwise
|
||||
*/
|
||||
async getLabel(name) { return await this.getAttribute(LABEL, name); }
|
||||
|
||||
/**
|
||||
* @param {string} name - label name
|
||||
* @returns {Promise<Attribute|null>} label if it exists, null otherwise
|
||||
*/
|
||||
async getOwnedLabel(name) { return await this.getOwnedAttribute(LABEL, name); }
|
||||
|
||||
/**
|
||||
* @param {string} name - relation name
|
||||
* @returns {Promise<Attribute>} relation if it exists, null otherwise
|
||||
* @returns {Promise<Attribute|null>} relation if it exists, null otherwise
|
||||
*/
|
||||
async getRelation(name) { return await this.getAttribute(RELATION, name); }
|
||||
|
||||
/**
|
||||
* @param {string} name - relation name
|
||||
* @returns {Promise<Attribute|null>} relation if it exists, null otherwise
|
||||
*/
|
||||
async getOwnedRelation(name) { return await this.getOwnedAttribute(RELATION, name); }
|
||||
|
||||
/**
|
||||
* @param {string} name - label name
|
||||
* @returns {Promise<string>} label value if label exists, null otherwise
|
||||
* @returns {Promise<string|null>} label value if label exists, null otherwise
|
||||
*/
|
||||
async getLabelValue(name) { return await this.getAttributeValue(LABEL, name); }
|
||||
|
||||
/**
|
||||
* @param {string} name - label name
|
||||
* @returns {Promise<string|null>} label value if label exists, null otherwise
|
||||
*/
|
||||
async getOwnedLabelValue(name) { return await this.getOwnedAttributeValue(LABEL, name); }
|
||||
|
||||
/**
|
||||
* @param {string} name - relation name
|
||||
* @returns {Promise<string>} relation value if relation exists, null otherwise
|
||||
* @returns {Promise<string|null>} relation value if relation exists, null otherwise
|
||||
*/
|
||||
async getRelationValue(name) { return await this.getAttributeValue(RELATION, name); }
|
||||
|
||||
/**
|
||||
* @param {string} name - relation name
|
||||
* @returns {Promise<string|null>} relation value if relation exists, null otherwise
|
||||
*/
|
||||
async getOwnedRelationValue(name) { return await this.getOwnedAttributeValue(RELATION, name); }
|
||||
|
||||
/**
|
||||
* @param {string} name
|
||||
* @returns {Promise<Note>|null} target note of the relation or null (if target is empty or note was not found)
|
||||
|
@ -576,6 +670,16 @@ class Note extends Entity {
|
|||
return relation ? await repository.getNote(relation.value) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} name
|
||||
* @returns {Promise<Note>|null} target note of the relation or null (if target is empty or note was not found)
|
||||
*/
|
||||
async getOwnedRelationTarget(name) {
|
||||
const relation = await this.getOwnedRelation(name);
|
||||
|
||||
return relation ? await repository.getNote(relation.value) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Based on enabled, label is either set or removed.
|
||||
*
|
||||
|
|
|
@ -596,6 +596,480 @@
|
|||
|
||||
|
||||
|
||||
|
||||
<h4 class="name" id="CreateNoteAttribute">CreateNoteAttribute</h4>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<h5>Type:</h5>
|
||||
<ul>
|
||||
<li>
|
||||
|
||||
<span class="param-type">object</span>
|
||||
|
||||
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<h5 class="subsection-title">Properties:</h5>
|
||||
|
||||
|
||||
|
||||
<table class="props">
|
||||
<thead>
|
||||
<tr>
|
||||
|
||||
<th>Name</th>
|
||||
|
||||
|
||||
<th>Type</th>
|
||||
|
||||
|
||||
<th>Attributes</th>
|
||||
|
||||
|
||||
|
||||
|
||||
<th class="last">Description</th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
|
||||
|
||||
<tr>
|
||||
|
||||
<td class="name"><code>type</code></td>
|
||||
|
||||
|
||||
<td class="type">
|
||||
|
||||
|
||||
<span class="param-type">string</span>
|
||||
|
||||
|
||||
|
||||
</td>
|
||||
|
||||
|
||||
<td class="attributes">
|
||||
|
||||
|
||||
|
||||
</td>
|
||||
|
||||
|
||||
|
||||
|
||||
<td class="description last">attribute type - label, relation etc.</td>
|
||||
</tr>
|
||||
|
||||
|
||||
|
||||
<tr>
|
||||
|
||||
<td class="name"><code>name</code></td>
|
||||
|
||||
|
||||
<td class="type">
|
||||
|
||||
|
||||
<span class="param-type">string</span>
|
||||
|
||||
|
||||
|
||||
</td>
|
||||
|
||||
|
||||
<td class="attributes">
|
||||
|
||||
|
||||
|
||||
</td>
|
||||
|
||||
|
||||
|
||||
|
||||
<td class="description last">attribute name</td>
|
||||
</tr>
|
||||
|
||||
|
||||
|
||||
<tr>
|
||||
|
||||
<td class="name"><code>value</code></td>
|
||||
|
||||
|
||||
<td class="type">
|
||||
|
||||
|
||||
<span class="param-type">string</span>
|
||||
|
||||
|
||||
|
||||
</td>
|
||||
|
||||
|
||||
<td class="attributes">
|
||||
|
||||
<optional><br>
|
||||
|
||||
|
||||
|
||||
</td>
|
||||
|
||||
|
||||
|
||||
|
||||
<td class="description last">attribute value</td>
|
||||
</tr>
|
||||
|
||||
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
|
||||
|
||||
|
||||
<dl class="details">
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<dt class="tag-source">Source:</dt>
|
||||
<dd class="tag-source"><ul class="dummy"><li>
|
||||
<a href="services_backend_script_api.js.html">services/backend_script_api.js</a>, <a href="services_backend_script_api.js.html#line233">line 233</a>
|
||||
</li></ul></dd>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</dl>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<h4 class="name" id="CreateNoteExtraOptions">CreateNoteExtraOptions</h4>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<h5>Type:</h5>
|
||||
<ul>
|
||||
<li>
|
||||
|
||||
<span class="param-type">object</span>
|
||||
|
||||
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<h5 class="subsection-title">Properties:</h5>
|
||||
|
||||
|
||||
|
||||
<table class="props">
|
||||
<thead>
|
||||
<tr>
|
||||
|
||||
<th>Name</th>
|
||||
|
||||
|
||||
<th>Type</th>
|
||||
|
||||
|
||||
<th>Attributes</th>
|
||||
|
||||
|
||||
|
||||
<th>Default</th>
|
||||
|
||||
|
||||
<th class="last">Description</th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
|
||||
|
||||
<tr>
|
||||
|
||||
<td class="name"><code>json</code></td>
|
||||
|
||||
|
||||
<td class="type">
|
||||
|
||||
|
||||
<span class="param-type">boolean</span>
|
||||
|
||||
|
||||
|
||||
</td>
|
||||
|
||||
|
||||
<td class="attributes">
|
||||
|
||||
<optional><br>
|
||||
|
||||
|
||||
|
||||
</td>
|
||||
|
||||
|
||||
|
||||
<td class="default">
|
||||
|
||||
false
|
||||
|
||||
</td>
|
||||
|
||||
|
||||
<td class="description last">should the note be JSON</td>
|
||||
</tr>
|
||||
|
||||
|
||||
|
||||
<tr>
|
||||
|
||||
<td class="name"><code>isProtected</code></td>
|
||||
|
||||
|
||||
<td class="type">
|
||||
|
||||
|
||||
<span class="param-type">boolean</span>
|
||||
|
||||
|
||||
|
||||
</td>
|
||||
|
||||
|
||||
<td class="attributes">
|
||||
|
||||
<optional><br>
|
||||
|
||||
|
||||
|
||||
</td>
|
||||
|
||||
|
||||
|
||||
<td class="default">
|
||||
|
||||
false
|
||||
|
||||
</td>
|
||||
|
||||
|
||||
<td class="description last">should the note be protected</td>
|
||||
</tr>
|
||||
|
||||
|
||||
|
||||
<tr>
|
||||
|
||||
<td class="name"><code>type</code></td>
|
||||
|
||||
|
||||
<td class="type">
|
||||
|
||||
|
||||
<span class="param-type">string</span>
|
||||
|
||||
|
||||
|
||||
</td>
|
||||
|
||||
|
||||
<td class="attributes">
|
||||
|
||||
<optional><br>
|
||||
|
||||
|
||||
|
||||
</td>
|
||||
|
||||
|
||||
|
||||
<td class="default">
|
||||
|
||||
'text'
|
||||
|
||||
</td>
|
||||
|
||||
|
||||
<td class="description last">note type</td>
|
||||
</tr>
|
||||
|
||||
|
||||
|
||||
<tr>
|
||||
|
||||
<td class="name"><code>mime</code></td>
|
||||
|
||||
|
||||
<td class="type">
|
||||
|
||||
|
||||
<span class="param-type">string</span>
|
||||
|
||||
|
||||
|
||||
</td>
|
||||
|
||||
|
||||
<td class="attributes">
|
||||
|
||||
<optional><br>
|
||||
|
||||
|
||||
|
||||
</td>
|
||||
|
||||
|
||||
|
||||
<td class="default">
|
||||
|
||||
'text/html'
|
||||
|
||||
</td>
|
||||
|
||||
|
||||
<td class="description last">MIME type of the note</td>
|
||||
</tr>
|
||||
|
||||
|
||||
|
||||
<tr>
|
||||
|
||||
<td class="name"><code>attributes</code></td>
|
||||
|
||||
|
||||
<td class="type">
|
||||
|
||||
|
||||
<span class="param-type">Array.<<a href="global.html#CreateNoteAttribute">CreateNoteAttribute</a>></span>
|
||||
|
||||
|
||||
|
||||
</td>
|
||||
|
||||
|
||||
<td class="attributes">
|
||||
|
||||
<optional><br>
|
||||
|
||||
|
||||
|
||||
</td>
|
||||
|
||||
|
||||
|
||||
<td class="default">
|
||||
|
||||
[]
|
||||
|
||||
</td>
|
||||
|
||||
|
||||
<td class="description last">attributes to be created for this note</td>
|
||||
</tr>
|
||||
|
||||
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
|
||||
|
||||
|
||||
<dl class="details">
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<dt class="tag-source">Source:</dt>
|
||||
<dd class="tag-source"><ul class="dummy"><li>
|
||||
<a href="services_backend_script_api.js.html">services/backend_script_api.js</a>, <a href="services_backend_script_api.js.html#line240">line 240</a>
|
||||
</li></ul></dd>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</dl>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -258,6 +258,65 @@ function BackendScriptApi(currentNote, apiParams) {
|
|||
*/
|
||||
this.createNewNote = noteService.createNewNote;
|
||||
|
||||
/**
|
||||
* @typedef {object} CreateNoteAttribute
|
||||
* @property {string} type - attribute type - label, relation etc.
|
||||
* @property {string} name - attribute name
|
||||
* @property {string} [value] - attribute value
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {object} CreateNoteExtraOptions
|
||||
* @property {boolean} [json=false] - should the note be JSON
|
||||
* @property {boolean} [isProtected=false] - should the note be protected
|
||||
* @property {string} [type='text'] - note type
|
||||
* @property {string} [mime='text/html'] - MIME type of the note
|
||||
* @property {CreateNoteAttribute[]} [attributes=[]] - attributes to be created for this note
|
||||
*/
|
||||
|
||||
/**
|
||||
* @method
|
||||
*
|
||||
* @param {string} parentNoteId - create new note under this parent
|
||||
* @param {string} title
|
||||
* @param {string} [content=""]
|
||||
* @param {CreateNoteExtraOptions} [extraOptions={}]
|
||||
* @returns {Promise<{note: Note, branch: Branch}>} object contains newly created entities note and branch
|
||||
*/
|
||||
this.createNote = async (parentNoteId, title, content = "", extraOptions= {}) => {
|
||||
extraOptions.parentNoteId = parentNoteId;
|
||||
extraOptions.title = title;
|
||||
|
||||
const parentNote = await repository.getNote(parentNoteId);
|
||||
|
||||
// code note type can be inherited, otherwise text is default
|
||||
extraOptions.type = parentNote.type === 'code' ? 'code' : 'text';
|
||||
extraOptions.mime = parentNote.type === 'code' ? parentNote.mime : 'text/html';
|
||||
|
||||
if (extraOptions.json) {
|
||||
extraOptions.content = JSON.stringify(content || {}, null, '\t');
|
||||
extraOptions.type = 'code';
|
||||
extraOptions.mime = 'application/json';
|
||||
}
|
||||
else {
|
||||
extraOptions.content = content;
|
||||
}
|
||||
|
||||
const {note, branch} = await noteService.createNewNote(extraOptions);
|
||||
|
||||
for (const attr of extraOptions.attributes || []) {
|
||||
await attributeService.createAttribute({
|
||||
noteId: note.noteId,
|
||||
type: attr.type,
|
||||
name: attr.name,
|
||||
value: attr.value,
|
||||
isInheritable: !!attr.isInheritable
|
||||
});
|
||||
}
|
||||
|
||||
return {note, branch};
|
||||
};
|
||||
|
||||
/**
|
||||
* Log given message to trilium logs.
|
||||
*
|
||||
|
|
|
@ -1220,7 +1220,7 @@
|
|||
|
||||
<dt class="tag-source">Source:</dt>
|
||||
<dd class="tag-source"><ul class="dummy"><li>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line220">line 220</a>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line227">line 227</a>
|
||||
</li></ul></dd>
|
||||
|
||||
|
||||
|
@ -1278,7 +1278,7 @@
|
|||
|
||||
|
||||
|
||||
<h4 class="name" id="getAttributes"><span class="type-signature">(async) </span>getAttributes<span class="signature">(name<span class="signature-attributes">opt</span>)</span><span class="type-signature"> → {Promise.<Array.<<a href="Attribute.html">Attribute</a>>>}</span></h4>
|
||||
<h4 class="name" id="getAttributes"><span class="type-signature">(async) </span>getAttributes<span class="signature">(type<span class="signature-attributes">opt</span>, name<span class="signature-attributes">opt</span>)</span><span class="type-signature"> → {Promise.<Array.<<a href="Attribute.html">Attribute</a>>>}</span></h4>
|
||||
|
||||
|
||||
|
||||
|
@ -1318,6 +1318,39 @@
|
|||
<tbody>
|
||||
|
||||
|
||||
<tr>
|
||||
|
||||
<td class="name"><code>type</code></td>
|
||||
|
||||
|
||||
<td class="type">
|
||||
|
||||
|
||||
<span class="param-type">string</span>
|
||||
|
||||
|
||||
|
||||
</td>
|
||||
|
||||
|
||||
<td class="attributes">
|
||||
|
||||
<optional><br>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</td>
|
||||
|
||||
|
||||
|
||||
|
||||
<td class="description last">(optional) attribute type to filter</td>
|
||||
</tr>
|
||||
|
||||
|
||||
|
||||
<tr>
|
||||
|
||||
<td class="name"><code>name</code></td>
|
||||
|
@ -1346,7 +1379,7 @@
|
|||
|
||||
|
||||
|
||||
<td class="description last">attribute name to filter</td>
|
||||
<td class="description last">(optional) attribute name to filter</td>
|
||||
</tr>
|
||||
|
||||
|
||||
|
@ -1387,7 +1420,7 @@
|
|||
|
||||
<dt class="tag-source">Source:</dt>
|
||||
<dd class="tag-source"><ul class="dummy"><li>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line160">line 160</a>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line161">line 161</a>
|
||||
</li></ul></dd>
|
||||
|
||||
|
||||
|
@ -1415,6 +1448,10 @@
|
|||
<h5>Returns:</h5>
|
||||
|
||||
|
||||
<div class="param-desc">
|
||||
all note's attributes, including inherited ones
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
<dl>
|
||||
|
@ -1561,7 +1598,7 @@
|
|||
|
||||
<dt class="tag-source">Source:</dt>
|
||||
<dd class="tag-source"><ul class="dummy"><li>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line231">line 231</a>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line238">line 238</a>
|
||||
</li></ul></dd>
|
||||
|
||||
|
||||
|
@ -2124,7 +2161,7 @@
|
|||
|
||||
<dt class="tag-source">Source:</dt>
|
||||
<dd class="tag-source"><ul class="dummy"><li>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line253">line 253</a>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line260">line 260</a>
|
||||
</li></ul></dd>
|
||||
|
||||
|
||||
|
@ -2291,7 +2328,7 @@
|
|||
|
||||
<dt class="tag-source">Source:</dt>
|
||||
<dd class="tag-source"><ul class="dummy"><li>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line186">line 186</a>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line193">line 193</a>
|
||||
</li></ul></dd>
|
||||
|
||||
|
||||
|
@ -2458,7 +2495,7 @@
|
|||
|
||||
<dt class="tag-source">Source:</dt>
|
||||
<dd class="tag-source"><ul class="dummy"><li>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line178">line 178</a>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line185">line 185</a>
|
||||
</li></ul></dd>
|
||||
|
||||
|
||||
|
@ -2613,7 +2650,7 @@
|
|||
|
||||
<dt class="tag-source">Source:</dt>
|
||||
<dd class="tag-source"><ul class="dummy"><li>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line265">line 265</a>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line272">line 272</a>
|
||||
</li></ul></dd>
|
||||
|
||||
|
||||
|
@ -2972,7 +3009,7 @@
|
|||
|
||||
<dt class="tag-source">Source:</dt>
|
||||
<dd class="tag-source"><ul class="dummy"><li>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line259">line 259</a>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line266">line 266</a>
|
||||
</li></ul></dd>
|
||||
|
||||
|
||||
|
@ -3139,7 +3176,7 @@
|
|||
|
||||
<dt class="tag-source">Source:</dt>
|
||||
<dd class="tag-source"><ul class="dummy"><li>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line202">line 202</a>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line209">line 209</a>
|
||||
</li></ul></dd>
|
||||
|
||||
|
||||
|
@ -3306,7 +3343,7 @@
|
|||
|
||||
<dt class="tag-source">Source:</dt>
|
||||
<dd class="tag-source"><ul class="dummy"><li>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line194">line 194</a>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line201">line 201</a>
|
||||
</li></ul></dd>
|
||||
|
||||
|
||||
|
@ -3461,7 +3498,7 @@
|
|||
|
||||
<dt class="tag-source">Source:</dt>
|
||||
<dd class="tag-source"><ul class="dummy"><li>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line277">line 277</a>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line284">line 284</a>
|
||||
</li></ul></dd>
|
||||
|
||||
|
||||
|
@ -3631,7 +3668,7 @@
|
|||
|
||||
<dt class="tag-source">Source:</dt>
|
||||
<dd class="tag-source"><ul class="dummy"><li>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line287">line 287</a>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line294">line 294</a>
|
||||
</li></ul></dd>
|
||||
|
||||
|
||||
|
@ -3782,7 +3819,7 @@
|
|||
|
||||
<dt class="tag-source">Source:</dt>
|
||||
<dd class="tag-source"><ul class="dummy"><li>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line271">line 271</a>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line278">line 278</a>
|
||||
</li></ul></dd>
|
||||
|
||||
|
||||
|
@ -3892,7 +3929,7 @@
|
|||
|
||||
<dt class="tag-source">Source:</dt>
|
||||
<dd class="tag-source"><ul class="dummy"><li>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line311">line 311</a>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line318">line 318</a>
|
||||
</li></ul></dd>
|
||||
|
||||
|
||||
|
@ -4066,7 +4103,7 @@
|
|||
|
||||
<dt class="tag-source">Source:</dt>
|
||||
<dd class="tag-source"><ul class="dummy"><li>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line211">line 211</a>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line218">line 218</a>
|
||||
</li></ul></dd>
|
||||
|
||||
|
||||
|
@ -4323,7 +4360,7 @@
|
|||
|
||||
<dt class="tag-source">Source:</dt>
|
||||
<dd class="tag-source"><ul class="dummy"><li>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line241">line 241</a>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line248">line 248</a>
|
||||
</li></ul></dd>
|
||||
|
||||
|
||||
|
@ -4478,7 +4515,7 @@
|
|||
|
||||
<dt class="tag-source">Source:</dt>
|
||||
<dd class="tag-source"><ul class="dummy"><li>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line247">line 247</a>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line254">line 254</a>
|
||||
</li></ul></dd>
|
||||
|
||||
|
||||
|
@ -4536,7 +4573,7 @@
|
|||
|
||||
|
||||
|
||||
<h4 class="name" id="invalidateAttributeCache"><span class="type-signature"></span>invalidateAttributeCache<span class="signature">()</span><span class="type-signature"></span></h4>
|
||||
<h4 class="name" id="invalidate__attributeCache"><span class="type-signature"></span>invalidate__attributeCache<span class="signature">()</span><span class="type-signature"></span></h4>
|
||||
|
||||
|
||||
|
||||
|
@ -4589,7 +4626,7 @@ Cache is note instance scoped.
|
|||
|
||||
<dt class="tag-source">Source:</dt>
|
||||
<dd class="tag-source"><ul class="dummy"><li>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line302">line 302</a>
|
||||
<a href="entities_note_short.js.html">entities/note_short.js</a>, <a href="entities_note_short.js.html#line309">line 309</a>
|
||||
</li></ul></dd>
|
||||
|
||||
|
||||
|
|
|
@ -182,20 +182,27 @@ class NoteShort {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param {string} [name] - attribute name to filter
|
||||
* @returns {Promise<Attribute[]>}
|
||||
* @param {string} [type] - (optional) attribute type to filter
|
||||
* @param {string} [name] - (optional) attribute name to filter
|
||||
* @returns {Promise<Attribute[]>} all note's attributes, including inherited ones
|
||||
*/
|
||||
async getAttributes(name) {
|
||||
if (!this.attributeCache) {
|
||||
this.attributeCache = (await server.get('notes/' + this.noteId + '/attributes'))
|
||||
async getAttributes(type, name) {
|
||||
if (!this.__attributeCache) {
|
||||
this.__attributeCache = (await server.get('notes/' + this.noteId + '/attributes'))
|
||||
.map(attrRow => new Attribute(this.treeCache, attrRow));
|
||||
}
|
||||
|
||||
if (name) {
|
||||
return this.attributeCache.filter(attr => attr.name === name);
|
||||
if (type && name) {
|
||||
return this.__attributeCache.filter(attr => attr.type === type && 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 {
|
||||
return this.attributeCache;
|
||||
return this.__attributeCache.slice();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -204,7 +211,7 @@ class NoteShort {
|
|||
* @returns {Promise<Attribute[]>} all note's labels (attributes with type label), including inherited ones
|
||||
*/
|
||||
async getLabels(name) {
|
||||
return (await this.getAttributes(name)).filter(attr => attr.type === LABEL);
|
||||
return await this.getAttributes(LABEL, name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -212,7 +219,7 @@ class NoteShort {
|
|||
* @returns {Promise<Attribute[]>} all note's label definitions, including inherited ones
|
||||
*/
|
||||
async getLabelDefinitions(name) {
|
||||
return (await this.getAttributes(name)).filter(attr => attr.type === LABEL_DEFINITION);
|
||||
return await this.getAttributes(LABEL_DEFINITION, name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -220,7 +227,7 @@ class NoteShort {
|
|||
* @returns {Promise<Attribute[]>} all note's relations (attributes with type relation), including inherited ones
|
||||
*/
|
||||
async getRelations(name) {
|
||||
return (await this.getAttributes(name)).filter(attr => attr.type === RELATION);
|
||||
return await this.getAttributes(RELATION, name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -228,7 +235,7 @@ class NoteShort {
|
|||
* @returns {Promise<Attribute[]>} all note's relation definitions including inherited ones
|
||||
*/
|
||||
async getRelationDefinitions(name) {
|
||||
return (await this.getAttributes(name)).filter(attr => attr.type === RELATION_DEFINITION);
|
||||
return await this.getAttributes(RELATION_DEFINITION, name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -327,8 +334,8 @@ class NoteShort {
|
|||
* Clear note's attributes cache to force fresh reload for next attribute request.
|
||||
* Cache is note instance scoped.
|
||||
*/
|
||||
invalidateAttributeCache() {
|
||||
this.attributeCache = null;
|
||||
invalidate__attributeCache() {
|
||||
this.__attributeCache = null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -349,7 +356,7 @@ class NoteShort {
|
|||
const dto = Object.assign({}, this);
|
||||
delete dto.treeCache;
|
||||
delete dto.archived;
|
||||
delete dto.attributeCache;
|
||||
delete dto.__attributeCache;
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue