2018-07-27 10:52:48 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const repository = require('./repository');
|
|
|
|
const Relation = require('../entities/relation');
|
|
|
|
|
|
|
|
const BUILTIN_RELATIONS = [
|
2018-07-29 12:34:40 +02:00
|
|
|
'runOnNoteView'
|
2018-07-27 10:52:48 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getNotesWithRelation,
|
|
|
|
getNoteWithRelation,
|
|
|
|
createRelation,
|
|
|
|
BUILTIN_RELATIONS
|
|
|
|
};
|