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.

103 lines (102 loc) 3.18 kB
/** * @module Lock */ import type { IDatabaseLockAdapter, ILockData, ILockExpirationData } from "../../../../lock/contracts/_module-exports.js"; import { type Kysely } from "kysely"; import { type IDeinitizable, type IInitizable, type IPrunable, TimeSpan } from "../../../../utilities/_module-exports.js"; /** * * IMPORT_PATH: `"@daiso-tech/core/lock/adapters"` * @group Adapters */ export type KyselyLockAdapterTable = { key: string; owner: string; expiration: number | bigint | string | null; }; /** * * IMPORT_PATH: `"@daiso-tech/core/lock/adapters"` * @group Adapters */ export type KyselyLockAdapterTables = { lock: KyselyLockAdapterTable; }; /** * * IMPORT_PATH: `"@daiso-tech/core/lock/adapters"` * @group Adapters */ export type KyselyLockAdapterSettings = { kysely: Kysely<KyselyLockAdapterTables>; /** * @default * ```ts * TimeSpan.fromMinutes(1) * ``` */ expiredKeysRemovalInterval?: TimeSpan; /** * @default true */ shouldRemoveExpiredKeys?: boolean; /** * @default * ```ts * () => new Date() * ``` */ currentDate?: () => Date; /** * @default * ```ts * globalThis.setInterval * ``` */ setInterval?: typeof setInterval; }; /** * To utilize the `KyselyLockAdapter`, you must install the [`"kysely"`](https://www.npmjs.com/package/kysely) package and configure a `Kysely` class instance. * * Note in order to use `KyselyLockAdapter` correctly, ensure you use a single, consistent database across all server instances. * The adapter have been tested with `sqlite`, `postgres` and `mysql` databases. * * IMPORT_PATH: `"@daiso-tech/core/lock/adapters"` * @group Adapters */ export declare class KyselyLockAdapter implements IDatabaseLockAdapter, IDeinitizable, IInitizable, IPrunable { private readonly kysely; private readonly expiredKeysRemovalInterval; private readonly shouldRemoveExpiredKeys; private timeoutId; private readonly currentDate; private readonly setInterval; private readonly isMysql; /** * @example * ```ts * import { KyselyLockAdapter } from "@daiso-tech/core/lock/adapters"; * import Sqlite from "better-sqlite3"; * * const lockAdapter = new KyselyLockAdapter({ * kysely: new Kysely({ * dialect: new SqliteDialect({ * database: new Sqlite("local.db"), * }), * }), * }); * // You need initialize the adapter once before using it. * await lockAdapter.init(); * ``` */ constructor(settings: KyselyLockAdapterSettings); deInit(): Promise<void>; init(): Promise<void>; removeAllExpired(): Promise<void>; insert(key: string, owner: string, expiration: Date | null): Promise<void>; updateIfExpired(key: string, owner: string, expiration: Date | null): Promise<number>; remove(key: string): Promise<ILockExpirationData | null>; removeIfOwner(key: string, owner: string): Promise<ILockData | null>; updateExpirationIfOwner(key: string, owner: string, expiration: Date): Promise<number>; find(key: string): Promise<ILockData | null>; }