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.

100 lines (99 loc) 3.45 kB
/** * @module Cache */ import { type ICacheData, type ICacheInsert, type ICacheUpdate, type IDatabaseCacheAdapter } from "../../../../cache/contracts/_module-exports.js"; import type { ISerde } from "../../../../serde/contracts/_module-exports.js"; import type { IDeinitizable, IInitizable, IPrunable } from "../../../../utilities/_module-exports.js"; import { TimeSpan } from "../../../../utilities/_module-exports.js"; import type { Kysely } from "kysely"; /** * * IMPORT_PATH: `"@daiso-tech/core/cache/adapters"` * @group Adapters */ export type KyselyCacheAdapterTable = { key: string; value: string; expiration: number | string | null; }; /** * * IMPORT_PATH: `"@daiso-tech/core/cache/adapters"` * @group Adapters */ export type KyselyCacheAdapterTables = { cache: KyselyCacheAdapterTable; }; /** * * IMPORT_PATH: `"@daiso-tech/core/cache/adapters"` * @group Adapters */ export type KyselyCacheAdapterSettings = { kysely: Kysely<KyselyCacheAdapterTables>; serde: ISerde<string>; /** * @default * ```ts * TimeSpan.fromMinutes(1) * ``` */ expiredKeysRemovalInterval?: TimeSpan; /** * @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/adapters"` * @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/adapters"; * import { Serde } from "@daiso-tech/core/serde"; * import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/adapters" * import Sqlite from "better-sqlite3"; * * 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>; 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>; }