Storz icon indicating copy to clipboard operation
Storz copied to clipboard

💡 [REQUEST] - Refactoring the server/ dir to MVC model

Open BiswajitSahoo-tech opened this issue 2 years ago • 6 comments

Start Date

09/04/2022

Implementation PR

No response

Reference Issues

No response

Summary

Below are some of my proposed changes

Controller

  1. Create a Controller folder, which contains controller files
  2. There is a controller file for each route e,g - route/user.js <-- controller/userController.js route/upload.js <-- controller/uploadController.js
  3. Each controller file contains the route handler function specific to its corresponding route endpoint

Misc

  1. 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

BiswajitSahoo-tech avatar Oct 03 '22 13:10 BiswajitSahoo-tech

@anomic30 please assign this issue to me for Hacktoberfest-2022 Thanks

BiswajitSahoo-tech avatar Oct 03 '22 13:10 BiswajitSahoo-tech

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 avatar Oct 03 '22 14:10 anomic30

@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

BiswajitSahoo-tech avatar Oct 03 '22 14:10 BiswajitSahoo-tech

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

anomic30 avatar Oct 03 '22 14:10 anomic30

Ok then, we will keep the folder structure as my suggestion But, how to co-author a PR @anomic30

BiswajitSahoo-tech avatar Oct 03 '22 14:10 BiswajitSahoo-tech

@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.

Onesco avatar Oct 04 '22 14:10 Onesco