mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 23:19:40 +01:00
api: add basic (local) database api using storage
This commit is contained in:
87
packages/core/api/database.js
Normal file
87
packages/core/api/database.js
Normal file
@@ -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;
|
||||||
Reference in New Issue
Block a user