Hey there,
I just started using FaunaDB and I also started to write queries for different situations.
For example: I have a route /videos
and anther route /videos/admin
. My backend controller forwards the request to my video-service then queries the database. The difference between the admin-route and the non-admin route is that for the non-admin route only videos are returned that have a true-flag at isPublished
.
My attempt is the following:
-
Creating two indexes:
get_all_published_videos_index
andget_all_videos_index
-
get_all_published_videos_index
has theterm
data.isPublished
and an emptyvalue
--> gives me theref
by default -
get_all_videos_index
has both fieldsterm
andvalue
empty -
Creating two functions:
getAllPublishedVideosFn
andgetAllVideosFn
-
The functions are called by the service with:
fauna_client.query(q.Call(q.Function('getAllVideosFn')))
and
fauna_client.query(q.Call(q.Function('getAllPublishedVideosFn')))
respectively -
getAllPublishedVideosFn
looks like this:Query( Lambda( "", Select( "data", Map( Paginate(Match(Index("get_all_published_videos_index"), true)), Lambda( "videoRef", Let( { videoDoc: Get(Var("videoRef")) }, { id: Select(["ref", "id"], Var("videoDoc")), title: Select(["data", "title"], Var("videoDoc")), slug: Select(["data", "slug"], Var("videoDoc")), urls: Select(["data", "urls"], Var("videoDoc")), ... } ) ) ) ) ) )
Gives me back an array with all videos that have the isPublished: true
flag
2.2) getAllVideosFn
looks like this:
Query(
Lambda(
[],
Select(
"data",
Map(
Paginate(Match(Index("get_all_videos_index"))),
Lambda(
"videoRef",
Let(
{ videoDoc: Get(Var("videoRef")) },
{
id: Select(["ref", "id"], Var("videoDoc")),
title: Select(["data", "title"], Var("videoDoc")),
urls: Select(["data", "urls"], Var("videoDoc")),
...
}
)
)
)
)
)
)
Gives me all videos back.
Questions: (1) Is this most efficient way and (2) are the queries supposed to be that explicit where I have to select every property I want fill with values manually?
Thanks a lot!