advanced
Unit of Work
Collect entity changes and flush them in dependency order while preserving transaction boundaries.
MikroORM Unit of Work tracks new, managed, and removed entities, then `flush()` emits ordered SQL—often inside a transaction. Property changes on managed entities are dirty-checked.
const order = em.create(Order, { status: 'open' });
order.status = 'paid';
await em.flush(); // INSERT or UPDATE as needed
On interviews: `persist`, `flush`, cascades, transaction boundaries, dirty checking, explicit flush timing.
Common pitfalls: assuming assignment always writes SQL immediately; large flushes with many dirty entities; unexpected cascades.
The trade-off is convenient domain workflows versus flush surprise.
Checklist:
- Know dirty checking behavior.
- Flush deliberately.
- Keep cascades understandable.
- Bound flush to business operations.