Typeorm find with and & or where block

Viewed 4281

I am trying to build below query in typeorm.

SELECT * from Department where
type = 'Employee' and
((from_date BETWEEN '2013-01-03'AND '2013-01-09') OR 
(to_date BETWEEN '2013-01-03' AND '2013-01-09') OR 
(from_date <= '2013-01-03' AND to_date >= '2013-01-09'))

Below is my typeorm equivalent.

connection.find(Department, {
      where: [
        { fromDate: Between(filter.fromDate, filter.toDate) },
        { toDate: Between(filter.fromDate, filter.toDate) },
        {
          fromDate: LessThanOrEqual(filter.fromDate),
          toDate: MoreThanOrEqual(filter.toDate),
        },
      ],
      andWhere: { type: 'Employee' },
    });

But somehow I am getting wrong number of output. It is as if andWhere is not working. Thanks in advance

1 Answers
let qb = this.repository.createQueryBuilder("department");

qb.where("department.type= :type", {type: "Employee"});
qb.andWhere("((department.from_date BETWEEN '2013-01-03'AND '2013-01-09') OR 
(department.to_date BETWEEN '2013-01-03' AND '2013-01-09') OR 
(department.from_date <= '2013-01-03' AND department.to_date >= '2013-01-09'))");
Related