Files
notesnook/packages/core/database/indexed-collection.js

100 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-03-30 15:52:48 +05:00
import { EVENTS } from "../common";
import Indexer from "./indexer";
2020-03-19 11:30:05 +05:00
export default class IndexedCollection {
2022-03-30 15:52:48 +05:00
constructor(context, type, eventManager) {
this.indexer = new Indexer(context, type);
2022-03-30 15:52:48 +05:00
this.eventManager = eventManager;
// this.encryptionKeyFactory = encryptionKeyFactory;
2020-03-19 11:30:05 +05:00
}
clear() {
return this.indexer.clear();
}
2020-03-19 11:30:05 +05:00
async init() {
await this.indexer.init();
2020-03-19 11:30:05 +05:00
}
async addItem(item) {
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();
await this.updateItem(item);
if (!exists) {
await this.indexer.index(item.id);
}
2020-03-19 11:30:05 +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.");
2022-03-30 20:45:16 +05:00
this.eventManager.publish(EVENTS.databaseUpdated, item.id, item);
2020-03-19 11:30:05 +05:00
// if item is newly synced, remote will be true.
2022-03-30 20:45:16 +05:00
if (!item.remote) {
item.dateModified = Date.now();
item.synced = false;
}
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;
2022-02-08 13:16:41 +05:00
2022-03-28 10:22:26 +05:00
// if (await this.getEncryptionKey()) {
// const encrypted = await this.indexer.encrypt(
// await this.getEncryptionKey(),
// JSON.stringify(item)
// );
// encrypted.dateModified = item.dateModified;
// encrypted.localOnly = item.localOnly;
// encrypted.migrated = item.migrated;
// encrypted.id = item.id;
// await this.indexer.write(item.id, encrypted);
// } else
await this.indexer.write(item.id, item);
2020-03-19 11:30:05 +05:00
}
removeItem(id) {
2022-03-30 15:52:48 +05:00
this.eventManager.publish(EVENTS.databaseUpdated, id);
return this.updateItem({
id,
deleted: true,
});
2020-03-19 11:30:05 +05:00
}
2021-02-16 16:56:06 +05:00
async deleteItem(id) {
2022-03-30 15:52:48 +05:00
this.eventManager.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
}
exists(id) {
return this.indexer.exists(id);
2020-03-19 11:30:05 +05:00
}
2022-02-08 13:16:41 +05:00
async getItem(id) {
const item = await this.indexer.read(id);
if (!item) return;
2022-03-28 10:22:26 +05:00
// if ((await this.getEncryptionKey()) && item.iv && item.cipher) {
// return JSON.parse(
// await this.indexer.decrypt(await this.getEncryptionKey(), item)
// );
// } else
return item;
2020-03-19 11:30:05 +05:00
}
2020-03-19 12:38:33 +05:00
async getItems(indices) {
const data = await this.indexer.readMulti(indices);
return Object.fromEntries(data);
2020-03-19 12:38:33 +05:00
}
2022-02-08 13:16:41 +05:00
async getEncryptionKey() {
if (!this.encryptionKeyFactory) return;
if (this.encryptionKey) return this.encryptionKey;
this.encryptionKey = await this.encryptionKeyFactory();
return this.encryptionKey;
}
2020-03-19 11:30:05 +05:00
}