mirror of
https://github.com/zadam/trilium.git
synced 2024-11-17 05:13:29 +08:00
53b39e2e82
- use CSS contain wherever possible to reduce subtrees of forced reflows - reduced dependency between note and note_contents updates which will reduce number of updates to components - optimization of "many rows" querying
63 lines
1.4 KiB
JavaScript
63 lines
1.4 KiB
JavaScript
"use strict";
|
|
|
|
const utils = require('../services/utils');
|
|
let repo = null;
|
|
|
|
class Entity {
|
|
/**
|
|
* @param {object} [row] - database row representing given entity
|
|
*/
|
|
constructor(row = {}) {
|
|
for (const key in row) {
|
|
// ! is used when joint-fetching notes and note_contents objects for performance
|
|
if (!key.startsWith('!')) {
|
|
this[key] = row[key];
|
|
}
|
|
}
|
|
|
|
if ('isDeleted' in this) {
|
|
this.isDeleted = !!this.isDeleted;
|
|
}
|
|
}
|
|
|
|
beforeSaving() {
|
|
this.generateIdIfNecessary();
|
|
|
|
const origHash = this.hash;
|
|
|
|
this.hash = this.generateHash();
|
|
this.isChanged = origHash !== this.hash;
|
|
}
|
|
|
|
generateIdIfNecessary() {
|
|
if (!this[this.constructor.primaryKeyName]) {
|
|
this[this.constructor.primaryKeyName] = utils.newEntityId();
|
|
}
|
|
}
|
|
|
|
generateHash() {
|
|
let contentToHash = "";
|
|
|
|
for (const propertyName of this.constructor.hashedProperties) {
|
|
contentToHash += "|" + this[propertyName];
|
|
}
|
|
|
|
return utils.hash(contentToHash).substr(0, 10);
|
|
}
|
|
|
|
get repository() {
|
|
if (!repo) {
|
|
repo = require('../services/repository');
|
|
}
|
|
|
|
return repo;
|
|
}
|
|
|
|
save() {
|
|
this.repository.updateEntity(this);
|
|
|
|
return this;
|
|
}
|
|
}
|
|
|
|
module.exports = Entity;
|