Notes/src/services/labels.js

86 lines
2.5 KiB
JavaScript
Raw Normal View History

"use strict";
const sql = require('./sql');
const utils = require('./utils');
const sync_table = require('./sync_table');
2018-03-31 09:07:58 -04:00
const repository = require('./repository');
2018-03-31 23:19:54 -04:00
const Label = require('../entities/label');
const BUILTIN_LABELS = [
'frontend_startup',
'backend_startup',
'disable_versioning',
'calendar_root',
'hide_in_autocomplete',
'exclude_from_export',
'run',
'manual_transaction_handling',
'disable_inclusion',
'app_css'
];
async function getNoteLabelMap(noteId) {
return await sql.getMap(`SELECT name, value FROM labels WHERE noteId = ? AND isDeleted = 0`, [noteId]);
}
async function getNoteIdWithLabel(name, value) {
return await sql.getValue(`SELECT notes.noteId FROM notes JOIN labels USING(noteId)
WHERE notes.isDeleted = 0
AND labels.isDeleted = 0
AND labels.name = ?
AND labels.value = ?`, [name, value]);
}
2018-03-31 09:07:58 -04:00
async function getNotesWithLabel(name, value) {
let notes;
if (value !== undefined) {
notes = await repository.getEntities(`SELECT notes.* FROM notes JOIN labels USING(noteId)
WHERE notes.isDeleted = 0 AND labels.isDeleted = 0 AND labels.name = ? AND labels.value = ?`, [name, value]);
}
else {
notes = await repository.getEntities(`SELECT notes.* FROM notes JOIN labels USING(noteId)
WHERE notes.isDeleted = 0 AND labels.isDeleted = 0 AND labels.name = ?`, [name]);
}
return notes;
}
2018-03-31 09:07:58 -04:00
async function getNoteWithLabel(name, value) {
const notes = getNotesWithLabel(name, value);
return notes.length > 0 ? notes[0] : null;
}
async function getNoteIdsWithLabel(name) {
return await sql.getColumn(`SELECT DISTINCT notes.noteId FROM notes JOIN labels USING(noteId)
WHERE notes.isDeleted = 0 AND labels.isDeleted = 0 AND labels.name = ? AND labels.isDeleted = 0`, [name]);
}
async function createLabel(noteId, name, value = "") {
if (value === null || value === undefined) {
value = "";
}
const labelId = utils.newLabelId();
const position = 1 + await sql.getValue(`SELECT COALESCE(MAX(position), 0) FROM labels WHERE noteId = ?`, [noteId]);
2018-03-31 23:19:54 -04:00
await (new Label({
labelId: labelId,
noteId: noteId,
name: name,
value: value,
position: position,
isDeleted: false
2018-03-31 23:19:54 -04:00
})).save();
}
module.exports = {
getNoteLabelMap,
getNoteIdWithLabel,
getNotesWithLabel,
getNoteWithLabel,
getNoteIdsWithLabel,
createLabel,
BUILTIN_LABELS
};