import type { Database, Key } from 'lmdb'; import type { AztecAsyncSingleton, AztecSingleton } from '../interfaces/singleton.js'; /** The slot where this singleton will store its value */ type ValueSlot = ['singleton', string, 'value']; /** * Stores a single value in LMDB. */ export class LmdbAztecSingleton implements AztecSingleton, AztecAsyncSingleton { #db: Database; #slot: ValueSlot; constructor(db: Database, name: string) { this.#db = db as Database; this.#slot = ['singleton', name, 'value']; } get(): T | undefined { return this.#db.get(this.#slot); } getAsync(): Promise { return Promise.resolve(this.get()); } set(val: T): Promise { return this.#db.put(this.#slot, val); } delete(): Promise { return this.#db.remove(this.#slot); } }