Notes/src/becca/becca.js

127 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-05-17 09:48:24 +02:00
"use strict";
2020-05-16 23:12:29 +02:00
2021-05-17 22:10:00 +02:00
const sql = require("../services/sql.js");
const NoteRevision = require("./entities/note_revision.js");
2021-05-02 19:59:16 +02:00
const RecentNote = require("./entities/recent_note.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>} */
2021-04-30 23:10:25 +02:00
this.notes = {};
2020-05-16 23:12:29 +02:00
/** @type {Object.<String, Branch>} */
2021-04-30 23:10:25 +02:00
this.branches = {};
2020-05-16 23:12:29 +02:00
/** @type {Object.<String, Branch>} */
this.childParentToBranch = {};
/** @type {Object.<String, Attribute>} */
2021-04-30 23:10:25 +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 = {};
2021-04-30 23:10:25 +02:00
/** @type {Object.<String, Option>} */
this.options = {};
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];
}
2021-04-26 22:24:55 +02:00
getNotes(noteIds) {
2021-05-08 11:20:20 +02:00
const filteredNotes = [];
for (const noteId of noteIds) {
const note = this.notes[noteId];
if (!note) {
throw new Error(`Note '${noteId}' was not found in becca.`);
}
filteredNotes.push(note);
}
return filteredNotes;
2021-04-26 22:24:55 +02:00
}
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;
}
2021-05-01 21:52:22 +02:00
getOption(name) {
return this.options[name];
}
2021-05-02 12:02:32 +02:00
2021-05-17 22:05:35 +02:00
getEntity(entityName, entityId) {
2021-05-02 12:02:32 +02:00
if (!entityName || !entityId) {
return null;
}
const camelCaseEntityName = entityName.toLowerCase().replace(/(_[a-z])/g,
group =>
group
.toUpperCase()
.replace('_', '')
);
return this[camelCaseEntityName][entityId];
}
2021-05-02 19:59:16 +02:00
getRecentNotesFromQuery(query, params = []) {
const rows = sql.getRows(query, params);
return rows.map(row => new RecentNote(row));
}
getNoteRevisionsFromQuery(query, params = []) {
const rows = sql.getRows(query, params);
return rows.map(row => new NoteRevision(row));
}
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;