@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.
117 lines (116 loc) • 4.23 kB
TypeScript
/**
* @module Lock
*/
import { type Kysely } from "kysely";
import { type IReadableContext } from "../../../../execution-context/contracts/_module.js";
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;
};
/**
* Configuration for `KyselyLockAdapter`.
* Requires a Kysely database instance with the lock schema applied.
*
* IMPORT_PATH: `"@daiso-tech/core/lock/kysely-lock-adapter"`
* @group Adapters
*/
export type KyselyLockAdapterSettings = {
/**
* The Kysely database instance typed with the required lock table.
*/
kysely: Kysely<KyselyLockTables>;
/**
* @default
* ```ts
* import { TimeSpan } from "@daiso-tech/core/time-span";
*
* TimeSpan.fromMinutes(1)
* ```
*/
expiredKeysRemovalInterval?: ITimeSpan;
/**
* When `true`, a background task periodically removes expired lock records.
* Set to `false` to disable automatic cleanup.
* @default true
*/
shouldRemoveExpiredKeys?: boolean;
/**
* @default
* ```ts
* import { Transaction } from "kysely"
*
* !(settings.kysely instanceof Transaction)
* ```
*/
enableTransactions?: 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;
private readonly enableTransactions;
/**
* @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);
private _transaction;
/**
* 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>(_context: IReadableContext, fn: InvokableFn<[
transaction: IDatabaseLockTransaction
], Promise<TReturn>>): Promise<TReturn>;
remove(_context: IReadableContext, key: string): Promise<ILockExpirationData | null>;
removeIfOwner(_context: IReadableContext, key: string, lockId: string): Promise<ILockData | null>;
updateExpiration(_context: IReadableContext, key: string, lockId: string, expiration: Date): Promise<number>;
find(_context: IReadableContext, key: string): Promise<ILockData | null>;
}