import { env } from "actionhero"; import { Includeable } from "sequelize"; type StatefulCachedModel = { state: string }; type FindAllWithCache = ( modelId?: string, state?: T["state"] ) => Promise; type FindOneWithCache = ( value: string, modelId?: string, state?: T["state"], lookupKey?: keyof T ) => Promise; export class ModelCache { TTL = env === "test" ? -1 : 1000 * 30; expires: number = 0; instances: T[]; findAllWithCache: FindAllWithCache; findOneWithCache: FindOneWithCache; // The include constructor needs to be a method rather than static or else the caches will create circular dependencies. This is not-unlike how Sequelize-Typescript handles relationships. include: () => Includeable[]; constructor( findAllWithCache: FindAllWithCache, findOneWithCache: FindOneWithCache, include: () => Includeable[] ) { this.findAllWithCache = findAllWithCache.bind(this); this.findOneWithCache = findOneWithCache.bind(this); this.include = include; this.instances = []; } invalidate() { this.expires = 0; } }