Create an array of N*M length with specific values in pure FQL to Map-Create multiple documents

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:

  1. A way to generate monthList in pure FQL with endMonth as input,
  2. 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…)

I was actually able to solve my issue in a different way, by instead of adding months to validFrom, I subtracted months from validTo instead.

However, I do believe the question is interesting, and would love to see a pure FQL approach for this - if possible?