NangoHQ / nango

A single API for all your integrations.
https://www.nango.dev
Other
4.82k stars 439 forks source link

fix(server): Update query to paginate correctly against filter #3012

Closed nalanj closed 5 days ago

nalanj commented 1 week ago

Describe your changes

Update the connections list query to filter errored connections appropriately. This was a little tricky! The key element though is the FILTER against json_agg. The query I'm targeting roughly looks like this:

SELECT row_to_json(_nango_connections.*) as connection,
  row_to_json(end_users.*) as end_user,
  _nango_configs.provider,
  COALESCE(
    json_agg(
        json_build_object('type', _nango_active_logs.type, 'log_id', _nango_active_logs.log_id)
    ) FILTER (WHERE _nango_active_logs.id IS NOT NULL)
    , '[]'::json
  ) as active_logs,
  count(_nango_active_logs.id) as active_logs_count
FROM _nango_connections
    INNER JOIN _nango_configs ON "_nango_connections"."config_id" = "_nango_configs"."id"
    LEFT JOIN end_users ON end_users.id = _nango_connections.end_user_id
    LEFT JOIN _nango_active_logs ON _nango_connections.id = _nango_active_logs.connection_id
        AND _nango_active_logs.active = true
WHERE _nango_connections.environment_id = 2
    AND _nango_connections.deleted = false
GROUP BY _nango_connections.id, end_users.id, _nango_configs.provider
    HAVING count(_nango_active_logs.id) = 0

Issue ticket number and link

https://linear.app/nango/issue/NAN-2134/bug-with-connection-errors

Checklist before requesting a review (skip if just adding/editing APIs & templates)

linear[bot] commented 1 week ago

NAN-2134 Bug with connection errors