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
···11+alter type public."pull_result" add attribute "publications" json;
22+33+set check_function_bodies = off;
44+55+CREATE OR REPLACE FUNCTION public.pull_data(token_id uuid, client_group_id text)
66+ RETURNS pull_result
77+ LANGUAGE plpgsql
88+AS $function$DECLARE
99+ result pull_result;
1010+BEGIN
1111+ -- Get client group data as JSON array
1212+ SELECT json_agg(row_to_json(rc))
1313+ FROM replicache_clients rc
1414+ WHERE rc.client_group = client_group_id
1515+ INTO result.client_groups;
1616+1717+ -- Get facts as JSON array
1818+ SELECT json_agg(row_to_json(f))
1919+ FROM permission_tokens pt,
2020+ get_facts(pt.root_entity) f
2121+ WHERE pt.id = token_id
2222+ INTO result.facts;
2323+2424+ -- Get publication data
2525+ SELECT json_agg(row_to_json(lip))
2626+ FROM leaflets_in_publications lip
2727+ WHERE lip.leaflet = token_id
2828+ INTO result.publications;
2929+3030+ RETURN result;
3131+END;$function$
3232+;