import { KyselySemaphoreAdapter } from "@daiso-tech/core/semaphore/kysely-semaphore-adapter";
import Sqlite from "better-sqlite3";
import { Kysely, SqliteDialect } from "kysely";
const semaphoreAdapter = new KyselySemaphoreAdapter({
kysely: new Kysely({
dialect: new SqliteDialect({
database: new Sqlite("local.db"),
}),
}),
});
// You need initialize the adapter once before using it.
await semaphoreAdapter.init();
Removes all slots from a semaphore. Used for cleanup or reset operations. Returns expiration info for all removed slots.
The semaphore identifier
Array of expiration info for all removed slots
Removes a specific slot, making that resource unit available again. Returns the slot's expiration info. Returns null if slot doesn't exist (idempotent - safe to call multiple times).
The semaphore identifier
The slot to remove
Expiration info of the removed slot, or null if not found
Executes a function within a database transaction. Ensures all operations in the function are atomic and isolated.
Implementations must use the strictest transaction isolation level available (SERIALIZABLE for SQL, highest for document databases) to prevent race conditions.
Async function receiving transaction object, returns value of any type
The value returned by the function
Updates a slot's expiration time if it's currently valid (not expired). Useful for extending a held slot's lifetime (e.g., heartbeat/keep-alive).
Conditions for success:
The semaphore identifier
The slot to update
New expiration time (or null for persistent)
Number of rows affected (1 if updated, 0 if slot not found or expired)
To utilize the
KyselySemaphoreAdapter, you must install the"kysely"package and configure aKyselyclass instance.Note in order to use
KyselySemaphoreAdaptercorrectly, 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/semaphore/kysely-semaphore-adapter"