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' },
|
|
|
|
{ type: 'relation', name: 'runOnNoteTitleChange' }
|
2018-08-03 04:48:21 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
async function getNotesWithAttribute(name, value) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getNoteWithAttribute(name, value) {
|
|
|
|
const notes = await getNotesWithAttribute(name, value);
|
|
|
|
|
|
|
|
return notes.length > 0 ? notes[0] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createAttribute(noteId, name, value = "") {
|
|
|
|
return await new Attribute({
|
|
|
|
noteId: noteId,
|
|
|
|
name: name,
|
|
|
|
value: value
|
|
|
|
}).save();
|
|
|
|
}
|
|
|
|
|
2018-08-03 17:11:57 +08:00
|
|
|
async function getAttributeNames(type, nameLike) {
|
|
|
|
const names = await sql.getColumn(
|
|
|
|
`SELECT DISTINCT name
|
|
|
|
FROM attributes
|
|
|
|
WHERE isDeleted = 0
|
|
|
|
AND type = ?
|
|
|
|
AND name LIKE '%${utils.sanitizeSql(nameLike)}%'`, [ type ]);
|
|
|
|
|
|
|
|
for (const attribute of BUILTIN_ATTRIBUTES) {
|
|
|
|
if (attribute.type === type && !names.includes(attribute.name)) {
|
|
|
|
names.push(attribute.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
names.sort();
|
|
|
|
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
2018-08-03 04:48:21 +08:00
|
|
|
module.exports = {
|
|
|
|
getNotesWithAttribute,
|
|
|
|
getNoteWithAttribute,
|
|
|
|
createAttribute,
|
2018-08-03 17:11:57 +08:00
|
|
|
getAttributeNames,
|
2018-08-03 04:48:21 +08:00
|
|
|
BUILTIN_ATTRIBUTES
|
|
|
|
};
|