Skip to content

Commit

Permalink
fix: denormalize json column with top-level array
Browse files Browse the repository at this point in the history
  • Loading branch information
psteinroe committed Mar 19, 2024
1 parent f137657 commit 99b8f68
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-pianos-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@supabase-cache-helpers/postgrest-core": patch
---

fix: denormalize json column with top-level array
54 changes: 54 additions & 0 deletions packages/postgrest-core/__tests__/filter/denormalize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,60 @@ describe('denormalize', () => {
});
});

it('should work with json array of objects', () => {
expect(
denormalize(
[
{
declaration: 'id',
path: 'id',
},
{
declaration: 'template:template_id.id',
alias: 'template.id',
path: 'template_id.id',
},
{
declaration: 'template:template_id.buttons',
alias: 'template.buttons',
path: 'template_id.buttons',
},
],
{
id: '741c29ab-e03d-4b97-8d51-954579effa10',
'template_id.id': 'da6d45ca-6644-437a-8a58-0da73ecda566',
'template_id.buttons.0.url': 'https://hellomateo.de',
'template_id.buttons.0.text': 'Visit us',
'template_id.buttons.0.type': 'call_to_action',
'template_id.buttons.0.subtype': 'url',
'template_id.buttons.1.text': 'Call us',
'template_id.buttons.1.type': 'call_to_action',
'template_id.buttons.1.subtype': 'phone_number',
'template_id.buttons.1.phone_number': '+420123456789',
},
),
).toEqual({
id: '741c29ab-e03d-4b97-8d51-954579effa10',
template: {
id: 'da6d45ca-6644-437a-8a58-0da73ecda566',
buttons: [
{
url: 'https://hellomateo.de',
text: 'Visit us',
type: 'call_to_action',
subtype: 'url',
},
{
text: 'Call us',
type: 'call_to_action',
subtype: 'phone_number',
phone_number: '+420123456789',
},
],
},
});
});

it('should work with json column', () => {
expect(
denormalize(
Expand Down
22 changes: 20 additions & 2 deletions packages/postgrest-core/src/filter/denormalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,35 @@ export const denormalize = <R extends Record<string, unknown>>(
}
if (typeof value === 'undefined') {
// if json(b) column, unflatten
let isArray = false;
const jsonValue = Object.entries(obj).reduce<Record<string, unknown>>(
(prev, [k, v]) => {
if (k.startsWith(`${curr.path}.`)) {
prev[k.slice(curr.path.length + 1)] = v;
const key = k.slice(curr.path.length + 1);
const maybeIdx = key.match(/^\b\d+\b/);
if (maybeIdx && isFlatNestedArray(prev)) {
isArray = true;
prev = {
...prev,
[maybeIdx[0]]: {
...(prev[maybeIdx[0]] ? prev[maybeIdx[0]] : {}),
[key.slice(maybeIdx[0].length + 1)]: v,
},
};
} else {
prev[maybeIdx ? maybeIdx[0] : key] = v;
}
}
return prev;
},
{},
);
if (Object.keys(jsonValue).length > 0) {
value = flat.unflatten(jsonValue);
if (isArray) {
value = Object.values(jsonValue).map((v) => flat.unflatten(v));
} else {
value = flat.unflatten(jsonValue);
}
}
}
if (typeof value === 'undefined') {
Expand Down

0 comments on commit 99b8f68

Please sign in to comment.