Configuration TypeORM Nest Databases with MySQL


Introduction to MySQL

MySQL is the most popular Open-Source SQL database management system developed, distributed, and supported by Oracle.

  • MySQL is a database management system. A database is a structured collection of data. It may be anything from a simple shopping list to a picture gallery or the vast amounts of information in a corporate network. To add, access, and process data stored in a computer database, you need a database management system such as MySQL.
  • MySQL databases are relational. A relational database stores data in separate tables rather than putting all the data in one big storeroom. The database structures are organized into physical files optimized for speed. MySQL provides a logical model, with objects such as databases, tables, views, rows, and columns to offer a flexible programming environment.

MySQL tables

A table is used to organize data in the form of rows and columns. It is used for both storing and displaying records in a structured format. It is like worksheets in a spreadsheet application. The columns specify the data type, whereas the rows contain the actual data. Below is how you could imagine a MySQL table 

MySQL installation

There are several different ways of installing MySQL. In this book, we will install MySQL and a MySQL administration tool called phpMyAdmin. Both tools can be found in a development environment called XAMPP. So, let’s install XAMPP.

XAMPP

XAMPP is a popular PHP development environment. XAMPP is a free, easy to install Apache distribution containing MySQL, PHP, and Perl. XAMPP also includes phpMyAdmin. If you don’t have XAMPP installed, go to https://www.apachefriends.org/download.html, download and install it.

Configuring our database

Execute XAMPP, start the Apache Module, then start MySQL module, and click the MySQL Admin button (of the MySQL module). This takes us to the phpMyAdmin application

Note: If you are using WAMP or another similar application, the phpMyAdmin application can be commonly accessed through the following route: http://localhost/phpmyadmin/.

In the phpMyAdmin application enter your username and password. Default values are “root” (for the username) and an empty password

Once logged in to phpMyAdmin, click the Databases tab. Enter the database name “online_store”, and click Create 

We will use the username, password, and database name in the following chapter.

Configuration of TypeORM Nest Databases

Nest is database agnostic, allowing you to integrate with any SQL or NoSQL database easily. Nest provides tight integration with TypeORM, Sequelize, and Mongoose through the @nestjs/typeorm, @nestjs/sequelize, and @nestjs/mongoose packages respectively (https://docs.nestjs.com/techniques/database). Nest suggests using TypeORM because it's the most mature Object Relational Mapper (ORM) available for TypeScript. Since it's written in TypeScript, it integrates well with the Nest framework.

Introduction to TypeORM

TypeORM is an ORM that can run in NodeJS, Browser, Cordova, PhoneGap, Ionic, React Native, NativeScript, Expo, and Electron platforms and can be used with TypeScript and JavaScript. Its goal is to help you develop any application that uses databases, from small applications with a few tables to large scale enterprise applications with multiple databases.

TypeORM provides support for many relational databases, such as PostgreSQL, MySQL, Oracle, Microsoft SQL Server, SQLite, and even NoSQL databases like MongoDB.

Nest TypeORM integration

TypeORM installation

To connect our Online Store application with our MySQL database, we need to install and integrate TypeORM. In the Terminal, go to the project directory, and execute the following:

Execute in Terminal

npm install --save @nestjs/typeorm typeorm mysql2

This installs TypeORM and other required libraries to work with MySQL.

TypeORM config file

Now, let’s define the configuration file (where we will place our database credentials). In the project root directory, create a new file called ormconfig.json , and fill it with the following code.

We used the username, password, and database name from the previous chapter.

TypeORM import

We import the TypeORM in our AppModule . In src/app.module.ts , make the following changes in bold.

We imported the TypeOrmModule and register it in the AppModule imports property by invoking the TypeOrmModule.forRoot() method. Remember that the @Module imports property is used to register other modules required by the current module. In this case, our root module ( AppModule ) requires TypeOrmModule . The TypeOrmModule.forRoot() method configures the TypeORM library. It receives an optional parameter with the configuration values (i.e., database credentials). If no parameter is passed, the Nest application will try to load ormconfig.json file and pass the information to the TypeORM library.

Running the app

In the Terminal, go to the project directory, and execute the following:

Execute in Terminal

npm run start:dev

You should see the application running normally. This means that the integration was successful, and the application is now connected to the database. If you get an error like “ERROR [TypeOrmModule] Unable to connect to the database”, please check that MySQL is running and the database credentials are correct.


See Next Article Nest JS ...

Post a Comment for "Configuration TypeORM Nest Databases with MySQL"