How to Use One Command to Create RESTful APIs with TypeORM CLI on Node.js

Get Started with TypeORM, MySQL, and Express.js

Songtham Tung
Dec 12, 2019 · 3 min read
Image for post
Image for post
Original Photo by Amy Shamblen on Unsplash. Modified by Songtham Tungkitkancharoen.

TypeORM connects applications to databases. In this article, I’ll show you how to quickly create a new RESTful API microservice on Express (NodeJS) that integrates with MySQL using TypeORM.

Image for post
Image for post
ORM is the bridge between the API and your database

If you’re already familiar with using ORMs, let’s jump to one liners.

Outline

  1. Prerequisites
  2. One Liners
  3. Testing

Definitions

  • Object-relational mapping (ORM) — connects apps to databases. You can think of it as using code at the app level to execute queries.
  • MySQL — a popular open-source relational database.
  • Express — web framework for NodeJS.
  • RESTful API — see “WTF is an API?

Prerequisites

  • MySQL
  • Docker (if you don’t have MySQL)

One Liners

1. Install TypeORM

npm install typeorm

2. Initialize

typeorm init --name user-microservice --database mysql --express

Explanation: This command creates a new TypeORM project with the name user-microservice and scaffolds the necessary code to use Express and MySQL.

Image for post
Image for post
Easy! We just created a Node.js project which includes routes, controller, and entity

Explanation: routes.ts is the entry point for the API. UserController.ts is the orchestrator between routes and entity. User.ts is the entity that defines the table schema for the database.

Pause for a minute to look at the code inside each file and see how each of the pieces connect with each other. Notice any patterns?

3. Install packages

cd user-microservice
npm install

4. Database configurations

Otherwise, use the following Docker command:

docker run --name songtham-mysql -e MYSQL_ROOT_PASSWORD=test -e MYSQL_USER=test -e MYSQL_PASSWORD=test -e MYSQL_DATABASE=test -p 3306:3306 -d mysql:latest --default-authentication-plugin=mysql_native_password

Explanation: This Docker command grabs the latest MySQL version and runs it locally on your computer. Note: If you run this, you don’t need to make any changes to ormconfig.json, as it’s mapped to the default config file already.

5. Start

npm start

Testing

Express server has started on port 3000. Open http://localhost:3000/users to see results

GET

[{"id":1,"firstName":"Timber","lastName":"Saw","age":27},{"id":2,"firstName":"Phantom","lastName":"Assassin","age":24}]
Image for post
Image for post

You might be wondering, “How did these users appear in the database?”

If you take a look inside index.ts, you can see starting the server creates the users with the following lines of code:

// insert new users for test

await connection.manager.save(connection.manager.create(User, {

firstName: “Timber”,

lastName: “Saw”,

age: 27

}));

await connection.manager.save(connection.manager.create(User, {

firstName: “Phantom”,

lastName: “Assassin”,

age: 24

}));

POST

curl -d ‘{“firstName”:”Bob”, “lastName”:”Ross”, “age”:”52"}’ -H “Content-Type: application/json” -X POST http://localhost:3000/users

If done correctly, you should get a new entry in your database, and you’ll see the same entry if you go to http://localhost:3000/users.

[{"id":1,"firstName":"Timber","lastName":"Saw","age":27},{"id":2,"firstName":"Phantom","lastName":"Assassin","age":24},{"id":3,"firstName":"Bob","lastName":"Ross","age":52}]

Closing

This guide covers the basics of using the CLI to initialize a new app, to connect it to MySQL, and to perform GET and POST API requests. There are more advanced features such as migration, fixtures, and entity relations that I’ll cover in future posts. Stay tuned.

Thanks for reading!

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store