How to use typeorm with MySQL in NestJS

NestJS is Backend framework based on NodeJS and use Typescript for main language.

I will use TypeORM for handle database and use MySQL database. You must makesure in your computer must have MySQL database.

Create project with Nest CLI

nest new my_nest_app

Pick package manager that you want. Pick npm if you want to use npm and then Enter

Open your project with Visual Studio Code

Add Typeorm and mysql with npm in your terminal

npm install --save @nestjs/typeorm typeorm mysql2

Create database in MySQL databases. this example I use “my_nest_app” database

Modify file src/app.module.ts and import TypeormModule

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';

  imports: [
      type: 'mysql',
      host: 'localhost',
      port: 3306,
      username: 'root',
      password: 'mysql',
      database: 'my_nest_app',
      entities: [],
      synchronize: true,
  controllers: [AppController],
  providers: [AppService],
export class AppModule {}

Run “npm run start:dev” for run nestJS with development mode

npm run start:dev

Next i will create Entities

Create entities directory inside src directory

in this example i use UserEntity for intead of users table in MySQL database

Create users.entity.ts inside src/entities

import { Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn } from "typeorm";

export class UsersEntity {

    id: number

    name: string

    email: string

    createdAt: Date

    updatedAt: Date


Import Entity to app.module

That will be automaticly create users table in MySQL database

Next I will make CRUD in users table.

create user module,service,controller and repository

nest g mo user
nest g s user
nest g co user

Create user.repository file in src/user/user.repository.ts

import { UsersEntity } from "src/entities/users.entity";
import { EntityRepository, Repository } from "typeorm";

export class UserRepository extends Repository<UsersEntity>{

import user.repository.ts in user.module.ts

import { Module } from '@nestjs/common';
import { UserService } from './user.service';
import { UserController } from './user.controller';
import { UserRepository } from './user.repository';
import { TypeOrmModule } from '@nestjs/typeorm';

let repositories = TypeOrmModule.forFeature([

  imports: [
  providers: [UserService],
  controllers: [UserController],
  exports: [
export class UserModule { }

edit user.service file such as bellow

import { Injectable } from '@nestjs/common';
import { UserRepository } from './user.repository';

export class UserService {

    constructor(private userRepository: UserRepository) {


     * list of users
    async listUser() {
        return await this.userRepository.find();

     * Insert new user
     * @param user 
     * @returns 
    async insert(user: any) {
        return await this.userRepository.insert(user);

Create DTO (Data Transfer Object) and install “class-validator”

npm install class-validator

Create user-create.dto.ts

Modify user.controller.ts

Test request create user dan get list user