import { type AggregateOptions, type CreateIndexesOptions, type Db, type Document, FindOneOptions, type IndexSpecification, ObjectId } from 'mongodb'; import { type IPagination } from '../../index'; /** * Represents the result of a paginated MongoDB query. * * @typeParam T - The document structure returned by the query. */ interface IRetrievePaginated { /** The documents retrieved for the current page. */ data: T[]; /** Pagination metadata, including page count and total documents. */ pagination: IPagination; } /** * Options for retrieving paginated MongoDB documents. */ export interface IRetrievePaginatedOptions { /** The page number to retrieve. */ page?: number; /** The maximum number of documents per page. */ pageSize?: number; /** * Sort expression using field names. * Prefix a field with `-` for descending order. * * @example * '-createdAt' * 'name,-createdAt' */ sort?: string; /** * Additional aggregation stages appended after pagination. * * Useful for operations such as `$lookup` or `$project` * that should execute after `$skip` and `$limit`. */ afterPagination?: Document[]; /** Additional MongoDB aggregation options. */ aggregateOptions?: AggregateOptions; /** Dot-notation paths of the fields that hold IDs. */ objectIdFields?: readonly string[]; } /** * Provides methods for managing MongoDB collections, indexes, * schema validation, and document retrieval. * * Supports standard queries and aggregation-based pagination. * * @see https://www.mongodb.com/docs/drivers/node/current/fundamentals/indexes/ * @see https://www.mongodb.com/docs/manual/reference/collation/ * @see https://www.mongodb.com/docs/manual/core/index-sparse/ * @see https://www.mongodb.com/docs/manual/core/index-partial/ */ export declare class MongoService { private database; /** * Creates a MongoDB service instance. * * @param db - The MongoDB database instance used by this service. */ constructor(db: Db); /** * Creates a unique index on the specified collection. * * Uses case-insensitive collation to prevent duplicate values * that differ only in letter casing. * * @param collection - The name of the collection to index. * @param spec - The index specification containing the fields and sort order. * * @example * // Create a unique index on the name field. * await mongoDBService.createUnique('users', { * name: 1 * }) * * @example * // Create a compound unique index on firstName and lastName. * await mongoDBService.createUnique('users', { * firstName: 1, * lastName: 1 * }) */ createUnique(collection: string, spec: IndexSpecification): Promise; /** * Creates a unique sparse index on the specified collection. * * Documents that do not contain the indexed field are excluded * from the index. Documents containing the indexed field must * have unique values. * * Uses case-insensitive collation. * * @param collection - The name of the collection to index. * @param spec - The index specification containing the fields and sort order. * * @example * // Allow multiple documents without an email field, * // but enforce uniqueness when email is present. * await mongoDBService.createUniqueIfNotNull('users', { * email: 1 * }) */ createUniqueIfNotNull(collection: string, spec: IndexSpecification): Promise; /** * Creates an index on the specified collection. * * Supports standard MongoDB index options, including unique, * sparse, partialFilterExpression, and expireAfterSeconds. * * @param collection - The name of the collection to index. * @param spec - The index specification containing the fields and sort order. * @param options - Optional MongoDB index creation options. * * @example * // Create a standard index on the createdAt field. * await mongoDBService.createIndex('users', { * createdAt: -1 * }) * * @example * // Create a partial index for active users. * await mongoDBService.createIndex( * 'users', * { email: 1 }, * { * partialFilterExpression: { * status: 'active' * } * } * ) */ createIndex(collection: string, spec: IndexSpecification, options?: CreateIndexesOptions): Promise; /** * Checks whether a collection exists in the current database. * * @param name - The name of the collection to check. * @returns True if the collection exists; otherwise, false. * * @example * const exists = await mongoDBService.isExists('users') */ isExists(name: string): Promise; /** * Updates the JSON Schema validator of an existing MongoDB collection. * * Uses the `collMod` command to modify the collection's validation rules * without dropping or recreating the collection. * * @param name - Name of the collection to update. * @param schema - JSON Schema definition used to validate collection documents. * @throws {Error} If the database connection is not available. */ updateSchema(name: string, schema: unknown): Promise; /** * Retrieves paginated documents from a MongoDB collection using an aggregation pipeline. * * Executes the aggregation pipeline twice: * - The count pipeline calculates the total number of documents matching the pipeline. * - The data pipeline retrieves documents for the requested page, applying sorting, * skipping, and limiting when applicable. * * @typeParam T - The expected document structure of the retrieved data. * * @param collection - The name of the MongoDB collection. * @param pipeline - The aggregation pipeline used to filter or transform documents. * @param options - Pagination, sorting, and aggregation options. * * @returns The retrieved documents and pagination metadata. * * @throws RangeError If the page or page size is invalid. * * @example * // Retrieve the first page of active users, sorted by creation date. * const result = await mongoDBService.retrievePaginated( * 'users', * [ * { * $match: { * status: 'active', * }, * }, * ], * { * page: 1, * pageSize: 10, * sort: '-createdAt', * }, * ); * * @example * // Retrieve users with their associated profiles. * const result = await mongoDBService.retrievePaginated( * 'users', * [ * { * $lookup: { * from: 'profiles', * localField: '_id', * foreignField: 'user_id', * as: 'profile', * }, * }, * ], * { * page: 2, * pageSize: 20, * }, * ); */ retrievePaginated(collection: string, pipeline?: Document[], options?: IRetrievePaginatedOptions): Promise>; /** * Retrieves a single document by its ID. * * Finds and returns the document matching the provided MongoDB `_id`. * Returns `null` if no document is found. * * @typeParam T - The expected document structure of the retrieved data. * * @param collection - The name of the MongoDB collection. * @param _id - The ID of the document to retrieve. * @param options - MongoDB find options. * * @returns The matching document, or `null` if no document is found. * * @example * // Retrieve a user by ID. * const user = await mongoDBService.retrieveOne( * 'users', * userId, * ); * * @example * // Retrieve a user by ID with query options. * const user = await mongoDBService.retrieveOne( * 'users', * userId, * { * projection: { * name: 1, * email: 1, * }, * }, * ); */ retrieveOne(collection: string, _id: string | ObjectId, options?: { findOneOptions?: FindOneOptions; objectIdFields?: readonly string[]; }): Promise; } export {}; //# sourceMappingURL=mongo.service.d.ts.map