I'm fetching tasks from multiple APIs, namely Notion, Todoist and DevOps for now, and trying to merge them into a single collection of tasks so that I can sync them all.
First off, I normalize the tasks so they all have a similar signature like this for Notion:
return {
title: task.properties.Name.title[0].text.content,
status: status(), // Status enum mapping
priority: priority(), // Priority enum mapping
integrations: {
'Notion': {
id: task.id,
original: task
},
'Todoist': {
id: task.properties['Todoist'].number ?? undefined
},
'DevOps': {
id: task.properties['DevOps'].number ?? undefined
}
}
}
And I fetch them all into different arrays:
const fetchedTasks = Promise.all(integrations.map(integration => await integration.getTasks()))
But I don't know how to merge those arrays into a single one based on matching integration ids in an efficient manner. The only solution I could think of would be to loop over every item of every array, adding them to a new collection and checking that entire collection for a matching integration every time, but it just has too many loops to be the right solution.
Something like:
const merged = []
for (const collection of tasks) {
for (const task of collection) {
for (const integration of Object.keys(task.integrations)) {
const mergedTask = merged.find(mergedTask => mergedTask.integrations[integration].id === task.integrations[integration].id)
if (mergedTask)
mergedTask.integrations[integration].original = task
else
merged.push(task)
}
}
}
So that at the end I have a list of tasks with the same signature as the normalized task but with all existing integrations having their original value set.
As as sidenote, are there patterns for syncing similar data from multiple sources? I coulnd't find anything that applied here.