Skip to content

Commit

Permalink
Merge pull request #326 from psteinroe/fix/enrich-filter-paths-with-d…
Browse files Browse the repository at this point in the history
…eclaration

fix: enrich filter paths with declaration from query paths
  • Loading branch information
psteinroe authored Nov 10, 2023
2 parents 6252f5f + 1946068 commit 17e53e5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-suits-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@supabase-cache-helpers/postgrest-core": patch
---

fix: enrich parsed filter paths with declaration from query paths
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { SupabaseClient, createClient } from '@supabase/supabase-js';

import { extractPathsFromFilters } from '../../src/lib/extract-paths-from-filter';
import { PostgrestParser } from '../../src/postgrest-parser';

describe('extractPathsFromFilters', () => {
let c: SupabaseClient;

beforeAll(() => {
c = createClient('https://localhost', '1234');
});

it('should add declarations from path to matching filter path', () => {
const parser = new PostgrestParser(
c
.from('conversation')
.select('inbox:inbox_id!inner(id,name,emoji)')
.eq('inbox_id.id', 'inbox-id'),
);
expect(extractPathsFromFilters(parser.filters, parser.paths)).toEqual([
{
alias: 'inbox.id',
declaration: 'inbox:inbox_id!inner.id',
path: 'inbox_id.id',
},
]);
});
});
5 changes: 4 additions & 1 deletion packages/postgrest-core/src/fetch/build-normalized-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ export const buildNormalizedQuery = <Q extends string = '*'>({

if (!disabled) {
for (const tableQuery of queriesForTable()) {
for (const filterPath of extractPathsFromFilters(tableQuery.filters)) {
for (const filterPath of extractPathsFromFilters(
tableQuery.filters,
tableQuery.paths,
)) {
// add paths used in filter
const path = tableQuery.paths.find(
(p) => p.path === filterPath.path && p.alias === filterPath.alias,
Expand Down
20 changes: 15 additions & 5 deletions packages/postgrest-core/src/lib/extract-paths-from-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,31 @@ import {
FilterDefinitions,
} from './query-types';

export const extractPathsFromFilters = (f: FilterDefinitions) => {
export const extractPathsFromFilters = (f: FilterDefinitions, p: Path[]) => {
return f.reduce<Path[]>((prev, filter) => {
if (isAndFilter(filter)) {
prev.push(...extractPathsFromFilters(filter.and));
prev.push(...extractPathsFromFilters(filter.and, p));
} else if (isOrFilter(filter)) {
prev.push(...extractPathsFromFilters(filter.or));
prev.push(...extractPathsFromFilters(filter.or, p));
} else if (isFilterDefinition(filter)) {
const relatedPath = p.find((p) => p.path === filter.path);
const pathElements = filter.path.split('.');
const aliasElements = filter.alias?.split('.');
const declaration = pathElements
.map(
(el, idx) => `${aliasElements ? `${aliasElements[idx]}:` : ''}${el}`,
(el, idx) =>
`${
aliasElements && aliasElements[idx] !== el
? `${aliasElements[idx]}:`
: ''
}${el}`,
)
.join('.');
prev.push({ path: filter.path, alias: filter.alias, declaration });
prev.push({
path: filter.path,
alias: filter.alias,
declaration: relatedPath ? relatedPath.declaration : declaration,
});
}
return prev;
}, []);
Expand Down
5 changes: 4 additions & 1 deletion packages/postgrest-core/src/postgrest-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export class PostgrestFilter<Result extends Record<string, unknown>> {
constructor(
public readonly params: { filters: FilterDefinitions; paths: Path[] },
) {
this._filterPaths = extractPathsFromFilters(this.params.filters);
this._filterPaths = extractPathsFromFilters(
this.params.filters,
this.params.paths,
);
}

public static fromQuery(query: string, opts?: PostgrestQueryParserOptions) {
Expand Down

2 comments on commit 17e53e5

@vercel
Copy link

@vercel vercel bot commented on 17e53e5 Nov 10, 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-psteinroe.vercel.app
supabase-cache-helpers-react-query.vercel.app
supabase-cache-helpers-react-query-git-main-psteinroe.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 17e53e5 Nov 10, 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.