TypeORM AND Find Option

Viewed 791

I have products with a price, I want to find products whose price will be less than 10 but more than 5. So that SQL query looks like this:

SELECT * FROM products WHERE products.price < 10 AND products.price > 5

Is it possible to do this without using Query Builder?

I don't find the And operator in the documentation

2 Answers

You just need to use where operator.

userRepository.find({ where: { firstName: "Timber", lastName: "Saw" } });

This code executes this query:

SELECT * FROM "user" WHERE "firstName" = 'Timber' AND "lastName" = 'Saw'

And if you want to use OR, you use an array of conditions:

userRepository.find({
    where: [
        { firstName: "Timber", lastName: "Saw" },
        { firstName: "Stan", lastName: "Lee" },
    ],
});

This code will executes this query:

SELECT * FROM "user" WHERE ("firstName" = 'Timber' AND "lastName" = 'Saw') OR ("firstName" = 'Stan' AND "lastName" = 'Lee')

More infos in the doc: https://github.com/typeorm/typeorm/blob/master/docs/find-options.md

Related