Connect nestjs project created by Nx with mysql

This is a continuation of this

Connect nestjs project created with Nx with mysql

Table of Contents

  1. Create mysql easily with docker
  2. Connect to a database such as MySql
  3. Connect to MySql database (connect application using typeorm)

1. Create mysql easily with docker

The current directory structure assumes the following format.

$ tree -L 1 -I node_modules
├── README.md
├── apps
│   └── nestjs-sample
│       ├── jest.config.js
│       ├── src
│       │   ├── app
│       │   │   ├── app.controller.spec.ts
│       │   │   ├── app.controller.ts
│       │   │   ├── app.module.ts
│       │   │   ├── app.service.spec.ts
│       │   │   ├── app.service.ts
│       │   │   └── todo
│       │   ├── assets
│       │   ├── environments
│       │   │   ├── environment.prod.ts
│       │   │   └── environment.ts
│       │   └── main.ts
│       ├── tsconfig.app.json
│       ├── tsconfig.json
│       └── tsconfig.spec.json
├── jest.config.js
├── libs
├── nx.json
├── package.json
├── tmp
│   └── apps
│       └── nestjs-sample
│           └── tsconfig.generated.json
├── tools
│   ├── schematics
│   └── tsconfig.tools.json
├── tsconfig.base.json
├── workspace.json
└── yarn.lock

Next, simply create a docker file with an image of mysql.

$ touch docker-compose.yml
$ touch Dockerfile

Describe the contents of docker-compose.

docker-compose.yml


version: '3.8'
services:
  db:
    container_name: nestjs-sample-db
    image: mysql:5.7
    tty: true
    restart: always
    environment:
      MYSQL_DATABASE: nestjs-sample-db
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: nestjs-sample-db
      MYSQL_PASSWORD: password
    ports:
      - '3306:3306'

The contents of Docker are as follows.

FROM mysql:5.7

Start docker.

$ docker-compose up

2. Connect to MySql database (connect application using typeorm)

Since this chapter will be implemented, I think that this is better for those who want to deal with it in Official Documents.

The things to do are the same

  1. TypeORM install
  2. Enable Type ORM

Will be carried out.

2-1. Install TypeORM

Install the dependency module.

$ yarn add @nestjs/typeorm typeorm mysql

2-2. Enabling Type ORM

Edit app.module.ts_ to enable TypeORM.

The current app.module.ts is as follows.

app.module.ts


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

@Module({
  imports: [],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Add the TypeOrm settings here.

app.module.ts


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

@Module({
  imports: [
  ##Addition from here
    TypeOrmModule.forRoot({
      type: "mysql",
      host: "localhost",
      port: 3306,
      username: "nestjs-sample-db",
      password: "password",
      database: "nestjs-sample-db",
      entities: ["dist/**/*.entity{.ts,.js}"],
      "synchronize": true
    }),,
  ##Added up to here
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

If you go with the official document, this is the end, but since it does not read the Entity settings, change it to TypeormModule.forRoot as follows.

app.module.ts


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

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: "mysql",
      host: "localhost",
      port: 3306,
      username: "nestjs-sample-db",
      password: "password",
      database: "nestjs-sample-db",
      entities: [], ##Change here
      autoLoadEntities: true,
      synchronize: true ##Change here
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

"@ nrwl / workspace": With "10.3.1", I will describe it someday because connection and database relation will not work with this setting alone. (Issues have been raised as of 11/7, but it has been resolved. It doesn't work + rewriting tsconfig works, but ...)

Roughly speaking, change tsconfig.app.json and app.module.ts to the following.

tsconfig.app.json


{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "../../dist/out-tsc",
    "types": ["node"],
    "emitDecoratorMetadata": true,
    "skipLibCheck": true,
    "module": "commonjs", // <-- here
    "target": "es2017", // <-- here
  },
  "exclude": ["**/*.spec.ts"],
  "include": ["**/*.ts"]
}

app.module.ts


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

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: "mysql",
      host: "localhost",
      port: 3306,
      username: "nestjs-sample-db",
      password: "password",
      database: "nestjs-sample-db",
      entities: getMetadataArgsStorage().tables.map(t => t.name),
      synchronize: true,
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Repo

Recommended Posts

Connect nestjs project created by Nx with mysql
Connect to MySQL 8 with Java
Build Spring Boot project by environment with Gradle
Get data with api created by curl command
How to connect MySQL / MariaDB + HikariCP with Liferay 7 / DXP
I started MySQL 5.7 with docker-compose and tried to connect