entities_entity.js.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: entities/entity.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/entity.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>"use strict";
  20. const utils = require('../services/utils');
  21. class Entity {
  22. /**
  23. * @param {object} [row] - database row representing given entity
  24. */
  25. constructor(row = {}) {
  26. for (const key in row) {
  27. // ! is used when joint-fetching notes and note_contents objects for performance
  28. if (!key.startsWith('!')) {
  29. this[key] = row[key];
  30. }
  31. }
  32. if ('isDeleted' in this) {
  33. this.isDeleted = !!this.isDeleted;
  34. }
  35. }
  36. beforeSaving() {
  37. this.generateIdIfNecessary();
  38. const origHash = this.hash;
  39. this.hash = this.generateHash();
  40. if (this.forcedChange) {
  41. this.isChanged = true;
  42. delete this.forcedChange;
  43. }
  44. else {
  45. this.isChanged = origHash !== this.hash;
  46. }
  47. }
  48. generateIdIfNecessary() {
  49. if (!this[this.constructor.primaryKeyName]) {
  50. this[this.constructor.primaryKeyName] = utils.newEntityId();
  51. }
  52. }
  53. generateHash() {
  54. let contentToHash = "";
  55. for (const propertyName of this.constructor.hashedProperties) {
  56. contentToHash += "|" + this[propertyName];
  57. }
  58. return utils.hash(contentToHash).substr(0, 10);
  59. }
  60. async save() {
  61. await require('../services/repository').updateEntity(this);
  62. return this;
  63. }
  64. }
  65. module.exports = Entity;</code></pre>
  66. </article>
  67. </section>
  68. </div>
  69. <nav>
  70. <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>
  71. </nav>
  72. <br class="clear">
  73. <footer>
  74. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.4</a>
  75. </footer>
  76. <script> prettyPrint(); </script>
  77. <script src="scripts/linenumber.js"> </script>
  78. </body>
  79. </html>