diff --git a/packages/core/database/cached-collection.js b/packages/core/database/cached-collection.js index 51d8c8197..e1364a6cc 100644 --- a/packages/core/database/cached-collection.js +++ b/packages/core/database/cached-collection.js @@ -63,7 +63,7 @@ export default class CachedCollection extends IndexedCollection { } exists(id) { - return this.map.has(id) && !this.map.get(id).deleted; + return this.has(id) && !this.getItem(id).deleted; } has(id) { diff --git a/packages/core/database/indexer.js b/packages/core/database/indexer.js index 663ee35f9..c2431dd70 100644 --- a/packages/core/database/indexer.js +++ b/packages/core/database/indexer.js @@ -20,14 +20,15 @@ along with this program. If not, see . import Storage from "./storage"; export default class Indexer extends Storage { - constructor(context, type) { - super(context); + constructor(storage, type) { + super(storage); this.type = type; this.indices = []; } async init() { - this.indices = (await this.read(this.type, true)) || []; + this.indices = (await super.read(this.type, true)) || []; + await this.migrateIndices(); } exists(key) { @@ -37,7 +38,7 @@ export default class Indexer extends Storage { async index(key) { if (this.exists(key)) return; this.indices.push(key); - await this.write(this.type, this.indices); + await super.write(this.type, this.indices); } getIndices() { @@ -47,11 +48,52 @@ export default class Indexer extends Storage { async deindex(key) { if (!this.exists(key)) return; this.indices.splice(this.indices.indexOf(key), 1); - await this.write(this.type, this.indices); + 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}`; + }; } diff --git a/packages/core/database/storage.js b/packages/core/database/storage.js index b661d0577..b432f75b5 100644 --- a/packages/core/database/storage.js +++ b/packages/core/database/storage.js @@ -20,8 +20,8 @@ along with this program. If not, see . import { randomBytes } from "../utils/random"; export default class Storage { - constructor(context) { - this.storage = context; + constructor(storage) { + this.storage = storage; } write(key, data) {