-- get_random_packages returns some random packages as a json object.
create or replace function get_random_packages()
returns setof json as $$
    select coalesce(json_agg(pkgJSON), '[]')
    from (
        select p.package_id
        from package p
        join snapshot s using (package_id)
        where s.version = p.latest_version
        and (s.deprecated is null or s.deprecated = false)
        and p.logo_image_id is not null
        and s.readme is not null
        order by random() limit 5
    ) rp
    cross join get_package_summary(rp.package_id) as pkgJSON;
$$ language sql;
