Skip to content

Commit

Permalink
Merge pull request #499 from psteinroe/fix/empty-array-cols
Browse files Browse the repository at this point in the history
fix: transform empty jsonb array cols properly
  • Loading branch information
psteinroe committed Sep 5, 2024
2 parents ff702d3 + eb2f5d4 commit 447074a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-moons-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@supabase-cache-helpers/postgrest-core": patch
---

fix: transform empty jsonb array cols properly
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { flatten } from 'flat';

import { get } from '../lib/get';
import { type NestedPath, isNestedPath } from '../lib/group-paths-recursive';
import { isPlainObject } from '../lib/is-plain-object';
import type { Path } from '../lib/query-types';
import type { BuildNormalizedQueryReturn } from './build-normalized-query';

Expand Down Expand Up @@ -83,7 +84,7 @@ export const normalizeResponse = <R>(
...flatten({
[curr.path]:
value !== null &&
(typeof value === 'object' || Array.isArray(value))
(isPlainObject(value) || (Array.isArray(value) && value.length > 0))
? flatten(value)
: value,
}),
Expand All @@ -102,8 +103,11 @@ export const normalizeResponse = <R>(
...flatten({
// add hint to path if it has dedupe alias
// can happen if the same relation is queried multiple times via different fkeys
[`${curr.path}${curr.alias?.startsWith('d_') && curr.declaration.split('!').length > 1 ? `!${curr.declaration.split('!')[1]}` : ''}`]:
normalizeResponse(curr.paths, value as Record<string, unknown>),
[`${curr.path}${
curr.alias?.startsWith('d_') && curr.declaration.split('!').length > 1
? `!${curr.declaration.split('!')[1]}`
: ''
}`]: normalizeResponse(curr.paths, value as Record<string, unknown>),
}),
};
}, {} as R);
Expand Down
5 changes: 5 additions & 0 deletions packages/postgrest-core/src/lib/is-plain-object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function isPlainObject(
value: unknown,
): value is Record<string, unknown> {
return Object.prototype.toString.call(value) === '[object Object]';
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ describe('buildMutationFetcherResponse', () => {
it('should work with json columns', () => {
const q = c
.from('campaign')
.select('jsoncol,jsonarraycol,jsonarrayobjcol')
.select(
'jsoncol,jsonarraycol,jsonarrayobjcol,jsonarraycolempty,jsonobjcolempty',
)
.eq('id', 'some-id');

const query = buildNormalizedQuery({
query: 'jsoncol,jsonarraycol,jsonarrayobjcol',
query:
'jsoncol,jsonarraycolempty,jsonobjcolempty,jsonarraycol,jsonarrayobjcol',
queriesForTable: () => [new PostgrestParser(q)],
});

Expand All @@ -29,6 +32,8 @@ describe('buildMutationFetcherResponse', () => {
test: '123',
},
jsonarraycol: ['123'],
jsonarraycolempty: [],
jsonobjcolempty: {},
jsonarrayobjcol: [{ some: 'value' }, { some: 'other' }],
},
{
Expand All @@ -41,6 +46,8 @@ describe('buildMutationFetcherResponse', () => {
id: 'some-id',
'jsoncol.test': '123',
'jsonarraycol.0': '123',
jsonarraycolempty: [],
jsonobjcolempty: {},
'jsonarrayobjcol.0.some': 'value',
'jsonarrayobjcol.1.some': 'other',
},
Expand All @@ -50,6 +57,8 @@ describe('buildMutationFetcherResponse', () => {
},
jsonarraycol: ['123'],
jsonarrayobjcol: [{ some: 'value' }, { some: 'other' }],
jsonarraycolempty: [],
jsonobjcolempty: {},
},
});
});
Expand Down

0 comments on commit 447074a

Please sign in to comment.