I am trying to query data from DB using typeorm. How can I make the same query using queryBuilder?
const data = await this.dataRepository.findOne({
where: [
{ field1: input1, field2: input2 },
{ field1: input2, field2: input1 }
]
});
CodePudding user response:
By using a complex where
clause using Brackets
import { Brackets } from "typeorm"
const data = await this.dataRepository(Data) // Data refers to your Entity Class
.createQueryBuilder("data")
.where(new Brackets(qb => {
qb.where("data.field1 = :field1", { field1: 'input1' })
.andWhere("data.field2 = :field2", { field2: "input2" })
}))
.orWhere(new Brackets(qb => {
qb.where("data.field1 = :field1", { field1: 'input2' })
.andWhere("data.field2 = :field2", { field2: "input1" })
}))
.getOne();