CRUD API Best Practices

Hi,

I’m trying to figure out what the best practice is for simple CRUD API built using lambda functions. In the Netlify Dev generator it recommends 1 function that handles all aspects of CRUD by checking the http method (GET,POST,PUT,DELETE) and mapping that to the appropriate place.

I’m trying to understand if it might be better for separate each into it’s own function instead. The main reason is I’d like to also use middy middleware and more specifically the validator package. There’s no way for me to validate all 4 actions with a single validator middleware with the way it’s currently written.

The only real benefit I can think of for grouping it into one file is it’s better organized in the sense that it lives in one place instead of having todo-create, todo-read, todo-update, and todo-delete folders. It would be nice if netlify supported nested folder structure so I could have something like todos/create todos/update, etc.

I guess I could also whip up my own validator middleware that allows you to define schema based on the http method.

Thoughts?

The thing preventing that is you can’t have multiple lambda functions of the same name. So you would have to do something crud-post and crud-get, etc. This means you have to invoke a different endpoint for each method.

You could have multiple files for a single function. You just need to make sure you require those files so our buildbot’s zip-it-and-ship-it knows to include them as dependencies.

Let me know if the helps.