NestJS · · 1 min read
7. 영속화
영속화(Persistence) 는 데이터를 DB에 저장·조회하는 계층이다. NestJS는 주로 TypeORM(또는 Prisma)과 연동하며 리포지토리 패턴으로 접근한다.
7.1. TypeORM 연결
npm install @nestjs/typeorm typeorm pg
TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5432,
database: 'app',
entities: [User],
synchronize: true, // 개발용. 운영에서는 마이그레이션 사용
});
7.2. 엔티티
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column({ unique: true })
email: string;
}
7.3. 리포지토리 주입과 CRUD
@Injectable()
export class UsersService {
constructor(
@InjectRepository(User) private repo: Repository<User>,
) {}
findAll() { return this.repo.find(); }
create(dto: CreateUserDto) { return this.repo.save(dto); }
}
모듈에서 TypeOrmModule.forFeature([User]) 로 리포지토리를 등록해야 주입된다.
synchronize: true는 스키마를 자동 동기화해 편하지만 데이터 유실 위험이 있어 운영 환경에서는 마이그레이션을 쓴다(부록 참고).