R
R
Robin Alikhanov2022-02-06 14:31:27
JavaScript
Robin Alikhanov, 2022-02-06 14:31:27

How to make a search query on multiple TypeOrm parameters?

Conditions is the essence of the order
in it the fields status, name, description, the main text
is the code

queryBuilder
        .where("orders.status = :status", {
          status: query.status,
        })
        .orWhere("orders.title LIKE :title", {
          title: `%${query.name}%`,
        })
        .orWhere("orders.description LIKE :description", {
          description: `%${query.name}%`,
        });


I need to sort by status necessarily and at the same time to search for the keyword in the title and description and in the body of the order.
If I put andWhere after Where, it will search only in the title and search by status, if I leave it like right now, then sorting by status falls off.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Loli E1ON, 2022-02-06
@robin1985

You can use the callback in where:

queryBuilder.where("orders.status = :status", {
  status: query.status,
}).andWhere(
  new Brackets((qb) => {
    qb.orWhere("orders.title LIKE :title", {
      title: `%${query.name}%`,
    }).orWhere("orders.description LIKE :description", {
      description: `%${query.name}%`,
    });
  })
);

Either like this:
queryBuilder.where(
  "orders.status = :status AND (orders.title LIKE :title OR orders.description LIKE :description)",
  {
    status: query.status,
    title: `%${query.name}%`,
    description: `%${query.name}%`,
  }
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question