Guides
Transactions
All or Nothing
Mongster wraps MongoDB transactions with automatic commit and rollback handling. The recommended flow is to use transaction-scoped models through ctx.use(Model).
[!IMPORTANT] Transactions require a replica set or compatible sharded deployment.
Recommended: ctx.use(Model)
import { } from "mongster";
import { , } from "./models";
await .(async () => {
const = .();
const = .();
const = await .({ : "alice@example.com" });
if (!) throw new ("Missing source account");
await .({ : . }, { : { : -50 } });
await .({
: .,
: 50,
: "debit",
});
});Manual session management
import { } from "mongster";
const = await .();
try {
await .(async () => {
// pass { session } into model calls here when you want full manual control
});
} finally {
await .();
}When to use which style
- Use
mongster.transaction()for most application code. - Use manual sessions when you need lower-level orchestration around the MongoDB driver.
- Keep all related writes inside the same callback so the transaction boundary stays obvious.