mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 23:19:40 +01:00
collections: refactor notes collection initialization
This commit is contained in:
@@ -14,8 +14,10 @@ class Database {
|
|||||||
constructor(context) {
|
constructor(context) {
|
||||||
this.context = new Storage(context);
|
this.context = new Storage(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
async init() {
|
async init() {
|
||||||
this.notebooks = new Notebooks(this.context);
|
this.notes = await Notes.new(this);
|
||||||
|
/* this.notebooks = new Notebooks(this.context);
|
||||||
this.notes = new Notes(this.context);
|
this.notes = new Notes(this.context);
|
||||||
this.trash = new Trash(this.context);
|
this.trash = new Trash(this.context);
|
||||||
this.user = new User(this.context);
|
this.user = new User(this.context);
|
||||||
@@ -40,7 +42,7 @@ class Database {
|
|||||||
this.syncer = new Sync(this);
|
this.syncer = new Sync(this);
|
||||||
this.vault = new Vault(this, this.context);
|
this.vault = new Vault(this, this.context);
|
||||||
this.conflicts = new Conflicts(this);
|
this.conflicts = new Conflicts(this);
|
||||||
this.lookup = new Lookup(this);
|
this.lookup = new Lookup(this); */
|
||||||
}
|
}
|
||||||
|
|
||||||
sync() {
|
sync() {
|
||||||
|
|||||||
19
packages/core/collections/collection.js
Normal file
19
packages/core/collections/collection.js
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import CachedCollection from "../database/cached-collection";
|
||||||
|
|
||||||
|
class Collection {
|
||||||
|
static async new(db) {
|
||||||
|
const collection = new this(db, this.name.toLowerCase());
|
||||||
|
await collection._collection.init();
|
||||||
|
return collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {import("../api").default} db
|
||||||
|
*/
|
||||||
|
constructor(db, name) {
|
||||||
|
this._db = db;
|
||||||
|
this._collection = new CachedCollection(this._db.context, name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export default Collection;
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
import CachedCollection from "../database/cached-collection";
|
|
||||||
import fuzzysearch from "fuzzysearch";
|
|
||||||
import { groupBy, isHex } from "../utils";
|
import { groupBy, isHex } from "../utils";
|
||||||
|
import Collection from "./collection";
|
||||||
import sort from "fast-sort";
|
import sort from "fast-sort";
|
||||||
import {
|
import {
|
||||||
getWeekGroupFromTimestamp,
|
getWeekGroupFromTimestamp,
|
||||||
@@ -8,42 +7,14 @@ import {
|
|||||||
getLastWeekTimestamp,
|
getLastWeekTimestamp,
|
||||||
get7DayTimestamp,
|
get7DayTimestamp,
|
||||||
} from "../utils/date";
|
} from "../utils/date";
|
||||||
import Notebooks from "./notebooks";
|
|
||||||
import Note from "../models/note";
|
import Note from "../models/note";
|
||||||
import Trash from "./trash";
|
|
||||||
import getId from "../utils/id";
|
import getId from "../utils/id";
|
||||||
import Tags from "./tags";
|
|
||||||
import Content from "./content";
|
|
||||||
|
|
||||||
var tfun = require("transfun/transfun.js").tfun;
|
var tfun = require("transfun/transfun.js").tfun;
|
||||||
if (!tfun) {
|
if (!tfun) {
|
||||||
tfun = global.tfun;
|
tfun = global.tfun;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class Notes {
|
export default class Notes extends Collection {
|
||||||
constructor(context) {
|
|
||||||
this._collection = new CachedCollection(context, "notes");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param {Notebooks} notebooks
|
|
||||||
* @param {Trash} trash
|
|
||||||
* @param {Tags} tags
|
|
||||||
* @param {Tags} colors
|
|
||||||
* @param {Content} delta
|
|
||||||
* @param {Content} text
|
|
||||||
*/
|
|
||||||
async init(notebooks, trash, tags, colors, delta, text) {
|
|
||||||
await this._collection.init();
|
|
||||||
this._notebooks = notebooks;
|
|
||||||
this._trash = trash;
|
|
||||||
this._tagsCollection = tags;
|
|
||||||
this._colorsCollection = colors;
|
|
||||||
this._deltaCollection = delta;
|
|
||||||
this._textCollection = text;
|
|
||||||
}
|
|
||||||
|
|
||||||
async add(noteArg) {
|
async add(noteArg) {
|
||||||
if (!noteArg) return;
|
if (!noteArg) return;
|
||||||
if (noteArg.remote) {
|
if (noteArg.remote) {
|
||||||
@@ -76,7 +47,7 @@ export default class Notes {
|
|||||||
if (!deltaId && isHex(delta)) deltaId = delta;
|
if (!deltaId && isHex(delta)) deltaId = delta;
|
||||||
|
|
||||||
if (delta && typeof delta === "object") {
|
if (delta && typeof delta === "object") {
|
||||||
deltaId = await this._deltaCollection.add({
|
deltaId = await this._db.delta.add({
|
||||||
noteId: id,
|
noteId: id,
|
||||||
id: deltaId,
|
id: deltaId,
|
||||||
data: delta.data || delta,
|
data: delta.data || delta,
|
||||||
@@ -86,7 +57,7 @@ export default class Notes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (text !== textId) {
|
if (text !== textId) {
|
||||||
textId = await this._textCollection.add({
|
textId = await this._db.text.add({
|
||||||
noteId: id,
|
noteId: id,
|
||||||
id: textId,
|
id: textId,
|
||||||
data: text,
|
data: text,
|
||||||
@@ -113,11 +84,11 @@ export default class Notes {
|
|||||||
|
|
||||||
if (!oldNote) {
|
if (!oldNote) {
|
||||||
for (let color of note.colors) {
|
for (let color of note.colors) {
|
||||||
await this._colorsCollection.add(color, id);
|
await this._db.colors.add(color, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let tag of note.tags) {
|
for (let tag of note.tags) {
|
||||||
await this._tagsCollection.add(tag, id);
|
await this._db.tags.add(tag, id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,13 +129,11 @@ export default class Notes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tagged(tag) {
|
tagged(tag) {
|
||||||
return this._tagsCollection
|
return this._db.tags.notes(tag).map((id) => this._collection.getItem(id));
|
||||||
.notes(tag)
|
|
||||||
.map((id) => this._collection.getItem(id));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
colored(color) {
|
colored(color) {
|
||||||
return this._colorsCollection
|
return this._db.colors
|
||||||
.notes(color)
|
.notes(color)
|
||||||
.map((id) => this._collection.getItem(id));
|
.map((id) => this._collection.getItem(id));
|
||||||
}
|
}
|
||||||
@@ -219,20 +188,20 @@ export default class Notes {
|
|||||||
if (!item) continue;
|
if (!item) continue;
|
||||||
if (item.notebook && item.notebook.id && item.notebook.topic) {
|
if (item.notebook && item.notebook.id && item.notebook.topic) {
|
||||||
await this._collection.transaction(() =>
|
await this._collection.transaction(() =>
|
||||||
this._notebooks
|
this._db.notebooks
|
||||||
.notebook(item.notebook.id)
|
.notebook(item.notebook.id)
|
||||||
.topics.topic(item.notebook.topic)
|
.topics.topic(item.notebook.topic)
|
||||||
.delete(id)
|
.delete(id)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
for (let tag of item.tags) {
|
for (let tag of item.tags) {
|
||||||
await this._tagsCollection.remove(tag, id);
|
await this._db.tags.remove(tag, id);
|
||||||
}
|
}
|
||||||
for (let color of item.colors) {
|
for (let color of item.colors) {
|
||||||
await this._colorsCollection.remove(color, id);
|
await this._db.colors.remove(color, id);
|
||||||
}
|
}
|
||||||
await this._collection.removeItem(id);
|
await this._collection.removeItem(id);
|
||||||
await this._trash.add(item.data);
|
await this._db.trash.add(item.data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -242,7 +211,7 @@ export default class Notes {
|
|||||||
throw new Error(
|
throw new Error(
|
||||||
"The destination notebook must contain notebookId and topic."
|
"The destination notebook must contain notebookId and topic."
|
||||||
);
|
);
|
||||||
let topic = this._notebooks.notebook(to.id).topics.topic(to.topic);
|
let topic = this._db.notebooks.notebook(to.id).topics.topic(to.topic);
|
||||||
if (!topic) throw new Error("No such topic exists.");
|
if (!topic) throw new Error("No such topic exists.");
|
||||||
await topic.add(...noteIds);
|
await topic.add(...noteIds);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user