Hey friends! ππ»
I wanted to share another cool backend fix I implemented in my microforum, Axion. This time, it involves MongoDB transactions, session management, and taking advantage of Express 5's upgraded error handling to keep my code clean.
Why Use Transactions in the First Place? When you're building a platform like Axion, some actions require multiple database operations to happen together. For example, in the post-deletion route, I need to remove related documents like bookmarks, likes, and comments all at once.
Transactions act as a way to execute a set of database operations where if a single operation fails, everything rolls back and no operations execute afterwards. This guarantees data consistency.
The Bug I Encountered: Session Leaks. When running a transaction in MongoDB, you must start a client session and end it when you are finished. My code was written sequentially, assuming it would always reach the cleanup phase at the bottom.β¦