* 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) {
}
}
'JS > Nest.js' 카테고리의 다른 글
[Nest JS] 유효성체크 with class-validator, 파이프 (0) | 2024.08.18 |
---|---|
[Nest JS] 회원가입 구현 / TypeError: this.userRepository.createUser is not a function 해결 (0) | 2024.08.18 |
[Nest JS] 모든 게시글 조회 (0) | 2024.08.18 |
[Nest JS] 게시글 업데이트 (0) | 2024.08.18 |
[Nest JS] 게시글 삭제 (0) | 2024.08.18 |