Skip to content

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

refactor: make everything independent

thecodrr64add84e722c
diff --git a/packages/core/api/index.js b/packages/core/api/index.js
index 29e3dd6a2..ef5b1fa32 100644
--- a/packages/core/api/index.js
+++ b/packages/core/api/index.js
@@ -16,34 +16,27 @@ class Database {
}
async init() {
- this.notes = await Notes.new(this);
- this.notebooks = await Notebooks.new(this);
- /* this.notebooks = new Notebooks(this.context);
- this.notes = new Notes(this.context);
- this.trash = new Trash(this.context);
this.user = new User(this.context);
- this.tags = new Tags(this.context, "tags");
- this.colors = new Tags(this.context, "colors");
- this.delta = new Content(this.context, "delta");
- this.text = new Content(this.context, "text");
- await this.delta.init();
- await this.text.init();
- await this.tags.init();
- await this.colors.init();
- await this.notes.init(
- this.notebooks,
- this.trash,
- this.tags,
- this.colors,
- this.delta,
- this.text
- );
- await this.notebooks.init(this.notes, this.trash);
- await this.trash.init(this.notes, this.notebooks, this.delta, this.text);
this.syncer = new Sync(this);
this.vault = new Vault(this, this.context);
this.conflicts = new Conflicts(this);
- this.lookup = new Lookup(this); */
+ this.lookup = new Lookup(this);
+
+ // collections
+ /** @type {Notes} */
+ this.notes = await Notes.new(this);
+ /** @type {Notebooks} */
+ this.notebooks = await Notebooks.new(this);
+ /** @type {Tags} */
+ this.tags = await Tags.new(this, "tags");
+ /** @type {Tags} */
+ this.colors = await Tags.new(this, "colors");
+ /** @type {Trash} */
+ this.trash = await Trash.new(this);
+ /** @type {Content} */
+ this.delta = await Content.new(this, false, "delta");
+ /** @type {Content} */
+ this.text = await Content.new(this, false, "text");
}
sync() {
diff --git a/packages/core/collections/collection.js b/packages/core/collections/collection.js
index 1c2bbb811..8f90e9cfe 100644
--- a/packages/core/collections/collection.js
+++ b/packages/core/collections/collection.js
@@ -1,9 +1,11 @@
import CachedCollection from "../database/cached-collection";
+import IndexedCollection from "../database/indexed-collection";
class Collection {
- static async new(db) {
- const collection = new this(db, this.name.toLowerCase());
+ static async new(db, cached = true, name = undefined) {
+ const collection = new this(db, cached, name || this.name.toLowerCase());
await collection._collection.init();
+ if (collection.init) await collection.init();
return collection;
}
@@ -11,9 +13,10 @@ class Collection {
*
* @param {import("../api").default} db
*/
- constructor(db, name) {
+ constructor(db, cached, name) {
this._db = db;
- this._collection = new CachedCollection(this._db.context, name);
+ if (cached) this._collection = new CachedCollection(this._db.context, name);
+ else this._collection = new IndexedCollection(this._db.context, name);
}
}
export default Collection;
diff --git a/packages/core/collections/content.js b/packages/core/collections/content.js
index 10db9df1f..751c1f778 100644
--- a/packages/core/collections/content.js
+++ b/packages/core/collections/content.js
@@ -1,15 +1,7 @@
-import IndexedCollection from "../database/indexed-collection";
+import Collection from "./collection";
import getId from "../utils/id";
-export default class Content {
- constructor(context, name) {
- this._collection = new IndexedCollection(context, name);
- }
-
- init() {
- return this._collection.init();
- }
-
+export default class Content extends Collection {
async add(content) {
if (!content) return;
diff --git a/packages/core/collections/notebooks.js b/packages/core/collections/notebooks.js
index 83b8d3757..fd20438d7 100644
--- a/packages/core/collections/notebooks.js
+++ b/packages/core/collections/notebooks.js
@@ -71,7 +71,7 @@ export default class Notebooks extends Collection {
notebook(id) {
let notebook = id.type ? id : this._collection.getItem(id);
if (!notebook) return;
- return new Notebook(this, notebook);
+ return new Notebook(notebook, this._db);
}
async delete(...ids) {
diff --git a/packages/core/collections/notes.js b/packages/core/collections/notes.js
index 563ab0d3d..0277decc5 100644
--- a/packages/core/collections/notes.js
+++ b/packages/core/collections/notes.js
@@ -105,7 +105,7 @@ export default class Notes extends Collection {
if (!id) return;
let note = id.type ? id : this._collection.getItem(id);
if (!note || note.deleted) return;
- return new Note(this, note);
+ return new Note(note, this._db);
}
get raw() {
diff --git a/packages/core/collections/tags.js b/packages/core/collections/tags.js
index 7bc5763eb..44552555c 100644
--- a/packages/core/collections/tags.js
+++ b/packages/core/collections/tags.js
@@ -1,17 +1,9 @@
-import CachedCollection from "../database/cached-collection";
+import Collection from "./collection";
import getId from "../utils/id";
import { qclone } from "qclone";
import set from "../utils/set";
-export default class Tags {
- constructor(context, name) {
- this._collection = new CachedCollection(context, name);
- }
-
- init() {
- return this._collection.init();
- }
-
+export default class Tags extends Collection {
notes(tag) {
const tagItem = this.all.find((t) => t.title === tag);
if (!tagItem) return [];
diff --git a/packages/core/collections/topics.js b/packages/core/collections/topics.js
index 05f29ab3f..9b732f88f 100644
--- a/packages/core/collections/topics.js
+++ b/packages/core/collections/topics.js
@@ -4,11 +4,11 @@ import { qclone } from "qclone";
export default class Topics {
/**
*
- * @param {import('./notebooks').default} notebooks
+ * @param {import('../api').default} db
* @param {string} notebookId
*/
- constructor(notebooks, notebookId) {
- this._notebooks = notebooks;
+ constructor(notebookId, db) {
+ this._db = db;
this._notebookId = notebookId;
}
@@ -37,7 +37,7 @@ export default class Topics {
}
async add(...topics) {
- let notebook = qclone(this._notebooks.notebook(this._notebookId).data);
+ let notebook = qclone(this._db.notebooks.notebook(this._notebookId).data);
let allTopics = [...notebook.topics, ...topics];
const unique = this._dedupe(allTopics);
@@ -50,14 +50,14 @@ export default class Topics {
notebook.totalNotes += topic.totalNotes;
});
- return this._notebooks._collection.addItem(notebook);
+ return this._db.notebooks._collection.addItem(notebook);
}
/**
* @returns {Array} an array containing all the topics
*/
get all() {
- return this._notebooks.notebook(this._notebookId).data.topics;
+ return this._db.notebooks.notebook(this._notebookId).data.topics;
}
/**
@@ -70,7 +70,7 @@ export default class Topics {
topic = this.all.find((t) => t.title === topic);
}
if (!topic) return;
- return new Topic(this, topic);
+ return new Topic(topic, this._notebookId, this._db);
}
async delete(...topics) {
@@ -85,7 +85,7 @@ export default class Topics {
allTopics.splice(i, 1);
}
}
- await this._notebooks.add({ id: this._notebookId, topics: allTopics });
+ await this._db.notebooks.add({ id: this._notebookId, topics: allTopics });
}
}
diff --git a/packages/core/collections/trash.js b/packages/core/collections/trash.js
index 21ba611da..e5cf7a21c 100644
--- a/packages/core/collections/trash.js
+++ b/packages/core/collections/trash.js
@@ -1,25 +1,9 @@
-import CachedCollection from "../database/cached-collection";
+import Collection from "./collection";
import getId from "../utils/id";
import { get7DayTimestamp } from "../utils/date";
-export default class Trash {
- constructor(context) {
- this._collection = new CachedCollection(context, "trash");
- }
-
- /**
- *
- * @param {import('./notes').default} notes
- * @param {import('./notebooks').default} notebooks
- * @param {import('./content').default} delta
- * @param {import('./content').default} text
- */
- async init(notes, notebooks, delta, text) {
- this._notes = notes;
- this._notebooks = notebooks;
- this._deltaCollection = delta;
- this._textCollection = text;
- await this._collection.init();
+export default class Trash extends Collection {
+ async init() {
await this.cleanup();
}
@@ -58,8 +42,8 @@ export default class Trash {
let item = this._collection.getItem(id);
if (!item) continue;
if (item.type === "note") {
- await this._deltaCollection.remove(item.content.delta);
- await this._textCollection.remove(item.content.text);
+ await this._db.delta.remove(item.content.delta);
+ await this._db.text.remove(item.content.text);
}
await this._collection.removeItem(id);
}
@@ -76,29 +60,32 @@ export default class Trash {
if (item.type === "note") {
let { notebook } = item;
item.notebook = {};
- await this._notes.add(item);
+ await this._db.notes.add(item);
if (notebook && notebook.id && notebook.topic) {
const { id, topic } = notebook;
// if the notebook or topic has been deleted
if (
- !this._notebooks._collection.exists(id) ||
- !this._notebooks.notebook(id).topics.has(topic)
+ !this._db.notebooks._collection.exists(id) ||
+ !this._db.notebooks.notebook(id).topics.has(topic)
) {
notebook = {};
}
// restore the note to the topic it was in before deletion
if (notebook.id && notebook.topic) {
- await this._notebooks.notebook(id).topics.topic(topic).add(item.id);
+ await this._db.notebooks
+ .notebook(id)
+ .topics.topic(topic)
+ .add(item.id);
}
}
} else if (item.type === "notebook") {
const { topics } = item;
item.topics = [];
- await this._notebooks.add(item);
- let notebook = this._notebooks.notebook(item.id);
+ await this._db.notebooks.add(item);
+ let notebook = this._db.notebooks.notebook(item.id);
for (let topic of topics) {
await notebook.topics.add(topic.title);
let t = notebook.topics.topic(topic.title);
diff --git a/packages/core/models/note.js b/packages/core/models/note.js
index 3656f1047..5bc108ba0 100644
--- a/packages/core/models/note.js
+++ b/packages/core/models/note.js
@@ -1,12 +1,12 @@
export default class Note {
/**
*
- * @param {import('../collections/notes').default} notes
+ * @param {import('../api').default} db
* @param {Object} note
*/
- constructor(notes, note) {
+ constructor(note, db) {
this._note = note;
- this._notes = notes;
+ this._db = db;
}
get data() {
@@ -42,29 +42,29 @@ export default class Note {
}
delta() {
- return this._notes._deltaCollection.get(this._note.content.delta);
+ return this._db.delta.get(this._note.content.delta);
}
text() {
- return this._notes._textCollection.get(this._note.content.text);
+ return this._db.text.get(this._note.content.text);
}
color(color) {
- return addTag.call(this, color, "_colorsCollection", "colors");
+ return addTag.call(this, color, "colors");
}
uncolor(color) {
- return removeTag.call(this, color, "_colorsCollection", "colors");
+ return removeTag.call(this, color, "colors");
}
tag(tag) {
- return addTag.call(this, tag, "_tagsCollection", "tags");
+ return addTag.call(this, tag, "tags");
}
untag(tag) {
- return removeTag.call(this, tag, "_tagsCollection", "tags");
+ return removeTag.call(this, tag, "tags");
}
_toggle(prop) {
- return this._notes.add({ id: this._note.id, [prop]: !this._note[prop] });
+ return this._db.notes.add({ id: this._note.id, [prop]: !this._note[prop] });
}
favorite() {
@@ -76,21 +76,21 @@ export default class Note {
}
}
-async function addTag(tag, collection, array) {
+async function addTag(tag, array) {
if (this._note[array].indexOf(tag) > -1)
throw new Error("Cannot add a duplicate tag.");
let arr = [...this._note[array], tag];
const note = { ...this._note, [array]: arr };
- await this._notes[collection].add(tag, note.id);
- await this._notes._collection.addItem(note);
+ await this._db[array].add(tag, note.id);
+ await this._db.notes._collection.addItem(note);
}
-async function removeTag(tag, collection, array) {
+async function removeTag(tag, array) {
if (this._note[array].indexOf(tag) <= -1)
throw new Error("This note is not tagged by the specified tag.");
let arr = [...this._note[array]];
arr.splice(arr.indexOf(tag), 1);
const note = { ...this._note, [array]: arr };
- await this._notes[collection].remove(tag, note.id);
- await this._notes._collection.addItem(note);
+ await this._db[array].remove(tag, note.id);
+ await this._db.notes._collection.addItem(note);
}
diff --git a/packages/core/models/notebook.js b/packages/core/models/notebook.js
index 252c788cd..563e424ad 100644
--- a/packages/core/models/notebook.js
+++ b/packages/core/models/notebook.js
@@ -3,12 +3,12 @@ import Topics from "../collections/topics";
export default class Notebook {
/**
*
- * @param {import ('../collections/notebooks').default} notebooks
* @param {Object} notebook
+ * @param {import ('../api').default} db
*/
- constructor(notebooks, notebook) {
+ constructor(notebook, db) {
this._notebook = notebook;
- this._notebooks = notebooks;
+ this._db = db;
}
get title() {
@@ -20,7 +20,7 @@ export default class Notebook {
}
get topics() {
- return new Topics(this._notebooks, this._notebook.id);
+ return new Topics(this._notebook.id, this._db);
}
get dateEdited() {
@@ -28,7 +28,7 @@ export default class Notebook {
}
_toggle(prop) {
- return this._notebooks.add({
+ return this._db.notebooks.add({
id: this._notebook.id,
[prop]: !this._notebook[prop],
});
diff --git a/packages/core/models/topic.js b/packages/core/models/topic.js
index 4a7425847..d3774978c 100644
--- a/packages/core/models/topic.js
+++ b/packages/core/models/topic.js
@@ -1,13 +1,14 @@
import { qclone } from "qclone";
export default class Topic {
/**
- *
- * @param {import('../collections/topics').default} topics
* @param {Object} topic
+ * @param {string} notebookId
+ * @param {import('../api').default} db
*/
- constructor(topics, topic) {
+ constructor(topic, notebookId, db) {
this._topic = topic;
- this._topics = topics;
+ this._db = db;
+ this._notebookId = notebookId;
this._transactionOpen = false;
}
@@ -31,23 +32,23 @@ export default class Topic {
async add(...noteIds) {
const topic = qclone(this._topic);
for (let noteId of noteIds) {
- let note = this._topics._notebooks._notes.note(noteId);
+ let note = this._db.notes.note(noteId);
if (this.has(noteId) || !note || note.data.deleted) continue;
topic.notes.push(noteId);
if (note.notebook && note.notebook.id && note.notebook.topic) {
if (
- note.notebook.id === this._topics.notebookId &&
+ note.notebook.id === this._notebookId &&
note.notebook.topic === topic.title
)
return this;
- await this._topics._notebooks
+ await this._db.notebooks
.notebook(note.notebook.id)
.topics.topic(note.notebook.topic)
.delete(note.id);
}
- await this._topics._notebooks._notes.add({
+ await this._db.notes.add({
id: noteId,
- notebook: { id: this._topics._notebookId, topic: topic.title },
+ notebook: { id: this._notebookId, topic: topic.title },
});
topic.totalNotes++;
}
@@ -60,7 +61,7 @@ export default class Topic {
if (!this.has(noteId)) return this;
let index = topic.notes.findIndex((n) => n === noteId);
topic.notes.splice(index, 1);
- await this._topics._notebooks._notes.add({
+ await this._db.notes.add({
id: noteId,
notebook: {},
});
@@ -71,14 +72,14 @@ export default class Topic {
async _save(topic) {
if (this._transactionOpen) return this;
- await this._topics.add(topic);
+ await this._db.notebooks.notebook(this._notebookId).topics.add(topic);
return this;
}
get all() {
return this._topic.notes
.map((note) => {
- let fullNote = this._topics._notebooks._notes.note(note);
+ let fullNote = this._db.notes.note(note);
if (fullNote) return fullNote.data;
})
.filter((v) => v);