@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.
76 lines (75 loc) • 2.54 kB
TypeScript
/**
* @module Lock
*/
import { type IDeinitizable, type IInitizable } from "../../../../utilities/_module-exports.js";
import type { IDatabaseLockAdapter, ILockData } from "../../../../lock/contracts/_module-exports.js";
import type { CollectionOptions, Db } from "mongodb";
import { ObjectId } from "mongodb";
/**
*
* IMPORT_PATH: `"@daiso-tech/core/lock/adapters"`
* @group Adapters
*/
export type MongodbLockAdapterSettings = {
database: Db;
/**
* @default "lock"
*/
collectionName?: string;
collectionSettings?: CollectionOptions;
};
/**
*
* IMPORT_PATH: `"@daiso-tech/core/lock/adapters"`
* @group Adapters
*/
export type MongodbLockDocument = {
_id: ObjectId;
key: string;
owner: string;
expiresAt: Date | null;
};
/**
* To utilize the `MongodbLockAdapter`, you must install the [`"mongodb"`](https://www.npmjs.com/package/mongodb) package.
*
* Note in order to use `MongodbLockAdapter` correctly, ensure you use a single, consistent database across all server instances.
*
* IMPORT_PATH: `"@daiso-tech/core/lock/adapters"`
* @group Adapters
*/
export declare class MongodbLockAdapter implements IDatabaseLockAdapter, IDeinitizable, IInitizable {
private readonly database;
private readonly collection;
private readonly collectionName;
/**
* @example
* ```ts
* import { MongodbLockAdapter } from "@daiso-tech/core/lock/adapters";
* import { MongoClient } from "mongodb";
*
* const client = await MongoClient.connect("YOUR_MONGODB_CONNECTION_STRING");
* const database = client.db("database");
* const lockAdapter = new MongodbLockAdapter({
* database
* });
* // You need initialize the adapter once before using it.
* await lockAdapter.init()
* ```
*/
constructor(settings: MongodbLockAdapterSettings);
/**
* Creates all related indexes.
* Note the `init` method needs to be called before using the adapter.
*/
init(): Promise<void>;
/**
* Removes the collection where the lock keys are stored and all it's related indexes.
* Note all lock data will be removed.
*/
deInit(): Promise<void>;
insert(key: string, owner: string, expiration: Date | null): Promise<void>;
update(key: string, owner: string, expiration: Date | null): Promise<number>;
remove(key: string, owner: string | null): Promise<void>;
refresh(key: string, owner: string, expiration: Date): Promise<number>;
find(key: string): Promise<ILockData | null>;
}