Documentation Index
Fetch the complete documentation index at: https://mintlify.com/abejarano/ts-mongo-criteria/llms.txt
Use this file to discover all available pages before exploring further.
MongoClientFactory is the connection manager for @abejarano/ts-mongodb-criteria. It maintains a single MongoClient instance for the lifetime of the Node.js process, reads the connection string from the MONGO_URI environment variable, and exposes two static lifecycle methods: createClient() to get-or-create the connection, and closeClient() to tear it down gracefully.
You do not normally call MongoClientFactory directly — MongoRepository calls it internally every time it needs the collection. However, it is part of the public API and is useful for application startup checks, graceful shutdown handlers, and testing.
Methods
MongoClientFactory.createClient()
Returns the existing singleton MongoClient, or creates and connects a new one if none exists yet.
Signature
Promise<MongoClient> — the connected driver client.
On the first call the factory:
- Reads
process.env.MONGO_URI. - Creates a
new MongoClient(uri, { ignoreUndefined: true }). - Calls
client.connect(). - Stores the result in the private static
clientfield.
MongoClientFactory.closeClient()
Closes the underlying MongoClient connection and resets the internal singleton to null so that the next createClient() call will establish a fresh connection.
Signature
createClient() was never called, or closeClient() was already called) this method is a no-op.
Environment variable
| Variable | Required | Description |
|---|---|---|
MONGO_URI | ✅ Yes | Full MongoDB connection URI read from process.env at connection time. |
Example .env values
Singleton behavior
CallingcreateClient() multiple times is safe and efficient:
MongoClient instance. You should never need to call createClient() more than once at application startup, but doing so has no penalty.
MongoClient options
The factory hard-codes ignoreUndefined: true when constructing the MongoClient:
undefined fields in documents and query filters rather than serialising them as BSON null. It is the correct default for TypeScript applications that use optional fields heavily. No other driver options are set by the factory; if you need custom TLS certificates, read preferences, or pool sizing, connect your client separately and bypass MongoClientFactory.