Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

api: add basic (local) database api using storage

thecodrrf6056e52d05a
diff --git a/packages/core/api/database.js b/packages/core/api/database.js
new file mode 100644
index 000000000..0f231df1f
--- /dev/null
+++ b/packages/core/api/database.js
@@ -0,0 +1,87 @@
+import Storage from "../helpers/storage";
+
+class Database {
+ constructor(storage) {
+ this.storage = new Storage(storage);
+ this.notes = {};
+ }
+
+ /**
+ * Get all notes from the database
+ */
+ async getNotes() {
+ //update our cache
+ this.notes = await this.storage.read("notes");
+ return this.notes;
+ }
+
+ /**
+ * Adds or updates the note in the database
+ * @param {object} note The note to add or update
+ */
+ async addNote(note) {
+ if (!note || !note.content || note.content.length <= 0) return undefined;
+
+ let timestamp = note.dateCreated || Date.now();
+ //add or update a note into the database
+ let title =
+ note.title ||
+ note.content.text
+ .split(" ")
+ .slice(0, 3)
+ .join(" ");
+ this.notes[timestamp] = {
+ title,
+ content: note.content,
+ pinned: note.pinned || false,
+ tags: note.tags || [],
+ notebooks: note.notebooks || [],
+ colors: note.colors || [],
+ favorite: note.favorite || false,
+ headline: note.content.text.substring(0, 60),
+ length: note.content.text.length,
+ dateEditted: Date.now(),
+ dateCreated: timestamp
+ };
+ await this.storage.write("notes", this.notes);
+ return timestamp;
+ }
+
+ /**
+ * Deletes one or more notes from the database
+ * @param {array} notes the notes to be deleted
+ */
+ async deleteNotes(notes) {
+ if (!notes || notes.length <= 0 || !this.notes || this.notes.length <= 0)
+ return;
+ for (note of notes) {
+ if (this.notes.hasOwnProperty(note.dateCreated)) {
+ delete this.notes[note.dateCreated];
+ }
+ }
+ await this.storage.write("notes", this.notes);
+ }
+
+ /**
+ * Gets a note from the database
+ * @param {string} id the id of the note (must be a timestamp)
+ */
+ getNote(id) {
+ if (this.notes.hasOwnProperty(id)) {
+ return this.notes[id];
+ }
+ }
+
+ //Notebooks
+ getNotebooks() {}
+ getNotebook() {}
+ addNotebook() {}
+
+ // Lists
+ getLists() {}
+ getList() {}
+ addList() {}
+ deleteLists() {}
+}
+
+export default Database;
diff --git a/packages/core/sync/storage.js b/packages/core/helpers/storage.js
similarity index 100%
rename from packages/core/sync/storage.js
rename to packages/core/helpers/storage.js