Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

Mini

[Nest JS] 인증 구현 준비 (컨트롤러, 서비스, 리포지토리 생성) 본문

JS/Nest.js

[Nest JS] 인증 구현 준비 (컨트롤러, 서비스, 리포지토리 생성)

Mini_96 2024. 8. 18. 12:25

 

* user table 생성

import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from "typeorm";

@Entity()
export class User extends BaseEntity{
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  username: string;

  @Column()
  password: string;
}

* repository 생성

import { Injectable } from "@nestjs/common";
import { DataSource, Repository } from "typeorm";
import { User } from "./user.entity";

@Injectable()
export class UserRepository extends Repository<User>{
  constructor(dataSource: DataSource) {
    super(User, dataSource.createEntityManager());
  }
  
}

 

* 모듈에 리포지터리 임포트

import { Module } from '@nestjs/common';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { TypeOrmModule } from "@nestjs/typeorm";
import { UserRepository } from "./user.repository";

@Module({
  imports:[
    TypeOrmModule.forFeature([UserRepository])
  ],
  controllers: [AuthController],
  providers: [AuthService]
})
export class AuthModule {}

 

* 서비스에서 리포지토리 사용

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

@Injectable()
export class AuthService {
  constructor(
    @InjectRepository(UserRepository) //변수에 의존성 주입해줘
    private userRepository: UserRepository) {
  }
}