DEV Community

Ali Hamza
Ali Hamza

Posted on

Day 53 of Learning MERN Stack

Hello Dev Community! 👋

It is officially Day 53 of my unbroken full-stack engineering streak! Yesterday, I decoupled my local file storage using multiple OOP classes. Today, I completely upgraded my persistence engine by moving away from hard-drive JSON text files and connecting a live Relational Database (MySQL) inside Prashant Sir's backend masterclass!

Instead of parsing string arrays on the fly, my system now communicates with a structured SQL server via a high-performance connection pool executing semantic query scripts!


🧠 Key Learnings From Node.js Lecture 27 (MySQL Integration)

As shown in my implementation files across "Screenshot (122).png" and "Screenshot (123).png", refactoring file-based classes into relational database queries makes the system incredibly fast and stable:

1. Connection Pooling with mysql2

Instead of opening and closing single database channels constantly, I setup a connection pool using mysql2/promise inside utils/databaseUtils.js. This creates a set of reusable channels ready to pick up database requests instantly:


javascript
const sql = require('mysql2');
const pool = sql.createPool({
    host: 'localhost',
    user: 'root',
    password: '', // Kept secure for local development
    database: 'airbnb'
});
module.exports = pool.promise();  // A look at my new promise-based static lookup method
static fetchAll() {
    return db.execute('SELECT * FROM homes');
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)