Skip to content

Commit

Permalink
Merge pull request #300 from psteinroe/fix/null-relation
Browse files Browse the repository at this point in the history
feat: fix: set relation to null if it is null instead of removing the property altogether when denormalizing
  • Loading branch information
psteinroe authored Oct 19, 2023
2 parents 65b052d + 1668390 commit 04d12cb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-teachers-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@supabase-cache-helpers/postgrest-core": patch
---

fix: set relation to null if it is null instead of removing the property altogether when denormalizing
19 changes: 19 additions & 0 deletions packages/postgrest-core/__tests__/filter/denormalize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,23 @@ describe('denormalize', () => {
},
});
});

it('should set null if relation is null', () => {
expect(
denormalize(
[
{
declaration: 'assignee:assignee_id.id',
alias: 'assignee.id',
path: 'assignee_id.id',
},
],
{
assignee_id: null,
}
)
).toEqual({
assignee: null,
});
});
});
9 changes: 9 additions & 0 deletions packages/postgrest-core/src/filter/denormalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ 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) {
return {
...prev,
[curr.alias || curr.path]: null,
};
}

let isArray = false;
const flatNestedObjectOrArray = Object.entries(obj).reduce<
Record<string, Record<string, unknown>> | Record<string, unknown>
Expand All @@ -66,6 +74,7 @@ export const denormalize = <R extends Record<string, unknown>>(
[flatKey]: v,
};
}, {});

if (Object.keys(flatNestedObjectOrArray).length === 0) return prev;
if (isArray && isFlatNestedArray(flatNestedObjectOrArray)) {
return {
Expand Down

2 comments on commit 04d12cb

@vercel
Copy link

@vercel vercel bot commented on 04d12cb 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 04d12cb 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-psteinroe.vercel.app
supabase-cache-helpers-swr-demo-git-main-psteinroe.vercel.app
supabase-cache-helpers-swr.vercel.app

Please sign in to comment.