osmlab / to-fix-backend

The to-fix server
BSD 3-Clause "New" or "Revised" License
15 stars 13 forks source link

Epic: Backend refactor sprint (MVC edition) #255

Open batpad opened 6 years ago

batpad commented 6 years ago

There are some distinct anti-patterns emerging in the code as we built features quickly, and I would like to do a focussed refactor sprint, mostly focussed on consolidating business logic and validation at the model layer of the code.

Currently, all the business logic is handled in our routes (the controller in a Model-View-Controller paradigm), which violates the principle of fat models, thin controllers - encapsulating all logic related to the data in the models layer offers distinct benefits, and I think solves a lot of anti-patterns that are emerging in the code, enumerating some here:

Some proposals, which I will ticket separately and link here:

What we should end up with is a folder structure roughly like:

 - models
       - item
          - index.js
          - db.js   <-- contains the actual db definition
          - validation.js  <---- contains all the validation rules for `item`
          - search.js   <----- contains all the code to handle querying / search
          - logging.js   <------- contains all code to handle logging

^^ and similarly for other models

This should make a lot of the code much clearer and easier to reason about, with concerns separated. Also, this will allow us to start writing more granular unit tests at the model / data layer.

cc @kepta @emilymdubois @coxchapman

recursivefunk commented 6 years ago

Lots of good stuff in here. I'll mark it as a refactor and tooling will look into sprinting on these tasks in the coming weeks.