1. NestJS์ Typeorm ์ค์
1yarn add @nestjs/typeorm typeorm pg2yarn start:dev
docker-compose up์ผ๋ก ๋จผ์ ์ผ์ฃผ๊ณ , yarn start:dev๋ก ์คํ์ํจ๋ค.
app.module.ts
1import { Module } from '@nestjs/common'2import { TypeOrmModule } from '@nestjs/typeorm'34import { AppController } from './app.controller'5import { AppService } from './app.service'6import { PostsModule } from './posts/posts.module'78@Module({9imports: [10PostsModule,11TypeOrmModule.forRoot({12// ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ์13type: 'postgres',14host: '127.0.0.1',15port: 5808,16username: 'postgres',17password: 'postgres',18database: 'postgres',19// entitiesํด๋์ ์์ฑํ PostsModel ๊ฐ์ ธ์ค๊ธฐ20entities: [],21synchronize: true,22}),23],24controllers: [AppController],25providers: [AppService],26})27export class AppModule {}
2. Entity๋ก ํ ์ด๋ธ ์์ฑ
posts/entities/posts.entity.ts๋ก ํ์ผ์ ์์ฑํ๋ค.
posts.entity.ts
1import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'23@Entity()4export class PostsModel {5@PrimaryGeneratedColumn()6id: number78@Column()9author: string1011@Column()12title: string1314@Column()15content: string1617@Column()18likeCount: number1920@Column()21commentCount: number22}
app.module.ts
1// ์๋ต2import { PostsModel } from './posts/entities/posts.entity'34@Module({5imports: [6PostsModule,7TypeOrmModule.forRoot({8// ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ์9type: 'postgres',10host: '127.0.0.1',11port: 5808,12username: 'postgres',13password: 'postgres',14database: 'postgres',15// entitiesํด๋์ ์์ฑํ PostsModel ๊ฐ์ ธ์ค๊ธฐ16entities: [PostsModel],17synchronize: true,18}),19],20controllers: [AppController],21providers: [AppService],22})23export class AppModule {}
3. Repository ์ฃผ์
- ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ์ผ์ข ์ bridge ์ญํ === Repository
posts.module.ts
1import { Module } from '@nestjs/common'2import { PostsService } from './posts.service'3import { PostsController } from './posts.controller'4import { TypeOrmModule } from '@nestjs/typeorm'5import { PostsModel } from './entities/posts.entity'67@Module({8imports: [9/*** ๋ชจ๋ธ์ ํด๋นํ๋ repostory๋ฅผ ์ฃผ์ ==> forFeature10* repository : ํด๋น ๋ชจ๋ธ์ ๋ค๋ฃฐ ์ ์๊ฒ ํด์ฃผ๋ ํด๋์ค11*/12TypeOrmModule.forFeature([PostsModel]),13],14/** ์ปจํธ๋กค๋ฌ๋ก ์ฌ์ฉํ ํ์ผ์ ์ ์15* ์ปจํธ๋กค๋ฌ๋ก ์ฌ์ฉํ ํ์ผ์ ์ ์,16* ํน์ path๋ก ์์ฒญ์ด ์ค๋ฉด ๋ผ์ฐํ ํด์ฃผ๋ ์ญํ17*18* PostsController() ===> ์ธ์คํด์คํ19* PostsController ===> ํด๋์ค ๊ทธ ์์ฒด20* ์ ๋ณด๋ฉด, ์ธ์คํด์ค๊ฐ ์๋ ํด๋์ค ๊ทธ ์์ฒด๋ฅผ ๋ฃ์๋ค21* ์๋ํ๋ฉด IoC ์ปจํ ์ด๋๊ฐ ์ธ์คํด์ค๋ฅผ ์์ฑ, ์์ , ์ญ์ ํ๊ธธ ๋ฐ๋ผ๋๊น22*/23controllers: [PostsController],24/** ์ปจํธ๋กค๋ฌ์์ ์ฃผ์ ํ ๊ฐ๋ค์ providers ์์ ์ ์25* PostsService๋ ์ด๋ค ์ญํ ์ ํ๋์ง์ ๋ํ ์ ์๋ค.26* ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃจ๋ ๋ก์ง์ ์์ฑํ๋ ํด๋์ค === Service27*28* ์๋น์ค๊ฐ ์๋๋๋ผ๋ ์ฃผ์ ํด์ผํ ํด๋์ค๋ค์29* ์ ๋ถ providers ์์ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.30*31* porviders ์์ ๋ฑ๋ก๋ ๋ชจ๋ ํด๋์ค๋ค์ ์ธ์คํด์คํ ์์ด32* IoC ์ปจํ ์ด๋๊ฐ ์์กดํ๋ฉด์ ์ฌ์ฉํ ์ ์๊ฒ ๋๋ค.33*/34providers: [PostsService],35})36export class PostsModule {}
posts.service.ts
1// posts.service.ts2import { Injectable, NotFoundException } from '@nestjs/common'3import { InjectRepository } from '@nestjs/typeorm'4import { Repository } from 'typeorm'5import { PostsModel } from './entities/posts.entity'67// ์๋ต89@Injectable()10export class PostsService {11constructor(12@InjectRepository(PostsModel)13private readonly postsRepository: Repository<PostsModel>,14) {}1516// ์๋ต17}
4. Find() ์ฌ์ฉํด ๋ค์ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
posts.service.ts
1// posts.service.ts ์๋ต2async getAllPosts() {3return this.postsRepository.find()4}
5. FindOne() ์ฌ์ฉํด ํ๋์ ๋ฐ์ดํฐ๋ง ์ฐพ๊ธฐ
posts.service.ts
1// posts.service.ts ์๋ต2async getPostById(id: number) {3const post = await this.postsRepository.findOne({4// PostsModel์ id๊ฐ ์ ๋ ฅ๋ฐ์ id์ ๊ฐ์์ง ํํฐ๋ง5where: {6id,7},8})9if (!post) {10throw new NotFoundException()11}12return post13}
6. Create() ์ฌ์ฉํด ์ ๋ฐ์ดํฐ ์์ฑํ๊ธฐ
posts.service.ts
1/**2* 1) create : ์ ์ฅํ ๊ฐ์ฒด๋ฅผ ์์ฑ3* 2) save : ๊ฐ์ฒด๋ฅผ ์ ์ฅ (create aใ ์๋์์ ์์ฑํ ๊ฐ์ฒด๋ก)4*/5async createPost(author: string, title: string, content: string) {6const post = this.postsRepository.create({7author,8title,9content,10likeCount: 0,11commentCount: 0,12})1314const newPost = await this.postsRepository.save(post)1516return newPost17}
7. Save()๋ก ์ ๋ฐ์ดํธํ๊ธฐ
posts.service.ts
1/** save์ 2๊ฐ์ง ๊ธฐ๋ฅ2* 1) ๋ง์ฝ์ ๋ฐ์ดํฐ๊ฐ ์กด์ฌํ์ง ์๋๋ค๋ฉด(id ๊ธฐ์ค) ์๋ก ์์ฑํ๋ค.3* 2) ๋ง์ฝ์ ๋ฐ์ดํฐ๊ฐ ์กด์ฌํ๋ค๋ฉด(๊ฐ์ id์ ๊ฐ์ด ์กด์ฌํ๋ค๋ฉด) ์กด์ฌํ๋ ๊ฐ์ ์ ๋ฐ์ดํธํ๋ค.4*/5async updatePost(postId: number, author: string, title: string, content: string) {6const post = await this.postsRepository.findOne({7where: { id: postId },8})910if (!post) throw new NotFoundException()11if (author) post.author = author12if (title) post.title = title13if (content) post.content = content1415const newPost = await this.postsRepository.save(post)16return newPost17}
8. Delete()๋ก ๋ฐ์ดํฐ ์ญ์ ํ๊ธฐ
1async deletePost(postId: number) {2const post = await this.postsRepository.findOne({3where: { id: postId },4})56if (!post) throw new NotFoundException()78await this.postsRepository.delete(postId)910return postId11}