services_backend_script_api.js.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: services/backend_script_api.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: services/backend_script_api.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>const log = require('./log');
  20. const noteService = require('./notes');
  21. const sql = require('./sql');
  22. const utils = require('./utils');
  23. const attributeService = require('./attributes');
  24. const dateNoteService = require('./date_notes');
  25. const treeService = require('./tree');
  26. const config = require('./config');
  27. const repository = require('./repository');
  28. const axios = require('axios');
  29. const dayjs = require('dayjs');
  30. const cloningService = require('./cloning');
  31. const ws = require('./ws.js');
  32. const appInfo = require('./app_info');
  33. const searchService = require('./search');
  34. /**
  35. * This is the main backend API interface for scripts. It's published in the local "api" object.
  36. *
  37. * @constructor
  38. * @hideconstructor
  39. */
  40. function BackendScriptApi(currentNote, apiParams) {
  41. /** @property {Note} note where script started executing */
  42. this.startNote = apiParams.startNote;
  43. /** @property {Note} note where script is currently executing. Don't mix this up with concept of active note */
  44. this.currentNote = currentNote;
  45. /** @property {Entity} entity whose event triggered this executions */
  46. this.originEntity = apiParams.originEntity;
  47. for (const key in apiParams) {
  48. this[key] = apiParams[key];
  49. }
  50. this.axios = axios;
  51. this.dayjs = dayjs;
  52. this.utils = {
  53. unescapeHtml: utils.unescapeHtml
  54. };
  55. /**
  56. * Instance name identifies particular Trilium instance. It can be useful for scripts
  57. * if some action needs to happen on only one specific instance.
  58. *
  59. * @returns {string|null}
  60. */
  61. this.getInstanceName = () => config.General ? config.General.instanceName : null;
  62. /**
  63. * @method
  64. * @param {string} noteId
  65. * @returns {Promise&lt;Note|null>}
  66. */
  67. this.getNote = repository.getNote;
  68. /**
  69. * @method
  70. * @param {string} branchId
  71. * @returns {Promise&lt;Branch|null>}
  72. */
  73. this.getBranch = repository.getBranch;
  74. /**
  75. * @method
  76. * @param {string} attributeId
  77. * @returns {Promise&lt;Attribute|null>}
  78. */
  79. this.getAttribute = repository.getAttribute;
  80. /**
  81. * @method
  82. * @param {string} imageId
  83. * @returns {Promise&lt;Image|null>}
  84. */
  85. this.getImage = repository.getImage;
  86. /**
  87. * Retrieves first entity from the SQL's result set.
  88. *
  89. * @method
  90. * @param {string} SQL query
  91. * @param {Array.&lt;?>} array of params
  92. * @returns {Promise&lt;Entity|null>}
  93. */
  94. this.getEntity = repository.getEntity;
  95. /**
  96. * @method
  97. * @param {string} SQL query
  98. * @param {Array.&lt;?>} array of params
  99. * @returns {Promise&lt;Entity[]>}
  100. */
  101. this.getEntities = repository.getEntities;
  102. /**
  103. * This is a powerful search method - you can search by attributes and their values, e.g.:
  104. * "@dateModified =* MONTH AND @log". See full documentation for all options at: https://github.com/zadam/trilium/wiki/Search
  105. *
  106. * @method
  107. * @param {string} searchString
  108. * @returns {Promise&lt;Note[]>}
  109. */
  110. this.searchForNotes = searchService.searchForNotes;
  111. /**
  112. * This is a powerful search method - you can search by attributes and their values, e.g.:
  113. * "@dateModified =* MONTH AND @log". See full documentation for all options at: https://github.com/zadam/trilium/wiki/Search
  114. *
  115. * @method
  116. * @param {string} searchString
  117. * @returns {Promise&lt;Note|null>}
  118. */
  119. this.searchForNote = async searchString => {
  120. const notes = await searchService.searchForNotes(searchString);
  121. return notes.length > 0 ? notes[0] : null;
  122. };
  123. /**
  124. * Retrieves notes with given label name &amp; value
  125. *
  126. * @method
  127. * @param {string} name - attribute name
  128. * @param {string} [value] - attribute value
  129. * @returns {Promise&lt;Note[]>}
  130. */
  131. this.getNotesWithLabel = attributeService.getNotesWithLabel;
  132. /**
  133. * Retrieves first note with given label name &amp; value
  134. *
  135. * @method
  136. * @param {string} name - attribute name
  137. * @param {string} [value] - attribute value
  138. * @returns {Promise&lt;Note|null>}
  139. */
  140. this.getNoteWithLabel = attributeService.getNoteWithLabel;
  141. /**
  142. * If there's no branch between note and parent note, create one. Otherwise do nothing.
  143. *
  144. * @method
  145. * @param {string} noteId
  146. * @param {string} parentNoteId
  147. * @param {string} prefix - if branch will be create between note and parent note, set this prefix
  148. * @returns {Promise&lt;void>}
  149. */
  150. this.ensureNoteIsPresentInParent = cloningService.ensureNoteIsPresentInParent;
  151. /**
  152. * If there's a branch between note and parent note, remove it. Otherwise do nothing.
  153. *
  154. * @method
  155. * @param {string} noteId
  156. * @param {string} parentNoteId
  157. * @returns {Promise&lt;void>}
  158. */
  159. this.ensureNoteIsAbsentFromParent = cloningService.ensureNoteIsAbsentFromParent;
  160. /**
  161. * Based on the value, either create or remove branch between note and parent note.
  162. *
  163. * @method
  164. * @param {boolean} present - true if we want the branch to exist, false if we want it gone
  165. * @param {string} noteId
  166. * @param {string} parentNoteId
  167. * @param {string} prefix - if branch will be create between note and parent note, set this prefix
  168. * @returns {Promise&lt;void>}
  169. */
  170. this.toggleNoteInParent = cloningService.toggleNoteInParent;
  171. /**
  172. * @typedef {object} CreateNoteAttribute
  173. * @property {string} type - attribute type - label, relation etc.
  174. * @property {string} name - attribute name
  175. * @property {string} [value] - attribute value
  176. */
  177. /**
  178. * Create text note. See also createNewNote() for more options.
  179. *
  180. * @param {string} parentNoteId
  181. * @param {string} title
  182. * @param {string} content
  183. * @return {Promise&lt;{note: Note, branch: Branch}>}
  184. */
  185. this.createTextNote = async (parentNoteId, title, content = '') => await noteService.createNewNote({
  186. parentNoteId,
  187. title,
  188. content,
  189. type: 'text'
  190. });
  191. /**
  192. * Create data note - data in this context means object serializable to JSON. Created note will be of type 'code' and
  193. * JSON MIME type. See also createNewNote() for more options.
  194. *
  195. * @param {string} parentNoteId
  196. * @param {string} title
  197. * @param {object} content
  198. * @return {Promise&lt;{note: Note, branch: Branch}>}
  199. */
  200. this.createDataNote = async (parentNoteId, title, content = {}) => await noteService.createNewNote({
  201. parentNoteId,
  202. title,
  203. content: JSON.stringify(content, null, '\t'),
  204. type: 'code',
  205. mime: 'application/json'
  206. });
  207. /**
  208. * @typedef {object} CreateNewNoteParams
  209. * @property {string} parentNoteId - MANDATORY
  210. * @property {string} title - MANDATORY
  211. * @property {string|buffer} content - MANDATORY
  212. * @property {string} type - text, code, file, image, search, book, relation-map - MANDATORY
  213. * @property {string} mime - value is derived from default mimes for type
  214. * @property {boolean} isProtected - default is false
  215. * @property {boolean} isExpanded - default is false
  216. * @property {string} prefix - default is empty string
  217. * @property {int} notePosition - default is last existing notePosition in a parent + 10
  218. */
  219. /**
  220. * @method
  221. *
  222. * @param {CreateNewNoteParams} [params]
  223. * @returns {Promise&lt;{note: Note, branch: Branch}>} object contains newly created entities note and branch
  224. */
  225. this.createNewNote = noteService.createNewNote;
  226. /**
  227. * @typedef {object} CreateNoteAttribute
  228. * @property {string} type - attribute type - label, relation etc.
  229. * @property {string} name - attribute name
  230. * @property {string} [value] - attribute value
  231. */
  232. /**
  233. * @typedef {object} CreateNoteExtraOptions
  234. * @property {boolean} [json=false] - should the note be JSON
  235. * @property {boolean} [isProtected=false] - should the note be protected
  236. * @property {string} [type='text'] - note type
  237. * @property {string} [mime='text/html'] - MIME type of the note
  238. * @property {CreateNoteAttribute[]} [attributes=[]] - attributes to be created for this note
  239. */
  240. /**
  241. * @method
  242. * @deprecated please use createNote() API method instead
  243. *
  244. * @param {string} parentNoteId - create new note under this parent
  245. * @param {string} title
  246. * @param {string} [content=""]
  247. * @param {CreateNoteExtraOptions} [extraOptions={}]
  248. * @returns {Promise&lt;{note: Note, branch: Branch}>} object contains newly created entities note and branch
  249. */
  250. this.createNote = async (parentNoteId, title, content = "", extraOptions= {}) => {
  251. extraOptions.parentNoteId = parentNoteId;
  252. extraOptions.title = title;
  253. const parentNote = await repository.getNote(parentNoteId);
  254. // code note type can be inherited, otherwise text is default
  255. extraOptions.type = parentNote.type === 'code' ? 'code' : 'text';
  256. extraOptions.mime = parentNote.type === 'code' ? parentNote.mime : 'text/html';
  257. if (extraOptions.json) {
  258. extraOptions.content = JSON.stringify(content || {}, null, '\t');
  259. extraOptions.type = 'code';
  260. extraOptions.mime = 'application/json';
  261. }
  262. else {
  263. extraOptions.content = content;
  264. }
  265. const {note, branch} = await noteService.createNewNote(extraOptions);
  266. for (const attr of extraOptions.attributes || []) {
  267. await attributeService.createAttribute({
  268. noteId: note.noteId,
  269. type: attr.type,
  270. name: attr.name,
  271. value: attr.value,
  272. isInheritable: !!attr.isInheritable
  273. });
  274. }
  275. return {note, branch};
  276. };
  277. /**
  278. * Log given message to trilium logs.
  279. *
  280. * @param message
  281. */
  282. this.log = message => log.info(`Script "${currentNote.title}" (${currentNote.noteId}): ${message}`);
  283. /**
  284. * Returns root note of the calendar.
  285. *
  286. * @method
  287. * @returns {Promise&lt;Note|null>}
  288. */
  289. this.getRootCalendarNote = dateNoteService.getRootCalendarNote;
  290. /**
  291. * Returns day note for given date. If such note doesn't exist, it is created.
  292. *
  293. * @method
  294. * @param {string} date in YYYY-MM-DD format
  295. * @returns {Promise&lt;Note|null>}
  296. */
  297. this.getDateNote = dateNoteService.getDateNote;
  298. /**
  299. * Returns today's day note. If such note doesn't exist, it is created.
  300. *
  301. * @method
  302. * @returns {Promise&lt;Note|null>}
  303. */
  304. this.getTodayNote = dateNoteService.getTodayNote;
  305. /**
  306. * Returns note for the first date of the week of the given date.
  307. *
  308. * @method
  309. * @param {string} date in YYYY-MM-DD format
  310. * @param {object} options - "startOfTheWeek" - either "monday" (default) or "sunday"
  311. * @returns {Promise&lt;Note|null>}
  312. */
  313. this.getWeekNote = dateNoteService.getWeekNote;
  314. /**
  315. * Returns month note for given date. If such note doesn't exist, it is created.
  316. *
  317. * @method
  318. * @param {string} date in YYYY-MM format
  319. * @returns {Promise&lt;Note|null>}
  320. */
  321. this.getMonthNote = dateNoteService.getMonthNote;
  322. /**
  323. * Returns year note for given year. If such note doesn't exist, it is created.
  324. *
  325. * @method
  326. * @param {string} year in YYYY format
  327. * @returns {Promise&lt;Note|null>}
  328. */
  329. this.getYearNote = dateNoteService.getYearNote;
  330. /**
  331. * @method
  332. * @param {string} parentNoteId - this note's child notes will be sorted
  333. * @returns Promise&lt;void>
  334. */
  335. this.sortNotesAlphabetically = treeService.sortNotesAlphabetically;
  336. /**
  337. * This method finds note by its noteId and prefix and either sets it to the given parentNoteId
  338. * or removes the branch (if parentNoteId is not given).
  339. *
  340. * This method looks similar to toggleNoteInParent() but differs because we're looking up branch by prefix.
  341. *
  342. * @method
  343. * @deprecated - this method is pretty confusing and serves specialized purpose only
  344. * @param {string} noteId
  345. * @param {string} prefix
  346. * @param {string|null} parentNoteId
  347. */
  348. this.setNoteToParent = treeService.setNoteToParent;
  349. /**
  350. * This functions wraps code which is supposed to be running in transaction. If transaction already
  351. * exists, then we'll use that transaction.
  352. *
  353. * This method is required only when script has label manualTransactionHandling, all other scripts are
  354. * transactional by default.
  355. *
  356. * @method
  357. * @param {function} func
  358. * @returns {Promise&lt;?>} result of func callback
  359. */
  360. this.transactional = sql.transactional;
  361. this.sql = sql;
  362. /**
  363. * @method
  364. * @deprecated - this is now no-op since all the changes should be gracefully handled per widget
  365. */
  366. this.refreshTree = () => {};
  367. /**
  368. * @return {{syncVersion, appVersion, buildRevision, dbVersion, dataDirectory, buildDate}|*} - object representing basic info about running Trilium version
  369. */
  370. this.getAppInfo = () => appInfo
  371. }
  372. module.exports = BackendScriptApi;
  373. </code></pre>
  374. </article>
  375. </section>
  376. </div>
  377. <nav>
  378. <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>
  379. </nav>
  380. <br class="clear">
  381. <footer>
  382. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.4</a>
  383. </footer>
  384. <script> prettyPrint(); </script>
  385. <script src="scripts/linenumber.js"> </script>
  386. </body>
  387. </html>