Comments (6)
유저와 게시물 데이터의 관계 형성을 위해 서로 간의 엔티티에 필드를 넣어줘야 한다.
일 대 다, 다 대 일
일 대 다 : OneToMany( 타입, 상대, 옵션 )
유저 하나 당 여러 게시글을 쓸 수 있다.
유저 객체 보드는 일 대 다 관계
보드 여러 개가 무조건 하나의 유저 ( 작성자 ) 를 가지고 있다.
보드 객체 유저는 다 대 일 관계
OneToMany
첫 번째 Type 객체의 타입을 지정하고,
두 번째 inverseSide 유저는 board.user 로 접근해야한다.
세 번째 Option은 eager : true 면 user 정보 가져올 때 board도 같이 가져온다.
from nestj.
로그인하면 AccessToken이 발급된다. 그걸 Bearer Token 으로 넣은 상태로 새로운 board 를 post 하면 그 게시물에 작성자 아이디가 넣어진다.
from nestj.
해당 유저의 게시물을 찾기 위한 쿼리 빌더 사용하기
물론 트센에서는 사용을 권장하진 않는다.
query.where() 는 SQL 문 중 where 절을 의미한다. 'board.userId = :userId' 와 { userId: user.id } 를 인자로 넣는데,
지금 백 로직 상에는 user.id 값을 가져와 userId에 주입한 걸 인자로 SQL where 조건절에 들어가게 되는 것이다.
createQueryBuilder에서 해당 도메인을 담으면 그걸 디비 단에서 가져오는 것 같다.
JPA 느낌이랄까
from nestj.
삭제를 할 때, 유저 객체를 인자로 넘겨야되는데 또 지원을 안 해준단다. 그래서 두 가지 방법이 있는데
하나는 fix 저거이고 다른 하나는 아예 delete 함수 안에 들어가서 Repository.d.ts 안에서 criteria: 앞에 { id:number, user:User } 이렇게 넣어도 된다. 이게 뭔가 신선하지만 호환은 안될 것 같아서 명시적으로 user 타입을 받는 조건 주는 걸로 해결함.
from nestj.
const result = await this.boardRepository.createQueryBuilder('board')
.delete()
.from(Board)
.where("userId = :userId", { userId: user.id})
.andWhere( "id = :id", { id: id } ).execute();
from nestj.
다음 이슈는 로그다!
from nestj.
Related Issues (13)
- [feat] : NestJS install, start HOT 2
- [feat] 게시판 CRUD : R HOT 5
- [feat] 게시판 CRUD : C, D, U HOT 6
- [feat] : TypeORM HOT 1
- [feat] : Pipe 로 전송과 검증을 하기 HOT 6
- [Feat] : Entity 와 Database를 이용한 CRUD HOT 13
- [Feat] : User HOT 22
- [Feat] : Logging HOT 3
- [Feat] : configuration HOT 2
- [Feat] : WS HOT 17
- socket.io HOT 3
- 오늘까지 로그인 데모 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nestj.