diff --git a/packages/cursor-pagination/lib/interceptors/cursor-pagination.interceptor.ts b/packages/cursor-pagination/lib/interceptors/cursor-pagination.interceptor.ts index ff233047..3aabadb1 100644 --- a/packages/cursor-pagination/lib/interceptors/cursor-pagination.interceptor.ts +++ b/packages/cursor-pagination/lib/interceptors/cursor-pagination.interceptor.ts @@ -14,7 +14,7 @@ import { ComparisonOperator, CursorPaginationOptions, } from '../interfaces'; -import { getSortDirection } from '@knexion/sort'; +import { buildSortPath, getSortDirection } from '@knexion/sort'; export class CursorPaginationInterceptor< TRecord, @@ -155,15 +155,14 @@ 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, ); @@ -171,7 +170,7 @@ export class CursorPaginationInterceptor< builder.andWhere((andWereBuilder) => { if (!isNullLastSortValue) { andWereBuilder.where( - rawBuilder(prefixedPlaceholder, bindings), + rawBuilder(placeholder, bindings), this.getAndWhereSortOperator(dir, next), lastSortValue, ); @@ -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], - ]; - } } diff --git a/packages/sort/lib/interceptors/sort.interceptor.ts b/packages/sort/lib/interceptors/sort.interceptor.ts index f8088285..e7800872 100644 --- a/packages/sort/lib/interceptors/sort.interceptor.ts +++ b/packages/sort/lib/interceptors/sort.interceptor.ts @@ -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 @@ -27,9 +26,9 @@ export class SortInterceptor 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, @@ -39,19 +38,4 @@ export class SortInterceptor 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], - ]; - } } diff --git a/packages/sort/lib/utils/build-sort-path.ts b/packages/sort/lib/utils/build-sort-path.ts new file mode 100644 index 00000000..3f1184da --- /dev/null +++ b/packages/sort/lib/utils/build-sort-path.ts @@ -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); +}; diff --git a/packages/sort/lib/utils/index.ts b/packages/sort/lib/utils/index.ts index 4a9ce3e1..cde57144 100644 --- a/packages/sort/lib/utils/index.ts +++ b/packages/sort/lib/utils/index.ts @@ -1 +1,2 @@ +export * from './build-sort-path'; export * from './get-sort-direction';