seyfert
Version:
The most advanced framework for discord bots
83 lines (82 loc) • 2.58 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseResource = void 0;
const common_1 = require("../../../common");
class BaseResource {
cache;
client;
namespace = 'base';
constructor(cache, client) {
this.cache = cache;
this.client = client;
}
//@ts-expect-error
filter(data, id, from) {
return true;
}
get adapter() {
return this.cache.adapter;
}
removeIfNI(intent, id) {
if (!this.cache.hasIntent(intent)) {
return this.remove(id);
}
return;
}
setIfNI(from, intent, id, data) {
if (!this.cache.hasIntent(intent)) {
return this.set(from, id, data);
}
}
get(id) {
return this.adapter.get(this.hashId(id));
}
bulk(ids) {
return (0, common_1.fakePromise)(this.adapter.bulkGet(ids.map(id => this.hashId(id)))).then(x => x.filter(y => y));
}
set(from, id, data) {
if (!this.filter(data, id, from))
return;
return (0, common_1.fakePromise)(this.addToRelationship(id)).then(() => this.adapter.set(this.hashId(id), data));
}
patch(from, id, data) {
if (!this.filter(data, id, from))
return;
return (0, common_1.fakePromise)(this.addToRelationship(id)).then(() => this.adapter.patch(this.hashId(id), data));
}
remove(id) {
return (0, common_1.fakePromise)(this.removeToRelationship(id)).then(() => this.adapter.remove(this.hashId(id)));
}
keys() {
return this.adapter.keys(this.namespace);
}
values() {
return this.adapter.values(this.namespace);
}
count() {
return this.adapter.count(this.namespace);
}
contains(id) {
return this.adapter.contains(this.namespace, id);
}
getToRelationship() {
return this.adapter.getToRelationship(this.namespace);
}
addToRelationship(id) {
return this.adapter.addToRelationship(this.namespace, id);
}
removeToRelationship(id) {
return this.adapter.removeToRelationship(this.namespace, id);
}
flush() {
return (0, common_1.fakePromise)(this.adapter.keys(this.namespace)).then(keys => {
return (0, common_1.fakePromise)(this.adapter.bulkRemove(keys)).then(() => {
return this.adapter.removeRelationship(this.namespace);
});
});
}
hashId(id) {
return id.startsWith(this.namespace) ? id : `${this.namespace}.${id}`;
}
}
exports.BaseResource = BaseResource;