@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.
137 lines (136 loc) • 4.6 kB
TypeScript
/**
* @module SharedLock
*/
import { type Kysely } from "kysely";
import { type IReadableContext } from "../../../../execution-context/contracts/_module.js";
import { type IDatabaseSharedLockAdapter, type IDatabaseSharedLockTransaction } from "../../../../shared-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/shared-lock/kysely-shared-lock-adapter"`
* @group Adapters
*/
export type KyselyWriterLockTable = {
key: string;
owner: string;
expiration: number | bigint | string | null;
};
/**
* IMPORT_PATH: `"@daiso-tech/core/shared-lock/kysely-shared-lock-adapter"`
* @group Adapters
*/
export type KyselyReaderSemaphoreTable = {
key: string;
limit: number;
};
/**
* IMPORT_PATH: `"@daiso-tech/core/shared-lock/kysely-shared-lock-adapter"`
* @group Adapters
*/
export type KyselyReaderSemaphoreSlotTable = {
id: string;
key: string;
expiration: number | string | null;
};
/**
* IMPORT_PATH: `"@daiso-tech/core/shared-lock/kysely-shared-lock-adapter"`
* @group Adapters
*/
export type KyselySharedLockTables = {
writerLock: KyselyWriterLockTable;
readerSemaphore: KyselyReaderSemaphoreTable;
readerSemaphoreSlot: KyselyReaderSemaphoreSlotTable;
};
/**
* Configuration for `KyselySharedLockAdapter`.
* Requires a Kysely database instance with the shared-lock schema applied.
*
* IMPORT_PATH: `"@daiso-tech/core/shared-lock/kysely-shared-lock-adapter"`
* @group Adapters
*/
export type KyselySharedLockAdapterSettings = {
/**
* The Kysely database instance with the required shared-lock schema tables applied.
*/
kysely: Kysely<KyselySharedLockTables>;
/**
* @default
* ```ts
* import { TimeSpan } from "@daiso-tech/core/time-span";
*
* TimeSpan.fromMinutes(1)
* ```
*/
expiredKeysRemovalInterval?: ITimeSpan;
/**
* When `true`, a background task periodically removes expired shared-lock records.
* Set to `false` to disable automatic cleanup.
* @default true
*/
shouldRemoveExpiredKeys?: boolean;
/**
* @default
* ```ts
* () => new Date()
* ```
*/
currentDate?: () => Date;
/**
* @default
* ```ts
* import { Transaction } from "kysely"
*
* !(settings.kysely instanceof Transaction)
* ```
*/
enableTransactions?: boolean;
};
/**
* To utilize the `KyselySharedLockAdapter`, you must install the [`"kysely"`](https://www.npmjs.com/package/kysely) package and configure a `Kysely` class instance.
*
* Note in order to use `KyselySharedLockAdapter` 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/shared-lock/kysely-shared-lock-adapter"`
* @group Adapters
*/
export declare class KyselySharedLockAdapter implements IDatabaseSharedLockAdapter, IDeinitizable, IInitizable, IPrunable {
private readonly kysely;
private readonly expiredKeysRemovalInterval;
private readonly shouldRemoveExpiredKeys;
private intervalId;
private readonly currentDate;
private readonly enableTransactions;
/**
* @example
* ```ts
* import { KyselySharedLockAdapter } from "@daiso-tech/core/shared-lock/kysely-shared-lock-adapter";
* import Sqlite from "better-sqlite3";
* import { Kysely, SqliteDialect } from "kysely";
*
* const sharedLockAdapter = new KyselySharedLockAdapter({
* kysely: new Kysely({
* dialect: new SqliteDialect({
* database: new Sqlite("local.db"),
* }),
* }),
* });
* // You need initialize the adapter once before using it.
* await sharedLockAdapter.init();
* ```
*/
constructor(settings: KyselySharedLockAdapterSettings);
private _transaction;
init(): Promise<void>;
/**
* Removes all related shared-lock tables and their rows.
* Note all shared-lock data will be removed.
*/
deInit(): Promise<void>;
private removeAllExpiredReaders;
private removeAllExpiredWriters;
removeAllExpired(): Promise<void>;
transaction<TReturn>(_context: IReadableContext, fn: InvokableFn<[
transaction: IDatabaseSharedLockTransaction
], Promise<TReturn>>): Promise<TReturn>;
}