Skip to content

Commit febbaf8

Browse files
authored
Paging as links (#266)
* 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
1 parent fbe9203 commit febbaf8

File tree

6 files changed

+213
-118
lines changed

6 files changed

+213
-118
lines changed

src/pgstac/migrations/pgstac.0.8.5-unreleased.sql

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,8 @@ DECLARE
603603
_fields jsonb := coalesce(_search->'fields', '{}'::jsonb);
604604
has_prev boolean := FALSE;
605605
has_next boolean := FALSE;
606+
links jsonb := '[]'::jsonb;
607+
base_url text:= concat(rtrim(base_url(_search->'conf'),'/'));
606608
BEGIN
607609
searches := search_query(_search);
608610
_where := searches._where;
@@ -685,26 +687,51 @@ BEGIN
685687
END IF;
686688
END IF;
687689

690+
691+
links := links || jsonb_build_object(
692+
'rel', 'root',
693+
'type', 'application/json',
694+
'href', base_url
695+
) || jsonb_build_object(
696+
'rel', 'self',
697+
'type', 'application/json',
698+
'href', concat(base_url, '/search')
699+
);
700+
688701
IF has_next THEN
689702
next := concat(out_records->-1->>'collection', ':', out_records->-1->>'id');
690703
RAISE NOTICE 'HAS NEXT | %', next;
704+
links := links || jsonb_build_object(
705+
'rel', 'next',
706+
'type', 'application/geo+json',
707+
'method', 'GET',
708+
'href', concat(base_url, '/search?token=next:', next)
709+
);
691710
END IF;
692711

693712
IF has_prev THEN
694713
prev := concat(out_records->0->>'collection', ':', out_records->0->>'id');
695714
RAISE NOTICE 'HAS PREV | %', prev;
715+
links := links || jsonb_build_object(
716+
'rel', 'prev',
717+
'type', 'application/geo+json',
718+
'method', 'GET',
719+
'href', concat(base_url, '/search?token=prev:', prev)
720+
);
696721
END IF;
697722

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

726+
701727
collection := jsonb_build_object(
702728
'type', 'FeatureCollection',
703729
'features', coalesce(out_records, '[]'::jsonb),
704-
'next', next,
705-
'prev', prev
730+
'links', links
706731
);
707732

733+
734+
708735
IF context(_search->'conf') != 'off' THEN
709736
collection := collection || jsonb_strip_nulls(jsonb_build_object(
710737
'numberMatched', total_count,

src/pgstac/migrations/pgstac.unreleased.sql

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3761,6 +3761,8 @@ DECLARE
37613761
_fields jsonb := coalesce(_search->'fields', '{}'::jsonb);
37623762
has_prev boolean := FALSE;
37633763
has_next boolean := FALSE;
3764+
links jsonb := '[]'::jsonb;
3765+
base_url text:= concat(rtrim(base_url(_search->'conf'),'/'));
37643766
BEGIN
37653767
searches := search_query(_search);
37663768
_where := searches._where;
@@ -3843,26 +3845,51 @@ BEGIN
38433845
END IF;
38443846
END IF;
38453847

3848+
3849+
links := links || jsonb_build_object(
3850+
'rel', 'root',
3851+
'type', 'application/json',
3852+
'href', base_url
3853+
) || jsonb_build_object(
3854+
'rel', 'self',
3855+
'type', 'application/json',
3856+
'href', concat(base_url, '/search')
3857+
);
3858+
38463859
IF has_next THEN
38473860
next := concat(out_records->-1->>'collection', ':', out_records->-1->>'id');
38483861
RAISE NOTICE 'HAS NEXT | %', next;
3862+
links := links || jsonb_build_object(
3863+
'rel', 'next',
3864+
'type', 'application/geo+json',
3865+
'method', 'GET',
3866+
'href', concat(base_url, '/search?token=next:', next)
3867+
);
38493868
END IF;
38503869

38513870
IF has_prev THEN
38523871
prev := concat(out_records->0->>'collection', ':', out_records->0->>'id');
38533872
RAISE NOTICE 'HAS PREV | %', prev;
3873+
links := links || jsonb_build_object(
3874+
'rel', 'prev',
3875+
'type', 'application/geo+json',
3876+
'method', 'GET',
3877+
'href', concat(base_url, '/search?token=prev:', prev)
3878+
);
38543879
END IF;
38553880

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

3884+
38593885
collection := jsonb_build_object(
38603886
'type', 'FeatureCollection',
38613887
'features', coalesce(out_records, '[]'::jsonb),
3862-
'next', next,
3863-
'prev', prev
3888+
'links', links
38643889
);
38653890

3891+
3892+
38663893
IF context(_search->'conf') != 'off' THEN
38673894
collection := collection || jsonb_strip_nulls(jsonb_build_object(
38683895
'numberMatched', total_count,

src/pgstac/sql/004_search.sql

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,8 @@ DECLARE
843843
_fields jsonb := coalesce(_search->'fields', '{}'::jsonb);
844844
has_prev boolean := FALSE;
845845
has_next boolean := FALSE;
846+
links jsonb := '[]'::jsonb;
847+
base_url text:= concat(rtrim(base_url(_search->'conf'),'/'));
846848
BEGIN
847849
searches := search_query(_search);
848850
_where := searches._where;
@@ -925,26 +927,51 @@ BEGIN
925927
END IF;
926928
END IF;
927929

930+
931+
links := links || jsonb_build_object(
932+
'rel', 'root',
933+
'type', 'application/json',
934+
'href', base_url
935+
) || jsonb_build_object(
936+
'rel', 'self',
937+
'type', 'application/json',
938+
'href', concat(base_url, '/search')
939+
);
940+
928941
IF has_next THEN
929942
next := concat(out_records->-1->>'collection', ':', out_records->-1->>'id');
930943
RAISE NOTICE 'HAS NEXT | %', next;
944+
links := links || jsonb_build_object(
945+
'rel', 'next',
946+
'type', 'application/geo+json',
947+
'method', 'GET',
948+
'href', concat(base_url, '/search?token=next:', next)
949+
);
931950
END IF;
932951

933952
IF has_prev THEN
934953
prev := concat(out_records->0->>'collection', ':', out_records->0->>'id');
935954
RAISE NOTICE 'HAS PREV | %', prev;
955+
links := links || jsonb_build_object(
956+
'rel', 'prev',
957+
'type', 'application/geo+json',
958+
'method', 'GET',
959+
'href', concat(base_url, '/search?token=prev:', prev)
960+
);
936961
END IF;
937962

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

966+
941967
collection := jsonb_build_object(
942968
'type', 'FeatureCollection',
943969
'features', coalesce(out_records, '[]'::jsonb),
944-
'next', next,
945-
'prev', prev
970+
'links', links
946971
);
947972

973+
974+
948975
IF context(_search->'conf') != 'off' THEN
949976
collection := collection || jsonb_strip_nulls(jsonb_build_object(
950977
'numberMatched', total_count,

0 commit comments

Comments
 (0)