mastercache
Version:
Multi-tier cache module for Node.js. Redis, Upstash, CloudfareKV, File, in-memory and others drivers
142 lines (139 loc) • 3.39 kB
TypeScript
import { Knex } from 'knex';
import { Kysely } from 'kysely';
import { DynamoDBClientConfig } from '@aws-sdk/client-dynamodb';
import { Redis, RedisOptions } from 'ioredis';
import { DbResult, DefaultColumnTypes, DefaultSchemaConfig } from 'orchid-orm';
import { Duration } from '../helpers.js';
import 'typescript-log';
/**
* Options that are common to all drivers
*
* Some of theses options may be also defined in
* the MasterCache options. Setting them specifically
* for a driver will override the MasterCache options.
*/
type DriverCommonOptions = {
prefix?: string;
};
/**
* Options for DynamoDB driver
*/
type DynamoDBConfig = {
/**
* DynamoDB table name to use.
*/
table: {
name: string;
};
/**
* AWS credentials
*/
credentials?: DynamoDBClientConfig['credentials'];
/**
* Region of your DynamoDB instance
*/
region: DynamoDBClientConfig['region'];
/**
* Endpoint to your DynamoDB instance
*/
endpoint: DynamoDBClientConfig['endpoint'];
} & DriverCommonOptions;
/**
* Options for Memory driver
*/
type MemoryConfig = {
/**
* Maximum number of items to store in the cache.
*
* Note that fewer items may be stored if you
* are also using `maxSize` and the cache is full.
*
* @default 1000
*/
maxItems?: number;
/**
* Maximum size of the cache in bytes.
*/
maxSize?: number;
/**
* Maximum size of one entry in bytes.
*
* If an entry is larger than this value,
* it will NOT be stored
*/
maxEntrySize?: number;
} & DriverCommonOptions;
/**
* Options for Redis driver
*/
type RedisConfig = {
/**
* A IoRedis connection instance or connection options
*/
connection: Redis | RedisOptions;
} & DriverCommonOptions;
/**
* Options for File driver
*/
type FileConfig = {
/**
* Directory where the cache files will be stored
*/
directory: string;
/**
* The interval between each expired entry pruning
* Can be set to `false` to disable pruning.
*
* @default false
*/
pruneInterval?: Duration | false;
} & DriverCommonOptions;
/**
* Common options for database drivers
*/
interface DatabaseConfig extends DriverCommonOptions {
/**
* Table name to use
*/
tableName?: string;
/**
* Should the driver automatically create the table
* @default true
*/
autoCreateTable?: boolean;
/**
* The interval between each expired entry pruning
* run. Can be set to `false` to disable pruning.
*
* @default false
*/
pruneInterval?: Duration | false;
}
/**
* Configuration accepted by the Knex adapter
*/
interface KnexConfig extends DatabaseConfig {
/**
* The Knex instance
*/
connection: Knex;
}
/**
* Configuration accepted by the Kysely adapter
*/
interface KyselyConfig extends DatabaseConfig {
/**
* The Kysely instance
*/
connection: Kysely<any>;
}
/**
* Configuration accepted by the Orchid ORM adapter
*/
interface OrchidConfig extends DatabaseConfig {
/**
* The Orchid ORM instance
*/
connection: DbResult<DefaultColumnTypes<DefaultSchemaConfig>>;
}
export type { DatabaseConfig, DriverCommonOptions, DynamoDBConfig, FileConfig, KnexConfig, KyselyConfig, MemoryConfig, OrchidConfig, RedisConfig };