본문으로 바로가기

typeorm과 raw query를 조합하는 방법.

category DB, ORM/🧊 typeORM 2021. 3. 25. 04:05

raw query를 사용하고 싶다면 아래와 같이 작성하면 된다. 

공식 문서에 따르면 EntityManager는 모든 엔티티를 관리하기 위한 단일 장소라고 한다.

getManager나 Connection을 통해서 얻을 수 있다. 여기서는 getManager를 이용해보자.

Using EntityManager you can manage (insert, update, delete, load, etc.) any entity. EntityManager is just like a collection of all entity repositories in a single place.
You can access the entity manager via getManager() or from Connection. Example how to use it:

 

import { getManager } from 'typeorm';


const entityManager = getManager();
const [data] = await entityManager.query(`
  select token from biz_msg_token where id = (select max(id) from biz_msg_token);
`);

 

 

일부분만 raw query를 사용하고 싶을 때는 Raw를 이용하면 된다.

import { Raw } from "typeorm"

const [restaurants, totalResults] = await this.restaurants.findAndCount({
   where: {
        name: Raw(name => `${name} ILIKE '%${query}%'`),
    },
    skip: (page - 1) * 25,
    take: 25,
});

 


darren, dev blog
블로그 이미지 DarrenKwonDev 님의 블로그
VISITOR 오늘 / 전체