方案:
-
让我们假设一个事件模型:
模型事件{
ID int @id @default(autoincrement())
标题字符串
发生DateTime? //可无效
CreateT DateTime @default(NOW())
}
-
让我们假设这是数据:
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 |
-
要求:
按事件日期对升序(发生
),或者如果尚未按计划进行,请改用 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:
-
Let's assume a model of Events:
model Event {
id Int @id @default(autoincrement())
title String
happeningAt DateTime? // ???????? nullable
createdAt DateTime @default(now())
}
-
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 |
-
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.
发布评论
评论(1)
prisma 在
orderby
子句中提及case
的能力。我发现一些问题似乎讨论了此功能或解决方法。
发生的
日期,或者如果尚未计划,则createionat
- 并对其进行排序。 https://github.com/prisma/prisma/prisma/issues/issues/3394The prisma docs don't mention the ability for using
CASE
in theorderBy
clause.I have found some issues which seem to discuss this functionality or workaround.
happeningAt
date or if not yet scheduled then thecreationAt
- and sort on it. https://github.com/prisma/prisma/issues/3394