Skip to content

Commit

Permalink
Merge pull request #302 from psteinroe/fix/null-relation
Browse files Browse the repository at this point in the history
fix: return empty array if to-many relation is empty instead of removing the property altogether
  • Loading branch information
psteinroe authored Oct 19, 2023
2 parents 6e71111 + f2ab921 commit 79bb7c3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-spiders-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@supabase-cache-helpers/postgrest-core": patch
---

fix: return empty array if to-many relation is empty instead of removing the property altogether
29 changes: 29 additions & 0 deletions packages/postgrest-core/__tests__/filter/denormalize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,33 @@ describe('denormalize', () => {
assignee: null,
});
});

it('should set empty array if relation is empty array', () => {
expect(
denormalize(
[
{
declaration: 'tags:tag.id',
alias: 'tags.id',
path: 'tag.id',
},
{
declaration: 'tags:tag.name',
alias: 'tags.name',
path: 'tag.name',
},
{
declaration: 'tags:tag.color',
alias: 'tags.color',
path: 'tag.color',
},
],
{
tag: [],
}
)
).toEqual({
tags: [],
});
});
});
6 changes: 3 additions & 3 deletions packages/postgrest-core/src/filter/denormalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ export const denormalize = <R extends Record<string, unknown>>(
};
}

// if value is null, the relation is not set and we can return null
if (value === null) {
// if value is null or an empty array, the relation is not set and we can return the "empty" value
if (value === null || (Array.isArray(value) && value.length === 0)) {
return {
...prev,
[curr.alias || curr.path]: null,
[curr.alias || curr.path]: value,
};
}

Expand Down

2 comments on commit 79bb7c3

@vercel
Copy link

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

@vercel
Copy link

@vercel vercel bot commented on 79bb7c3 Oct 19, 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.vercel.app
supabase-cache-helpers-swr-demo-psteinroe.vercel.app

Please sign in to comment.