harry
harry

Reputation: 647

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.

Upvotes: 7

Views: 13895

Answers (2)

harry
harry

Reputation: 647

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 });

Upvotes: 4

Prashant Sinha
Prashant Sinha

Reputation: 71

Let me know if below is what you are looking for.

const item = await getManager().createQueryBuilder(Item, "item").innerJoinAndSelect("item.item_location", "item_location", "item_location.locationId = :queryfilter", { queryfilter: queryObject.filter}).select("item.name, item.id").getMany();


Upvotes: 0

Related Questions