@imqueue/pg-cache
Version:
PostgreSQL managed cache on Redis for @imqueue-based service methods
73 lines (72 loc) • 3.36 kB
TypeScript
/*!
* I'm Queue Software Project
* Copyright (C) 2025 imqueue.com <support@imqueue.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* If you want to use this code in a closed source (commercial) project, you can
* purchase a proprietary commercial license. Please contact us at
* <support@imqueue.com> to get commercial licensing options.
*/
import { Model } from 'sequelize-typescript';
import { type MethodDecorator } from './env.js';
/**
* Options expected by `@cacheBy`() decorator factory
*/
export interface CacheByOptions {
/**
* Time to live for cached values. If not specified - default is used.
* Default is equivalent of 24 hours. Must be specified in milliseconds.
*
*/
ttl?: number;
/**
* Zero-index based position of fields argument in a method arguments,
* which are passed at runtime. Fields argument are usually passed from
* a client to specify a query map to be extracted and returned from
* a service method. For example, fields map can be built from an
* incoming GraphQL request using fieldsMap() function from
* graphql-fields-list package.
*
* Usually pg-based `@imqueue` services, which utilize `@imqueue/pg-sequelize`
* package passing fields as a second argument to service methods,
* so if this option is omitted, it will try to check for the second
* passed argument. If you need to explicitly disable it, pass -1.
*
* @see https://github.com/Mikhus/graphql-fields-list
*
*/
fieldsArg?: number;
}
/**
* Retrieves table names as channels from the given model and filter them by
* a given fields map, if passed. Returns result as list of table names.
*
* @param model - sequelize-style model whose table name and associations are
* read to derive the channels
* @param fields - fields the cached method depends on; an association field
* pulls in that association's table as well
* @param tables - extra table names to watch that the model does not reach
*/
export declare function channelsOf(model: typeof Model, fields?: any, tables?: string[]): string[];
/**
* Decorator factory `@cacheBy`(Model, CacheByOptions)
* This decorator should be used on a service methods, to set the caching
* rules for a method. Caching rules within this decorator are defined by a
* passed model, which is treated as a root model of the call and it analyzes
* cache invalidation based on passed runtime fields arguments, which
* prevents unnecessary cache invalidations. So it is more intellectual way
* to invalidate cache instead of any changes on described list of tables.
*/
export declare function cacheBy(model: typeof Model, options?: CacheByOptions): MethodDecorator;