entities_branch.js.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: entities/branch.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: entities/branch.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>"use strict";
  20. const Entity = require('./entity');
  21. const dateUtils = require('../services/date_utils');
  22. const repository = require('../services/repository');
  23. const sql = require('../services/sql');
  24. /**
  25. * Branch represents note's placement in the tree - it's essentially pair of noteId and parentNoteId.
  26. * Each note can have multiple (at least one) branches, meaning it can be placed into multiple places in the tree.
  27. *
  28. * @property {string} branchId - primary key
  29. * @property {string} noteId
  30. * @property {string} parentNoteId
  31. * @property {int} notePosition
  32. * @property {string} prefix
  33. * @property {boolean} isExpanded
  34. * @property {boolean} isDeleted
  35. * @property {string|null} deleteId - ID identifying delete transaction
  36. * @property {string} utcDateModified
  37. * @property {string} utcDateCreated
  38. *
  39. * @extends Entity
  40. */
  41. class Branch extends Entity {
  42. static get entityName() { return "branches"; }
  43. static get primaryKeyName() { return "branchId"; }
  44. // notePosition is not part of hash because it would produce a lot of updates in case of reordering
  45. static get hashedProperties() { return ["branchId", "noteId", "parentNoteId", "isDeleted", "deleteId", "prefix"]; }
  46. /** @returns {Promise&lt;Note|null>} */
  47. async getNote() {
  48. return await repository.getNote(this.noteId);
  49. }
  50. /** @returns {Promise&lt;Note|null>} */
  51. async getParentNote() {
  52. return await repository.getNote(this.parentNoteId);
  53. }
  54. async beforeSaving() {
  55. if (this.notePosition === undefined) {
  56. const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [this.parentNoteId]);
  57. this.notePosition = maxNotePos === null ? 0 : maxNotePos + 10;
  58. }
  59. if (!this.isExpanded) {
  60. this.isExpanded = false;
  61. }
  62. if (!this.isDeleted) {
  63. this.isDeleted = false;
  64. }
  65. if (!this.utcDateCreated) {
  66. this.utcDateCreated = dateUtils.utcNowDateTime();
  67. }
  68. super.beforeSaving();
  69. if (this.isChanged) {
  70. this.utcDateModified = dateUtils.utcNowDateTime();
  71. }
  72. }
  73. createClone(parentNoteId, notePosition) {
  74. return new Branch({
  75. noteId: this.noteId,
  76. parentNoteId: parentNoteId,
  77. notePosition: notePosition,
  78. prefix: this.prefix,
  79. isExpanded: this.isExpanded,
  80. isDeleted: false,
  81. utcDateCreated: this.utcDateCreated,
  82. utcDateModified: this.utcDateModified
  83. });
  84. }
  85. }
  86. module.exports = Branch;</code></pre>
  87. </article>
  88. </section>
  89. </div>
  90. <nav>
  91. <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>
  92. </nav>
  93. <br class="clear">
  94. <footer>
  95. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.4</a>
  96. </footer>
  97. <script> prettyPrint(); </script>
  98. <script src="scripts/linenumber.js"> </script>
  99. </body>
  100. </html>