출처: https://velog.io/@yukina1418/TypeORM-CreateQueryBuilder-%EC%BB%A8%EB%8B%9D%ED%8E%98%EC%9D%B4%ED%8D%BC
이것을 포스팅하는 이유는 정말 단순하다.
내가 찾아보는데 너무 짜증나고 답답해서 걍 내가 생각날 때 마다, 사용하는 것 마다 모아놓으려고(....)
근데 이게 0.2버전이라 0.3버전에서도 적용되는지 솔직히 모르겠다.
대부분 호환이 된다고는 알고있는데 (쿼리빌더쪽은) 정말 되는지는...해봐야알듯..ㅠ
주의 ! ${} 사이에 들어가는 값이 문자열이라면, 쌍따옴표를 꼭 넣어줘야한다 !
(적힌 것 외에도 다양한 방법이 존재하고, 각각 방향성이 약간씩 차이가 있다고한다.)
await this.connection
.createQueryBuilder(User, 'user')
.where(`id = ${userId}`)
.getOne();
await this.connection
.createQueryBuilder(User, 'user')
.where(`user.id = ${userId}`)
.getOne();
await this.connection
.createQueryBuilder(User, 'user')
.where('user.id = :userId', { userId })
.getOne();
await this.connection
.createQueryBuilder(User, 'user')
.where('user.id = :userId', { userId : payload.userId })
.getOne();
await this.connection
.createQueryBuilder(User, 'user')
.where({ id : userId })
.getOne();
await this.connection
.createQueryBuilder()
.select('user')
.from(User, 'user')
.where({ id : userId })
.getOne();
await this.connection
.createQueryBuilder(User, 'user')
.where('user.name = :name', { name })
.getOne();
await this.connection
.createQueryBuilder(User, 'user')
.where('user.name = :name ', { name:'바보' })
.getOne();
await this.connection
.createQueryBuilder(User, 'user')
.where('user.name = :name', { name })
.andWhere('user.gender =:gender',{gender:'man'})
.getOne()
await this.connection
.createQueryBuilder(User, 'user')
.where('user.name LIKE :name', { name: `%${김}%` })
.getOne();
아마 아래꺼도 되는걸로 앎
await this.connection
.createQueryBuilder(User, 'user')
.where('user.name LIKE :name', { name: `%"김"%` })
.getOne();
await this.connection
.createQueryBuilder(User, 'user')
.where('user.name IN (:names)', { names })
.getMany();
await this.connection
.createQueryBuilder(User, 'user')
.select(['user.name','user.age'])
.where('user.name IN (:names)', { names })
.getOne();
await this.connection
.createQueryBuilder(User, 'user')
.leftJoinAndSelect('user.item','item')
.where('user.name IN (:names)', { names })
.getOne();
await this.connection
.createQueryBuilder(User, 'user')
.select(['user.name','user.age','item.name'])
.leftJoinAndSelect('user.item','item')
.where('user.name IN (:names)', { names })
.getOne();
await this.connection
.createQueryBuilder(User, 'user')
.select([
'user.address AS juso',
'user.name AS namae'
])
.where('user.name = :name', { name })
.getRawOne();
await this.connection
.createQueryBuilder(User, 'user')
.select([
'CONCAT('user.address,user.DetailAddres') address',
])
.where('user.name = :name', { name })
.getRawOne();
문자열을 넣고 싶을 경우 " " < 사이에 넣어야함!!
await this.connection
.createQueryBuilder(User, 'user')
.select([
'CONCAT("서울특별시 한강공원") address',
])
.where('user.name = :name', { name })
.getRawOne();
const data = "피자"
await this.connection
.createQueryBuilder(User, 'user')
.select([
'CONCAT("${data}") likeMenu',
])
.where('user.name = :name', { name })
.getRawOne();
await this.connection
.createQueryBuilder(User, 'user')
.select([
'CASE WHEN user.likeMenu ="피자" THEN "치킨" ELSE user.name END AS menu'
])
.where('user.age = :age', { age:10 })
.getRawOne();
await this.connection
.createQueryBuilder(User, 'user')
.select([
'user.address AS juso',
'user.name AS namae'
])
.where('user.name = :name', { name })
.andWhere('user.tags IS NOT NULL')
.getRawOne();
await this.connection
.createQueryBuilder(User, 'user')
.select([
'user.address AS juso',
'user.name AS namae'
])
.where('user.name = :name', { name })
.andWhere('user.tags IS NULL')
.getRawOne();
await this.connection
.createQueryBuilder(User, 'user')
.select([
'COUNT(CASE WHEN user.sity IN("인천","수원") THEN 1 END) AS gyeonggi'
])
.where('user.gender = :gender', { gender:"man" })
.getRawMany();
await this.connection
.createQueryBuilder(User, 'user')
.where('user.gender = :gender', { gender:"man" })
.andWhere(`user.createdAt BETWEEN '${start.toISOString()}' AND '${end.toISOString()}'`)
.getMany();
const payload = {name:"김씨",age:10}
await this.connection
.createQueryBuilder(User, 'user')
.where('user.gender = :gender', { gender:"man" })
.andWhere(new Brackets(qb)=>{
qb.orWhere(user.name = "${payload.name}");
qb.orWhere(user.age = "${payload.age}");
})
.getMany();
const menus = ["치킨","피자","족발"]
await this.connection
.createQueryBuilder(User, 'user')
.where('user.gender = :gender', { gender:"man" })
.andWhere(new Brackets(qb)=>{
menus.forEach(menu:string) => {
qb.orWhere(`user.likeMenu = "${menu}"`)
})
.getMany();
주기적으로 생각나고 까먹을 때 마다 추가 예정
Designed by sketchbooks.co.kr / sketchbook5 board skin
Sketchbook5, 스케치북5
Sketchbook5, 스케치북5
Sketchbook5, 스케치북5
Sketchbook5, 스케치북5