Hi there,
Brecht gave a great answer to this thread SQL group by, having counterpart in FaunaDB (which ty Cory for pointing it out to me in my prior tangentially-related forum post), but I’m wondering how one would modify that answer to work in a GraphQL UDF that supports pages in the same way the docs demonstrate with UDF that returns a database page. Since the former relies on mapping through a collection and the latter relies on paginating an index match, I’m struggling to combine them into a single solution.
For context, in a simplified version of my particular use case, I have a Reviews
collection and a Movies
collection, and want to find “trending” movies based on # of reviews in the last 30 days.
I can do something like the below to calculate a review count within a dynamic date range, though I am not sure how to sort or paginate with that review count since it’s not coming from an index.
CreateIndex({
name: "reviews_by_movie",
source: Collection("Reviews"),
terms: [
{ field: ["data", "movie"] }
],
values: [
{ field: ["data", "created"], reverse: true },
{ field: ["ref"] }
]
})
Query(
Lambda(
["input"],
Let(
{
trending: Map(
Paginate(Documents(Collection("Movies"))),
Lambda(
["movie"],
Let(
{
movieRefs: Range(
Match(Index("reviews_by_movie"), Var("movie")),
Now(),
TimeSubtract(Now(), 30, "days")
),
count: Count(Var("movieRefs"))
},
{ trendingCount: Var("count"), movie: Var("movie") }
)
)
)
},
Select("data", Var("trending"))
)
)
)
If what I’m after is not directly possible with FQL, would the best practice approach be some sort of cron job to routinely Map
over my Movies
collection and update each document with a freshly calculated trendingCount
I can use with an index? Any and all feedback greatly appreciated
Cheers,
Ross