用于Prisma查询的多个逻辑运算符

时间:2019-07-19 01:52:21

标签: reactjs postgresql graphql prisma

您好,我正在查询数据库中符合以下条件的条目:

  1. A和B都必须满足。

  2. 如果C存在,则必须同时满足A,B和C。

每个类型中都有B,这意味着B对于A和C都是通用的。

在我的特定示例中,A对应于userId,B对应于isShared,C对应于lenderId。我希望能够查询同时符合userIdisShared的条目。但是,如果存在lenderId,我想要满足userIdisShared以及lenderidisShared的条目。

type Location {
    user: User!
    isShared: Boolean!
}

我提出了几种方案,但不确定它们是否是最佳方案。

        const [location] = await prisma.query.locations({
            where: {
                OR: [{
                    AND: [{
                        user: {
                            id: userId
                        },
                        isShared: true
                    }],
                    AND: [{
                        user: {
                            id: lenderId
                        },
                        isShared: true
                    }]
                }]
            },
            last: 1,
        }, info)

上面具有(A and B) or (C and B)的结构。

        const [location] = await prisma.query.locations({
            where: {
                OR: [{
                    user: {
                        id: userId
                    },
                    user: {
                        id: lenderId
                    }
                    }],
                AND: [{
                    isShared: true
                }]
            },
            last: 1,
        }, info)

上面具有(A or B) and C的结构。

我似乎遇到的问题是查询中始终包含C,即lenderId,就好像它使用的是AND运算符,而不是OR一样。

0 个答案:

没有答案