UNPKG

naughty-storage

Version:

Nodejs storage for inverting dependency of you application, same contract

114 lines (95 loc) 2.29 kB
"use strict"; const { setTimeout } = require("node:timers/promises"); const instances = new Map(); class MemoryStorage { constructor(collection) { const instance = instances.get(collection); if (instance) return instance; this.collection = collection; const storage = new Map(); this.instance = storage; instances.set(collection, this); } async get(name) { try { return await setTimeout(0, this.instance.get(name)); } catch { return; } } async set(name, data) { try { await setTimeout(0, this.instance.set(name, data)); return true; } catch { return false; } } async clear() { try { await setTimeout(0, this.instance.clear()); return true; } catch { return false; } } async delete(name) { try { return setTimeout(0, this.instance.delete(name)); } catch { return false; } } async has(name) { try { return setTimeout(0, this.instance.has(name)); } catch { return false; } } [Symbol.asyncIterator]() { const entries = this.instance.entries(); return { async next() { const entry = entries.next().value; await setTimeout(0); return { value: entry, done: !entry }; } } } entries() { return this; } async *values() { for await (const entry of this) { yield entry[1]; } } async *keys() { for await (const [key] of this) { yield key; } } pick(name, target, ...params) { return new Promise(resolve => { target(...params, (err, data) => { if (err) return void resolve(false); this.set(name, data) .then(() => void resolve(true)) .catch(() => void resolve(false)); }); }) } get size() { return setTimeout(0, this.instance.size); } static truncate = async () => { await setTimeout(0, instances.forEach(instance => void instance.clear())); } static destroy = async () => { await MemoryStorage.truncate(); await setTimeout(0, instances.clear()); } static location = () => { }; }; module.exports = MemoryStorage;