@decaf-ts/db-decorators
Version:
Agnostic database decorators and repository
42 lines (41 loc) • 1.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataCache = void 0;
const errors_1 = require("./errors.cjs");
class DataCache {
constructor() {
this.cache = {};
}
async get(key) {
if (!(key in this.cache))
throw new errors_1.NotFoundError(`Key ${key} not in dataStore`);
return this.cache[key];
}
async push(key, value) {
if (key in this.cache)
throw new errors_1.ConflictError(`Key ${key} already in dataStore`);
this.cache[key] = value;
}
async put(key, value) {
this.cache[key] = value;
}
async pop(key) {
const res = this.get(key);
delete this.cache[key];
return res;
}
async filter(filter) {
if (typeof filter === "string")
filter = new RegExp(filter);
return Object.keys(this.cache)
.filter((k) => !!filter.exec(k))
.map((k) => this.cache[k]);
}
async purge(key) {
if (!key)
this.cache = {};
else
await this.pop(key);
}
}
exports.DataCache = DataCache;