kor*_*rxz 5 typescript typeorm node.js-typeorm
我正在尝试使用存储库查找来编写查询,但似乎没有找到适合我的查询的解决方案。我知道这可以通过查询生成器或 Raw() 条件实现,但如果可能的话,我想使用存储库查找。
我想要实现的条件是field1 = 'string' AND (field2 IS NULL OR field2 >= Date())。
目前唯一适合我的解决方案是:
where: [
{ param1: 'string', field2: IsNull() },
{ param1: 'string', field2: MoreThenOrEqual(new Date() ) }
]
Run Code Online (Sandbox Code Playgroud)
但这将转换为(field1 = 'string' AND field2 IS NULL) OR (field1 = 'string' OR field2 >= Date())
我尝试过类似的东西:
where: [
{ param1: 'string', field2: ( IsNull() || MoreThenOrEqual(new Date() ) ) }
]
Run Code Online (Sandbox Code Playgroud)
但我可以让它发挥作用。有谁知道我原来的 where 条件是否可以在不更改查询的情况下完成?
仅供参考:我正在使用 Typescript 和 TypeORM。
编辑1. 根据评论的请求,我使用queryBuilder发布我当前的实现(只是where条件):
.where('param1 = :var1', {va1: 'string'})
.andWhere(new Brackets(query => {
query.where('field2 IS NULL')
.orWhere('field3 >= NOW()::DATE')
})
Run Code Online (Sandbox Code Playgroud)
typeorm中有一个括号
用法:
const sql = await connection
.createQueryBuilder(User, "user")
.where("user.isAdmin = :isAdmin", { isAdmin: true })
.orWhere(
new Brackets((qb) => {
qb.where("user.firstName = :firstName1", {
firstName1: "Hello",
}).andWhere("user.lastName = :lastName1", {
lastName1: "Mars",
})
}),
)
.orWhere(
new Brackets((qb) => {
qb.where("user.firstName = :firstName2", {
firstName2: "Hello",
}).andWhere("user.lastName = :lastName2", {
lastName2: "Earth",
})
}),
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16529 次 |
| 最近记录: |