Files
Trilium/src/entities/label.js

42 lines
1.1 KiB
JavaScript
Raw Normal View History

"use strict";
const Entity = require('./entity');
const repository = require('../services/repository');
2018-03-31 22:15:06 -04:00
const utils = require('../services/utils');
2018-04-01 12:45:35 -04:00
const sql = require('../services/sql');
class Label extends Entity {
static get tableName() { return "labels"; }
static get primaryKeyName() { return "labelId"; }
2018-01-29 23:35:36 -05:00
async getNote() {
return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]);
}
2018-03-31 22:15:06 -04:00
2018-04-01 12:45:35 -04:00
async beforeSaving() {
if (!this.labelId) {
this.labelId = utils.newLabelId();
}
2018-04-01 20:50:58 -04:00
if (!this.value) {
2018-04-01 12:45:35 -04:00
// null value isn't allowed
this.value = "";
}
if (this.position === undefined) {
2018-04-01 20:50:58 -04:00
this.position = 1 + await sql.getValue(`SELECT COALESCE(MAX(position), 0) FROM labels WHERE noteId = ?`, [this.noteId]);
2018-04-01 12:45:35 -04:00
}
if (!this.isDeleted) {
this.isDeleted = false;
}
2018-03-31 22:15:06 -04:00
if (!this.dateCreated) {
this.dateCreated = utils.nowDate();
}
this.dateModified = utils.nowDate();
}
}
module.exports = Label;