I’m trying to refactor some queries that I’ve written to be more efficient.
So I’ve been following this documentation.
One of my new queries looks like this:
Map(
Paginate(
Join(
Union(matches),
Index(
"sort_recipes_by_author_asc_updatedAt_desc"
)
),
options // <= size and the after cursor
),
Lambda((author, updatedAt, ref) => Get(ref))
)
);
matches
is being built like this:
const matches = ids.map((id: string) =>
Match(Index("find_recipes_by_chefId"), id)
);
The sort index looks. like this:
{
name: "sort_recipes_by_author_asc_updatedAt_desc",
unique: false,
serialized: true,
source: "Recipe",
terms: [
{
field: ["ref"]
}
],
values: [
{
field: ["data", "author"]
},
{
field: ["data", "updatedAt"],
reverse: true
},
{
field: ["ref"]
}
]
}
What I’m expecting is for the data to be sorted in “groups,” meaning that the resulting data will be sorted by chefName
ascending then by updatedAt
descending.
What I’m getting is: { before: [ null ], data: [] }
When I use the FaunaDB UI, can get a result from the sort query using a single ref:
While troubleshooting this, I created a few other indexes, and in doing so, I serendipitously created an index with a value that does not exist on a Recipe
: chefName
.
{
name: "sort_recipes_by_chefName_desc_updatedAt_desc",
unique: false,
serialized: true,
source: "Recipe",
terms: [
{
field: ["ref"]
}
],
values: [
{
field: ["data", "chefName"], <= does not exist in Recipe doc data
},
{
field: ["data", "updatedAt"],
reverse: true
},
{
field: ["ref"]
}
]
}
Using this as the sorting index, I do get data, but it’s unsorted, as–I believe–chefName
is null:
Again author
is definitely a data value in Recipe, while chefName
is not.
So, I’m confused. There’s a 99% chance that I’m doing something wrong here, but I can’t figure it out after hours of battling this.
Any help would be most appreciated.