-
[Nest.js] 5 - TypeORM / MySql(MariaDB)Nest.js 2021. 9. 8. 19:03
Contents
1. TypeORM?
2. TypeORM vs Pure Javascript
3. Why use TypeORM
4. Install modules
5. Create Configuration file
6. Register config file to Root Module
1. TypeORM?
- ORM(Object Relational Mapping)
-> Automatically Mapping work between Object and RDB(Relational DataBase)
-> it has an advantage for changes in object and DB- It available in the Node.js world
- Written in TypeScript
- We(developers) now focus on thinking and developing for OOP by using TypeORM,
no longer spending time on creating SQL query
2. TypeORM vs Pure Javascript
- TypeORM
// using TypeORM's find method const boards = Board.find({ title: 'Hello', status: 'PUBLIC'});
- Javascript
db.query('SELECT * FROM boards WHERE title = "Hello" AND status = "PUBLIC" , (err, result) => { if(err) { throw new Error('Error') } boards = result.rows; })
3. Why use TypeORM
- Based on Model, it automatically creates DB Table Schema.
- it makes easier to deal with CRUD queries, such as Insert, update, select, and delete.
- it makes mapping between tables( 1:N, N:M)
- TypeORM is easy to unite other modules
4. Install Modules - TypeORM, MySql
$ npm install --save @nestjs/typeorm typeorm mysql
modules installation
5. Create Configuration file for DB connection
- Create folder 'configs' to save configuration files
- Create file 'typeorm.config.ts' and write below properties
// src/configs/typeorm.config.ts import { TypeOrmModuleOptions } from '@nestjs/typeorm'; export const TypeORMConfig: TypeOrmModuleOptions = { type: 'mysql', host: 'localhost', port: 3306, username: 'root', password: '1234', database: 'test', entities: [__dirname + '/../**/*.entity.{js,ts}'], synchronize: true, };
- entities: [__dirname + '/../**/*.entity.{js,ts}']
-> it registers all entities within the path and naming convention.
-> Alternately, if we need to register specific entity.import { User } from "./payment/entity/User"; import {Post} from "./blog/entity/Post"; { ... "entities": [User, Post] ... }
- synchronize : true when in developing and testing
: false when in deploying because it sometimes deletes data after colunm removed by accident.
6. Register config file to Root Module
- configuration file of DB needed to register in Root Module
// src/app.module.ts import { Module } from '@nestjs/common'; import { BoardsModule } from './boards/boards.module'; import { TypeOrmModule } from '@nestjs/typeorm'; import { TypeORMConfig } from './configs/typeorm.config'; @Module({ imports: [ TypeOrmModule.forRoot(TypeORMConfig), // register here ! BoardsModule ], controllers: [], providers: [], }) export class AppModule {}
- TypeOrmModule.forRoot(configuration)
-> the configuration applies to all Sub-modules
# Connection error solution when testing in local server
- ERROR
Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client- SOLUTION
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'
'Nest.js' 카테고리의 다른 글
[Nest.js] 7- Entity & Repository (0) 2021.09.09 [Nest.js] 6 - DTO & PIPES (0) 2021.09.09 [Nest.js] 4 - Provider (0) 2021.09.08 [Nest.js] 3 - Controller (0) 2021.09.08 [Nest.js] 2 - Module (0) 2021.09.07