UNPKG

@grouparoo/core

Version:
32 lines (31 loc) 1.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AppsCache = void 0; const modelCache_1 = require("../modelCache"); const App_1 = require("../../models/App"); const Option_1 = require("../../models/Option"); async function findAllWithCache(modelId, state) { const now = Date.now(); if (this.expires > now && this.instances.length > 0) { return this.instances.filter((a) => !state || a.state === state); } else { this.instances = await App_1.App.unscoped().findAll({ include: this.include() }); this.expires = now + this.TTL; return this.instances.filter((a) => !state || a.state === state); } } async function findOneWithCache(value, modelId, state, lookupKey = "id") { const instances = await this.findAllWithCache(); let instance = instances.find((i) => i[lookupKey] === value); if (!instance) { const where = { [lookupKey]: value }; if (state) where.state = state; instance = await App_1.App.unscoped().findOne({ where, include: this.include() }); if (instance) this.invalidate(); } return instance; } exports.AppsCache = new modelCache_1.ModelCache(findAllWithCache, findOneWithCache, () => [{ model: Option_1.Option, required: false }]);