@sigiljs-community/mongo-plugin
Version:
Plugin for SigilJS framework that provides MongoDB interactions
79 lines (78 loc) • 2.72 kB
TypeScript
import { Db, MongoClient } from 'mongodb';
export declare enum ConnectionStatus {
/** The controller is currently connected to the database */
Connected = 0,
/** The controller is currently disconnected from the database */
Disconnected = 1
}
interface ILogOptions {
/** Log level (severity) */
level: "warning" | "error" | "info" | "success";
/** Message content, or function that formats the message */
message: string[] | string | ((dim: (payload: string) => string) => string);
/** Optional JSON payload to include in the log */
json?: any;
/** Optional condition; if false, skip logging */
condition?: any;
}
/**
* Controller for managing MongoDB connections and operations.
*/
export default class MongoController {
private readonly client;
private readonly _dbName;
private readonly log;
private _database?;
/**
* Tracks the current connection status.
* @default ConnectionStatus.Disconnected
* @private
*/
private _connectionStatus;
constructor(client: MongoClient, _dbName: string, log: (opts: ILogOptions) => void);
/**
* Gets the current connection status.
*/
get connectionStatus(): ConnectionStatus;
/**
* Gets the name of the configured database.
*/
get databaseName(): string;
/**
* Gets the current database instance, if connected.
*/
get db(): Db | undefined;
/**
* Gets the current database instance, cast as non-undefined.
* Use with caution; throws if not connected.
*/
get unsafeDB(): Db;
/**
* Ensures that there is an active connection to the database.
* @throws Error if not connected
*/
checkConnection(): void;
/**
* Disconnects from the MongoDB server.
* If already disconnected, does nothing.
* @returns promise that resolves to true if disconnection succeeded or was unnecessary,
* or false if an error occurred during client.close()
*/
disconnect(): Promise<boolean>;
/**
* Connects (or reconnects) to the MongoDB server and selects the database.
* If already connected, does nothing.
* @returns promise that resolves to true if connection succeeded or was unnecessary,
* or false if an error occurred during client.connect()
*/
reconnect(): Promise<boolean>;
/**
* Retrieves a collection from the connected database.
* @typeParam T shape of the documents in the collection
* @param name name of the collection to access
* @returns MongoDB Collection instance
* @throws Error if not connected
*/
collection<T extends object = any>(name: string): import('mongodb').Collection<T>;
}
export {};