2018-01-29 18:34:59 -05:00
|
|
|
"use strict";
|
|
|
|
|
2018-01-29 23:17:44 -05:00
|
|
|
const utils = require('../services/utils');
|
2018-03-31 23:08:22 -04:00
|
|
|
const repository = require('../services/repository');
|
2018-01-29 23:17:44 -05:00
|
|
|
|
2018-01-29 18:34:59 -05:00
|
|
|
class Entity {
|
2018-04-01 11:42:12 -04:00
|
|
|
constructor(row = {}) {
|
2018-01-29 18:34:59 -05:00
|
|
|
for (const key in row) {
|
|
|
|
this[key] = row[key];
|
|
|
|
}
|
|
|
|
}
|
2018-03-31 23:08:22 -04:00
|
|
|
|
2018-04-02 20:30:00 -04:00
|
|
|
beforeSaving() {
|
|
|
|
if (!this[this.constructor.primaryKeyName]) {
|
|
|
|
this[this.constructor.primaryKeyName] = utils.newEntityId();
|
|
|
|
}
|
2018-05-22 00:15:54 -04:00
|
|
|
|
|
|
|
let contentToHash = "";
|
|
|
|
|
2018-05-22 22:22:15 -04:00
|
|
|
for (const propertyName of this.constructor.hashedProperties) {
|
2018-05-22 00:15:54 -04:00
|
|
|
contentToHash += "|" + this[propertyName];
|
|
|
|
}
|
|
|
|
|
2018-05-22 23:51:13 -04:00
|
|
|
// this IF is to ease the migration from before hashed options, can be later removed
|
|
|
|
if (this.constructor.tableName !== 'options' || this.isSynced) {
|
|
|
|
this["hash"] = utils.hash(contentToHash).substr(0, 10);
|
|
|
|
}
|
2018-04-02 20:30:00 -04:00
|
|
|
}
|
|
|
|
|
2018-03-31 23:08:22 -04:00
|
|
|
async save() {
|
|
|
|
await repository.updateEntity(this);
|
2018-04-02 22:53:01 -04:00
|
|
|
|
|
|
|
return this;
|
2018-03-31 23:08:22 -04:00
|
|
|
}
|
2018-01-29 18:34:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Entity;
|