entities_attribute.js.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: entities/attribute.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/attribute.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>"use strict";
  20. const Entity = require('./entity');
  21. const repository = require('../services/repository');
  22. const dateUtils = require('../services/date_utils');
  23. const sql = require('../services/sql');
  24. /**
  25. * Attribute is key value pair owned by a note.
  26. *
  27. * @property {string} attributeId
  28. * @property {string} noteId
  29. * @property {string} type
  30. * @property {string} name
  31. * @property {string} value
  32. * @property {int} position
  33. * @property {boolean} isInheritable
  34. * @property {boolean} isDeleted
  35. * @property {string|null} deleteId - ID identifying delete transaction
  36. * @property {string} utcDateCreated
  37. * @property {string} utcDateModified
  38. *
  39. * @extends Entity
  40. */
  41. class Attribute extends Entity {
  42. static get entityName() { return "attributes"; }
  43. static get primaryKeyName() { return "attributeId"; }
  44. static get hashedProperties() { return ["attributeId", "noteId", "type", "name", "value", "isInheritable", "isDeleted", "utcDateCreated"]; }
  45. constructor(row) {
  46. super(row);
  47. this.isInheritable = !!this.isInheritable;
  48. if (this.isDefinition()) {
  49. try {
  50. this.value = JSON.parse(this.value);
  51. }
  52. catch (e) {
  53. }
  54. }
  55. }
  56. /**
  57. * @returns {Promise&lt;Note|null>}
  58. */
  59. async getNote() {
  60. return await repository.getNote(this.noteId);
  61. }
  62. /**
  63. * @returns {Promise&lt;Note|null>}
  64. */
  65. async getTargetNote() {
  66. if (this.type !== 'relation') {
  67. throw new Error(`Attribute ${this.attributeId} is not relation`);
  68. }
  69. if (!this.value) {
  70. return null;
  71. }
  72. return await repository.getNote(this.value);
  73. }
  74. /**
  75. * @return {boolean}
  76. */
  77. isDefinition() {
  78. return this.type === 'label-definition' || this.type === 'relation-definition';
  79. }
  80. async beforeSaving() {
  81. if (!this.value) {
  82. if (this.type === 'relation') {
  83. throw new Error(`Cannot save relation ${this.name} since it does not target any note.`);
  84. }
  85. // null value isn't allowed
  86. this.value = "";
  87. }
  88. if (this.position === undefined) {
  89. this.position = 1 + await sql.getValue(`SELECT COALESCE(MAX(position), 0) FROM attributes WHERE noteId = ?`, [this.noteId]);
  90. }
  91. if (!this.isInheritable) {
  92. this.isInheritable = false;
  93. }
  94. if (!this.isDeleted) {
  95. this.isDeleted = false;
  96. }
  97. if (!this.utcDateCreated) {
  98. this.utcDateCreated = dateUtils.utcNowDateTime();
  99. }
  100. super.beforeSaving();
  101. if (this.isChanged) {
  102. this.utcDateModified = dateUtils.utcNowDateTime();
  103. }
  104. }
  105. // cannot be static!
  106. updatePojo(pojo) {
  107. delete pojo.isOwned;
  108. delete pojo.__note;
  109. }
  110. createClone(type, name, value) {
  111. return new Attribute({
  112. noteId: this.noteId,
  113. type: type,
  114. name: name,
  115. value: value,
  116. position: this.position,
  117. isInheritable: this.isInheritable,
  118. isDeleted: false,
  119. utcDateCreated: this.utcDateCreated,
  120. utcDateModified: this.utcDateModified
  121. });
  122. }
  123. }
  124. module.exports = Attribute;</code></pre>
  125. </article>
  126. </section>
  127. </div>
  128. <nav>
  129. <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>
  130. </nav>
  131. <br class="clear">
  132. <footer>
  133. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.4</a>
  134. </footer>
  135. <script> prettyPrint(); </script>
  136. <script src="scripts/linenumber.js"> </script>
  137. </body>
  138. </html>