Migrating query to edge functions because it takes too long on the front end

I have following function
export const massImportCrmProjectsWithConfigs = async ({
  importedCrmProjects,
  crmId,
}: MassImportCrmProjectsWithConfigsParams) => {
  const insertedCrmProjectsIds: Tables<'crm_project'>['id'][] = [];
  try {
    for (const crmProject of importedCrmProjects) {
      const importedCrmProjectRes = await supabase
        .from('crm_project')
        .insert({
          ...crmProject,
          crm_id: crmId,
        })
        .select()
        .maybeSingle()
        .throwOnError()
        .then((res) => res.data);
      if (!importedCrmProjectRes)
        throw new Error('Failed to insert CRM Project');
      insertedCrmProjectsIds.push(importedCrmProjectRes.id);
      for (const crmConfig of crmProject.crm_config) {
        await supabase
          .from('crm_config')
          .insert({
            ...crmConfig,
            crm_id: crmId,
            crm_project_id: importedCrmProjectRes.id,
          })
          .select()
          .maybeSingle()
          .throwOnError()
          .then((res) => res.data);
      }
    }
  } catch (err) {
    await supabase
      .from('crm_project')
      .delete()
      .in('id', insertedCrmProjectsIds)
      .throwOnError();
    throw err;
  }
};
Was this page helpful?