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.

107 lines (106 loc) 3.76 kB
/** * @module Cache */ import { type Kysely } from "kysely"; import { type ICacheData, type ICacheInsert, type ICacheUpdate, type IDatabaseCacheAdapter } from "../../../../cache/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 IPrunable } from "../../../../utilities/_module.js"; /** * * IMPORT_PATH: `"@daiso-tech/core/cache/kysely-cache-adapter"` * @group Adapters */ export type KyselyCacheAdapterTable = { key: string; value: string; expiration: number | string | null; }; /** * * IMPORT_PATH: `"@daiso-tech/core/cache/kysely-cache-adapter"` * @group Adapters */ export type KyselyCacheAdapterTables = { cache: KyselyCacheAdapterTable; }; /** * * IMPORT_PATH: `"@daiso-tech/core/cache/kysely-cache-adapter"` * @group Adapters */ export type KyselyCacheAdapterSettings = { kysely: Kysely<KyselyCacheAdapterTables>; serde: ISerde<string>; /** * @default * ```ts * import { TimeSpan } from "@daiso-tech/core/time-span"; * * TimeSpan.fromMinutes(1) * ``` */ expiredKeysRemovalInterval?: ITimeSpan; /** * @default true */ shouldRemoveExpiredKeys?: boolean; }; /** * 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 static filterUnexpiredKeys; private static filterExpiredKeys; private readonly serde; private readonly kysely; private readonly shouldRemoveExpiredKeys; private readonly expiredKeysRemovalInterval; private timeoutId; private readonly disableTransaction; /** * @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(key: string): Promise<ICacheData<TType> | null>; insert(data: ICacheInsert<TType>): Promise<void>; private transaction; upsert(data: ICacheInsert<TType>): Promise<ICacheData<TType> | null>; updateExpired(data: ICacheInsert<TType>): Promise<number>; updateUnexpired(data: ICacheUpdate<TType>): Promise<number>; incrementUnexpired(data: ICacheUpdate<number>): Promise<number>; removeExpiredMany(keys: string[]): Promise<number>; removeUnexpiredMany(keys: string[]): Promise<number>; removeAll(): Promise<void>; removeByKeyPrefix(prefix: string): Promise<void>; }