@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.
115 lines • 3.88 kB
JavaScript
/**
* @module CircuitBreaker
*/
import {} from "mongodb";
import {} from "../../../../circuit-breaker/contracts/circuit-breaker-storage-adapter.contract.js";
import {} from "../../../../serde/contracts/serde.contract.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;
/**
* @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, } = settings;
this.client = client;
this.collection = database.collection(collectionName, collectionSettings);
this.serde = serde;
}
/**
* 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(key, state) {
await this.collection.updateOne({
key,
}, {
$set: {
state: this.serde.serialize(state),
},
}, {
upsert: true,
});
}
async transaction(fn) {
return await this.client.startSession().withTransaction(async () => {
return await fn({
upsert: (key, state) => this.upsert(key, state),
find: (key) => this.find(key),
});
});
}
async find(key) {
const doc = await this.collection.findOne({ key });
if (doc === null) {
return null;
}
return this.serde.deserialize(doc.state);
}
async remove(key) {
await this.collection.deleteOne({
key,
});
}
}
//# sourceMappingURL=mongodb-circuit-breaker-storage-adapter.js.map