Hi there,
Anyone managed to write a function that supports dynamically setting or not setting page_after, page_before options on a paginate depending on options passed into a UDF?
As a test, I store an object like this page_size : {size:3} which you can see in the below code. When I pass that into the paginate as a Var(‘page_size’) it errors out with:
“code”: “invalid expression”,
“description”: “No form/function found, or invalid argument keys: { paginate, raw }.”
Map(
[
[
"TestArray",
"options",
Ref(Collection("Options"), "351011260955886162"),
{
page_size: 3, // This is optional
page_after : null, // These can be removed, or null if not avail.
page_before : null // These can be removed, or null if not avail.
}
]
],
Lambda(
["collection_id", "field_name", "field_value", "config"],
Let(
{
index_idx: Select(["index_idx"], Var("config"), 0),
index_name: Concat([
Var("collection_id"),
"_by_",
Var("field_name"),
"_ref"
]),
page_size : {
size : 3
// Default to 100 items (to be set as const via JS)
//size : Select(["page_size"], Var("config"), 100)
},
page_before_raw : Select(["page_before"], Var("config"), null),
page_after_raw : Select(["page_after"], Var("config"), null),
page_before : If(IsNull(Var('page_before_raw')), {}, {'page_before' : Var('page_before_raw')}),
page_after : If(IsNull(Var('page_after_raw')), {}, {'page_after' : Var('page_after_raw')}),
// You MUST put a [] around the second pair, otherwise merge does NOT work
page_options : Merge(Var('page_size'), [Var('page_before'), Var('page_after')]),
},
Map(
// Problem here - cannnot set Paginate paging with variables...
Paginate(
Match(Index(Var("index_name")), Var("field_value"), 30),
// Fauna does not like this...
Var('page_size')
), //, Var('page_options')
Lambda(
"index_result",
Let(
{},
{
//sel : Select([Var('index_idx')], Var('index_result'), ''),
sel: Select([Var("index_idx")], Var("index_result"), ""),
idx: Var("index_idx"),
index_result: Var("index_result"),
index_name: Var("index_name"),
x : Var('page_options')
}
)
)
)
)
)
)
If I remove
Var(‘page_size’)
then the code works.
If I use the form below as an argument to paginate it also works:
{size:Select([“page_size”], Var(“config”), 100)}
In the test output, page_options is correctly building to the object - we can see that x below has the value {size:30}
{
sel: "",
idx: 0,
index_result: Ref(Collection("TestArray"), "351186618918371928"),
index_name: "TestArray_by_options_ref",
x: {
size: 30
}
}
Ultimately the aim is to pass
Var(‘page_options’)
which dynamically contains the config based on what is passed in. But paginate does not like it.
Does anyone have any ideas on how I could solve this?
I was reading other posts, and it occurred to me that using range, and doing some other calculations to manage paging might be possible, but for now, I’d prefer to know how others are dynamically setting page_after/page_before when they are needed, and not when they are not.
All documentation I have found has statically typed text, I have not found an example of passing in variables. If anyone knows of documentation that explains this, or a way to solve this, I would be very grateful.
Thanks for taking time to read this!