2018-08-03 04:48:21 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const repository = require('./repository');
|
2018-08-03 17:11:57 +08:00
|
|
|
const sql = require('./sql');
|
|
|
|
const utils = require('./utils');
|
2018-08-03 04:48:21 +08:00
|
|
|
const Attribute = require('../entities/attribute');
|
|
|
|
|
|
|
|
const BUILTIN_ATTRIBUTES = [
|
2018-08-03 17:11:57 +08:00
|
|
|
// label names
|
|
|
|
{ type: 'label', name: 'disableVersioning' },
|
|
|
|
{ type: 'label', name: 'calendarRoot' },
|
|
|
|
{ type: 'label', name: 'archived' },
|
|
|
|
{ type: 'label', name: 'excludeFromExport' },
|
|
|
|
{ type: 'label', name: 'run' },
|
|
|
|
{ type: 'label', name: 'manualTransactionHandling' },
|
|
|
|
{ type: 'label', name: 'disableInclusion' },
|
|
|
|
{ type: 'label', name: 'appCss' },
|
|
|
|
{ type: 'label', name: 'hideChildrenOverview' },
|
|
|
|
|
|
|
|
// relation names
|
|
|
|
{ type: 'relation', name: 'runOnNoteView' },
|
2018-08-10 20:31:57 +08:00
|
|
|
{ type: 'relation', name: 'runOnNoteTitleChange' },
|
2018-08-13 23:05:16 +08:00
|
|
|
{ type: 'relation', name: 'runOnAttributeChange' },
|
|
|
|
{ type: 'relation', name: 'inheritAttributes' }
|
2018-08-03 04:48:21 +08:00
|
|
|
];
|
|
|
|
|
2018-08-07 18:48:11 +08:00
|
|
|
async function getNotesWithLabel(name, value) {
|
2018-08-03 04:48:21 +08:00
|
|
|
let notes;
|
|
|
|
|
|
|
|
if (value !== undefined) {
|
|
|
|
notes = await repository.getEntities(`SELECT notes.* FROM notes JOIN attributes USING(noteId)
|
|
|
|
WHERE notes.isDeleted = 0 AND attributes.isDeleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
notes = await repository.getEntities(`SELECT notes.* FROM notes JOIN attributes USING(noteId)
|
|
|
|
WHERE notes.isDeleted = 0 AND attributes.isDeleted = 0 AND attributes.name = ?`, [name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return notes;
|
|
|
|
}
|
|
|
|
|
2018-08-07 18:48:11 +08:00
|
|
|
async function getNoteWithLabel(name, value) {
|
|
|
|
const notes = await getNotesWithLabel(name, value);
|
2018-08-03 04:48:21 +08:00
|
|
|
|
|
|
|
return notes.length > 0 ? notes[0] : null;
|
|
|
|
}
|
|
|
|
|
2018-08-07 19:33:10 +08:00
|
|
|
async function createLabel(noteId, name, value = "") {
|
|
|
|
return await createAttribute({
|
2018-08-03 04:48:21 +08:00
|
|
|
noteId: noteId,
|
2018-08-07 19:33:10 +08:00
|
|
|
type: 'label',
|
2018-08-03 04:48:21 +08:00
|
|
|
name: name,
|
|
|
|
value: value
|
2018-08-07 19:33:10 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createAttribute(attribute) {
|
|
|
|
return await new Attribute(attribute).save();
|
2018-08-03 04:48:21 +08:00
|
|
|
}
|
|
|
|
|
2018-08-03 17:11:57 +08:00
|
|
|
async function getAttributeNames(type, nameLike) {
|
2018-08-16 00:22:02 +08:00
|
|
|
let names;
|
|
|
|
|
|
|
|
if (!nameLike) {
|
|
|
|
names = BUILTIN_ATTRIBUTES
|
|
|
|
.filter(attribute => attribute.type === type)
|
|
|
|
.map(attribute => attribute.name);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
names = await sql.getColumn(
|
|
|
|
`SELECT DISTINCT name
|
|
|
|
FROM attributes
|
|
|
|
WHERE isDeleted = 0
|
|
|
|
AND type = ?
|
|
|
|
AND name LIKE '%${utils.sanitizeSql(nameLike)}%'`, [type]);
|
2018-08-03 17:11:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
names.sort();
|
|
|
|
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
2018-08-03 04:48:21 +08:00
|
|
|
module.exports = {
|
2018-08-07 18:48:11 +08:00
|
|
|
getNotesWithLabel,
|
|
|
|
getNoteWithLabel,
|
2018-08-07 19:33:10 +08:00
|
|
|
createLabel,
|
2018-08-03 04:48:21 +08:00
|
|
|
createAttribute,
|
2018-08-03 17:11:57 +08:00
|
|
|
getAttributeNames,
|
2018-08-03 04:48:21 +08:00
|
|
|
BUILTIN_ATTRIBUTES
|
|
|
|
};
|