@clickup/ent-framework
Version:
A PostgreSQL graph-database-alike library with microsharding and row-level security
82 lines • 3.42 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.CacheMixin = CacheMixin;
const misc_1 = require("../../internal/misc");
const types_1 = require("../../types");
const QueryCache_1 = require("../QueryCache");
const MULTI_ROW_AFFECTING_OPS = [
"loadByNullable",
"selectBy",
"select",
"count",
"exists",
];
/**
* Modifies the passed class adding VC-stored cache layer to it.
*/
function CacheMixin(Base) {
class CacheMixin extends Base {
static async insertIfNotExists(vc, input) {
const resPromise = super.insertIfNotExists(vc, input);
vc.cache(QueryCache_1.QueryCache)
.delete(this, ["loadNullable"], (0, misc_1.hasKey)(types_1.ID, input) ? input[types_1.ID] : null)
.delete(this, MULTI_ROW_AFFECTING_OPS);
return resPromise;
}
static async upsert(vc, input) {
const resPromise = super.upsert(vc, input);
vc.cache(QueryCache_1.QueryCache)
.delete(this, ["loadNullable"], (0, misc_1.hasKey)(types_1.ID, input) ? input[types_1.ID] : null)
.delete(this, MULTI_ROW_AFFECTING_OPS);
return resPromise;
}
static async loadNullable(vc, id) {
return vc
.cache(QueryCache_1.QueryCache)
.through(this, "loadNullable", id, async () => super.loadNullable(vc, id));
}
static async loadByNullable(vc, input) {
return vc
.cache(QueryCache_1.QueryCache)
.through(this, "loadByNullable", JSON.stringify(input), async () => super.loadByNullable(vc, input));
}
static async selectBy(vc, input) {
return vc
.cache(QueryCache_1.QueryCache)
.through(this, "selectBy", JSON.stringify(input), async () => super.selectBy(vc, input));
}
static async select(vc, where, limit, order, custom) {
return vc
.cache(QueryCache_1.QueryCache)
.through(this, "select", JSON.stringify([where, limit, order, custom]), async () => super.select(vc, where, limit, order, custom));
}
static async count(vc, where) {
return vc
.cache(QueryCache_1.QueryCache)
.through(this, "count", JSON.stringify(where), async () => super.count(vc, where));
}
static async exists(vc, where) {
return vc
.cache(QueryCache_1.QueryCache)
.through(this, "exists", JSON.stringify(where), async () => super.exists(vc, where));
}
async updateOriginal(input) {
const resPromise = super.updateOriginal(input);
this.vc
.cache(QueryCache_1.QueryCache)
.delete(this.constructor, ["loadNullable"], this[types_1.ID])
.delete(this.constructor, MULTI_ROW_AFFECTING_OPS);
return resPromise;
}
async deleteOriginal() {
const resPromise = super.deleteOriginal();
this.vc
.cache(QueryCache_1.QueryCache)
.delete(this.constructor, ["loadNullable"], this[types_1.ID])
.delete(this.constructor, MULTI_ROW_AFFECTING_OPS);
return resPromise;
}
}
return CacheMixin;
}
//# sourceMappingURL=CacheMixin.js.map
;