/** * @module Cache */ import { type ObjectId, type CollectionOptions, type Db } from "mongodb"; import { type ICacheAdapter } from "../../../../cache/contracts/_module.js"; import { type IReadableContext } from "../../../../execution-context/contracts/_module.js"; import { type ISerde } from "../../../../serde/contracts/_module.js"; import { type TimeSpan } from "../../../../time-span/implementations/_module.js"; import { type IDeinitizable, type IInitizable } from "../../../../utilities/_module.js"; /** * Configuration for `MongodbCacheAdapter`. * Requires a MongoDB `Db` instance and a serde for serialising cache values to strings. * * IMPORT_PATH: `"@daiso-tech/core/cache/mongodb-cache-adapter"` * @group Adapters */ export type MongodbCacheAdapterSettings = { /** * The MongoDB `Db` instance to store cache entries in. */ database: Db; /** * Serde instance for serializing and deserializing cache values to and from strings. */ serde: ISerde; /** * Name of the MongoDB collection used to store cache entries. * @default "cache" */ collectionName?: string; /** * Additional options passed when creating or accessing the MongoDB collection. */ collectionSettings?: CollectionOptions; }; /** * IMPORT_PATH: `"@daiso-tech/core/cache/mongodb-cache-adapter"` * @group Adapters */ export type MongodbCacheDocument = { _id: ObjectId; key: string; value: number | string; expiration: Date | null; }; /** * To utilize the `MongodbCacheAdapter`, you must install the [`"mongodb"`](https://www.npmjs.com/package/mongodb) package and supply a {@link ISerde | `ISerde`}, with an adapter like {@link SuperJsonSerdeAdapter | `SuperJsonSerdeAdapter`}. * * IMPORT_PATH: `"@daiso-tech/core/cache/mongodb-cache-adapter"` * @group Adapters */ export declare class MongodbCacheAdapter implements ICacheAdapter, IInitizable, IDeinitizable { private static filterUnexpiredKeys; private static isMongodbIncrementError; private readonly serde; private readonly collection; /** * @example * ```ts * import { MongodbCacheAdapter } from "@daiso-tech/core/cache/mongodb-cache-adapter"; * import { Serde } from "@daiso-tech/core/serde"; * import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/super-json-serde-adapter" * import { MongoClient } from "mongodb"; * * const client = await MongoClient.connect("YOUR_MONGODB_CONNECTION_STRING"); * const database = client.db("database"); * const serde = new Serde(new SuperJsonSerdeAdapter()); * const cacheAdapter = new MongodbCacheAdapter({ * database, * serde, * }); * // You need initialize the adapter once before using it. * await cacheAdapter.init(); * ``` */ constructor(settings: MongodbCacheAdapterSettings); getOrAdd(key: string, valueToAdd: TType, ttl: TimeSpan | null, _context: IReadableContext): Promise; /** * Creates all related indexes. * Note the `init` method needs to be called once before using the adapter. */ init(): Promise; /** * Removes the collection where the cache values are stored and all it's related indexes. * Note all cache data will be removed. */ deInit(): Promise; private getDocValue; get(key: string, _context: IReadableContext): Promise; getAndRemove(key: string, _context: IReadableContext): Promise; private isDocExpired; add(key: string, value: TType, ttl: TimeSpan | null, _context: IReadableContext): Promise; put(key: string, value: TType, ttl: TimeSpan | null, _context: IReadableContext): Promise; update(key: string, value: TType, _context: IReadableContext): Promise; increment(key: string, value: number, _context: IReadableContext): Promise; removeMany(keys: Array, _context: IReadableContext): Promise; removeAll(): Promise; removeByKeyPrefix(prefix: string, _context: IReadableContext): Promise; }