import { MongodbSemaphoreAdapter } from "@daiso-tech/core/semaphore/mongodb-semaphore-adapter";
import { MongoClient } from "mongodb";
const client = await MongoClient.connect("YOUR_MONGODB_CONNECTION_STRING");
const database = client.db("database");
const semaphoreAdapter = new MongodbSemaphoreAdapter({
database
});
// You need initialize the adapter once before using it.
await semaphoreAdapter.init()
The acquire method acquires a slot only if the slot limit is not reached.
Returns true if the slot limit is not reached otherwise false is returned.
The getState method returns the state of the semaphore.
Returns ISemaphoreAdapterState if the semaphore exists in the database or null if doesnt exists.
To utilize the
MongodbSemaphoreAdapter, you must install the"mongodb"package.Note in order to use
MongodbSemaphoreAdaptercorrectly, ensure you use a single, consistent database across all server instances.IMPORT_PATH:
"@daiso-tech/core/semaphore/mongodb-semaphore-adapter"