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.
MongoTransaction gives you multi-document, multi-collection ACID transactions with a simple callback API. Pass an async function to MongoTransaction.run(), perform any number of repository operations inside it using the provided transaction context, and the library handles commit, rollback, and session cleanup for you.
MongoTransaction.run(callback)
The sole entry-point for starting a transaction. It:
- Obtains a
MongoClientviaMongoClientFactory.createClient(). - Opens a new
ClientSessionwithclient.startSession(). - Wraps the callback in
session.withTransaction(), which handles retrying on transient errors and committing on success. - Calls
session.endSession()in afinallyblock regardless of outcome. - Returns the value resolved by
callback, or rethrows any error the callback raises.
An async function that receives the opaque
MongoTransaction context. Pass this context to every repository method that should participate in the transaction. The callback’s return value becomes the return value of run().Promise<T> — whatever value callback resolves to.
On success — session.withTransaction commits the transaction and endSession releases the server session back to the pool.
On error — any exception thrown inside callback causes session.withTransaction to abort the transaction. The exception is rethrown from run() so the caller can handle it.
MongoTransaction.sessionFor(transaction?) @internal
Used internally by every MongoRepository method that accepts a transaction? parameter. It extracts the driver ClientSession stored in the MongoTransaction instance (via a WeakMap) and forwards it to the native driver call.
Signature
undefined when transaction is undefined, which causes repository methods to run outside any session (auto-commit mode).
You will not normally call sessionFor yourself — it is exposed as public so that custom repository implementations and third-party extensions can forward sessions to driver operations they build directly.
Transactional repository methods
All methods onMongoRepository that mutate or read data accept an optional transaction parameter. When a MongoTransaction is passed the underlying driver call is bound to that session:
| Method | Driver operation wrapped |
|---|---|
one(filter, transaction?) | collection.findOne(…, { session }) |
many(filter, transaction?) | collection.find(…, { session }) |
list(criteria, fields?, transaction?) | collection.find(…, { session }) + countDocuments(…, { session }) |
upsert(entity, transaction?) | collection.updateOne(…, { upsert: true, session }) |
delete(filter, options?, transaction?) | collection.deleteMany(…, { session }) |
updateOne(filter, update, transaction?) (protected) | collection.updateOne(…, { upsert: true, session }) |
Full example
The example below creates a new order and decrements product inventory as a single atomic operation. If either operation fails, neither change is persisted.Nesting restriction
Do not callMongoTransaction.run() inside another MongoTransaction.run() callback. MongoDB does not support nested transactions — the inner run() would call client.startSession() a second time, creating an independent session rather than inheriting the outer transaction:
Error propagation
Any exception thrown inside therun() callback — whether from your application code or from a repository method — immediately aborts the transaction:
finally block, so no resources are leaked even when an error is thrown.