𝗗𝗮𝘆 𝟰𝟴 𝗼𝗳 𝗟𝗲𝗮𝗿𝗻𝗶𝗻𝗴 𝗠𝗘𝗥𝗡 𝗦𝘁𝗮𝗰𝗸
I am on day 48 of my full-stack engineering journey.
Yesterday, I moved my code to an MVC architecture. Today, I improved my model layer using JavaScript Classes and Object-Oriented Programming.
I stopped using loose object definitions. Instead, I built reusable classes to manage file operations and data storage. This change turns simple scripts into scalable engines.
Here is how I structured my code in home.js:
The Constructor: I use the ES6 class framework to create a standard data mold. Every house entry now includes a name, price, location, rating, and photo URL. This ensures data consistency across the server.
The Save Method: I created a .save() method. This method reads existing data asynchronously. It then uses fs.writeFile() to update the homesdata.json file. This replaces global arrays with a controlled storage process.
Static Methods: I implemented static methods for data retrieval. I use static fetchAll() to pull records from the disk. This allows controllers to access data without creating a new class instance first.
Using classes makes my code cleaner and easier to scale.
Source: https://dev.to/ali_hamza_589ec7b3eb6688d/day-48-of-leaning-mern-stack-123n