Skip to content

Commit

Permalink
feat : typeORM을 사용하여 DB와 연동 #35
Browse files Browse the repository at this point in the history
- table과 매핑될 user entity 생성
- DB관련 작업을 진행할 user repository 생성
- user module의 providers 배열에 repository 추가
- service에서 의존성 주입
- db관련 config 생성 및 app module에 import

Co-authored-by: GeunH <[email protected]>
  • Loading branch information
LeeTH916 and GeunH committed Nov 15, 2023
1 parent ff73edb commit 59e676a
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 9 deletions.
3 changes: 3 additions & 0 deletions be/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
/dist
/node_modules

#config
typeorm.config.ts

# Logs
logs
*.log
Expand Down
4 changes: 3 additions & 1 deletion be/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Module } from "@nestjs/common";
import { UserModule } from "./user/user.module";
import { TypeOrmModule } from "@nestjs/typeorm";
import { typeORMConfig } from "./configs/typeorm.config";

@Module({
imports: [UserModule],
imports: [UserModule, TypeOrmModule.forRoot(typeORMConfig)],
})
export class AppModule {}
42 changes: 42 additions & 0 deletions be/src/user/entities/user.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
UpdateDateColumn,
DeleteDateColumn
} from 'typeorm';

@Entity()
export class User {
@PrimaryGeneratedColumn('increment')
id: number;

@Column({ type: 'varchar', length: 20, unique : true })
nickName: string;

@Column({ type: 'varchar', length: 50 })
email: string;

@Column({ type: 'int' })
age: number;

@Column({ type: 'boolean' })
gender: boolean;

@Column({ type: 'varchar', length: 50, nullable: true })
password: string | null;

@Column({ type: 'varchar', length: 20, nullable: true })
social_provider: string | null;

@CreateDateColumn({ type: 'timestamp' })
created_at: Date;

@DeleteDateColumn({ type: 'timestamp', nullable: true})
deleted_at: Date | null;

@UpdateDateColumn({ type: 'timestamp'})
updated_at: Date;
}

3 changes: 2 additions & 1 deletion be/src/user/user.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Module } from "@nestjs/common";
import { UserController } from "./user.controller";
import { UserService } from "./user.service";
import { UserRepository } from "./user.repository";

@Module({
controllers: [UserController],
providers: [UserService],
providers: [UserService, UserRepository],
})
export class UserModule {}
16 changes: 16 additions & 0 deletions be/src/user/user.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { DataSource, Repository } from 'typeorm';
import { User } from './entities/user.entity';
import { UserInfoDto } from './dto/userInfo.dto';
import { Injectable } from '@nestjs/common';

@Injectable()
export class UserRepository extends Repository<User> {
constructor(private dataSource: DataSource){
super(User, dataSource.createEntityManager());
}
async createUser(userinfoDto: UserInfoDto): Promise<User> {
const newUser = this.create(userinfoDto);
await this.save(newUser);
return;
}
}
20 changes: 13 additions & 7 deletions be/src/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { Injectable } from "@nestjs/common";
import { UserInfoDto } from "./dto/userInfo.dto";
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from './entities/user.entity';
import { UserRepository } from "./user.repository";

@Injectable()
export class UserService {
signup(userInfoDto: UserInfoDto) {
const { email, password, provider, nickName, age, gender } = userInfoDto;

// DB에 저장

return userInfoDto;
}
constructor(
@InjectRepository(UserRepository)
private usersRepository: UserRepository,
) {}
signup(userInfoDto: UserInfoDto) {
this.usersRepository.createUser(userInfoDto);
return;
}
}

0 comments on commit 59e676a

Please sign in to comment.