parent-child relationship is now stored in notes_parent table

This commit is contained in:
azivner 2017-11-18 18:57:50 -05:00
parent 9a819cafed
commit cc3c9d6428
5 changed files with 58 additions and 13 deletions

View File

@ -0,0 +1,28 @@
CREATE TABLE [notes_parent] (
[parent_id] VARCHAR(30) NOT NULL,
[child_id] VARCHAR(30) NOT NULL,
date_created INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (parent_id, child_id)
);
INSERT INTO notes_parent (parent_id, child_id, date_created)
SELECT note_pid, note_tree_id, date_modified FROM notes_tree;
CREATE TABLE [notes_tree_mig] (
[note_tree_id] VARCHAR(30) PRIMARY KEY NOT NULL,
[note_id] VARCHAR(30) UNIQUE NOT NULL,
[note_pos] INTEGER NOT NULL,
[is_expanded] BOOLEAN NULL ,
date_modified INTEGER NOT NULL DEFAULT 0,
is_deleted INTEGER NOT NULL DEFAULT 0
);
INSERT INTO notes_tree_mig (note_tree_id, note_id, note_pos, is_expanded, date_modified, is_deleted)
SELECT note_tree_id, note_id, note_pos, is_expanded, date_modified, is_deleted FROM notes_tree;
DROP TABLE notes_tree;
ALTER TABLE notes_tree_mig RENAME TO notes_tree;
CREATE INDEX `IDX_notes_tree_note_id` ON `notes_tree` (
`note_id`
);

View File

@ -33,17 +33,20 @@ const noteTree = (function() {
clipboardNoteTreeId = cbNoteId;
}
function prepareNoteTree(notes) { showAppIfHidden();
function prepareNoteTree(notes, notesParent) {
parentToNotes = {};
notesMap = {};
for (const note of notes) {
if (!parentToNotes[note.note_pid]) {
parentToNotes[note.note_pid] = [];
notesMap[note.note_tree_id] = note;
}
for (const np of notesParent) {
if (!parentToNotes[np.parent_id]) {
parentToNotes[np.parent_id] = [];
}
notesMap[note.note_tree_id] = note;
parentToNotes[note.note_pid].push(note.note_tree_id);
parentToNotes[np.parent_id].push(np.child_id);
}
glob.allNoteIds = Object.keys(notesMap);
@ -160,9 +163,13 @@ const noteTree = (function() {
setExpandedToServer(getNoteTreeIdFromKey(data.node.key), false);
},
init: (event, data) => {
if (startNoteTreeId) {
treeUtils.activateNode(startNoteTreeId);
}
// if (startNoteTreeId) {
// treeUtils.activateNode(startNoteTreeId);
// }
showAppIfHidden();
},
hotkeys: {
keydown: keybindings
@ -258,7 +265,7 @@ const noteTree = (function() {
startNoteTreeId = document.location.hash.substr(1); // strip initial #
}
return prepareNoteTree(resp.notes);
return prepareNoteTree(resp.notes, resp.notes_parent);
});
}

View File

@ -19,7 +19,14 @@ router.get('/', auth.checkApiAuth, async (req, res, next) => {
+ "from notes_tree "
+ "join notes on notes.note_id = notes_tree.note_id "
+ "where notes.is_deleted = 0 and notes_tree.is_deleted = 0 "
+ "order by note_pid, note_pos");
+ "order by note_pos");
const notes_parent = await sql.getResults("" +
"select parent_id, child_id " +
"from notes_parent " +
"join notes_tree as child on child.note_tree_id = notes_parent.child_id " +
"left join notes_tree as parent on parent.note_tree_id = notes_parent.parent_id " +
"where child.is_deleted = 0 and (parent.is_deleted = 0 or notes_parent.parent_id = 'root')");
const dataKey = protected_session.getDataKey(req);
@ -31,6 +38,7 @@ router.get('/', auth.checkApiAuth, async (req, res, next) => {
res.send({
notes: notes,
notes_parent: notes_parent,
start_note_tree_id: await options.getOption('start_note_tree_id'),
tree_load_time: utils.nowTimestamp()
});

View File

@ -4,7 +4,7 @@ const options = require('./options');
const fs = require('fs-extra');
const log = require('./log');
const APP_DB_VERSION = 39;
const APP_DB_VERSION = 40;
const MIGRATIONS_DIR = "migrations";
async function migrate() {

View File

@ -128,9 +128,11 @@ async function wrap(func) {
return await func(db);
}
catch (e) {
log.error("Error executing query. Inner exception: " + e.stack + error.stack);
const thisError = new Error();
throw new Error();
log.error("Error executing query. Inner exception: " + e.stack + thisError.stack);
throw thisError;
}
}