Storz
Storz copied to clipboard
💡 [REQUEST] - Refactoring the server/ dir to MVC model
Start Date
09/04/2022
Implementation PR
No response
Reference Issues
No response
Summary
Below are some of my proposed changes
Controller
- Create a Controller folder, which contains controller files
- There is a controller file for each route e,g - route/user.js <-- controller/userController.js route/upload.js <-- controller/uploadController.js
- Each controller file contains the route handler function specific to its corresponding route endpoint
Misc
- Merge the auth.js route to the user.js route, keeping all the authMiddleware still working. Reason for that these two route share the same API endpoint,
/api/user/
e,g - auth -->/api/user/login
user -->/api/user/files
Basic Example
File Structure after refactoring into MVC would look like this -
controller/
userController.js
uploadController.js
downloadController.js
model/
routes/
user.js
download.js
upload.js
test.js
index.js
package.json
Drawbacks
Doing Refactoring to MVC only increases the require() lines and increases the number of files. Except that there is no other problem
Unresolved questions
No response
@anomic30 please assign this issue to me for Hacktoberfest-2022 Thanks
Hi @BiswajitSahoo-tech, this suggestion looks excellent. You might want to look into this PR [#18] and find a folder named "data-repository". What's your take on this?
@anomic30
Referring to PR [#18 ] data-repository/ contains a file named user.js
user.js file exports a User class
and all the method functions of this class are wrapper functions to mongoose functions
e,g -
instead of calling UserModel.findOne(fields).sort(sort);
now we call User.findOne(field, sort )
with two object parameters, which calls the above code inside of it
This kind of wrapping gives better understandability, but one drawback is that we practically can't wrap all of our mongoose code.
Thanks
Yup, that's true. We should then remove this wrapper for the time being and follow the folder structure that you have mentioned. You can co-author that pull request if you want.
Thanks @BiswajitSahoo-tech
Ok then, we will keep the folder structure as my suggestion But, how to co-author a PR @anomic30
@BiswajitSahoo-tech nice observation, we can it only requires us to create a method that wraps the underlying mongoose function. The advantage of this is that in the feature we can choose to integrate another form of a database management system that is not mongoose easily without changing the controller functions instead of just having to add a new data repository file that maps each method to the underlying database methods.