@ceramicnetwork/core
Version:
Typescript implementation of the Ceramic protocol
47 lines • 1.39 kB
JavaScript
export class ObjectStore {
constructor(generateKey, serialize, deserialize) {
this.generateKey = generateKey;
this.serialize = serialize;
this.deserialize = deserialize;
}
throwIfNotOpened() {
if (!this.store)
throw Error(`${this.constructor.name} is closed, you need to call async open(...), before performing other operations`);
}
async open(factory) {
this.store = await factory.open(this.useCaseName);
}
async close() {
if (!this.store)
return;
await this.store.close();
this.store = undefined;
}
async save(object, value) {
this.throwIfNotOpened();
await this.store.put(this.generateKey(object), this.serialize(value));
}
async load(object) {
this.throwIfNotOpened();
try {
const serialized = await this.store.get(this.generateKey(object));
if (serialized) {
return this.deserialize(serialized);
}
else {
return null;
}
}
catch (err) {
if (err.notFound) {
return null;
}
throw err;
}
}
async remove(object) {
this.throwIfNotOpened();
await this.store.del(this.generateKey(object));
}
}
//# sourceMappingURL=object-store.js.map