@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.
99 lines (98 loc) • 4.04 kB
TypeScript
/**
* @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<string>;
/**
* 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<TType = unknown> implements ICacheAdapter<TType>, 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);
/**
* Creates all related indexes.
* Note the `init` method needs to be called once before using the adapter.
*/
init(): Promise<void>;
/**
* Removes the collection where the cache values are stored and all it's related indexes.
* Note all cache data will be removed.
*/
deInit(): Promise<void>;
private getDocValue;
get(_context: IReadableContext, key: string): Promise<TType | null>;
getAndRemove(_context: IReadableContext, key: string): Promise<TType | null>;
private isDocExpired;
add(_context: IReadableContext, key: string, value: TType, ttl: TimeSpan | null): Promise<boolean>;
put(_context: IReadableContext, key: string, value: TType, ttl: TimeSpan | null): Promise<boolean>;
update(_context: IReadableContext, key: string, value: TType): Promise<boolean>;
increment(_context: IReadableContext, key: string, value: number): Promise<boolean>;
removeMany(_context: IReadableContext, keys: Array<string>): Promise<boolean>;
removeAll(): Promise<void>;
removeByKeyPrefix(_context: IReadableContext, prefix: string): Promise<void>;
}