This lets us handle mutations gracefully! A little unscalable, but honestly works well for what we're doing right now, and a useful pattern to have in the back pocket
···1+alter type public."pull_result" add attribute "publications" json;
2+3+set check_function_bodies = off;
4+5+CREATE OR REPLACE FUNCTION public.pull_data(token_id uuid, client_group_id text)
6+ RETURNS pull_result
7+ LANGUAGE plpgsql
8+AS $function$DECLARE
9+ result pull_result;
10+BEGIN
11+ -- Get client group data as JSON array
12+ SELECT json_agg(row_to_json(rc))
13+ FROM replicache_clients rc
14+ WHERE rc.client_group = client_group_id
15+ INTO result.client_groups;
16+17+ -- Get facts as JSON array
18+ SELECT json_agg(row_to_json(f))
19+ FROM permission_tokens pt,
20+ get_facts(pt.root_entity) f
21+ WHERE pt.id = token_id
22+ INTO result.facts;
23+24+ -- Get publication data
25+ SELECT json_agg(row_to_json(lip))
26+ FROM leaflets_in_publications lip
27+ WHERE lip.leaflet = token_id
28+ INTO result.publications;
29+30+ RETURN result;
31+END;$function$
32+;