2020-11-16 15:00:52 +05:00
|
|
|
import Indexer from "./indexer";
|
2020-11-05 15:50:10 +05:00
|
|
|
import HyperSearch from "hypersearch";
|
|
|
|
|
import { getSchema } from "./schemas";
|
2020-03-19 11:30:05 +05:00
|
|
|
|
|
|
|
|
export default class IndexedCollection {
|
2020-11-16 15:00:52 +05:00
|
|
|
constructor(context, type) {
|
|
|
|
|
this.indexer = new Indexer(context, type);
|
2020-11-05 15:50:10 +05:00
|
|
|
this.type = type;
|
2020-11-16 15:00:52 +05:00
|
|
|
this.search = new HyperSearch({
|
|
|
|
|
schema: getSchema(type),
|
|
|
|
|
tokenizer: "forward",
|
|
|
|
|
});
|
2020-03-19 11:30:05 +05:00
|
|
|
}
|
|
|
|
|
|
2020-09-19 11:33:31 +05:00
|
|
|
clear() {
|
2020-11-16 15:00:52 +05:00
|
|
|
return this.indexer.clear();
|
2020-09-19 11:33:31 +05:00
|
|
|
}
|
|
|
|
|
|
2020-03-19 11:30:05 +05:00
|
|
|
async init() {
|
2020-11-16 15:00:52 +05:00
|
|
|
await this.indexer.init();
|
|
|
|
|
const index = await this.indexer.read(`${this.type}-index`);
|
|
|
|
|
if (index) this.search.import(index);
|
2020-03-19 11:30:05 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async addItem(item) {
|
2020-11-16 15:00:52 +05:00
|
|
|
const exists = await this.exists(item.id);
|
2020-03-19 11:30:05 +05:00
|
|
|
if (!exists) item.dateCreated = item.dateCreated || Date.now();
|
2020-11-16 15:00:52 +05:00
|
|
|
await this.updateItem(item);
|
|
|
|
|
if (!exists) {
|
|
|
|
|
await this.indexer.index(item.id);
|
|
|
|
|
}
|
2020-03-19 11:30:05 +05:00
|
|
|
}
|
|
|
|
|
|
2020-11-16 15:00:52 +05:00
|
|
|
async updateItem(item, index = true) {
|
2020-03-19 11:30:05 +05:00
|
|
|
if (!item.id) throw new Error("The item must contain the id field.");
|
|
|
|
|
// if item is newly synced, remote will be true.
|
|
|
|
|
item.dateEdited = item.remote ? item.dateEdited : Date.now();
|
|
|
|
|
// the item has become local now, so remove the flag.
|
|
|
|
|
delete item.remote;
|
2020-11-16 15:00:52 +05:00
|
|
|
await this.indexer.write(item.id, item);
|
|
|
|
|
|
|
|
|
|
if (index && (this.type === "notes" || this.type === "notebooks")) {
|
|
|
|
|
this.search.addDoc(item);
|
|
|
|
|
this.indexer.write(`${this.type}-index`, this.search.export());
|
|
|
|
|
}
|
2020-03-19 11:30:05 +05:00
|
|
|
}
|
|
|
|
|
|
2020-11-05 15:50:10 +05:00
|
|
|
async removeItem(id) {
|
2020-11-16 15:00:52 +05:00
|
|
|
await this.updateItem(
|
|
|
|
|
{
|
|
|
|
|
id,
|
|
|
|
|
deleted: true,
|
|
|
|
|
dateCreated: Date.now(),
|
|
|
|
|
dateEdited: Date.now(),
|
|
|
|
|
},
|
|
|
|
|
false
|
|
|
|
|
);
|
|
|
|
|
if (this.type === "notes" || this.type === "notebooks")
|
|
|
|
|
this.search.remove(id);
|
2020-03-19 11:30:05 +05:00
|
|
|
}
|
|
|
|
|
|
2020-11-16 15:00:52 +05:00
|
|
|
exists(id) {
|
|
|
|
|
return this.indexer.exists(id);
|
2020-03-19 11:30:05 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getItem(id) {
|
2020-11-16 15:00:52 +05:00
|
|
|
return this.indexer.read(id);
|
2020-03-19 11:30:05 +05:00
|
|
|
}
|
2020-03-19 12:38:33 +05:00
|
|
|
|
2020-11-16 15:00:52 +05:00
|
|
|
async getItems(indices) {
|
|
|
|
|
const data = await this.indexer.readMulti(indices);
|
|
|
|
|
return data.reduce((total, current) => {
|
|
|
|
|
total.push(current[1]);
|
|
|
|
|
return total;
|
|
|
|
|
}, []);
|
2020-03-19 12:38:33 +05:00
|
|
|
}
|
2020-03-19 11:30:05 +05:00
|
|
|
}
|