trilium/src/entities/entity.js

48 lines
1 KiB
JavaScript
Raw Normal View History

"use strict";
const utils = require('../services/utils');
class Entity {
2018-08-23 05:37:06 +08:00
/**
* @param {object} [row] - database row representing given entity
*/
2018-04-01 23:42:12 +08:00
constructor(row = {}) {
for (const key in row) {
this[key] = row[key];
}
if ('isDeleted' in this) {
this.isDeleted = !!this.isDeleted;
}
}
2018-04-01 11:08:22 +08:00
2018-04-03 08:30:00 +08:00
beforeSaving() {
if (!this[this.constructor.primaryKeyName]) {
this[this.constructor.primaryKeyName] = utils.newEntityId();
}
const origHash = this.hash;
this.hash = this.generateHash();
this.isChanged = origHash !== this.hash;
}
generateHash() {
let contentToHash = "";
2018-05-23 10:22:15 +08:00
for (const propertyName of this.constructor.hashedProperties) {
contentToHash += "|" + this[propertyName];
}
return utils.hash(contentToHash).substr(0, 10);
2018-04-03 08:30:00 +08:00
}
2018-04-01 11:08:22 +08:00
async save() {
await require('../services/repository').updateEntity(this);
2018-04-03 10:53:01 +08:00
return this;
2018-04-01 11:08:22 +08:00
}
}
module.exports = Entity;