@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.
121 lines (120 loc) • 4.8 kB
TypeScript
/**
* @module SharedLock
*/
import { type CollectionOptions, type Db, type ObjectId } from "mongodb";
import { type IReadableContext } from "../../../../execution-context/contracts/_module.js";
import { type ISharedLockAdapter, type ISharedLockAdapterState, type SharedLockAcquireSettings } from "../../../../shared-lock/contracts/_module.js";
import { TimeSpan } from "../../../../time-span/implementations/_module.js";
import { type IDeinitizable, type IInitizable } from "../../../../utilities/_module.js";
/**
* Configuration for `MongodbSharedLockAdapter`.
* Requires a MongoDB `Db` instance.
*
* IMPORT_PATH: `"@daiso-tech/core/shared-lock/mongodb-shared-lock-adapter"`
* @group Adapters
*/
export type MongodbSharedLockAdapterSettings = {
/**
* The MongoDB `Db` instance to store shared-lock state in.
*/
database: Db;
/**
* Name of the MongoDB collection used to store shared-lock records.
* @default "sharedLock"
*/
collectionName?: string;
/**
* Additional options passed when creating or accessing the MongoDB collection.
*/
collectionSettings?: CollectionOptions;
};
/**
* IMPORT_PATH: `"@daiso-tech/core/shared-lock/mongodb-shared-lock"`
* @group Adapters
*/
export type MongodbWriterLockSubDocument = {
owner: string;
expiration: Date | null;
};
/**
* IMPORT_PATH: `"@daiso-tech/core/semaphore/mongodb-shared-lock"`
* @group Adapters
*/
export type MongodbReaderSemaphoreSlotSubDocument = {
id: string;
expiration: Date | null;
};
/**
* IMPORT_PATH: `"@daiso-tech/core/semaphore/mongodb-shared-lock"`
* @group Adapters
*/
export type MongodbReaderSemaphoreDocument = {
limit: number;
slots: Array<MongodbReaderSemaphoreSlotSubDocument>;
};
/**
* IMPORT_PATH: `"@daiso-tech/core/shared-lock/mongodb-shared-lock-adapter"`
* @group Adapters
*/
export type MongodbSharedLockDocument = {
_id: ObjectId;
key: string;
expiration: Date | null;
writer: MongodbWriterLockSubDocument | null;
reader: MongodbReaderSemaphoreDocument | null;
};
/**
* To utilize the `MongodbSharedLockAdapter`, you must install the [`"mongodb"`](https://www.npmjs.com/package/mongodb) package.
*
* Note in order to use `MongodbSharedLockAdapter` correctly, ensure you use a single, consistent database across all server instances.
*
* IMPORT_PATH: `"@daiso-tech/core/shared-lock/mongodb-shared-lock-adapter"`
* @group Adapters
*/
export declare class MongodbSharedLockAdapter implements ISharedLockAdapter, IDeinitizable, IInitizable {
private static isSlotNotExpired;
private readonly collection;
/**
* @example
* ```ts
* import { MongodbSharedLockAdapter } from "@daiso-tech/core/shared-lock/mongodb-shared-lock-adapter";
* import { MongoClient } from "mongodb";
*
* const client = await MongoClient.connect("YOUR_MONGODB_CONNECTION_STRING");
* const database = client.db("database");
* const sharedLockAdapter = new MongodbSharedLockAdapter({
* database
* });
* // You need initialize the adapter once before using it.
* await sharedLockAdapter.init()
* ```
*/
constructor(settings: MongodbSharedLockAdapterSettings);
/**
* Creates all related indexes.
* Note the `init` method needs to be called once before using the adapter.
*/
init(): Promise<void>;
/**
* Removes the collection where the shared-lock keys are stored and all it's related indexes.
* Note all shared-lock data will be removed.
*/
deInit(): Promise<void>;
private updateSemaphoreExpiration;
private removeWriterWhenReaderIsActive;
acquireWriter(_context: IReadableContext, key: string, lockId: string, ttl: TimeSpan | null): Promise<boolean>;
releaseWriter(_context: IReadableContext, key: string, lockId: string): Promise<boolean>;
forceReleaseWriter(_context: IReadableContext, key: string): Promise<boolean>;
refreshWriter(_context: IReadableContext, key: string, lockId: string, ttl: TimeSpan): Promise<boolean>;
private initSemaphoreIfNotExistsStage;
private removeExpiredSlotsStage;
private removeReaderWhenWriterIsActive;
acquireReader(settings: SharedLockAcquireSettings): Promise<boolean>;
releaseReader(_context: IReadableContext, key: string, slotId: string): Promise<boolean>;
forceReleaseAllReaders(_context: IReadableContext, key: string): Promise<boolean>;
refreshReader(_context: IReadableContext, key: string, slotId: string, ttl: TimeSpan): Promise<boolean>;
forceRelease(_context: IReadableContext, key: string): Promise<boolean>;
private static extractWriterState;
private static extractReaderState;
getState(_context: IReadableContext, key: string): Promise<ISharedLockAdapterState | null>;
}