3

Precondition: I'm using Typeorm to perform database queries and I can't use the queryBuilder methods to create this query, but only the find method.

I need to translate a query like:

SELECT * FROM address where zip = 123 and (street = 'asd' or city = 'New York');

I know that the solution can be trivial using the usual queryBuilder, but in this particular case I cannot use it.

Is it possible to implement these nested and/or conditions only using the find method?

LucaRoverelli
  • 1,116
  • 1
  • 9
  • 15

1 Answers1

2

Can you please try this?

Address.find([{
  zip: 123, street: 'asd'
}, {
  zip: 123, city: 'New York'
}]);

if you have just and, or conditions, yes! it's possible to implement the query only with find method. In typeorm query, you can represent or with the array of the conditions.

zip = 123 and (street = 'asd' or city = 'New York')

is same as

(zip = 123 and street = 'asd') or (zip = 123 and city = 'New York')
critrange
  • 5,652
  • 2
  • 16
  • 47
  • Well, you are right. The problem is that inverting the position of the OR and AND conditions can generate a less efficient query. But, at the moment, it seems to be the unique possibility. – LucaRoverelli Jul 13 '20 at 07:21