Skip to content

Commit

Permalink
fix: correctly handle filter on embedded 1-to-many relation
Browse files Browse the repository at this point in the history
  • Loading branch information
psteinroe committed Nov 6, 2023
1 parent dbe098b commit cdfb8c3
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-hornets-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@supabase-cache-helpers/postgrest-core": patch
---

fix: correctly handle filter on embedded 1-to-many relation
96 changes: 96 additions & 0 deletions packages/postgrest-core/__tests__/postgrest-filter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,102 @@ describe('PostgrestFilter', () => {
});
});

describe('applyFilters', () => {
it('should return true for filter on empty relation without inner!', () => {
expect(
new PostgrestFilter({
filters: [
{
path: 'conversation_tag.conversation_id',
alias: 'conversation_tags.conversation_id',
negate: false,
operator: 'eq',
value: 'a71d21d8-5b0f-4221-87c2-b45603d161aa',
},
{
path: 'organisation_id',
negate: false,
operator: 'eq',
value: 'c6b8d41c-1a24-4dc9-a9e9-7fb6dc4782c8',
},
],
paths: [
{
declaration: 'id',
path: 'id',
},
{
declaration: 'name',
path: 'name',
},
{
declaration: 'color',
path: 'color',
},
{
declaration: 'organisation_id',
path: 'organisation_id',
},
{
declaration: 'conversation_tags:conversation_tag.conversation_id',
alias: 'conversation_tags.conversation_id',
path: 'conversation_tag.conversation_id',
},
],
}).applyFilters({
id: '4a9f0f20-cc53-4f91-9765-31f78b3f145f',
name: 'Tag 1',
color: 'red',
organisation_id: 'c6b8d41c-1a24-4dc9-a9e9-7fb6dc4782c8',
conversation_tags: [],
}),
).toEqual(true);
});
it('should return true for filter on relation without inner!', () => {
expect(
new PostgrestFilter({
filters: [
{
path: 'conversation_tag.conversation_id',
alias: 'conversation_tags.conversation_id',
negate: false,
operator: 'eq',
value: 'a71d21d8-5b0f-4221-87c2-b45603d161aa',
},
],
paths: [
{
declaration: 'id',
path: 'id',
},
{
declaration: 'name',
path: 'name',
},
{
declaration: 'color',
path: 'color',
},
{
declaration: 'conversation_tags:conversation_tag.conversation_id',
alias: 'conversation_tags.conversation_id',
path: 'conversation_tag.conversation_id',
},
],
}).applyFilters({
id: '4a9f0f20-cc53-4f91-9765-31f78b3f145f',
name: 'Tag 1',
color: 'red',
conversation_tags: [
{
conversation_id: 'a71d21d8-5b0f-4221-87c2-b45603d161aa',
},
],
}),
).toEqual(true);
});
});

describe('.applyFiltersOnPaths', () => {
it('with and', () => {
expect(
Expand Down
13 changes: 11 additions & 2 deletions packages/postgrest-core/src/postgrest-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class PostgrestFilter<Result extends Record<string, unknown>> {
}

private applyFilterFn(
obj: object,
obj: object | any[],
path: string,
{
filterFn,
Expand All @@ -143,9 +143,18 @@ export class PostgrestFilter<Result extends Record<string, unknown>> {
): boolean {
// parse json operators "->" and "->>" to "."
const pathElements = path.replace(/->>|->/g, '.').split('.');

const v = get(obj, pathElements[0]);

if (typeof v === 'undefined') return false;
if (typeof v === 'undefined') {
// if obj is an array, we should apply the filter to all elements of the array
if (Array.isArray(obj)) {
return obj.every((o) =>
this.applyFilterFn(o, path, { filterFn, value, negate }),
);
}
return false;
}

if (pathElements.length > 1) {
// recursively resolve json path
Expand Down

1 comment on commit cdfb8c3

@vercel
Copy link

@vercel vercel bot commented on cdfb8c3 Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deployment failed with the following error:

Resource is limited - try again in 14 minutes (more than 100, code: "api-deployments-free-per-day").

Please sign in to comment.