diff --git a/.changeset/moody-pianos-collect.md b/.changeset/moody-pianos-collect.md new file mode 100644 index 00000000..a77580d6 --- /dev/null +++ b/.changeset/moody-pianos-collect.md @@ -0,0 +1,5 @@ +--- +"@supabase-cache-helpers/postgrest-core": patch +--- + +fix: denormalize json column with top-level array diff --git a/packages/postgrest-core/__tests__/filter/denormalize.spec.ts b/packages/postgrest-core/__tests__/filter/denormalize.spec.ts index 9781cc1e..2041190f 100644 --- a/packages/postgrest-core/__tests__/filter/denormalize.spec.ts +++ b/packages/postgrest-core/__tests__/filter/denormalize.spec.ts @@ -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( diff --git a/packages/postgrest-core/src/filter/denormalize.ts b/packages/postgrest-core/src/filter/denormalize.ts index 13d317c2..7db4efc0 100644 --- a/packages/postgrest-core/src/filter/denormalize.ts +++ b/packages/postgrest-core/src/filter/denormalize.ts @@ -36,17 +36,35 @@ export const denormalize = >( } if (typeof value === 'undefined') { // if json(b) column, unflatten + let isArray = false; const jsonValue = Object.entries(obj).reduce>( (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') {