v3
하… 이 이슈… 애초에 휴먼 에러 때문이었음;;
andWhere 써야 하는데 where 사용
const qb = this.postingsRepository
.createQueryBuilder('p')
.where('p.title LIKE :keyword', { keyword: `%${keyword}%` })
if (budget) {
// 휴먼 에러...
qb.where('p.budget = :budget', { budget });
// 이렇게 수정해야 함
qb.andWhere('p.budget = :budget', { budget });
}
다중 선택 가능 태그, andWhere 넣을 때 괄호( ) 생략
목표
a AND b AND ( c OR cc OR OR ccc ) AND d현실
a AND b AND c OR cc OR OR ccc AND dif (themes) {
const orCondtions = themes
.map((theme, index) => `JSON_CONTAINS(p.theme, :theme${index})`)
.join(' OR ');
const params = themes.reduce(
(params, theme, index) => ({
...params,
[`theme${index}`]: JSON.stringify(theme),
}),
{}
);
// 여기가 문제!
qb.andWhere(orCondtions, params);
// orConditions 앞뒤로 괄호 붙이도록 수정
qb.andWhere(`(${orCondtions})`, params);
}
v2
NestJS에서 TypeORM의 Brackets를 사용해보자!
v1
postings?kewyord=안&theme[]=맛집&theme[]=힐링
안
이 포함되면서 theme에 맛집 OR 힐링
이 포함된 게시글을 반환해야 하는데…
안
in title) AND (힐링
in theme OR 맛집
in theme)안
in title) OR (힐링
in theme OR 맛집
in theme)