Skip to content

Commit 1d579f8

Browse files
authored
Merge pull request #551 from psteinroe/feat/export-order-by-parser
feat: export parse order by helper
2 parents 1e18648 + 267d958 commit 1d579f8

File tree

5 files changed

+32
-17
lines changed

5 files changed

+32
-17
lines changed

.changeset/grumpy-rice-chew.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@supabase-cache-helpers/postgrest-core": patch
3+
---
4+
5+
feat: extrat and export parse order by helper

packages/postgrest-core/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export * from './lib/get';
1313
export * from './lib/set-filter-value';
1414
export * from './lib/parse-value';
1515
export * from './lib/parse-order-by-key';
16+
export * from './lib/parse-order-by';
1617
export * from './lib/find-filters';
1718

1819
export * from './cursor-pagination-fetcher';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { OrderDefinition } from './query-types';
2+
3+
export const parseOrderBy = (searchParams: URLSearchParams) => {
4+
const orderBy: OrderDefinition[] = [];
5+
searchParams.forEach((value, key) => {
6+
const split = key.split('.');
7+
if (split[split.length === 2 ? 1 : 0] === 'order') {
8+
// separated by ,
9+
const orderByDefs = value.split(',');
10+
orderByDefs.forEach((def) => {
11+
const [column, ascending, nullsFirst] = def.split('.');
12+
orderBy.push({
13+
ascending: ascending === 'asc',
14+
column,
15+
nullsFirst: nullsFirst === 'nullsfirst',
16+
foreignTable: split.length === 2 ? split[0] : undefined,
17+
});
18+
});
19+
}
20+
});
21+
22+
return orderBy;
23+
};

packages/postgrest-core/src/postgrest-parser.ts

+2-16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { PostgrestBuilder } from '@supabase/postgrest-js';
33
import { encodeObject } from './lib/encode-object';
44
import { getTableFromUrl } from './lib/get-table-from-url';
55
import { isObject } from './lib/is-object';
6+
import { parseOrderBy } from './lib/parse-order-by';
67
import type { OrderDefinition } from './lib/query-types';
78
import { sortSearchParams } from './lib/sort-search-param';
89
import {
@@ -69,22 +70,7 @@ export class PostgrestParser<Result> extends PostgrestQueryParser {
6970
const offset = this._url.searchParams.get('offset');
7071
this.offset = offset ? Number(offset) : undefined;
7172

72-
this._url.searchParams.forEach((value, key) => {
73-
const split = key.split('.');
74-
if (split[split.length === 2 ? 1 : 0] === 'order') {
75-
// separated by ,
76-
const orderByDefs = value.split(',');
77-
orderByDefs.forEach((def) => {
78-
const [column, ascending, nullsFirst] = def.split('.');
79-
this.orderBy.push({
80-
ascending: ascending === 'asc',
81-
column,
82-
nullsFirst: nullsFirst === 'nullsfirst',
83-
foreignTable: split.length === 2 ? split[0] : undefined,
84-
});
85-
});
86-
}
87-
});
73+
this.orderBy = parseOrderBy(this._url.searchParams);
8874
this.orderByKey = this.orderBy
8975
.map(
9076
({ column, ascending, nullsFirst, foreignTable }) =>

packages/postgrest-core/tests/index.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import * as Import from '../src';
33

44
describe('index exports', () => {
55
it('should export', () => {
6-
expect(Object.keys(Import)).toHaveLength(39);
6+
expect(Object.keys(Import)).toHaveLength(40);
77
});
88
});

0 commit comments

Comments
 (0)