@grouparoo/core
Version:
The Grouparoo Core
47 lines (46 loc) • 2.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PropertiesCache = void 0;
const modelCache_1 = require("../modelCache");
const Property_1 = require("../../models/Property");
const Source_1 = require("../../models/Source");
const Option_1 = require("../../models/Option");
const Filter_1 = require("../../models/Filter");
async function findAllWithCache(modelId, state) {
const now = Date.now();
if (this.expires > now && this.instances.length > 0) {
return modelId
? this.instances.filter((p) => { var _a; return ((_a = p === null || p === void 0 ? void 0 : p.source) === null || _a === void 0 ? void 0 : _a.modelId) === modelId && (!state || p.state === state); })
: this.instances.filter((p) => !state || p.state === state);
}
else {
this.instances = await Property_1.Property.unscoped().findAll({
include: this.include(),
});
this.expires = now + this.TTL;
return modelId
? this.instances.filter((p) => { var _a; return ((_a = p === null || p === void 0 ? void 0 : p.source) === null || _a === void 0 ? void 0 : _a.modelId) === modelId && (!state || p.state === state); })
: this.instances.filter((p) => !state || p.state === state);
}
}
async function findOneWithCache(value, modelId, state, lookupKey = "id") {
const instances = await this.findAllWithCache(modelId);
let instance = instances.find((i) => i[lookupKey] === value);
if (!instance) {
const where = { [lookupKey]: value };
if (state)
where.state = state;
instance = await Property_1.Property.unscoped().findOne({
where,
include: this.include(),
});
if (instance)
this.invalidate();
}
return instance;
}
exports.PropertiesCache = new modelCache_1.ModelCache(findAllWithCache, findOneWithCache, () => [
{ model: Source_1.Source.unscoped(), required: false },
{ model: Option_1.Option, required: false },
{ model: Filter_1.Filter, required: false },
]);