Skip to content

Commit

Permalink
fix: enrich filter paths with declaration from query paths
Browse files Browse the repository at this point in the history
  • Loading branch information
psteinroe committed Nov 10, 2023
1 parent 6252f5f commit 1946068
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

0 comments on commit 1946068

Please sign in to comment.