1

我有以下羽毛续集查询:

context.params.sequelize = {
    raw: false,
    include: [{
      model: organisations,
      where: organisationId ? { id: organisationId } : undefined,
      include: [{
        model: roles,
        where: roleId ? { id: roleId } : undefined,
      }],
    }],
    where: single ? (
      sequelize.and(
        sequelize.where(
          sequelize.col('"organisations->organisation_users"."roleId"'),
          sequelize.col('"organisations->roles"."id"'),
        ),
        sequelize.where(
          sequelize.col('"organisations->organisation_users"."userId"'),
          sequelize.col("users.id")
        )
      )
    ) : undefined
  }

我提出以下服务请求:

await UserService.find({ query: { email: data.email } }))

查询对象被忽略了,因为在上面的 sequelize 查询中我设置了自己的 where 语句。如果我删除 where 语句,则查询对象将起作用。如何使用我自己的 where 语句并包含羽毛查询对象?

谢谢和问候,

埃米尔

4

1 回答 1

0

为了向现有查询对象添加额外的条件,而不是使用params.sqeuelize,您可以将它们直接添加到查询对象中(因为它可以通过 context.params.query 获得)并且由于您使用了 before 钩子,因此查询对象得到执行前修改。在你的例子应该是这样的:

if(single) {
    context.params.query.push(
      sequelize.and(
        sequelize.where(
          sequelize.col('"organisations->organisation_users"."roleId"'),
          sequelize.col('"organisations->roles"."id"'),
        ),
        sequelize.where(
          sequelize.col('"organisations->organisation_users"."userId"'),
          sequelize.col("users.id")
        )
      )
   )
}

可能不是正确的语法,但这就是想法:直接在羽毛查询对象中添加额外的条件。希望这可以帮助。

于 2019-05-24T18:25:59.520 回答