This is a continuation of this
Table of Contents
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
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
Will be carried out.
Install the dependency module.
$ yarn add @nestjs/typeorm typeorm mysql
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 {}
Recommended Posts