Files
notesnook/packages/core/database/indexer.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

100 lines
2.4 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 Storage from "./storage";
export default class Indexer extends Storage {
constructor(storage, type) {
super(storage);
this.type = type;
this.indices = [];
}
async init() {
this.indices = (await super.read(this.type, true)) || [];
await this.migrateIndices();
}
exists(key) {
return this.indices.includes(key);
}
async index(key) {
if (this.exists(key)) return;
this.indices.push(key);
await super.write(this.type, this.indices);
}
getIndices() {
return this.indices;
}
async deindex(key) {
if (!this.exists(key)) return;
this.indices.splice(this.indices.indexOf(key), 1);
await super.write(this.type, this.indices);
}
async clear() {
this.indices = [];
await super.clear();
}
read(key, isArray = false) {
return super.read(this.makeId(key), isArray);
}
write(key, data) {
return super.write(this.makeId(key), data);
}
remove(key) {
return super.remove(this.makeId(key));
}
async readMulti(keys) {
const entries = await super.readMulti(keys.map(this.makeId));
entries.forEach((entry) => {
entry[0] = entry[0].replace(`_${this.type}`, "");
});
return entries;
}
async migrateIndices() {
const keys = (await super.getAllKeys()).filter(
(key) => !key.endsWith(`_${this.type}`) && this.exists(key)
);
for (const id of keys) {
const item = await super.read(id);
if (!item) continue;
await this.write(id, item);
}
// remove old ids once they have been moved
for (const id of keys) {
await this.remove(id);
}
}
makeId = (id) => {
return `${id}_${this.type}`;
};
}