Files
notesnook/packages/core/api/database.js
2019-12-04 15:08:01 +05:00

138 lines
3.4 KiB
JavaScript

import Storage from "../helpers/storage";
import fuzzysearch from "fuzzysearch";
import ff from "fast-filter";
import { extractValues } from "../utils";
const KEYS = {
notes: "notes",
notebooks: "notebooks"
};
class Database {
constructor(storage) {
this.storage = new Storage(storage);
this.notes = {};
this.notebooks = {};
}
/**
* Get all notes from the database
*/
async getNotes() {
//update our cache
this.notes = (await this.storage.read(KEYS.notes)) || {};
return extractValues(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.title && !note.content))
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, 150) + "...",
length: note.content.text.length,
dateEditted: Date.now(),
dateCreated: timestamp
};
await this.storage.write(KEYS.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 (let note of notes) {
if (this.notes.hasOwnProperty(note.dateCreated)) {
delete this.notes[note.dateCreated];
}
}
await this.storage.write(KEYS.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];
}
}
/**
* Searches all notes in the database with the given query
* @param {string} query the search query
*/
async searchNotes(query) {
if (!query) return [];
//TODO benchmark this and make it faster if necessary
let notes = await this.getNotes();
if (!notes) return [];
return ff(
notes,
v => fuzzysearch(query, v.title + " " + v.content.text),
this
);
}
//Notebooks
async getNotebooks() {
//update our cache
this.notebooks = (await this.storage.read(KEYS.notebooks)) || {};
return extractValues(this.notebooks);
}
async addNotebook(notebook) {
if (!notebook || !notebook.title) return;
const id = notebook.dateCreated || Date.now();
let topics = {};
for (let topic of notebook.topics) {
topics[topic] = [];
}
this.notebooks[id] = {
title: notebook.title,
description: notebook.description,
dateCreated: notebook.dateCreated,
pinned: notebook.pinned || false,
favorite: notebook.favorite || false,
topics,
totalNotes: 0,
tags: [],
colors: []
};
await this.storage.write(KEYS.notebooks, this.notebooks);
return id;
}
getNotebook(id) {}
// Lists
getLists() {}
getList() {}
addList() {}
deleteLists() {}
}
export default Database;