Skip to content

Commit

Permalink
Paging as links (#266)
Browse files Browse the repository at this point in the history
* Add paging as links rather than as prev/next

* adjust tests to use links, fix issues found while writing tests

* adjust basicsql tests for change in paging links
  • Loading branch information
bitner authored Apr 30, 2024
1 parent fbe9203 commit febbaf8
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 118 deletions.
31 changes: 29 additions & 2 deletions src/pgstac/migrations/pgstac.0.8.5-unreleased.sql
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,8 @@ DECLARE
_fields jsonb := coalesce(_search->'fields', '{}'::jsonb);
has_prev boolean := FALSE;
has_next boolean := FALSE;
links jsonb := '[]'::jsonb;
base_url text:= concat(rtrim(base_url(_search->'conf'),'/'));
BEGIN
searches := search_query(_search);
_where := searches._where;
Expand Down Expand Up @@ -685,26 +687,51 @@ BEGIN
END IF;
END IF;


links := links || jsonb_build_object(
'rel', 'root',
'type', 'application/json',
'href', base_url
) || jsonb_build_object(
'rel', 'self',
'type', 'application/json',
'href', concat(base_url, '/search')
);

IF has_next THEN
next := concat(out_records->-1->>'collection', ':', out_records->-1->>'id');
RAISE NOTICE 'HAS NEXT | %', next;
links := links || jsonb_build_object(
'rel', 'next',
'type', 'application/geo+json',
'method', 'GET',
'href', concat(base_url, '/search?token=next:', next)
);
END IF;

IF has_prev THEN
prev := concat(out_records->0->>'collection', ':', out_records->0->>'id');
RAISE NOTICE 'HAS PREV | %', prev;
links := links || jsonb_build_object(
'rel', 'prev',
'type', 'application/geo+json',
'method', 'GET',
'href', concat(base_url, '/search?token=prev:', prev)
);
END IF;

RAISE NOTICE 'Time to get prev/next %', age_ms(timer);
timer := clock_timestamp();


collection := jsonb_build_object(
'type', 'FeatureCollection',
'features', coalesce(out_records, '[]'::jsonb),
'next', next,
'prev', prev
'links', links
);



IF context(_search->'conf') != 'off' THEN
collection := collection || jsonb_strip_nulls(jsonb_build_object(
'numberMatched', total_count,
Expand Down
31 changes: 29 additions & 2 deletions src/pgstac/migrations/pgstac.unreleased.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3761,6 +3761,8 @@ DECLARE
_fields jsonb := coalesce(_search->'fields', '{}'::jsonb);
has_prev boolean := FALSE;
has_next boolean := FALSE;
links jsonb := '[]'::jsonb;
base_url text:= concat(rtrim(base_url(_search->'conf'),'/'));
BEGIN
searches := search_query(_search);
_where := searches._where;
Expand Down Expand Up @@ -3843,26 +3845,51 @@ BEGIN
END IF;
END IF;


links := links || jsonb_build_object(
'rel', 'root',
'type', 'application/json',
'href', base_url
) || jsonb_build_object(
'rel', 'self',
'type', 'application/json',
'href', concat(base_url, '/search')
);

IF has_next THEN
next := concat(out_records->-1->>'collection', ':', out_records->-1->>'id');
RAISE NOTICE 'HAS NEXT | %', next;
links := links || jsonb_build_object(
'rel', 'next',
'type', 'application/geo+json',
'method', 'GET',
'href', concat(base_url, '/search?token=next:', next)
);
END IF;

IF has_prev THEN
prev := concat(out_records->0->>'collection', ':', out_records->0->>'id');
RAISE NOTICE 'HAS PREV | %', prev;
links := links || jsonb_build_object(
'rel', 'prev',
'type', 'application/geo+json',
'method', 'GET',
'href', concat(base_url, '/search?token=prev:', prev)
);
END IF;

RAISE NOTICE 'Time to get prev/next %', age_ms(timer);
timer := clock_timestamp();


collection := jsonb_build_object(
'type', 'FeatureCollection',
'features', coalesce(out_records, '[]'::jsonb),
'next', next,
'prev', prev
'links', links
);



IF context(_search->'conf') != 'off' THEN
collection := collection || jsonb_strip_nulls(jsonb_build_object(
'numberMatched', total_count,
Expand Down
31 changes: 29 additions & 2 deletions src/pgstac/sql/004_search.sql
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,8 @@ DECLARE
_fields jsonb := coalesce(_search->'fields', '{}'::jsonb);
has_prev boolean := FALSE;
has_next boolean := FALSE;
links jsonb := '[]'::jsonb;
base_url text:= concat(rtrim(base_url(_search->'conf'),'/'));
BEGIN
searches := search_query(_search);
_where := searches._where;
Expand Down Expand Up @@ -925,26 +927,51 @@ BEGIN
END IF;
END IF;


links := links || jsonb_build_object(
'rel', 'root',
'type', 'application/json',
'href', base_url
) || jsonb_build_object(
'rel', 'self',
'type', 'application/json',
'href', concat(base_url, '/search')
);

IF has_next THEN
next := concat(out_records->-1->>'collection', ':', out_records->-1->>'id');
RAISE NOTICE 'HAS NEXT | %', next;
links := links || jsonb_build_object(
'rel', 'next',
'type', 'application/geo+json',
'method', 'GET',
'href', concat(base_url, '/search?token=next:', next)
);
END IF;

IF has_prev THEN
prev := concat(out_records->0->>'collection', ':', out_records->0->>'id');
RAISE NOTICE 'HAS PREV | %', prev;
links := links || jsonb_build_object(
'rel', 'prev',
'type', 'application/geo+json',
'method', 'GET',
'href', concat(base_url, '/search?token=prev:', prev)
);
END IF;

RAISE NOTICE 'Time to get prev/next %', age_ms(timer);
timer := clock_timestamp();


collection := jsonb_build_object(
'type', 'FeatureCollection',
'features', coalesce(out_records, '[]'::jsonb),
'next', next,
'prev', prev
'links', links
);



IF context(_search->'conf') != 'off' THEN
collection := collection || jsonb_strip_nulls(jsonb_build_object(
'numberMatched', total_count,
Expand Down
Loading

0 comments on commit febbaf8

Please sign in to comment.