2021-02-20 18:25:34 +05:00
|
|
|
import { EV, EVENTS } from "../common";
|
2020-11-16 15:00:52 +05:00
|
|
|
import Indexer from "./indexer";
|
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-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();
|
2020-03-19 11:30:05 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async addItem(item) {
|
2020-11-16 15:01:16 +05:00
|
|
|
if (!item.id) throw new Error("The item must contain the id field.");
|
|
|
|
|
|
2021-02-18 19:46:44 +05:00
|
|
|
const exists = 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:01:16 +05:00
|
|
|
async updateItem(item) {
|
2020-03-19 11:30:05 +05:00
|
|
|
if (!item.id) throw new Error("The item must contain the id field.");
|
2021-10-04 11:55:14 +05:00
|
|
|
EV.publish(EVENTS.databaseUpdated, item);
|
|
|
|
|
|
2020-03-19 11:30:05 +05:00
|
|
|
// if item is newly synced, remote will be true.
|
2021-10-27 10:53:36 +05:00
|
|
|
item.dateEdited =
|
|
|
|
|
item.remote || item.persistDateEdited ? item.dateEdited : Date.now();
|
|
|
|
|
|
2021-02-18 20:23:21 +05:00
|
|
|
// the item has become local now, so remove the flags
|
2020-03-19 11:30:05 +05:00
|
|
|
delete item.remote;
|
2021-02-18 20:23:21 +05:00
|
|
|
delete item.migrated;
|
2020-11-16 15:00:52 +05:00
|
|
|
await this.indexer.write(item.id, item);
|
2020-03-19 11:30:05 +05:00
|
|
|
}
|
|
|
|
|
|
2020-11-16 15:01:16 +05:00
|
|
|
removeItem(id) {
|
2021-02-20 18:25:34 +05:00
|
|
|
EV.publish(EVENTS.databaseUpdated, id);
|
2020-11-16 15:01:16 +05:00
|
|
|
return this.updateItem({
|
|
|
|
|
id,
|
|
|
|
|
deleted: true,
|
|
|
|
|
dateEdited: Date.now(),
|
|
|
|
|
});
|
2020-03-19 11:30:05 +05:00
|
|
|
}
|
|
|
|
|
|
2021-02-16 16:56:06 +05:00
|
|
|
async deleteItem(id) {
|
2021-02-20 18:25:34 +05:00
|
|
|
EV.publish(EVENTS.databaseUpdated, id);
|
2021-02-16 16:56:06 +05:00
|
|
|
await this.indexer.deindex(id);
|
|
|
|
|
return await this.indexer.remove(id);
|
2021-01-13 11:39:10 +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
|
|
|
}
|