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.41 kB
/** * @module Lock */ import { type Kysely } from "kysely"; import { type IDatabaseLockAdapter, type IDatabaseLockTransaction, type ILockData, type ILockExpirationData } from "../../../../lock/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/lock/kysely-lock-adapter"` * @group Adapters */ export type KyselyLockTable = { key: string; owner: string; expiration: number | bigint | string | null; }; /** * * IMPORT_PATH: `"@daiso-tech/core/lock/kysely-lock-adapter"` * @group Adapters */ export type KyselyLockTables = { lock: KyselyLockTable; }; /** * * IMPORT_PATH: `"@daiso-tech/core/lock/kysely-lock-adapter"` * @group Adapters */ export type KyselyLockAdapterSettings = { kysely: Kysely<KyselyLockTables>; /** * @default * ```ts * import { TimeSpan } from "@daiso-tech/core/time-span"; * * TimeSpan.fromMinutes(1) * ``` */ expiredKeysRemovalInterval?: ITimeSpan; /** * @default true */ shouldRemoveExpiredKeys?: boolean; }; /** * 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 and use a database that has support for transactions. * The adapter have been tested with `sqlite`, `postgres` and `mysql` databases. * * IMPORT_PATH: `"@daiso-tech/core/lock/kysely-lock-adapter"` * @group Adapters */ export declare class KyselyLockAdapter implements IDatabaseLockAdapter, IDeinitizable, IInitizable, IPrunable { private readonly kysely; private readonly expiredKeysRemovalInterval; private readonly shouldRemoveExpiredKeys; private intervalId; private readonly isMysql; /** * @example * ```ts * import { KyselyLockAdapter } from "@daiso-tech/core/lock/kysely-lock-adapter"; * import Sqlite from "better-sqlite3"; * import { Kysely, SqliteDialect } from "kysely"; * * 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); /** * Removes all related lock tables and their rows. * Note all lock data will be removed. */ deInit(): Promise<void>; /** * Creates all related tables and indexes. * Note the `init` method needs to be called once before using the adapter. */ init(): Promise<void>; removeAllExpired(): Promise<void>; transaction<TReturn>(fn: InvokableFn<[ transaction: IDatabaseLockTransaction ], Promise<TReturn>>): Promise<TReturn>; remove(key: string): Promise<ILockExpirationData | null>; removeIfOwner(key: string, lockId: string): Promise<ILockData | null>; updateExpiration(key: string, lockId: string, expiration: Date): Promise<number>; find(key: string): Promise<ILockData | null>; }