Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to translate an SQL statement to TypeORM query builder?

How would I convert the code below to TypeORM querybuilder? I am trying to follow the documentation.

this.repository.manager.query(`
    SELECT item.name, item.id
    FROM item_location
    INNER JOIN item ON item.id = item_location.itemId
    WHERE item_location.locationId = ${queryObject.filter};
`)

Thanks.

like image 562
harry Avatar asked Jan 17 '18 08:01

harry


People also ask

What is type ORM?

TypeORM is a TypeScript ORM (object-relational mapper) library that makes it easy to link your TypeScript application up to a relational database database. TypeORM supports MySQL, SQlite, Postgres, MS SQL Server, and a host of other traditional options.


1 Answers

I figured it out.

await getManager()
    .createQueryBuilder(Item, 'item')
    .select(['item.id', 'item.name'])
    .innerJoin('item.location', 'location')
    .where('location.id = :id', { id: queryObject.filter });
like image 103
harry Avatar answered Sep 19 '22 09:09

harry