/** * Centralized MongoDB Wrapper — Native Driver, Singleton Pool * * ALL database access MUST go through this file. * NEVER create MongoClient instances in other files. * NEVER use mongoose or ODMs — native driver only. * * Based on production patterns from Claude Code Mastery Guides. * * Best practices enforced: * - Singleton pool per URI (prevents connection exhaustion) * - Aggregation framework for all reads (consistent, flexible) * - BulkWrite for all writes (atomic, performant) * - $limit BEFORE $lookup (critical for join performance) * - $inc for counters (no read-modify-write races) * - Smart NoSQL injection sanitization (allows safe operators, blocks $where/$function) * - Graceful shutdown with closePool() * - Next.js hot-reload persistence via globalThis * * Install: npm install mongodb */ import { type AnyBulkWriteOperation, type ClientSession, type Collection, type Document, type Filter, type TransactionOptions, type UpdateFilter } from 'mongodb'; export { connect, closePool, getDb, getCollection, gracefulShutdown } from './mongo-connection.js'; export type { ConnectOptions } from './mongo-connection.js'; export { configureSanitization, sanitizeFilter } from './mongo-sanitize.js'; export { registerIndex, ensureIndexes } from './mongo-indexes.js'; export type { IndexDefinition } from './mongo-indexes.js'; /** * Find a single document by filter. * Uses aggregation with automatic $limit: 1. * * Pass `{ trusted: true }` when the match filter is server-constructed * and uses MongoDB operators ($gte, $in, $regex, etc.). */ export declare function queryOne(collection: string, match: Filter, options?: { trusted?: boolean; }): Promise; /** * Find multiple documents using an aggregation pipeline. * Always use this over .find() for consistency. * * Pass `{ trusted: true }` when the pipeline is server-constructed * and uses MongoDB operators ($gte, $in, $regex, etc.) in $match stages. */ export declare function queryMany(collection: string, pipeline: Document[], options?: { trusted?: boolean; }): Promise; /** * Find a single document with a $lookup join. * Enforces $limit BEFORE $lookup for performance. */ export declare function queryWithLookup(collection: string, options: { match: Filter; lookup: { from: string; localField: string; foreignField: string; as: string; }; unwind?: string; postStages?: Document[]; }): Promise; /** * Count documents in a collection. * Uses aggregation $count for consistency. * * Pass `{ trusted: true }` when the match filter is server-constructed * and uses MongoDB operators ($gte, $in, $regex, etc.). */ export declare function count(collection: string, match?: Filter, options?: { trusted?: boolean; }): Promise; /** * Insert a single document. * Wraps in bulkWrite for consistency. */ export interface MongoWriteResult { insertedId?: string; insertedIds?: string[]; upsertedId?: string; matchedCount?: number; modifiedCount?: number; } export declare function insertOne(collection: string, doc: T): Promise; /** * Insert multiple documents in a single batch. * NEVER use insertOne in a loop — always batch with this. */ export declare function insertMany(collection: string, docs: T[]): Promise; /** * Update a single document. * Use $inc for counters, $set for fields — never read-modify-write. */ export declare function updateOne(collection: string, filter: Filter, update: UpdateFilter, upsert?: boolean): Promise; /** * Update multiple documents matching a filter. */ export declare function updateMany(collection: string, filter: Filter, update: UpdateFilter): Promise; /** * Execute arbitrary bulk operations. * Use for complex multi-operation writes. * Includes automatic retry for E11000 concurrent upsert races. */ export declare function bulkOps(collection: string, operations: AnyBulkWriteOperation[]): Promise; /** * Delete a single document. */ export declare function deleteOne(collection: string, filter: Filter): Promise; /** * Delete multiple documents matching a filter. */ export declare function deleteMany(collection: string, filter: Filter): Promise; /** * Execute an operation within a MongoDB transaction. * Use for multi-document atomic operations. */ export declare function withTransaction(operation: (session: ClientSession) => Promise, txOptions?: TransactionOptions): Promise; /** * Get raw access to a MongoDB Collection. * Use for Change Streams or operations not covered by the wrapper. */ export declare function rawCollection(name: string): Promise>; //# sourceMappingURL=mongo.d.ts.map