-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #246 from psteinroe/feat/cursor-pagination
feat: cursor pagination
- Loading branch information
Showing
21 changed files
with
798 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@supabase-cache-helpers/postgrest-fetcher": minor | ||
"@supabase-cache-helpers/postgrest-filter": minor | ||
"@supabase-cache-helpers/postgrest-swr": minor | ||
--- | ||
|
||
- feat: add cursor pagination | ||
- refactor: rename infinite queries to include the type (offset or cursor) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
packages/postgrest-fetcher/__tests__/cursor-pagination-fetcher.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { createClient, SupabaseClient } from '@supabase/supabase-js'; | ||
import { Database } from './database.types'; | ||
import './utils'; | ||
|
||
import { createCursorPaginationFetcher } from '../src'; | ||
|
||
const TEST_PREFIX = 'postgrest-fetcher-cursor-pagination-fetcher-'; | ||
|
||
describe('cursor-pagination-fetcher', () => { | ||
let client: SupabaseClient<Database>; | ||
let testRunPrefix: string; | ||
let contacts: Database['public']['Tables']['contact']['Row'][]; | ||
|
||
beforeAll(async () => { | ||
testRunPrefix = `${TEST_PREFIX}-${Math.floor(Math.random() * 100)}`; | ||
client = createClient( | ||
process.env.SUPABASE_URL as string, | ||
process.env.SUPABASE_ANON_KEY as string | ||
); | ||
await client.from('contact').delete().ilike('username', `${TEST_PREFIX}%`); | ||
|
||
const { data } = await client | ||
.from('contact') | ||
.insert([ | ||
{ username: `${testRunPrefix}-username-1` }, | ||
{ username: `${testRunPrefix}-username-2` }, | ||
{ username: `${testRunPrefix}-username-3` }, | ||
{ username: `${testRunPrefix}-username-4` }, | ||
]) | ||
.select('*') | ||
.throwOnError(); | ||
contacts = data ?? []; | ||
expect(contacts).toHaveLength(4); | ||
}); | ||
|
||
describe('createCursorPaginationFetcher', () => { | ||
it('should return null if query is undefined', () => { | ||
expect( | ||
createCursorPaginationFetcher( | ||
null, | ||
{ | ||
order: { column: 'username', ascending: true, nullsFirst: false }, | ||
}, | ||
(key) => ({ | ||
cursor: `${testRunPrefix}-username-2`, | ||
}) | ||
) | ||
).toEqual(null); | ||
}); | ||
|
||
it('should work with no cursor', async () => { | ||
const fetcher = createCursorPaginationFetcher( | ||
client | ||
.from('contact') | ||
.select('username') | ||
.ilike('username', `${testRunPrefix}%`) | ||
.order('username', { ascending: true, nullsFirst: false }) | ||
.limit(2), | ||
{ | ||
order: { column: 'username', ascending: true, nullsFirst: false }, | ||
}, | ||
() => ({ | ||
cursor: undefined, | ||
}) | ||
); | ||
expect(fetcher).toBeDefined(); | ||
const data = await fetcher!(''); | ||
expect(data).toHaveLength(2); | ||
expect(data).toEqual([ | ||
{ username: `${testRunPrefix}-username-1` }, | ||
{ username: `${testRunPrefix}-username-2` }, | ||
]); | ||
}); | ||
|
||
it('should apply cursor from key', async () => { | ||
const fetcher = createCursorPaginationFetcher( | ||
client | ||
.from('contact') | ||
.select('username') | ||
.ilike('username', `${testRunPrefix}%`) | ||
.limit(2) | ||
.order('username', { ascending: true, nullsFirst: false }), | ||
{ | ||
order: { column: 'username', ascending: true, nullsFirst: false }, | ||
}, | ||
(key) => ({ | ||
cursor: `${testRunPrefix}-username-2`, | ||
}) | ||
); | ||
expect(fetcher).toBeDefined(); | ||
const data = await fetcher!(''); | ||
expect(data).toHaveLength(2); | ||
expect(data).toEqual([ | ||
{ username: `${testRunPrefix}-username-3` }, | ||
{ username: `${testRunPrefix}-username-4` }, | ||
]); | ||
}); | ||
|
||
it('should work descending', async () => { | ||
const fetcher = createCursorPaginationFetcher( | ||
client | ||
.from('contact') | ||
.select('username') | ||
.ilike('username', `${testRunPrefix}%`) | ||
.limit(2) | ||
.order('username', { ascending: true, nullsFirst: false }), | ||
{ | ||
order: { column: 'username', ascending: false, nullsFirst: false }, | ||
}, | ||
(key) => ({ | ||
cursor: `${testRunPrefix}-username-3`, | ||
}) | ||
); | ||
expect(fetcher).toBeDefined(); | ||
const data = await fetcher!(''); | ||
expect(data).toHaveLength(2); | ||
expect(data).toEqual([ | ||
{ username: `${testRunPrefix}-username-1` }, | ||
{ username: `${testRunPrefix}-username-2` }, | ||
]); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
a6480be
There was a problem hiding this comment.
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-psteinroe.vercel.app
supabase-cache-helpers-react-query.vercel.app
a6480be
There was a problem hiding this comment.
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 – ./docs
supabase-cache-helpers.vercel.app
supabase-cache-helpers-psteinroe.vercel.app
supabase-cache-helpers-git-main-psteinroe.vercel.app
a6480be
There was a problem hiding this comment.
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