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.

106 lines (105 loc) 4.36 kB
/** * @module CircuitBreaker */ import { type ClientSession, type CollectionOptions, type Db, type MongoClient, type ObjectId } from "mongodb"; import { type ICircuitBreakerStorageAdapter, type ICircuitBreakerStorageAdapterTransaction } from "../../../../circuit-breaker/contracts/_module.js"; import { type IReadableContext } from "../../../../execution-context/contracts/_module.js"; import { type ISerde } from "../../../../serde/contracts/_module.js"; import { type IDeinitizable, type IInitizable, type InvokableFn } from "../../../../utilities/_module.js"; /** * IMPORT_PATH: `"@daiso-tech/core/circuit-breaker/mongodb-circuit-breaker-storage-adapter"` * @group Adapters */ export type MongodbCircuitBreakerStorageDocument = { _id: ObjectId; key: string; state: string; }; /** * Configuration for `MongodbCircuitBreakerStorageAdapter`. * Requires a MongoDB `Db` instance. * * IMPORT_PATH: `"@daiso-tech/core/circuit-breaker/mongodb-circuit-breaker-storage-adapter"` * @group Adapters */ export type MongodbCircuitBreakerStorageAdapterSettings = { /** * The MongoDB `MongoClient` instance, required for transaction support. */ client: MongoClient; /** * The MongoDB `Db` instance to store circuit-breaker state in. */ database: Db; /** * Name of the MongoDB collection used to store circuit-breaker state records. * @default "circuitBreaker" */ collectionName?: string; /** * Additional options passed when creating or accessing the MongoDB collection. */ collectionSettings?: CollectionOptions; /** * Serde instance for serializing and deserializing circuit-breaker state to and from strings. */ serde: ISerde<string>; /** * When `true`, operations are wrapped in MongoDB transactions for atomicity. * Requires a Replica Set or sharded cluster that supports transactions. * @default true */ enableTransactions?: boolean; }; /** * To utilize the `MongodbCircuitBreakerStorageAdapter`, you must install the [`"mongodb"`](https://www.npmjs.com/package/mongodb) package. * * Note in order to use `MongodbCircuitBreakerStorageAdapter` correctly, you need to use a database that has support for transactions. * * IMPORT_PATH: `"@daiso-tech/core/circuit-breaker/mongodb-circuit-breaker-storage-adapter"` * @group Adapters */ export declare class MongodbCircuitBreakerStorageAdapter<TType = unknown> implements ICircuitBreakerStorageAdapter<TType>, IInitizable, IDeinitizable { private readonly collection; private readonly client; private readonly serde; private readonly enableTransactions; /** * @example * ```ts * import { MongodbCircuitBreakerStorageAdapter } from "@daiso-tech/core/circuit-breaker/mongodb-circuit-breaker-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 circuitBreakerStorageAdapter = new MongodbCircuitBreakerStorageAdapter({ * client, * database, * serde * }); * // You need initialize the adapter once before using it. * await circuitBreakerStorageAdapter.init() * ``` */ constructor(settings: MongodbCircuitBreakerStorageAdapterSettings); /** * Removes the collection where the circuit breaker keys are stored and all it's related indexes. * Note all circuit breaker data will be removed. */ deInit(): Promise<void>; /** * Creates all related indexes. * Note the `init` method needs to be called once before using the adapter. */ init(): Promise<void>; private upsert; private _transaction; transaction<TValue>(_context: IReadableContext, fn: InvokableFn<[ transaction: ICircuitBreakerStorageAdapterTransaction<TType> ], Promise<TValue>>): Promise<TValue>; find(_context: IReadableContext, key: string, session?: ClientSession): Promise<TType | null>; remove(_context: IReadableContext, key: string, session?: ClientSession): Promise<void>; }