After setting up my express app with a /routes , /controllers , and /models directories, I enjoyed how everything has it’s place.
That is until I started repeating myself whenever i wanted to introduce a new resource.
I would have to define a model with mongoose, create controllers for creating, reading, updating, and deleting the model. Then I had to create each route (POST, GET, PUT, DELETE) to hit each one of those methods inside of my controller and check do validation logic to ensure some of the properties were present.
I started with about 5-8 models at first, then I got to 15-20 models as continued adding/prototyping new functionality. For each new resource I was
- Create a mongoose model representing the resource inside of the ./models directory
- Copying and pasting a recent controller in the /controllers directory
- Updating which properties to do validation on
- Setting up the routes in the ./routes directory
- Testing and validating each new route
Finally decided I had to do something about this. Introducing ‘Express AutoCRUD’
Pass in a configuration object (or array of objects) to quickly create a self documenting CRUD api.
I created an NPM package that can easily wrap all those routes, models, and controllers by a simple JSON config.
https://www.npmjs.com/package/express-autocrud
You can check out the open source code base here! :
https://github.com/JuanCarlosC/express-autocrud