script to generate large documents, closes #55

This commit is contained in:
azivner
2018-04-03 22:15:28 -04:00
parent 42dd8d4754
commit abfc64af95
15 changed files with 203 additions and 75 deletions

View File

@@ -8,8 +8,8 @@ const notes = require('../../services/notes');
const repository = require('../../services/repository');
/**
* Code in this file deals with moving and cloning note tree rows. Relationship between note and parent note is unique
* for not deleted note trees. There may be multiple deleted note-parent note relationships.
* Code in this file deals with moving and cloning branches. Relationship between note and parent note is unique
* for not deleted branches. There may be multiple deleted note-parent note relationships.
*/
async function moveBranchToParent(req) {
@@ -49,7 +49,7 @@ async function moveBranchBeforeNote(req) {
}
// we don't change dateModified so other changes are prioritized in case of conflict
// also we would have to sync all those modified note trees otherwise hash checks would fail
// also we would have to sync all those modified branches otherwise hash checks would fail
await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition >= ? AND isDeleted = 0",
[beforeNote.parentNoteId, beforeNote.notePosition]);
@@ -77,7 +77,7 @@ async function moveBranchAfterNote(req) {
}
// we don't change dateModified so other changes are prioritized in case of conflict
// also we would have to sync all those modified note trees otherwise hash checks would fail
// also we would have to sync all those modified branches otherwise hash checks would fail
await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",
[afterNote.parentNoteId, afterNote.notePosition]);

View File

@@ -1,64 +1,20 @@
"use strict";
const sql = require('../../services/sql');
const syncTable = require('../../services/sync_table');
const tree = require('../../services/tree');
const Branch = require('../../entities/branch');
const cloningService = require('../../services/cloning');
async function cloneNoteToParent(req) {
const noteId = req.params.noteId;
const parentNoteId = req.params.parentNoteId;
const prefix = req.body.prefix;
const validationResult = await tree.validateParentChild(parentNoteId, noteId);
if (!validationResult.success) {
return validationResult;
}
const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
const branch = await new Branch({
noteId: noteId,
parentNoteId: parentNoteId,
prefix: prefix,
notePosition: newNotePos,
isExpanded: 0
}).save();
await sql.execute("UPDATE branches SET isExpanded = 1 WHERE noteId = ?", [parentNoteId]);
return { success: true };
return await cloningService.cloneNoteToParent(noteId, parentNoteId, prefix);
}
async function cloneNoteAfter(req) {
const noteId = req.params.noteId;
const afterBranchId = req.params.afterBranchId;
const afterNote = await tree.getBranch(afterBranchId);
const validationResult = await tree.validateParentChild(afterNote.parentNoteId, noteId);
if (!validationResult.result) {
return validationResult;
}
// we don't change dateModified so other changes are prioritized in case of conflict
// also we would have to sync all those modified note trees otherwise hash checks would fail
await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",
[afterNote.parentNoteId, afterNote.notePosition]);
await syncTable.addNoteReorderingSync(afterNote.parentNoteId);
const branch = await new Branch({
noteId: noteId,
parentNoteId: afterNote.parentNoteId,
notePosition: afterNote.notePosition + 1,
isExpanded: 0
}).save();
return { success: true };
return await cloningService.cloneNoteAfter(noteId, afterBranchId);
}
module.exports = {

View File

@@ -110,17 +110,17 @@ async function importNotes(files, parentNoteId) {
file.data = file.data.toString("UTF-8");
}
const noteId = await noteService.createNote(parentNoteId, file.meta.title, file.data, {
const {note} = await noteService.createNote(parentNoteId, file.meta.title, file.data, {
type: file.meta.type,
mime: file.meta.mime
});
for (const label of file.meta.labels) {
await labelService.createLabel(noteId, label.name, label.value);
await labelService.createLabel(note.noteId, label.name, label.value);
}
if (file.children.length > 0) {
await importNotes(file.children, noteId);
await importNotes(file.children, note.noteId);
}
}
}

View File

@@ -20,13 +20,13 @@ async function saveSearchToNote(req) {
searchString: req.params.searchString
};
const noteId = await noteService.createNote('root', 'Search note', noteContent, {
const {note} = await noteService.createNote('root', 'Search note', noteContent, {
json: true,
type: 'search',
mime: "application/json"
});
return { noteId };
return { noteId: note.noteId };
}
module.exports = {