@thalorlabs/database
Version:
Database utilities and connection helpers for TypeScript applications with MongoDB support
32 lines (31 loc) • 1.07 kB
TypeScript
import { Mongoose, ConnectOptions } from 'mongoose';
/**
* Configuration options for MongoDB connection.
*/
export interface MongoConnectOptions extends ConnectOptions {
/** Whether to cache the connection for reuse (default: true) */
useCache?: boolean;
}
/**
* Establishes a MongoDB connection with optional caching.
* Returns the existing connection if caching is enabled and already connected.
*
* @param uri - MongoDB connection URI
* @param options - MongoDB connection options including caching behavior
* @returns Promise resolving to Mongoose instance
* @throws Error if connection fails
*
* @example
* const connection = await mongoDBConnect(
* 'mongodb://localhost:27017/myapp',
* { maxPoolSize: 10, useCache: true }
* );
*
* @example
* const connection = await mongoDBConnect(process.env.MONGO_URI!);
*
* @example
* // Force new connection every time
* const connection = await mongoDBConnect(uri, { useCache: false });
*/
export declare function mongoDBConnect(uri: string, options?: MongoConnectOptions): Promise<Mongoose>;