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.

126 lines 3.96 kB
/** * @module RateLimiter */ import {} from "mongodb"; import {} from "../../../../rate-limiter/contracts/_module.js"; import {} from "../../../../serde/contracts/serde.contract.js"; import {} from "../../../../utilities/_module.js"; /** * IMPORT_PATH: `"@daiso-tech/core/rate-limiter/mongodb-rate-limiter-storage-adapter"` * @group Adapters */ export class MongodbRateLimiterStorageAdapter { client; collection; 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) { const { client, collectionName = "rateLimiter", collectionSettings, database, serde, } = settings; this.client = client; this.collection = database.collection(collectionName, collectionSettings); this.serde = serde; } /** * Removes the collection where the rate limiter keys are stored and all it's related indexes. * Note all rate limiter 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 */ } // Should throw if the index already exists thats why the try catch is used. try { await this.collection.createIndex("expiration", { expireAfterSeconds: 0, }); } catch { /* EMPTY */ } } async upsert(key, state, expiration) { await this.collection.updateOne({ key, }, { $set: { state: this.serde.serialize(state), expiration, }, }, { upsert: true, }); } async transaction(fn) { return await this.client.startSession().withTransaction(async () => { return await fn({ upsert: (key, state, exiration) => this.upsert(key, state, exiration), find: (key) => this.find(key), }); }); } async find(key) { const doc = await this.collection.findOne({ key, }); if (doc === null) { return null; } return { state: this.serde.deserialize(doc.state), expiration: doc.expiration, }; } async remove(key) { await this.collection.deleteOne({ key, }); } } //# sourceMappingURL=mongodb-rate-limiter-storage-adapter.js.map