UNPKG

@daiso-tech/core

Version:

The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.

134 lines (133 loc) 5.04 kB
/** * @module Cache */ import { type Kysely } from "kysely"; import { type ICacheData, type ICacheDataExpiration, type IDatabaseCacheAdapter, type IDatabaseCacheTransaction } from "../../../../cache/contracts/_module.js"; import { type IReadableContext } from "../../../../execution-context/contracts/_module.js"; import { type ISerde } from "../../../../serde/contracts/_module.js"; import { type ITimeSpan } from "../../../../time-span/contracts/_module.js"; import { type IDeinitizable, type IInitizable, type InvokableFn, type IPrunable } from "../../../../utilities/_module.js"; /** * IMPORT_PATH: `"@daiso-tech/core/cache/kysely-cache-adapter"` * @group Adapters */ export type KyselyCacheTable = { key: string; value: string; expiration: number | string | null; }; /** * IMPORT_PATH: `"@daiso-tech/core/cache/kysely-cache-adapter"` * @group Adapters */ export type KyselyCacheTables = { cache: KyselyCacheTable; }; /** * Configuration for `KyselyCacheAdapter`. * Requires a Kysely database instance and a serde for serialising cache values to strings. * * IMPORT_PATH: `"@daiso-tech/core/cache/kysely-cache-adapter"` * @group Adapters */ export type KyselyCacheAdapterSettings = { /** * The Kysely database instance with the required cache schema tables applied. */ kysely: Kysely<KyselyCacheTables>; /** * Serde instance for serializing and deserializing cache values to and from strings. */ serde: ISerde<string>; /** * How often expired cache entries are automatically removed in the background. * @default * ```ts * import { TimeSpan } from "@daiso-tech/core/time-span"; * * TimeSpan.fromMinutes(1) * ``` */ expiredKeysRemovalInterval?: ITimeSpan; /** * When `true`, a background task periodically removes expired keys. * Set to `false` to disable automatic cleanup. * @default true */ shouldRemoveExpiredKeys?: boolean; /** * @default * ```ts * import { Transaction } from "kysely" * * !(settings.kysely instanceof Transaction) * ``` */ enableTransactions?: boolean; }; /** * @internal */ export declare class DatabaseCacheTransaction<TType> implements IDatabaseCacheTransaction<TType> { private readonly kysely; private readonly serde; private readonly isMysql; constructor(kysely: Kysely<KyselyCacheTables>, serde: ISerde<string>); find(_context: IReadableContext, key: string): Promise<ICacheData<TType> | null>; upsert(_context: IReadableContext, key: string, value: TType, expiration?: Date | null): Promise<void>; } /** * To utilize the `KyselyCacheAdapter`, you must install the [`"kysely"`](https://www.npmjs.com/package/kysely) package and configure a `Kysely` class instance. * The adapter have been tested with `sqlite`, `postgres` and `mysql` databases. * * IMPORT_PATH: `"@daiso-tech/core/cache/kysely-cache-adapter"` * @group Adapters */ export declare class KyselyCacheAdapter<TType = unknown> implements IDatabaseCacheAdapter<TType>, IInitizable, IDeinitizable, IPrunable { private readonly isMysql; private readonly serde; private readonly kysely; private readonly shouldRemoveExpiredKeys; private readonly expiredKeysRemovalInterval; private timeoutId; private readonly enableTransactions; /** * @example * ```ts * import { KyselyCacheAdapter } from "@daiso-tech/core/cache/kysely-cache-adapter"; * import { Serde } from "@daiso-tech/core/serde"; * import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/super-json-serde-adapter" * import SQLite from 'better-sqlite3' * import { Kysely, SqliteDialect } from 'kysely' * * const serde = new Serde(new SuperJsonSerdeAdapter()); * const cacheAdapter = new KyselyCacheAdapter({ * kysely: new Kysely({ * dialect: new SqliteDialect({ * database: new Sqlite("local.db"), * }), * }), * serde, * }); * // You need initialize the adapter once before using it. * await cacheAdapter.init(); * ``` */ constructor(settings: KyselyCacheAdapterSettings); removeAllExpired(): Promise<void>; init(): Promise<void>; /** * Removes all related cache tables and their rows. * Note all cache data will be removed. */ deInit(): Promise<void>; find(_context: IReadableContext, key: string): Promise<ICacheData<TType> | null>; private _transaction; transaction<TValue>(_context: IReadableContext, trxFn: InvokableFn<[ trx: IDatabaseCacheTransaction<TType> ], Promise<TValue>>): Promise<TValue>; update(context: IReadableContext, key: string, value: TType): Promise<ICacheDataExpiration | null>; removeMany(context: IReadableContext, keys: Array<string>): Promise<Array<ICacheDataExpiration>>; removeAll(_context: IReadableContext): Promise<void>; removeByKeyPrefix(_context: IReadableContext, prefix: string): Promise<void>; }