Notes/src/services/becca/entities/abstract_entity.js

59 lines
1.1 KiB
JavaScript
Raw Normal View History

"use strict";
2021-04-25 21:19:18 +02:00
const utils = require('../../utils');
2021-04-30 22:13:13 +02:00
let becca = null;
2021-04-25 21:19:18 +02:00
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() {
2021-04-25 22:02:32 +02:00
// FIXME
return this.utcDateModified || this.utcDateCreated || "FAKE";
}
2021-04-30 22:13:13 +02:00
get becca() {
if (!becca) {
becca = require('../becca');
}
return becca;
}
// temporarily needed for saving entities
get repository() {
if (!repo) {
2021-04-25 21:19:18 +02:00
repo = require('../../repository');
}
return repo;
}
save() {
this.repository.updateEntity(this);
return this;
}
}
module.exports = AbstractEntity;