mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
relation target noteIds need to be translated into local noteIds
This commit is contained in:
parent
dbe0eb3f3a
commit
df9acd0504
@ -77,11 +77,25 @@ async function importTar(file, parentNoteId) {
|
|||||||
|
|
||||||
// maps from original noteId (in tar file) to newly generated noteId
|
// maps from original noteId (in tar file) to newly generated noteId
|
||||||
const noteIdMap = {};
|
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
|
// we save attributes after importing notes because we need to have all the relation
|
||||||
await attributeService.removeInvalidRelations();
|
// 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) {
|
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) {
|
for (const file of files) {
|
||||||
if (file.meta.version !== 1) {
|
if (file.meta.version !== 1) {
|
||||||
throw new Error("Can't read meta data version " + file.meta.version);
|
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;
|
noteIdMap[file.meta.noteId] = note.noteId;
|
||||||
|
|
||||||
for (const attribute of file.meta.attributes) {
|
for (const attribute of file.meta.attributes) {
|
||||||
await attributeService.createAttribute({
|
attributes.push({
|
||||||
noteId: note.noteId,
|
noteId: note.noteId,
|
||||||
type: attribute.type,
|
type: attribute.type,
|
||||||
name: attribute.name,
|
name: attribute.name,
|
||||||
@ -202,7 +216,7 @@ async function importNotes(files, parentNoteId, noteIdMap) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (file.children.length > 0) {
|
if (file.children.length > 0) {
|
||||||
await importNotes(file.children, note.noteId, noteIdMap);
|
await importNotes(file.children, note.noteId, noteIdMap, attributes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -80,31 +80,11 @@ async function getAttributeNames(type, nameLike) {
|
|||||||
return names;
|
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 = {
|
module.exports = {
|
||||||
getNotesWithLabel,
|
getNotesWithLabel,
|
||||||
getNoteWithLabel,
|
getNoteWithLabel,
|
||||||
createLabel,
|
createLabel,
|
||||||
createAttribute,
|
createAttribute,
|
||||||
getAttributeNames,
|
getAttributeNames,
|
||||||
removeInvalidRelations,
|
|
||||||
BUILTIN_ATTRIBUTES
|
BUILTIN_ATTRIBUTES
|
||||||
};
|
};
|
Loading…
x
Reference in New Issue
Block a user