Skip to content

Commit

Permalink
Merge pull request #256 from psteinroe/fix/cursor-pagination
Browse files Browse the repository at this point in the history
fix: cursor value parsing
  • Loading branch information
psteinroe authored Jul 25, 2023
2 parents cd02d39 + 90da3cd commit 582fa40
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-clouds-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@supabase-cache-helpers/postgrest-swr": patch
---

fix: cursor value parsing
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ describe('useCursorInfiniteScrollQuery', () => {
let testRunPrefix: string;
let contacts: Database['public']['Tables']['contact']['Row'][];

let d1: Date;
let d2: Date;
let d3: Date;
let d4: Date;

beforeAll(async () => {
testRunPrefix = `${TEST_PREFIX}-${Math.floor(Math.random() * 100)}`;
client = createClient(
Expand All @@ -22,13 +27,37 @@ describe('useCursorInfiniteScrollQuery', () => {
);
await client.from('contact').delete().ilike('username', `${TEST_PREFIX}%`);

d1 = new Date();
d1.setSeconds(d1.getSeconds() + 10);

d2 = new Date();
d2.setSeconds(d2.getSeconds() + 20);

d3 = new Date();
d3.setSeconds(d3.getSeconds() + 30);

d4 = new Date();
d4.setSeconds(d4.getSeconds() + 40);

const { data } = await client
.from('contact')
.insert([
{ username: `${testRunPrefix}-username-1` },
{ username: `${testRunPrefix}-username-2` },
{ username: `${testRunPrefix}-username-3` },
{ username: `${testRunPrefix}-username-4` },
{
username: `${testRunPrefix}-username-1`,
created_at: d1.toISOString(),
},
{
username: `${testRunPrefix}-username-2`,
created_at: d2.toISOString(),
},
{
username: `${testRunPrefix}-username-3`,
created_at: d3.toISOString(),
},
{
username: `${testRunPrefix}-username-4`,
created_at: d4.toISOString(),
},
])
.select('*')
.throwOnError();
Expand Down Expand Up @@ -381,4 +410,55 @@ describe('useCursorInfiniteScrollQuery', () => {

expect(screen.queryByTestId('loadMore')).toBeNull();
});

it('should stop at lastCursor with timestamptz column', async () => {
function Page() {
const { data, loadMore, isValidating, error } =
useCursorInfiniteScrollQuery(
client
.from('contact')
.select('id,created_at,username')
.ilike('username', `${testRunPrefix}%`)
.order('created_at', { ascending: true })
.limit(1),
{
path: 'created_at',
until: d2.toISOString(),
}
);

return (
<div>
{loadMore && (
<div data-testid="loadMore" onClick={() => loadMore()} />
)}
<div data-testid="list">
{(data ?? []).map((p) => (
<div key={p.id}>{p.username}</div>
))}
</div>
</div>
);
}

renderWithConfig(<Page />, { provider: () => provider });
await screen.findByText(
`${testRunPrefix}-username-1`,
{},
{ timeout: 10000 }
);
const list = screen.getByTestId('list');
expect(list.childElementCount).toEqual(1);

fireEvent.click(screen.getByTestId('loadMore'));
await screen.findByText(
`${testRunPrefix}-username-2`,
{},
{ timeout: 10000 }
);

expect(list.childElementCount).toEqual(2);

expect(screen.queryByTestId('loadMore')).toBeNull();
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createCursorPaginationFetcher } from '@supabase-cache-helpers/postgrest-fetcher';
import { get } from '@supabase-cache-helpers/postgrest-filter';
import { get, parseValue } from '@supabase-cache-helpers/postgrest-filter';
import {
PostgrestPaginationCacheData,
PostgrestPaginationResponse,
Expand Down Expand Up @@ -126,7 +126,9 @@ function useCursorInfiniteScrollQuery<
},
};
}
const cursorValue = filter.split('.')[1];
const s = filter.split('.');
s.shift();
const cursorValue = s.join('.');
return {
cursor: cursorValue,
order: {
Expand Down Expand Up @@ -180,11 +182,16 @@ function useCursorInfiniteScrollQuery<
const [column, ascending, _] = orderingValue.split('.');

const path = `${foreignTablePath ? `${foreignTablePath}.` : ''}${column}`;
const lastElem = get(flatData[flatData.length - 1], path) as string;
if (ascending === 'asc') {
hasLoadMore = lastElem < cursor.until;
} else {
hasLoadMore = lastElem > cursor.until;
const lastElem = parseValue(
get(flatData[flatData.length - 1], path) as string
);
const until = parseValue(cursor.until);
if (lastElem && until) {
if (ascending === 'asc') {
hasLoadMore = lastElem < until;
} else {
hasLoadMore = lastElem > until;
}
}
}

Expand Down

2 comments on commit 582fa40

@vercel
Copy link

@vercel vercel bot commented on 582fa40 Jul 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

supabase-cache-helpers-react-query – ./examples/react-query

supabase-cache-helpers-react-query-git-main-psteinroe.vercel.app
supabase-cache-helpers-react-query.vercel.app
supabase-cache-helpers-react-query-psteinroe.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 582fa40 Jul 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

supabase-cache-helpers-swr-demo – ./examples/swr

supabase-cache-helpers-swr-demo-git-main-psteinroe.vercel.app
supabase-cache-helpers-swr-demo-psteinroe.vercel.app
supabase-cache-helpers-swr.vercel.app

Please sign in to comment.