Link entity migrated to Attribute, WIP

This commit is contained in:
zadam
2019-08-19 20:12:00 +02:00
parent fd9b79e115
commit 3cb421143f
22 changed files with 172 additions and 174 deletions

View File

@@ -2,38 +2,34 @@
const sql = require('../../services/sql');
async function getLinks(noteIds, linkTypes) {
async function getRelations(noteIds, relationNames) {
return (await sql.getManyRows(`
SELECT noteId, targetNoteId, type
FROM links
WHERE (noteId IN (???) OR targetNoteId IN (???))
AND isDeleted = 0
UNION
SELECT noteId, value, 'relation'
SELECT noteId, name, value AS targetNoteId
FROM attributes
WHERE (noteId IN (???) OR value IN (???))
AND type = 'relation'
AND isDeleted = 0
`, Array.from(noteIds))).filter(l => linkTypes.includes(l.type));
`, Array.from(noteIds))).filter(l => relationNames.includes(l.name));
}
async function getLinkMap(req) {
const {noteId} = req.params;
const {linkTypes, maxNotes, maxDepth} = req.body;
const {relationNames, maxNotes, maxDepth} = req.body;
let noteIds = new Set([noteId]);
let links = [];
let relations;
let depth = 0;
while (true) {
links = await getLinks(noteIds, linkTypes);
relations = await getRelations(noteIds, relationNames);
if (depth === maxDepth) {
break;
}
const newNoteIds = new Set(links.map(l => l.noteId).concat(links.map(l => l.targetNoteId)));
const newNoteIds = new Set(relations.map(rel => rel.noteId)
.concat(relations.map(rel => rel.targetNoteId)));
if (newNoteIds.size === noteIds.size) {
// no new note discovered, no need to search any further
@@ -51,9 +47,9 @@ async function getLinkMap(req) {
}
// keep only links coming from and targetting some note in the noteIds set
links = links.filter(l => noteIds.has(l.noteId) && noteIds.has(l.targetNoteId));
relations = relations.filter(rel => noteIds.has(rel.noteId) && noteIds.has(rel.targetNoteId));
return links;
return relations;
}
module.exports = {