【问题标题】:Prisma - Sum amount - more than 350’000 rowsPrisma - 总和 - 超过 350'000 行
【发布时间】:2019-12-10 07:11:16
【问题描述】:

我有一个发票模型,例如:

type Invoice {
  id
  amount
}

我有 350,000 张发票。我如何汇总所有发票的金额。 (最大限制为 1000)

这个 hack 行不通:https://www.prisma.io/forum/t/query-all-size-limit/557,因为我的行太多。

相关问题:https://github.com/prisma/prisma/issues/2162https://github.com/prisma/prisma/issues/259https://www.prisma.io/forum/t/query-all-size-limit/557https://www.prisma.io/forum/t/sun-amount-more-than-350000-rows/7611

【问题讨论】:

  • 这样的事情听起来最好在 GraphQL 服务器上计算和提供; Invoice 是否有 pagination 实现?如果您坚持查询,则可以改为进行多个查询,一次获取 1,000 个。
  • 感谢@Jim“最好在 GraphQL 服务器上计算和服务” ==> 我在 GraphQL 服务器上执行此查询.. 在解析器中。我只会返回结果到前端

标签: sum graphql prisma


【解决方案1】:

您可以在 Prisma 中使用自定义 SQL 查询构建自定义解析器,该查询将为您求和;

一个示例实现可能类似于::

const client = new Client({
  database: "prisma",
  user: "...",
  password: "...",
  host: "localhost",
  port: 3000
})

const resolvers = {
  Query: {
    async sumInvoices(parent, args, context, info){
      const response = await client.query(
        `SELECT SUM(amount) FROM Invoice WHERE YOUR_CONDITION`
      );
      return response;
    };
};

您也可以查看 Ben Awad 的视频以获取更多示例:https://www.youtube.com/watch?time_continue=12&v=YUjlBuI8xsU

【讨论】:

    猜你喜欢
    • 2021-12-15
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多