@tmlmobilidade/connectors
Version:
This package provides pre-made database connectors to streamline development and reduce boilerplate. By using these connectors, you can avoid re-implementing controller classes every time, ensuring consistency and saving development time.
40 lines (39 loc) • 1.52 kB
TypeScript
import { type Collection, type Db, type DbOptions, type Document, MongoClient, type MongoClientOptions } from 'mongodb';
export declare class MongoConnector {
/**
* Get the MongoClient instance.
*/
get client(): MongoClient;
private _client;
constructor(uri: string, options?: MongoClientOptions);
/**
* Aggregates data from a collection.
*/
aggregate<T extends Document>(collection: Collection<T>, pipeline: Document[]): Promise<Document[]>;
/**
* Connect to MongoDB and return the database instance.
*/
connect(): Promise<MongoClient>;
/**
* Create a new Db instance sharing the current socket connections.
*
* @param dbName - The name of the database we want to use. If not provided, use database name from connection string.
* @param options - Optional settings for Db construction
*/
db(dbName?: string, options?: DbOptions): Db;
/**
* Close the MongoDB connection.
*/
disconnect(): Promise<void>;
/**
* Get a specific collection by name.
* @param db - The database instance.
* @param collectionName - The name of the collection to retrieve.
* @returns The collection instance.
*/
getCollection<T extends Document>(db: Db, collectionName: string): Promise<Collection<T>>;
/**
* Watches a collection for changes.
*/
watch<T extends Document>(collection: Collection<T>): Promise<import("mongodb").ChangeStream<T, import("mongodb").ChangeStreamDocument<T>>>;
}