UNPKG

@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.

83 lines (82 loc) 3.1 kB
/** * @module Lock */ import { type CollectionOptions, type Db, type ObjectId } from "mongodb"; import { type IReadableContext } from "../../../../execution-context/contracts/_module.js"; import { type ILockAdapter, type ILockAdapterState } from "../../../../lock/contracts/_module.js"; import { type TimeSpan } from "../../../../time-span/implementations/_module.js"; import { type IDeinitizable, type IInitizable } from "../../../../utilities/_module.js"; /** * Configuration for `MongodbLockAdapter`. * Requires a MongoDB `Db` instance. * * IMPORT_PATH: `"@daiso-tech/core/lock/mongodb-lock-adapter"` * @group Adapters */ export type MongodbLockAdapterSettings = { /** * The MongoDB `Db` instance to store lock state in. */ database: Db; /** * Name of the MongoDB collection used to store lock records. * @default "lock" */ collectionName?: string; /** * Additional options passed when creating or accessing the MongoDB collection. */ collectionSettings?: CollectionOptions; }; /** * IMPORT_PATH: `"@daiso-tech/core/lock/mongodb-lock-adapter"` * @group Adapters */ export type MongodbLockDocument = { _id: ObjectId; key: string; owner: string; expiration: Date | null; }; /** * To utilize the `MongodbLockAdapter`, you must install the [`"mongodb"`](https://www.npmjs.com/package/mongodb) package. * * Note in order to use `MongodbLockAdapter` correctly, ensure you use a single, consistent database across all server instances. * * IMPORT_PATH: `"@daiso-tech/core/lock/mongodb-lock-adapter"` * @group Adapters */ export declare class MongodbLockAdapter implements ILockAdapter, IDeinitizable, IInitizable { private readonly collection; /** * @example * ```ts * import { MongodbLockAdapter } from "@daiso-tech/core/lock/mongodb-lock-adapter"; * import { MongoClient } from "mongodb"; * * const client = await MongoClient.connect("YOUR_MONGODB_CONNECTION_STRING"); * const database = client.db("database"); * const lockAdapter = new MongodbLockAdapter({ * database * }); * // You need initialize the adapter once before using it. * await lockAdapter.init() * ``` */ constructor(settings: MongodbLockAdapterSettings); /** * 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 lock keys are stored and all it's related indexes. * Note all lock data will be removed. */ deInit(): Promise<void>; acquire(_context: IReadableContext, key: string, lockId: string, ttl: TimeSpan | null): Promise<boolean>; release(_context: IReadableContext, key: string, lockId: string): Promise<boolean>; forceRelease(_context: IReadableContext, key: string): Promise<boolean>; refresh(_context: IReadableContext, key: string, lockId: string, ttl: TimeSpan): Promise<boolean>; getState(_context: IReadableContext, key: string): Promise<ILockAdapterState | null>; }