-
-
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 #293 from psteinroe/feat/export-order-by
feat: export order by parser and add findFilters helper
- Loading branch information
Showing
6 changed files
with
177 additions
and
1 deletion.
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,5 @@ | ||
--- | ||
"@supabase-cache-helpers/postgrest-core": patch | ||
--- | ||
|
||
feat: export order by parser fn |
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,5 @@ | ||
--- | ||
"@supabase-cache-helpers/postgrest-core": minor | ||
--- | ||
|
||
feat: add findFilters helper fn |
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
129 changes: 129 additions & 0 deletions
129
packages/postgrest-core/__tests__/lib/find-filters.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,129 @@ | ||
import { SupabaseClient, createClient } from '@supabase/supabase-js'; | ||
|
||
import { findFilters } from '../../src/lib/find-filters'; | ||
import { PostgrestParser } from '../../src/postgrest-parser'; | ||
|
||
describe('findFilters', () => { | ||
let c: SupabaseClient; | ||
|
||
beforeAll(() => { | ||
c = createClient('https://localhost', '1234'); | ||
}); | ||
|
||
it('by path', () => { | ||
expect( | ||
findFilters( | ||
new PostgrestParser( | ||
c | ||
.from('test') | ||
.select('id', { head: true, count: 'exact' }) | ||
.eq('id', '123') | ||
.contains('id', '456') | ||
).filters, | ||
{ path: 'id' } | ||
) | ||
).toEqual([ | ||
{ | ||
alias: undefined, | ||
negate: false, | ||
operator: 'eq', | ||
path: 'id', | ||
value: 123, | ||
}, | ||
{ | ||
alias: undefined, | ||
negate: false, | ||
operator: 'cs', | ||
path: 'id', | ||
value: 456, | ||
}, | ||
]); | ||
}); | ||
|
||
it('by alias', () => { | ||
expect( | ||
findFilters( | ||
new PostgrestParser( | ||
c | ||
.from('test') | ||
.select('test:id', { head: true, count: 'exact' }) | ||
.eq('id', '123') | ||
.contains('id', '456') | ||
).filters, | ||
{ alias: 'test' } | ||
) | ||
).toEqual([ | ||
{ alias: 'test', negate: false, operator: 'eq', path: 'id', value: 123 }, | ||
{ alias: 'test', negate: false, operator: 'cs', path: 'id', value: 456 }, | ||
]); | ||
}); | ||
|
||
it('by operator', () => { | ||
expect( | ||
findFilters( | ||
new PostgrestParser( | ||
c | ||
.from('test') | ||
.select('id', { head: true, count: 'exact' }) | ||
.eq('id', '123') | ||
.contains('id', '456') | ||
).filters, | ||
{ operator: 'eq' } | ||
) | ||
).toEqual([ | ||
{ | ||
alias: undefined, | ||
negate: false, | ||
operator: 'eq', | ||
path: 'id', | ||
value: 123, | ||
}, | ||
]); | ||
}); | ||
|
||
it('by negated operator', () => { | ||
expect( | ||
findFilters( | ||
new PostgrestParser( | ||
c | ||
.from('test') | ||
.select('id', { head: true, count: 'exact' }) | ||
.not('id', 'eq', '123') | ||
.contains('id', '456') | ||
).filters, | ||
{ negate: true, operator: 'eq' } | ||
) | ||
).toEqual([ | ||
{ | ||
alias: undefined, | ||
negate: true, | ||
operator: 'eq', | ||
path: 'id', | ||
value: 123, | ||
}, | ||
]); | ||
}); | ||
|
||
it('by value', () => { | ||
expect( | ||
findFilters( | ||
new PostgrestParser( | ||
c | ||
.from('test') | ||
.select('id', { head: true, count: 'exact' }) | ||
.neq('id', '123') | ||
.contains('id', '456') | ||
).filters, | ||
{ value: 123 } | ||
) | ||
).toEqual([ | ||
{ | ||
alias: undefined, | ||
negate: false, | ||
operator: 'neq', | ||
path: 'id', | ||
value: 123, | ||
}, | ||
]); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { | ||
isAndFilter, | ||
isOrFilter, | ||
isFilterDefinition, | ||
FilterDefinitions, | ||
FilterDefinition, | ||
} from './query-types'; | ||
|
||
// Helper to search for filters in a filter definition | ||
export const findFilters = ( | ||
f: FilterDefinitions, | ||
by: Partial<FilterDefinition> | ||
) => { | ||
const filters: FilterDefinition[] = []; | ||
f.forEach((filter) => { | ||
if (isAndFilter(filter)) { | ||
filters.push(...findFilters(filter.and, by)); | ||
} | ||
if (isOrFilter(filter)) { | ||
filters.push(...findFilters(filter.or, by)); | ||
} | ||
if (isFilterDefinition(filter)) { | ||
if ( | ||
(typeof by.path === 'undefined' || filter.path === by.path) && | ||
(typeof by.alias === 'undefined' || filter.alias === by.alias) && | ||
(typeof by.value === 'undefined' || filter.value === by.value) && | ||
(typeof by.negate === 'undefined' || filter.negate === by.negate) && | ||
(typeof by.operator === 'undefined' || filter.operator === by.operator) | ||
) { | ||
filters.push(filter); | ||
} | ||
} | ||
}); | ||
return filters; | ||
}; |
ec966ac
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-psteinroe.vercel.app
supabase-cache-helpers-swr-demo-git-main-psteinroe.vercel.app
supabase-cache-helpers-swr.vercel.app
ec966ac
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.vercel.app
supabase-cache-helpers-react-query-psteinroe.vercel.app
supabase-cache-helpers-react-query-git-main-psteinroe.vercel.app