@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.
60 lines (59 loc) • 2.35 kB
TypeScript
/**
* @module Lock
*/
import type { IDatabaseLockAdapter, ILockData } from "../../../../lock/contracts/_module-exports.js";
import { type IDeinitizable, type IInitizable, type ISqliteDatabase, TimeSpan } from "../../../../utilities/_module-exports.js";
/**
*
* IMPORT_PATH: `"@daiso-tech/core/lock/adapters"`
* @group Adapters
*/
export type SqliteLockAdapterSettings = {
database: ISqliteDatabase;
tableName?: string;
expiredKeysRemovalInterval?: TimeSpan;
shouldRemoveExpiredKeys?: boolean;
};
/**
* To utilize the `SqliteLockAdapter`, you must install the `"better-sqlite3"` and `"@types/better-sqlite3"` packages.
*
* Note the `SqliteLockAdapter` is limited to single server usage and cannot be shared across multiple servers but it can be shared between different processes.
* To use it correctly, ensure all process instances access the same consistent, persisted database.
*
* IMPORT_PATH: `"@daiso-tech/core/lock/adapters"`
* @group Adapters
*/
export declare class SqliteLockAdapter implements IDatabaseLockAdapter, IDeinitizable, IInitizable {
private databaseLockAdapter;
/**
* @example
* ```ts
* import { SqliteLockAdapter } from "@daiso-tech/core/lock/adapters";
* import Sqlite from "better-sqlite3";
*
* const database = new Sqlite("local.db");
* const lockAdapter = new SqliteLockAdapter({
* database,
* });
* // You need initialize the adapter once before using it.
* await lockAdapter.init();
* ```
*/
constructor(settings: SqliteLockAdapterSettings);
removeExpiredKeys(): Promise<void>;
/**
* Removes the table where the lock keys are stored and removes the table indexes.
* Note all lock data will be removed.
*/
deInit(): Promise<void>;
/**
* Creates the table where the lock keys are stored and it's related indexes.
* Note the `init` method needs to be called before using the adapter.
*/
init(): Promise<void>;
insert(key: string, owner: string, expiration: Date | null): Promise<void>;
update(key: string, owner: string, expiration: Date | null): Promise<number>;
remove(key: string, owner: string | null): Promise<void>;
refresh(key: string, owner: string, expiration: Date): Promise<number>;
find(key: string): Promise<ILockData | null>;
}