等效于sql`情况何时...然后进行自定义排序列? (Prisma / GraphQl)

发布于 2025-02-11 19:41:04 字数 2722 浏览 1 评论 0 原文

方案:

  1. 让我们假设一个事件模型:

     模型事件{
      ID int @id @default(autoincrement())
      标题字符串
      发生DateTime? //可无效
      CreateT DateTime @default(NOW())
    }
     
  2. 让我们假设这是数据:

    id title 发生了 创建
    1 archcon 2022-07-01 12:00:00 Z 2022-06-29 11:53:00z
    2 bestcon 2022-08-03 12:00:00z 2022-06-29 11:54:00z
    3 comicon null 2022-07-02 10:11:00z
    4 devcon 2022-07-05 12:00:00z 2022-07-22 10:33:00z
  3. 要求
    按事件日期对升序(发生),或者如果尚未按计划进行,请改用 createionat 。为了说明,这将是所需的查询结果:

    id title sort_date
    1 archcon 2022-07-01 12:00:00 Z
    3 comicon 2022-07-02 10:11:00z
    4 devcon 2022-07-05 12:00:00z
    2 bestcon 2022-08-03 12:00:00z

现在我记得那天我会使用 case> case何时…然后… -construct 获取自定义sort_date column(或也许 isnull )从数据库中出现。

Prisma Orm功能可以实现吗?


xy-problem promeraper :我们的项目使用Redwoodjs,在此上面有一层GraphQl。我做了一项快速的研究,但是似乎并没有为我所追求的范围远程制作graphql,但是我也接受GraphQl的指示,因为我很高兴被证明是错误的。

当然,人们总是可以“手动”转换查询结果,但这里的问题是Prisma或GraphQl(或Redwoodjs中包含的任何其他机制)是否确实支持这一点。

Scenario:

  1. Let's assume a model of Events:

    model Event {
      id           Int         @id @default(autoincrement())
      title        String
      happeningAt  DateTime?   // ???????? nullable
      createdAt    DateTime    @default(now())
    }
    
  2. Let's assume this is the data:

    id title happeningAt createdAt
    1 ArchCon 2022-07-01 12:00:00Z 2022-06-29 11:53:00Z
    2 BestCon 2022-08-03 12:00:00Z 2022-06-29 11:54:00Z
    3 ComiCon null 2022-07-02 10:11:00Z
    4 DevCon 2022-07-05 12:00:00Z 2022-07-22 10:33:00Z
  3. The requirement:
    Sort ascending by event date (happeningAt), or if unscheduled yet, use creationAt instead. To illustrate, this would be the required query result:

    id title sort_date
    1 ArchCon 2022-07-01 12:00:00Z
    3 ComiCon 2022-07-02 10:11:00Z
    4 DevCon 2022-07-05 12:00:00Z
    2 BestCon 2022-08-03 12:00:00Z

Now i remember back in the days i would have just used the CASE WHEN … THEN …-construct to get custom sort_date column (or maybe ISNULL) out of the database.

Can this be achieved with Prisma ORM functionality?


XY-Problem disclaimer: Our project uses RedwoodJS, which has a layer of GraphQL on top of this. I did a quick research, but it doesn't seem that GraphQL even remotely is made for what i'm after – but i'd also accept a GraphQL-approach as an answer should i be happily proven wrong.

Of course one can always "manually" transform the query result in typescript, but the question here is whether Prisma or GraphQL (or any other mechanism included in RedwoodJS) do support this natively.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨光如昨 2025-02-18 19:41:04

prisma orderby 子句中提及 case 的能力。

我发现一些问题似乎讨论了此功能或解决方法。

  1. 这个问题似乎是您正在搜索的问题,但是从标题中不太清楚。
  2. https://github.com/prisma/prisma/prisma/issues/issues/10335 带有更精确标题的存档Prisma回购/128
  3. 计算的字段可能能够解决此问题。这是不可能的,但是看起来它位于Prisma客户端扩展的顶部epic https:> https: //github.com/prisma/client-planning/issues/25 。然后,您将能够添加此计算的字段 - 将拉动发生的日期,或者如果尚未计划,则 createionat - 并对其进行排序。 https://github.com/prisma/prisma/prisma/issues/issues/3394
  4. 我猜我猜想使用prisma raw查询将是另一个选项 https://github.com/prisma/prisma/prisma/client--client--client--client--计划/问题/25
  5. 如果您不进行编码,则可以在从数据库中提取记录后与JavaScript进行排序。
  6. 即使使用Paginating,您也可以获取所有记录并遵循选项5,但这可能会增加您的执行时间(您可能要做的原因开始的原因)

The prisma docs don't mention the ability for using CASE in the orderBy clause.

I have found some issues which seem to discuss this functionality or workaround.

  1. This issue seems to be what you're searching for however it's not so clear from the title. https://github.com/prisma/prisma/issues/10335
  2. This is in the archived prisma repo with a more precise title https://github.com/prisma/specs/issues/128
  3. Computed fields may be able to solve this. It's not possible yet but it looks like it's on the top of the Prisma Client Extension epic https://github.com/prisma/client-planning/issues/25. You would then be able to add this computed field- which would pull in the happeningAt date or if not yet scheduled then the creationAt- and sort on it. https://github.com/prisma/prisma/issues/3394
  4. I guess using a prisma raw query would be another option https://github.com/prisma/client-planning/issues/25.
  5. If you're not paginating you can sort with javascript after pulling the records from the database.
  6. Even with paginating you can get all the records and follow option 5, but this may add a lot to your execution time (the reason you may be doing paginating to begin with)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文