relation target noteIds need to be translated into local noteIds

This commit is contained in:
azivner 2018-08-15 18:32:06 +02:00
parent dbe0eb3f3a
commit df9acd0504
2 changed files with 20 additions and 26 deletions

View File

@ -77,11 +77,25 @@ async function importTar(file, parentNoteId) {
// maps from original noteId (in tar file) to newly generated noteId
const noteIdMap = {};
const attributes = [];
await importNotes(files, parentNoteId, noteIdMap);
await importNotes(files, parentNoteId, noteIdMap, attributes);
// import might contain relations targeting notes which are not in the import
await attributeService.removeInvalidRelations();
// we save attributes after importing notes because we need to have all the relation
// targets already existing
for (const attr of attributes) {
if (attr.type === 'relation') {
// map to local noteId
attr.value = noteIdMap[attr.value];
if (!attr.value) {
// relation is targeting note not present in the import
continue;
}
}
await attributeService.createAttribute(attr);
}
}
function getFileName(name) {
@ -162,7 +176,7 @@ async function parseImportFile(file) {
});
}
async function importNotes(files, parentNoteId, noteIdMap) {
async function importNotes(files, parentNoteId, noteIdMap, attributes) {
for (const file of files) {
if (file.meta.version !== 1) {
throw new Error("Can't read meta data version " + file.meta.version);
@ -191,7 +205,7 @@ async function importNotes(files, parentNoteId, noteIdMap) {
noteIdMap[file.meta.noteId] = note.noteId;
for (const attribute of file.meta.attributes) {
await attributeService.createAttribute({
attributes.push({
noteId: note.noteId,
type: attribute.type,
name: attribute.name,
@ -202,7 +216,7 @@ async function importNotes(files, parentNoteId, noteIdMap) {
}
if (file.children.length > 0) {
await importNotes(file.children, note.noteId, noteIdMap);
await importNotes(file.children, note.noteId, noteIdMap, attributes);
}
}
}

View File

@ -80,31 +80,11 @@ async function getAttributeNames(type, nameLike) {
return names;
}
async function removeInvalidRelations() {
const relations = await repository.getEntities(`
SELECT attributes.*
FROM attributes
LEFT JOIN notes AS sourceNote ON attributes.noteId = sourceNote.noteId
LEFT JOIN notes AS targetNote ON attributes.value = targetNote.noteId
WHERE
attributes.isDeleted = 0
AND attributes.type = 'relation'
AND (sourceNote.noteId IS NULL OR sourceNote.isDeleted
OR targetNote.noteId IS NULL OR targetNote.isDeleted)`);
for (const relation of relations) {
relation.isDeleted = true;
await relation.save();
}
}
module.exports = {
getNotesWithLabel,
getNoteWithLabel,
createLabel,
createAttribute,
getAttributeNames,
removeInvalidRelations,
BUILTIN_ATTRIBUTES
};