TypeORM에서 제공해주는 함수가 아닌 Query를 사용하여 CRUD를 구현



import { InjectEntityManager } from '@nestjs/typeorm';
import { EntityManager } from 'typeorm';
import { CreateBoardDto } from './dto/create-board.dto';
import { UpdateBoardDto } from './dto/update-board.dto';
import { Board } from './entities/board.entity';
@Injectable()
export class BoardsService {
constructor(@InjectEntityManager() private entityManager: EntityManager){}
async create(createBoardDto: CreateBoardDto) {
return await this.entityManager.transaction(async() => {
await this.entityManager.query(`INSERT INTO boards(title, contents)
values('${createBoardDto.title}','${createBoardDto.contents}');`);
})
}
async findAll(): Promise<Board> {
return await this.entityManager.transaction(async() => {
return await this.entityManager.query(`SELECT * FROM boards;`);
})
}
async findOne(id: number) {
return await this.entityManager.transaction(async() => {
return await this.entityManager.query(`SELECT * FROM boards WHERE id=${id};`);
})
}
async update(id: number, updateBoardDto: UpdateBoardDto) {
return await this.entityManager.transaction(async() => {
await this.entityManager.query(`UPDATE boards SET title='${updateBoardDto.title}',
contents='${updateBoardDto.contents}' WHERE id=${id}`);
})
}
async remove(id: number) {
return await this.entityManager.transaction(async() => {
await this.entityManager.query(`DELETE FROM boards WHERE id=${id}`);
})
}
}
