2018-08-03 04:48:21 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const Entity = require('./entity');
|
|
|
|
const repository = require('../services/repository');
|
|
|
|
const dateUtils = require('../services/date_utils');
|
|
|
|
const sql = require('../services/sql');
|
|
|
|
|
|
|
|
class Attribute extends Entity {
|
|
|
|
static get tableName() { return "attributes"; }
|
|
|
|
static get primaryKeyName() { return "attributeId"; }
|
2018-08-03 19:06:56 +08:00
|
|
|
static get hashedProperties() { return ["attributeId", "noteId", "type", "name", "value", "isInheritable", "dateModified", "dateCreated"]; }
|
2018-08-03 04:48:21 +08:00
|
|
|
|
2018-08-06 14:59:26 +08:00
|
|
|
constructor(row) {
|
|
|
|
super(row);
|
|
|
|
|
2018-08-07 17:38:00 +08:00
|
|
|
this.isInheritable = !!this.isInheritable;
|
|
|
|
|
2018-08-06 21:23:22 +08:00
|
|
|
if (this.isDefinition()) {
|
|
|
|
try {
|
|
|
|
this.value = JSON.parse(this.value);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
2018-08-06 14:59:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 04:48:21 +08:00
|
|
|
async getNote() {
|
|
|
|
return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]);
|
|
|
|
}
|
|
|
|
|
2018-08-06 14:59:26 +08:00
|
|
|
isDefinition() {
|
2018-08-06 21:23:22 +08:00
|
|
|
return this.type === 'label-definition' || this.type === 'relation-definition';
|
2018-08-06 14:59:26 +08:00
|
|
|
}
|
2018-08-03 04:48:21 +08:00
|
|
|
|
2018-08-06 14:59:26 +08:00
|
|
|
async beforeSaving() {
|
2018-08-03 04:48:21 +08:00
|
|
|
if (!this.value) {
|
|
|
|
// null value isn't allowed
|
|
|
|
this.value = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.position === undefined) {
|
|
|
|
this.position = 1 + await sql.getValue(`SELECT COALESCE(MAX(position), 0) FROM attributes WHERE noteId = ?`, [this.noteId]);
|
|
|
|
}
|
|
|
|
|
2018-08-03 19:06:56 +08:00
|
|
|
if (!this.isInheritable) {
|
|
|
|
this.isInheritable = false;
|
|
|
|
}
|
|
|
|
|
2018-08-03 04:48:21 +08:00
|
|
|
if (!this.isDeleted) {
|
|
|
|
this.isDeleted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.dateCreated) {
|
|
|
|
this.dateCreated = dateUtils.nowDate();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.dateModified = dateUtils.nowDate();
|
2018-08-06 14:59:26 +08:00
|
|
|
|
|
|
|
super.beforeSaving();
|
2018-08-03 04:48:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Attribute;
|