Skip to content

Commit

Permalink
feat: add sorting by joined column
Browse files Browse the repository at this point in the history
  • Loading branch information
kostyazgara committed Feb 26, 2024
1 parent f51f200 commit 934befe
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
ComparisonOperator,
CursorPaginationOptions,
} from '../interfaces';
import { getSortDirection } from '@knexion/sort';
import { buildSortPath, getSortDirection } from '@knexion/sort';

export class CursorPaginationInterceptor<
TRecord,
Expand Down Expand Up @@ -155,23 +155,22 @@ export class CursorPaginationInterceptor<
next?: boolean,
): void {
const [dir, column] = getSortDirection(currentSortProperty);
const [placeholder, bindings] = this.buildPath(column);
const prefixedPlaceholder = `${addPrefixColumn(placeholder, alias)}`;
const [placeholder, bindings] = buildSortPath(column, alias);
const lastSortValue = pageInfo[column];
const isNullLastSortValue = lastSortValue === null;
if (isNullLastSortValue) {
builder.whereRaw(`${prefixedPlaceholder} is null`, bindings);
builder.whereRaw(`${placeholder} is null`, bindings);
} else {
builder.where(
rawBuilder(prefixedPlaceholder, bindings),
rawBuilder(placeholder, bindings),
this.getWhereSortOperator(dir, next),
lastSortValue,
);
}
builder.andWhere((andWereBuilder) => {
if (!isNullLastSortValue) {
andWereBuilder.where(
rawBuilder(prefixedPlaceholder, bindings),
rawBuilder(placeholder, bindings),
this.getAndWhereSortOperator(dir, next),
lastSortValue,
);
Expand Down Expand Up @@ -222,19 +221,4 @@ export class CursorPaginationInterceptor<
}
return [];
}

private buildPath(path: string): [string, string[]] {
const [column, ...jsonPath] = path.split('.');
if (!jsonPath.length) {
return ['??', [column]];
}
const [lastProperty] = jsonPath.splice(-1);
if (!jsonPath.length) {
return [`??->>?`, [column, lastProperty]];
}
return [
`??->${jsonPath.map(() => '??').join('->')}->>?`,
[column, ...jsonPath, lastProperty],
];
}
}
22 changes: 3 additions & 19 deletions packages/sort/lib/interceptors/sort.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import {
KnexionExecutionContext,
KnexionCallHandler,
KnexionInterceptor,
addPrefixColumn,
SelectDatabaseOptions,
} from '@knexion/core';
import { getSortDirection } from '../utils';
import { buildSortPath, getSortDirection } from '../utils';
import { SortOptions } from '../interfaces';

export class SortInterceptor<TRecord, TResult>
Expand All @@ -27,9 +26,9 @@ export class SortInterceptor<TRecord, TResult>
if (sort && Array.isArray(sort)) {
sort.forEach((property) => {
const [direction, path] = getSortDirection(property);
const [placeholder, bindings] = this.buildPath(path);
const [placeholder, bindings] = buildSortPath(path, options.alias);
queryBuilder.orderByRaw(
`${addPrefixColumn(placeholder, options.alias)} ${direction} nulls ${
`${placeholder} ${direction} nulls ${
direction === 'desc' ? 'last' : 'first'
}`,
bindings,
Expand All @@ -39,19 +38,4 @@ export class SortInterceptor<TRecord, TResult>

return next.handle();
}

private buildPath(path: string): [string, string[]] {
const [column, ...jsonPath] = path.split('.');
if (!jsonPath.length) {
return ['??', [column]];
}
const [lastProperty] = jsonPath.splice(-1);
if (!jsonPath.length) {
return [`??->>?`, [column, lastProperty]];
}
return [
`??->${jsonPath.map(() => '??').join('->')}->>?`,
[column, ...jsonPath, lastProperty],
];
}
}
28 changes: 28 additions & 0 deletions packages/sort/lib/utils/build-sort-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { addPrefixColumn } from '@knexion/core';

const buildJsonSortPath = (
column: string,
jsonPath: string[],
): [string, string[]] => {
const [lastProperty] = jsonPath.splice(-1);
if (!jsonPath.length) {
return [`??->>?`, [column, lastProperty]];
}
return [
`??->${jsonPath.map(() => '??').join('->')}->>?`,
[column, ...jsonPath, lastProperty],
];
};

export const buildSortPath = (
path: string,
alias?: string,
): [string, string[]] => {
const [column, ...jsonPath] = path.split('->');
const prefixedColumn =
column.split('.').length > 1 ? column : addPrefixColumn(column, alias);
if (!jsonPath.length) {
return ['??', [prefixedColumn]];
}
return buildJsonSortPath(prefixedColumn, jsonPath);
};
1 change: 1 addition & 0 deletions packages/sort/lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './build-sort-path';
export * from './get-sort-direction';

0 comments on commit 934befe

Please sign in to comment.