mirror of https://github.com/TriliumNext/Notes
cleanup of labels and relations from backend
parent
3491235533
commit
1c0fd243d1
@ -1,70 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
const sql = require('../../services/sql');
|
||||
const labelService = require('../../services/labels');
|
||||
const repository = require('../../services/repository');
|
||||
const Label = require('../../entities/label');
|
||||
|
||||
async function getNoteLabels(req) {
|
||||
const noteId = req.params.noteId;
|
||||
|
||||
return await repository.getEntities("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
|
||||
}
|
||||
|
||||
async function updateNoteLabels(req) {
|
||||
const noteId = req.params.noteId;
|
||||
const labels = req.body;
|
||||
|
||||
for (const label of labels) {
|
||||
let labelEntity;
|
||||
|
||||
if (label.labelId) {
|
||||
labelEntity = await repository.getLabel(label.labelId);
|
||||
}
|
||||
else {
|
||||
// if it was "created" and then immediatelly deleted, we just don't create it at all
|
||||
if (label.isDeleted) {
|
||||
continue;
|
||||
}
|
||||
|
||||
labelEntity = new Label();
|
||||
labelEntity.noteId = noteId;
|
||||
}
|
||||
|
||||
labelEntity.name = label.name;
|
||||
labelEntity.value = label.value;
|
||||
labelEntity.position = label.position;
|
||||
labelEntity.isDeleted = label.isDeleted;
|
||||
|
||||
await labelEntity.save();
|
||||
}
|
||||
|
||||
return await repository.getEntities("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
|
||||
}
|
||||
|
||||
async function getAllLabelNames() {
|
||||
const names = await sql.getColumn("SELECT DISTINCT name FROM labels WHERE isDeleted = 0");
|
||||
|
||||
for (const label of labelService.BUILTIN_LABELS) {
|
||||
if (!names.includes(label)) {
|
||||
names.push(label);
|
||||
}
|
||||
}
|
||||
|
||||
names.sort();
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
async function getValuesForLabel(req) {
|
||||
const labelName = req.params.labelName;
|
||||
|
||||
return await sql.getColumn("SELECT DISTINCT value FROM labels WHERE isDeleted = 0 AND name = ? AND value != '' ORDER BY value", [labelName]);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getNoteLabels,
|
||||
updateNoteLabels,
|
||||
getAllLabelNames,
|
||||
getValuesForLabel
|
||||
};
|
||||
@ -1,64 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
const sql = require('../../services/sql');
|
||||
const relationService = require('../../services/relations');
|
||||
const repository = require('../../services/repository');
|
||||
const Relation = require('../../entities/relation');
|
||||
|
||||
async function getNoteRelations(req) {
|
||||
const noteId = req.params.noteId;
|
||||
|
||||
return await repository.getEntities("SELECT * FROM relations WHERE isDeleted = 0 AND sourceNoteId = ? ORDER BY position, dateCreated", [noteId]);
|
||||
}
|
||||
|
||||
async function updateNoteRelations(req) {
|
||||
const noteId = req.params.noteId;
|
||||
const relations = req.body;
|
||||
|
||||
for (const relation of relations) {
|
||||
let relationEntity;
|
||||
|
||||
if (relation.relationId) {
|
||||
relationEntity = await repository.getRelation(relation.relationId);
|
||||
}
|
||||
else {
|
||||
// if it was "created" and then immediatelly deleted, we just don't create it at all
|
||||
if (relation.isDeleted) {
|
||||
continue;
|
||||
}
|
||||
|
||||
relationEntity = new Relation();
|
||||
relationEntity.sourceNoteId = noteId;
|
||||
}
|
||||
|
||||
relationEntity.name = relation.name;
|
||||
relationEntity.targetNoteId = relation.targetNoteId;
|
||||
relationEntity.isInheritable = relation.isInheritable;
|
||||
relationEntity.position = relation.position;
|
||||
relationEntity.isDeleted = relation.isDeleted;
|
||||
|
||||
await relationEntity.save();
|
||||
}
|
||||
|
||||
return await repository.getEntities("SELECT * FROM relations WHERE isDeleted = 0 AND sourceNoteId = ? ORDER BY position, dateCreated", [noteId]);
|
||||
}
|
||||
|
||||
async function getAllRelationNames() {
|
||||
const names = await sql.getColumn("SELECT DISTINCT name FROM relations WHERE isDeleted = 0");
|
||||
|
||||
for (const relationName of relationService.BUILTIN_RELATIONS) {
|
||||
if (!names.includes(relationName)) {
|
||||
names.push(relationName);
|
||||
}
|
||||
}
|
||||
|
||||
names.sort();
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getNoteRelations,
|
||||
updateNoteRelations,
|
||||
getAllRelationNames
|
||||
};
|
||||
@ -1,52 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
const repository = require('./repository');
|
||||
const Label = require('../entities/label');
|
||||
|
||||
const BUILTIN_LABELS = [
|
||||
'disableVersioning',
|
||||
'calendarRoot',
|
||||
'archived',
|
||||
'excludeFromExport',
|
||||
'run',
|
||||
'manualTransactionHandling',
|
||||
'disableInclusion',
|
||||
'appCss',
|
||||
'hideChildrenOverview'
|
||||
];
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
async function getNoteWithLabel(name, value) {
|
||||
const notes = await getNotesWithLabel(name, value);
|
||||
|
||||
return notes.length > 0 ? notes[0] : null;
|
||||
}
|
||||
|
||||
async function createLabel(noteId, name, value = "") {
|
||||
return await new Label({
|
||||
noteId: noteId,
|
||||
name: name,
|
||||
value: value
|
||||
}).save();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getNotesWithLabel,
|
||||
getNoteWithLabel,
|
||||
createLabel,
|
||||
BUILTIN_LABELS
|
||||
};
|
||||
@ -1,67 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
const repository = require('./repository');
|
||||
const Relation = require('../entities/relation');
|
||||
|
||||
const BUILTIN_RELATIONS = [
|
||||
'runOnNoteView',
|
||||
'runOnNoteTitleChange'
|
||||
];
|
||||
|
||||
async function getNotesWithRelation(name, targetNoteId) {
|
||||
let notes;
|
||||
|
||||
if (targetNoteId !== undefined) {
|
||||
notes = await repository.getEntities(`SELECT notes.* FROM notes JOIN relations ON notes.noteId = relations.sourceNoteId
|
||||
WHERE notes.isDeleted = 0 AND relations.isDeleted = 0 AND relations.name = ? AND relations.targetNoteId = ?`, [name, targetNoteId]);
|
||||
}
|
||||
else {
|
||||
notes = await repository.getEntities(`SELECT notes.* FROM notes JOIN relations ON notes.noteId = relations.sourceNoteId
|
||||
WHERE notes.isDeleted = 0 AND relations.isDeleted = 0 AND relations.name = ?`, [name]);
|
||||
}
|
||||
|
||||
return notes;
|
||||
}
|
||||
|
||||
async function getNoteWithRelation(name, value) {
|
||||
const notes = await getNotesWithRelation(name, value);
|
||||
|
||||
return notes.length > 0 ? notes[0] : null;
|
||||
}
|
||||
|
||||
async function createRelation(sourceNoteId, name, targetNoteId) {
|
||||
return await new Relation({
|
||||
sourceNoteId: sourceNoteId,
|
||||
name: name,
|
||||
targetNoteId: targetNoteId
|
||||
}).save();
|
||||
}
|
||||
|
||||
async function getEffectiveRelations(noteId, relationName) {
|
||||
const relations = await repository.getEntities(`
|
||||
WITH RECURSIVE tree(noteId) AS (
|
||||
SELECT ?
|
||||
UNION
|
||||
SELECT branches.parentNoteId FROM branches
|
||||
JOIN tree ON branches.noteId = tree.noteId
|
||||
JOIN notes ON notes.noteId = branches.parentNoteId
|
||||
WHERE notes.isDeleted = 0 AND branches.isDeleted = 0
|
||||
)
|
||||
SELECT relations.* FROM relations JOIN tree ON relations.sourceNoteId = tree.noteId
|
||||
WHERE relations.isDeleted = 0 AND (relations.isInheritable = 1 OR relations.sourceNoteId = ?)`, [noteId, noteId]);
|
||||
|
||||
if (relationName) {
|
||||
return relations.filter(relation => relation.name === relationName);
|
||||
}
|
||||
else {
|
||||
return relations;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
BUILTIN_RELATIONS,
|
||||
getNotesWithRelation,
|
||||
getNoteWithRelation,
|
||||
createRelation,
|
||||
getEffectiveRelations
|
||||
};
|
||||
Loading…
Reference in New Issue