【问题标题】:Using @casl/mongoose & mongoose-paginate-v2 together一起使用 @casl/mongoose 和 mongoose-paginate-v2
【发布时间】:2021-11-10 00:32:03
【问题描述】:

我试图在我的express.js 应用程序中使用@casl/mongoosemongoose-paginate-v2,但问题是必须在model 对象上使用这两个库。

  // trying this
  const results = await Person
      .accessibleBy(req['ability'])
      .paginate({}, options);

  // or this, the same problem
  const results = await Person
      .paginate({}, options)
      .accessibleBy(req['ability']);

在这两种方式中,我都得到了 accessibleBy/paginate 不是一个函数,因为正如我所说,两个库都必须在模型(在我的情况下为 Person)对象上使用。

非常感谢任何帮助或建议。

【问题讨论】:

    标签: node.js mongoose pagination casl


    【解决方案1】:

    @casl/mongoose 支持 mongoose 的静态和查询方法。所以,你可以这样做:

    Person.findOne({ _id: id }).accessibleBy(ability).where({ createdAt: { $lt: new Date() } })
    
    // or
    Person.accessibleBy(ability).findOne({ _id: id }).where({ createdAt: { $lt: new Date() } })
    

    同时分页插件仅支持静态方法,因为它向数据库发送多个查询,因此(可能)无法实现您想要的。要么和那个包的作者谈谈,让他用那个来做点什么,要么写自己的分页插件

    【讨论】:

    • 我最终手动编写了自己的分页逻辑。谢谢@sergii。
    猜你喜欢
    • 2020-11-29
    • 2022-10-03
    • 2021-08-19
    • 2020-08-07
    • 2020-11-03
    • 2023-01-05
    • 1970-01-01
    • 2016-12-07
    • 2015-06-09
    相关资源
    最近更新 更多