/** * @module RateLimiter */ import { type ClientSession, type CollectionOptions, type Db, type MongoClient, type ObjectId } from "mongodb"; import { type IReadableContext } from "../../../../execution-context/contracts/_module.js"; import { type IRateLimiterData, type IRateLimiterStorageAdapter, type IRateLimiterStorageAdapterTransaction } from "../../../../rate-limiter/contracts/_module.js"; import { type ISerde } from "../../../../serde/contracts/_module.js"; import { type IDeinitizable, type IInitizable, type InvocableFn } from "../../../../utilities/_module.js"; /** * Configuration for `MongodbRateLimiterStorageAdapter`. * Requires a MongoDB `Db` instance. * * IMPORT_PATH: `"@daiso-tech/core/rate-limiter/mongodb-rate-limiter-storage-adapter"` * @group Adapters */ export type MongodbRateLimiterStorageAdapterSettings = { /** * The MongoDB `MongoClient` instance, required for transaction support. */ client: MongoClient; /** * The MongoDB `Db` instance to store rate-limiter state in. */ database: Db; /** * Name of the MongoDB collection used to store rate-limiter state records. * @default "rateLimiter" */ collectionName?: string; /** * Additional options passed when creating or accessing the MongoDB collection. */ collectionSettings?: CollectionOptions; /** * Serde instance for serializing and deserializing rate-limiter state to and from strings. */ serde: ISerde; }; /** * IMPORT_PATH: `"@daiso-tech/core/rate-limiter/mongodb-rate-limiter-storage-adapter"` * @group Adapters */ export type MongodbRateLimiterDocument = { _id: ObjectId; key: string; state: string; expiration: Date; }; /** * IMPORT_PATH: `"@daiso-tech/core/rate-limiter/mongodb-rate-limiter-storage-adapter"` * @group Adapters */ export declare class MongodbRateLimiterStorageAdapter implements IRateLimiterStorageAdapter, IInitizable, IDeinitizable { private readonly client; private readonly collection; private readonly serde; /** * @example * ```ts * import { MongodbRateLimiterStorageAdapter } from "@daiso-tech/core/rate-limiter/mongodb-rate-limiter-storage-adapter"; * import { MongoClient } from "mongodb"; * import { Serde } from "@daiso-tech/core/serde"; * import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/super-json-serde-adapter" * * const client = await MongoClient.connect("YOUR_MONGODB_CONNECTION_STRING"); * const database = client.db("database"); * const serde = new Serde(new SuperJsonSerdeAdapter()); * const rateLimiterStorageAdapter = new MongodbRateLimiterStorageAdapter({ * client, * database, * serde * }); * // You need initialize the adapter once before using it. * await rateLimiterStorageAdapter.init() * ``` */ constructor(settings: MongodbRateLimiterStorageAdapterSettings); /** * Removes the collection where the rate limiter keys are stored and all it's related indexes. * Note all rate limiter data will be removed. */ deInit(): Promise; /** * Creates all related indexes. * Note the `init` method needs to be called once before using the adapter. */ init(): Promise; private upsert; private _transaction; transaction(fn: InvocableFn<[ transaction: IRateLimiterStorageAdapterTransaction ], Promise>, _context: IReadableContext): Promise; find(key: string, _context: IReadableContext, session?: ClientSession): Promise | null>; remove(key: string, _context: IReadableContext, session?: ClientSession): Promise; }