trilium/src/services/becca/entities/abstract_entity.js
2021-04-25 22:02:32 +02:00

49 lines
962 B
JavaScript

"use strict";
const utils = require('../../utils');
let repo = null;
class AbstractEntity {
beforeSaving() {
this.generateIdIfNecessary();
}
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);
}
getUtcDateChanged() {
// FIXME
return this.utcDateModified || this.utcDateCreated || "FAKE";
}
get repository() {
if (!repo) {
repo = require('../../repository');
}
return repo;
}
save() {
this.repository.updateEntity(this);
return this;
}
}
module.exports = AbstractEntity;