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.

74 lines (73 loc) 2.56 kB
/** * @module Lock */ import { type CollectionOptions, type Db, type ObjectId } from "mongodb"; 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"; /** * * IMPORT_PATH: `"@daiso-tech/core/lock/mongodb-lock-adapter"` * @group Adapters */ export type MongodbLockAdapterSettings = { database: Db; /** * @default "lock" */ collectionName?: string; 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(key: string, lockId: string, ttl: TimeSpan | null): Promise<boolean>; release(key: string, lockId: string): Promise<boolean>; forceRelease(key: string): Promise<boolean>; refresh(key: string, lockId: string, ttl: TimeSpan): Promise<boolean>; getState(key: string): Promise<ILockAdapterState | null>; }