Adding and Using a Database on a TypeScript API with TypeORM

Adding and Using a Database on a TypeScript API with TypeORM

In this tutorial we will be adding TypeORM to our API and we will use it to create a database to hold products information. As a base, we…

In this tutorial we will be adding TypeORM to our API and we will use it to create a database to hold products information. As a base, we will be using the API of  this tutorial. If you prefer, you can get the  source files here and start right away.

Database Setup

The first thing that we will have to do is setup a database to use. If you already have a database (even if it’s not a MySQL one) you can’t skip this part.

I’ll be using Docker to speed the process. If you don’t know docker already, I recommend that you take a look at it. You can install it following this page:  https://docs.docker.com/get-docker/

Docker works with containers that contain a working OS with the tools and programs that you need. Instead of installing and setting things up by yourself you can use a ‘plug and play’ docker container with everything ready. For this tutorial, I’ll use a MySQL container. After installing docker, just run the following command into your terminal:

docker run --name database -e MYSQL_ROOT_PASSWORD=password -p 3306:3306 -d mysql:8

This will create a MySQL detached (-d) container with the name ‘database’, a user with the password ‘password’ (-e MYSQL_ROOT_PASSWORD) and with the container’s port 3306 mapped to the host’s 3306 port. This will be a lot of information if you’ve never used docker. The main things to remember here are the name and the password that you used.

You can check if the container is running using ‘_docker ps_’ at the terminal. If nothing shows up (maybe you restarted your PC), use ‘_docker ps -a_’ to show all the containers (including the ones that are not started). To start and stop the container, just use ‘_docker start database_’ and ‘_docker stop database_’. To summarize everything:

## List all running containers
docker ps

## List all containers
docker ps -a
## Start a container
docker start <name>
## Stop a container
docker stop <name>

typescript javascript development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

TypeScript Tutorial For JavaScript Developers - TypeScript Basics

TypeScript Tutorial For JavaScript Developers - TypeScript Basics. I will show you guys 4 example of JavaScript code, and how to convert it to TypeScript. This is a typescript beginners tutorial.

What is TypeScript? Why TypeScript? and Why Not TypeScript?

TypeScript extends JavaScript by adding Types. There are many great reasons to switch to TypeScript. Especially if your team uses JavaScript. There are some reasons to not use TypeScript as there are with any language or framework.

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.

Developer Career Path: To Become a Team Lead or Stay a Developer?

For a developer, becoming a team leader can be a trap or open up opportunities for creating software. Two years ago, when I was a developer, ... by Oleg Sklyarov, Fullstack Developer at Skyeng company

The What, Why, and How of TypeScript for JavaScript Developers

A typed introduction to JavaScript .The What, Why, and How of TypeScript for JavaScript Developers