datastore-core
Version:
Wrapper implementation for interface-datastore
46 lines • 1.37 kB
JavaScript
import { Key } from 'interface-datastore/key';
import { NotFoundError } from 'interface-store';
import { BaseDatastore } from './base.js';
export class MemoryDatastore extends BaseDatastore {
data;
constructor() {
super();
this.data = new Map();
}
put(key, val, options) {
options?.signal?.throwIfAborted();
this.data.set(key.toString(), val);
return key;
}
get(key, options) {
options?.signal?.throwIfAborted();
const result = this.data.get(key.toString());
if (result == null) {
throw new NotFoundError();
}
return result;
}
has(key, options) {
options?.signal?.throwIfAborted();
return this.data.has(key.toString());
}
delete(key, options) {
options?.signal?.throwIfAborted();
this.data.delete(key.toString());
}
*_all(q, options) {
options?.signal?.throwIfAborted();
for (const [key, value] of this.data.entries()) {
yield { key: new Key(key), value };
options?.signal?.throwIfAborted();
}
}
*_allKeys(q, options) {
options?.signal?.throwIfAborted();
for (const key of this.data.keys()) {
yield new Key(key);
options?.signal?.throwIfAborted();
}
}
}
//# sourceMappingURL=memory.js.map