Files
notesnook/packages/core/database/cached-collection.js
Abdullah Atta 0e6150a0f5 core: change db keys to include collection name
This should not affect the items or their server representation.
This change is necessary to allow multiple items with same id
that live in different collections.
For example, shortcuts have the same id as the inner reference they
point to. This was not possible before and would cause an overwrite
of the original value.
2022-10-13 19:18:52 +05:00

108 lines
2.8 KiB
JavaScript

/*
This file is part of the Notesnook project (https://notesnook.com/)
Copyright (C) 2022 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import IndexedCollection from "./indexed-collection";
import MapStub from "../utils/map";
export default class CachedCollection extends IndexedCollection {
constructor(context, type, eventManager) {
super(context, type, eventManager);
this.type = type;
this.map = new Map();
this.items = undefined;
// this.eventManager = eventManager;
// this.encryptionKeyFactory = encryptionKeyFactory;
}
async init() {
await super.init();
let data = await this.indexer.readMulti(this.indexer.indices);
if (this.map && this.map.dispose) this.map.dispose();
// const encryptionKey =
// this.encryptionKeyFactory && (await this.encryptionKeyFactory());
// if (encryptionKey) {
// for (let item of data) {
// const [_key, value] = item;
// const decryptedValue = JSON.parse(
// await this.indexer.decrypt(encryptionKey, value)
// );
// item[1] = decryptedValue;
// }
// }
this.map = new MapStub.Map(data, this.type);
}
async clear() {
await super.clear();
this.map.clear();
this.invalidateCache();
}
async updateItem(item) {
await super.updateItem(item);
this.map.set(item.id, item);
this.invalidateCache();
}
exists(id) {
return this.has(id) && !this.getItem(id).deleted;
}
has(id) {
return this.map.has(id);
}
count() {
return this.map.size;
}
getItem(id) {
return this.map.get(id);
}
async deleteItem(id) {
this.map.delete(id);
await super.deleteItem(id);
this.invalidateCache();
}
getRaw() {
return Array.from(this.map.values());
}
getItems(map = undefined) {
if (this.items && this.items.length === this.map.size) return this.items;
this.items = [];
this.map.forEach((value) => {
if (!value || value.deleted || !value.id) return;
value = map ? map(value) : value;
this.items.push(value);
});
this.items.sort((a, b) => b.dateCreated - a.dateCreated);
return this.items;
}
invalidateCache() {
this.items = undefined;
}
}