Skip to content

Commit

Permalink
Merge pull request #444 from adamstoffel/parse-json-paths
Browse files Browse the repository at this point in the history
  • Loading branch information
psteinroe authored May 8, 2024
2 parents 2e6ceb2 + 7a99711 commit 3ae8343
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-ads-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@supabase-cache-helpers/postgrest-core": minor
---

Adds support for parsing JSON arrow expressions in order clauses
25 changes: 25 additions & 0 deletions packages/postgrest-core/__tests__/lib/get.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { get } from '../../src/lib/get';

describe('get', () => {
it.each([
[{ a: 1 }, 'a', undefined, 1], // simple case
[{ a: 1 }, 'b', 2, 2], // default case
[{ a: 1 }, '', undefined, undefined], // empty case
[{ a: { b: 1 } }, 'a.b', undefined, 1], // dot syntax
[{ a: { b: 1 } }, 'a,b', undefined, 1], // comma syntax
[{ a: { b: 1 } }, 'a[b]', undefined, 1], // bracket syntax
[{ a: { b: { c: { d: 1 } } } }, 'a.b,c.[d]', undefined, 1], // combination syntax
[{ a: { b: 1 } }, 'a->b', undefined, 1], // json value syntax
[{ a: { b: 1 } }, 'a->>b', undefined, '1'], // json string syntax
[{ a: [1, 2] }, 'a->0', undefined, 1], // json array value syntax
[{ a: [1, 2] }, 'a->>0', undefined, '1'], // json array string syntax
[{ a: { b: { c: 1 } } }, 'a->b->c', undefined, 1], // nested json syntax
[{ a: { b: { c: 1 } } }, 'a->b->>c', undefined, '1'],
[{ a: { b: [1, 2] } }, 'a.b->0', undefined, 1],
[{ a: { b: [1, 2] } }, 'a.b->>0', undefined, '1'],
[{ a: { b: 1 } }, 'a->0', undefined, undefined], // not an array
[{ a: [1, 2] }, 'a->2', undefined, undefined], // missing array value
])('get(%j, "%s", %s) should be %s', (obj, path, defaultValue, expected) => {
expect(get(obj, path, defaultValue)).toEqual(expected);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('parseOrderByKey', () => {
c = createClient('https://localhost', '1234');
});

it('should parse forth and bock correctly', () => {
it('should parse forth and back correctly', () => {
const parser = new PostgrestParser(
c
.from('test')
Expand Down
25 changes: 25 additions & 0 deletions packages/postgrest-core/__tests__/lib/sorted-comparator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,29 @@ describe('sortedComparator', () => {
{ value_1: new Date('December 3, 1995 03:24:00'), value_2: 3 },
]);
});

it('json column', () => {
expect(
[
{ json: { value_1: 1 }, value_2: 1 },
{ json: { value_1: 3 }, value_2: 3 },
{ json: { value_1: 2 }, value_2: 2 },
].sort(
buildSortedComparator<{
json: { value_1: number };
value_2: number;
}>([
{
column: 'json->value_1',
ascending: true,
nullsFirst: false,
},
]),
),
).toEqual([
{ json: { value_1: 1 }, value_2: 1 },
{ json: { value_1: 2 }, value_2: 2 },
{ json: { value_1: 3 }, value_2: 3 },
]);
});
});
28 changes: 19 additions & 9 deletions packages/postgrest-core/src/lib/get.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
export const get = (obj: any, path: string, defaultValue: any = undefined) => {
const travel = (regexp: RegExp) =>
String.prototype.split
.call(path, regexp)
.filter(Boolean)
.reduce(
(res, key) => (res !== null && res !== undefined ? res[key] : res),
obj,
);
const result = travel(/[,[\]]+?/) || travel(/[,[\].]+?/);
const split = path.split(/((?:\.|,|\[|\]|->>|->)+)/g);
let result: any = obj;
for (let i = -1; i < split.length; i += 2) {
const separator = split[i];
let key: string | number = split[i + 1];
if (!key) {
continue;
}
if (separator?.endsWith('->') || separator?.endsWith('->>')) {
if (/^\d+$/.test(key)) {
key = parseInt(key, 10);
}
}
if (separator?.endsWith('->>')) {
result = `${result ? result[key] : result}`;
} else {
result = result ? result[key] : result;
}
}
return result === undefined || result === obj ? defaultValue : result;
};

0 comments on commit 3ae8343

Please sign in to comment.