TypeORM REST Scaffold
A nodejs library built on top of expressjs. It generates an expressjs handler function that can find or create a row in a table through TypeORM in response to a GET or POST request. It’s a quick way to get an express rest endpoint that directly matches a model without writing much code.
For example to GET a user, we would normally write an express handler to:
- Find a user,
- Return the data if available,
- Redirect to an error page if something goes wrong.
Using typeorm-rest-scaffold, we would instead call just call typeorm-rest-scaffond’s ServiceBuilder, pass it your model name and generate a handler function that will do all this with one line of code:
How to use
The first thing to do is setup a TypeORM project. With TypeORM installed, an easy way to do this is to run “typeorm init”:
Next install express:
Pull in typeorm-express-rest-scaffold as well:
Finally, create a model and update the index.ts file to register the TypeORM model as a route. In the example here we use a model called “User”:
You now have a REST service that can find Users by ID and firstName.