[typeOrm]Raw 쿼리 사용예시

코드깎는 노인·2021년 2월 26일
0
post-custom-banner

1

// already there
 
 querybuilder
  .[...]
  .where('LOWER(users.email) = :email', { email: email.toLowerCase() });

// unsafe via repository, since params are unescaped:
repo.findOne({
  where: {
    email: Raw(alias => `LOWER(${alias}) = '${email.toLowerCase()}'`).
  },
});

// would be nice to have:
repo.findOne({
  where: {
    email: Raw(alias => 'LOWER(${alias}) = :email', { email: email.toLowerCase() }),
  },
});

2

const [result, total] = await this.findAndCount(
    {
  where: { title: Raw(alias => `LOWER(${alias}) Like '%${value}%'`) },

take: take,
  skip: skip
}

);
  return {
    data: result,
    count: total
  }
  • ilike보다 소문자로 바꿔서 like로 찾는게 빠름

출처
https://github.com/typeorm/typeorm/issues/1231
https://github.com/typeorm/typeorm/blob/master/docs/find-options.md
https://github.com/typeorm/typeorm/issues/4418
https://github.com/typeorm/typeorm/issues/3329
https://github.com/typeorm/typeorm/issues/2475

profile
내가 볼려고 만든 블로그
post-custom-banner

0개의 댓글