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();
Retrieves the current lock data for a given key. Used for querying lock state without modifying it.
Unique identifier for the lock
Promise resolving to the lock's owner and expiration data if found, otherwise null
Removes a lock from the database regardless of its current owner. Used for administrative cleanup or when the current owner is unavailable.
Unique identifier for the lock
Promise resolving to the removed lock's expiration data if a lock existed, otherwise null
Removes a lock from the database only if owned by the specified lockId. Ownership verification prevents accidental deletion of locks held by others.
Unique identifier for the lock
The unique identifier of the expected lock owner
Promise resolving to the lock's owner and expiration data if successfully removed, null if the lock wasn't found or the owner didn't match
Executes the provided function within a database transaction. Ensures atomicity of all lock operations (upsert, find) within the transaction. Should use the strictest transaction isolation level to prevent race conditions.
Function to execute within the transaction, receives transaction object
Promise resolving to the return value of the transaction function
Updates the expiration time of a lock if owned by the specified lockId. Ownership verification ensures only the lock owner can extend the lock.
Unique identifier for the lock
The unique identifier of the expected lock owner
The new date and time when the lock should expire
Promise resolving to the number of locks updated (1 if successful, 0 if not found or ownership mismatch)
To utilize the
KyselyLockAdapter, you must install the"kysely"package and configure aKyselyclass instance.Note in order to use
KyselyLockAdaptercorrectly, 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 withsqlite,postgresandmysqldatabases.IMPORT_PATH:
"@daiso-tech/core/lock/kysely-lock-adapter"