Notes/src/services/becca/becca.js

77 lines
1.9 KiB
JavaScript
Raw Normal View History

2020-05-17 09:48:24 +02:00
"use strict";
2020-05-16 23:12:29 +02:00
const sql = require("../sql.js");
const NoteRevision = require("./entities/note_revision.js");
2021-04-16 23:00:08 +02:00
class Becca {
2020-05-16 23:12:29 +02:00
constructor() {
2020-05-22 09:38:30 +02:00
this.reset();
}
reset() {
2020-05-16 23:12:29 +02:00
/** @type {Object.<String, Note>} */
2020-05-22 09:38:30 +02:00
this.notes = [];
2020-05-16 23:12:29 +02:00
/** @type {Object.<String, Branch>} */
2020-05-22 09:38:30 +02:00
this.branches = [];
2020-05-16 23:12:29 +02:00
/** @type {Object.<String, Branch>} */
this.childParentToBranch = {};
/** @type {Object.<String, Attribute>} */
2020-05-22 09:38:30 +02:00
this.attributes = [];
/** @type {Object.<String, Attribute[]>} Points from attribute type-name to list of attributes */
2020-05-22 09:38:30 +02:00
this.attributeIndex = {};
2020-05-16 23:12:29 +02:00
this.loaded = false;
}
/** @return {Attribute[]} */
findAttributes(type, name) {
return this.attributeIndex[`${type}-${name.toLowerCase()}`] || [];
2020-05-16 23:12:29 +02:00
}
/** @return {Attribute[]} */
findAttributesWithPrefix(type, name) {
const resArr = [];
const key = `${type}-${name}`;
for (const idx in this.attributeIndex) {
if (idx.startsWith(key)) {
resArr.push(this.attributeIndex[idx]);
}
}
return resArr.flat();
}
2020-05-16 23:12:29 +02:00
decryptProtectedNotes() {
for (const note of Object.values(this.notes)) {
2020-05-17 09:48:24 +02:00
note.decrypt();
2020-05-16 23:12:29 +02:00
}
}
2020-05-17 09:48:24 +02:00
getNote(noteId) {
return this.notes[noteId];
}
getBranch(branchId) {
return this.branches[branchId];
}
getAttribute(attributeId) {
return this.attributes[attributeId];
}
getBranchFromChildAndParent(childNoteId, parentNoteId) {
2020-05-17 09:48:24 +02:00
return this.childParentToBranch[`${childNoteId}-${parentNoteId}`];
}
getNoteRevision(noteRevisionId) {
const row = sql.getRow("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [noteRevisionId]);
return row ? new NoteRevision(row) : null;
}
2020-05-16 23:12:29 +02:00
}
2021-04-16 23:00:08 +02:00
const becca = new Becca();
2020-05-16 23:12:29 +02:00
2021-04-16 23:00:08 +02:00
module.exports = becca;