I have this schema: Board collection where the documents contain an embedded attribute: kpis array (an array of objects)
“ref”: Ref(Collection(“Board”), “12345678910”),
I need to do a partial update on the Board to update the kpis array in this way:
- if the kpi exists (by nameKey) the kpi value will be updated by adding the new value to the current value
- if the kpi doesn’t exist in the kpis array, the kpi will be added to the array.
There are 2 options to implement this update operation:
- I create a custom gql mutation (updateBoardKpis mutation) that is mapped to an fql function that implements this complex update logic (from above)
I want to know what is your opinion of what could be the best approach.
The first approach implies writing a complex fql function that receives the boardId and a kpis array: inputKpis, smth like this:
Based on the existing functions in the fql language this could be implemented by creating a new kpis array with the following:
- filter the kpis that are in the board kpis array and are not in the input kpis array
- filter the kpis that are in the input kpis array and are not in the board kpis array
- get the kpis that are in both array and update the value by adding the values of the kpis
And this final kpis array should be used to update the board kpis (replacing the board kpis with the new created one from those 3 steps)
I think that this approach is rather complex, and too long which could be too hard to maintain in the future.
- a fetch of the board object (req to Fauna: find query)
- the composition of a new kpis array in js (simpler) and then
- an update of the board (req to Fauna: update mutation).
What do you think is the best approach to use here? And if you suggest the first one, do you know an easier and simpler way to do the fql function: UpdateBoardKpis?