Skip to content

Commit

Permalink
Merge pull request #293 from psteinroe/feat/export-order-by
Browse files Browse the repository at this point in the history
feat: export order by parser and add findFilters helper
  • Loading branch information
psteinroe authored Oct 4, 2023
2 parents 00c7df8 + 41a1b05 commit ec966ac
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/green-eyes-walk.md
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
5 changes: 5 additions & 0 deletions .changeset/silver-beans-invent.md
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
2 changes: 1 addition & 1 deletion packages/postgrest-core/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import * as Import from '../src';

describe('index exports', () => {
it('should export', () => {
expect(Object.keys(Import)).toHaveLength(30);
expect(Object.keys(Import)).toHaveLength(32);
});
});
129 changes: 129 additions & 0 deletions packages/postgrest-core/__tests__/lib/find-filters.spec.ts
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,
},
]);
});
});
2 changes: 2 additions & 0 deletions packages/postgrest-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export * from './lib/is-postgrest-builder';
export * from './lib/get';
export * from './lib/set-filter-value';
export * from './lib/parse-value';
export * from './lib/parse-order-by-key';
export * from './lib/find-filters';

export * from './cursor-pagination-fetcher';
export * from './delete-fetcher';
Expand Down
35 changes: 35 additions & 0 deletions packages/postgrest-core/src/lib/find-filters.ts
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;
};

2 comments on commit ec966ac

@vercel
Copy link

@vercel vercel bot commented on ec966ac Oct 4, 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-psteinroe.vercel.app
supabase-cache-helpers-swr-demo-git-main-psteinroe.vercel.app
supabase-cache-helpers-swr.vercel.app

@vercel
Copy link

@vercel vercel bot commented on ec966ac Oct 4, 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.vercel.app
supabase-cache-helpers-react-query-psteinroe.vercel.app
supabase-cache-helpers-react-query-git-main-psteinroe.vercel.app

Please sign in to comment.