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.

131 lines 4.52 kB
/** * @module CircuitBreaker */ import {} from "mongodb"; import {} from "../../../../circuit-breaker/contracts/_module.js"; import {} from "../../../../execution-context/contracts/_module.js"; import {} from "../../../../serde/contracts/_module.js"; import {} from "../../../../utilities/_module.js"; /** * 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 class MongodbCircuitBreakerStorageAdapter { collection; client; serde; 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) { const { client, collectionName = "circuitBreaker", collectionSettings, database, serde, enableTransactions = true, } = settings; this.client = client; this.collection = database.collection(collectionName, collectionSettings); this.serde = serde; this.enableTransactions = enableTransactions; } /** * Removes the collection where the circuit breaker keys are stored and all it's related indexes. * Note all circuit breaker data will be removed. */ async deInit() { // Should throw if the collection already does not exists thats why the try catch is used. try { await this.collection.dropIndexes(); } catch { /* EMPTY */ } // Should throw if the collection already does not exists thats why the try catch is used. try { await this.collection.drop(); } catch { /* EMPTY */ } } /** * Creates all related indexes. * Note the `init` method needs to be called once before using the adapter. */ async init() { // Should throw if the index already exists thats why the try catch is used. try { await this.collection.createIndex({ key: 1, }, { unique: true, }); } catch { /* EMPTY */ } } async upsert(_context, key, state, session) { await this.collection.updateOne({ key, }, { $set: { state: this.serde.serialize(state), }, }, { upsert: true, session, }); } async _transaction(trxFn) { if (this.enableTransactions) { return await this.client.withSession(async (session) => { return await session.withTransaction(async () => { return await trxFn(session); }); }); } return trxFn(); } async transaction(_context, fn) { return await this._transaction(async (session) => { return await fn({ upsert: (context, key, state) => this.upsert(context, key, state, session), find: (context, key) => this.find(context, key, session), }); }); } async find(_context, key, session) { const doc = await this.collection.findOne({ key }, { session, }); if (doc === null) { return null; } return this.serde.deserialize(doc.state); } async remove(_context, key, session) { await this.collection.deleteOne({ key, }, { session }); } } //# sourceMappingURL=mongodb-circuit-breaker-storage-adapter.js.map