Hi there!
I need to create N documents based on a value in a parent document. Currently I am doing this:
let monthList = [];
const END_MONTH = 11;
for (let i=0; i < nMonths; i++) {
for (let j=0; j < addedMonthly; j++) {
monthList.push(END_MONTH - i);
}
}
This typically returns a list like
// nMonths=3, addedMonthly=2
[11, 11, 10, 10, 9, 9]
I use FQL to write this to Fauna - and this works well enough today.
q.Map(
monthList,
q.Lambda(
'monthCount',
q.Create(
q.Collection("MyItem"),
{data: {
parentRef: q.Var('parentRef'),
valid: {
from: normalizeDate(q.Var('validFrom'), q.Var('monthCount')), // normalizeDate is a pure FQL method to return a valid date N months from a certain date.
to: q.Var('validTo') // The vars validFrom and validTo are extracted from the parent document
}
}}
)
)
)
However, I now need to set the END_MONTH
based on a variable from the parent document. Thus, if the parent document has endMonth: 5
, I need monthList=[5, 5, 4, 4, 3, 3]
So what I need is either:
- A way to generate
monthList
in pure FQL withendMonth
as input, - A tip for a different way of solving such a “bulk” create.
Or is it possible to use the FQL-retrieved value for endMonth
in a JS function somewhere within the query? (I suppose this does not work…)