import type { AbstractLevel } from 'abstract-level'; import type { KeyValueStore } from './types.js'; import { Level } from 'level'; export type LevelStoreOptions = { db?: AbstractLevel; location?: string; }; export class LevelStore implements KeyValueStore { private readonly store: AbstractLevel; public constructor({ db, location = 'DATASTORE' }: LevelStoreOptions = {}) { this.store = db ?? new Level(location); } public async open(): Promise { await this.store.open(); } public async clear(): Promise { await this.store.clear(); } public async close(): Promise { await this.store.close(); } public async delete(key: K): Promise { await this.store.del(key); } public async get(key: K): Promise { try { return await this.store.get(key); } catch (error: any) { // Don't throw when a key wasn't found. if (error.notFound) { return undefined; } throw error; } } public async set(key: K, value: V): Promise { await this.store.put(key, value); } }