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.

81 lines (80 loc) 3.28 kB
/** * @module CircuitBreaker */ import { type CollectionOptions, type Db, type MongoClient, type ObjectId } from "mongodb"; import { type ICircuitBreakerStorageAdapter, type ICircuitBreakerStorageAdapterTransaction } from "../../../../circuit-breaker/contracts/circuit-breaker-storage-adapter.contract.js"; import { type ISerde } from "../../../../serde/contracts/serde.contract.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; }; /** * IMPORT_PATH: `"@daiso-tech/core/circuit-breaker/mongodb-circuit-breaker-storage-adapter"` * @group Adapters */ export type MongodbCircuitBreakerStorageAdapterSettings = { client: MongoClient; database: Db; /** * @default "circuitBreaker" */ collectionName?: string; collectionSettings?: CollectionOptions; serde: ISerde<string>; }; /** * 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; /** * @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; transaction<TValue>(fn: InvokableFn<[ transaction: ICircuitBreakerStorageAdapterTransaction<TType> ], Promise<TValue>>): Promise<TValue>; find(key: string): Promise<TType | null>; remove(key: string): Promise<void>; }