Skip to content
This repository was archived by the owner on Jun 22, 2021. It is now read-only.

fix(createPaginationFilter): Fixes cursors for sorts with multiple keys. #15

Merged
merged 1 commit into from
May 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/utils/createPaginationFilter/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import createCursorFromEntity from '../createCursorFromEntity';
import createPaginationFilter from './index';

describe('createCursorFromEntity', () => {
const sort: Sort<TestEntity> = { id: asc, numberProp: desc };
const sort: Sort<TestEntity> = { numberProp: desc, id: asc };

it('should return empty filter when the cursor is start', () => {
const pagination: Pagination = { cursor: start, direction: forward, limit: 1 };
Expand All @@ -25,8 +25,12 @@ describe('createCursorFromEntity', () => {
const pagination: Pagination = { cursor, direction: forward, limit: 1 };
const actualResult = createPaginationFilter<TestEntity>(pagination, sort);
const expectedResult: Filter<TestEntity> = {
id: { $gt: testEntity.id },
numberProp: { $lte: testEntity.numberProp },
$or: [{
numberProp: { $lte: testEntity.numberProp },
}, {
id: { $gt: testEntity.id },
numberProp: testEntity.numberProp,
}],
};
assert.deepEqual(actualResult, expectedResult);
});
Expand All @@ -36,8 +40,12 @@ describe('createCursorFromEntity', () => {
const pagination: Pagination = { cursor, direction: backward, limit: 1 };
const actualResult = createPaginationFilter<TestEntity>(pagination, sort);
const expectedResult: Filter<TestEntity> = {
id: { $lt: testEntity.id },
numberProp: { $gte: testEntity.numberProp },
$or: [{
numberProp: { $gte: testEntity.numberProp },
}, {
id: { $lt: testEntity.id },
numberProp: testEntity.numberProp,
}],
};
assert.deepEqual(actualResult, expectedResult);
});
Expand Down
65 changes: 44 additions & 21 deletions src/utils/createPaginationFilter/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as atob from 'atob';
import { get, mapValues } from 'lodash';
import { get } from 'lodash';
import { start } from '../../types/Cursor';
import Entity from '../../types/Entity';
// tslint:disable-next-line:no-unused
Expand All @@ -13,30 +13,53 @@ const xor = (conditionA: boolean, conditionB: boolean) => {
return (conditionA && !conditionB) || (!conditionA && conditionB);
};

const getCursorKeyFilter = <E extends Entity>(
sortKey: string,
sort: Sort<E>,
pagination: Pagination,
cursorObj: any,
) => {
const sortOrder = get(sort, sortKey);
const ascendingPagination = !xor(
sortOrder === asc,
pagination.direction === forward,
);
const cursorValue = get(cursorObj, sortKey);
if (ascendingPagination) {
if (sortKey === 'id') {
return { $gt: cursorValue };
} else {
return { $gte: cursorValue };
}
} else {
if (sortKey === 'id') {
return { $lt: cursorValue };
} else {
return { $lte: cursorValue };
}
}
};

export default <E extends Entity>(pagination: Pagination, sort: Sort<E>): Filter<E> => {
if (pagination.cursor === start) {
const cursor = pagination.cursor;
if (cursor === start) {
return {};
}
const cursor = pagination.cursor;

const cursorObj = JSON.parse(atob(cursor));
const filter = mapValues(cursorObj, (cursorValue, sortKey) => {
const ascendingPagination = !xor(
get(sort, sortKey) === asc,
pagination.direction === forward,
);
if (ascendingPagination) {
if (sortKey === 'id') {
return { $gt: cursorValue };
} else {
return { $gte: cursorValue };
}
} else {
if (sortKey === 'id') {
return { $lt: cursorValue };
} else {
return { $lte: cursorValue };
}
}
const sortKeys = Object.keys(sort);
const sortKeyFilters = sortKeys.map((sortKey, keyIndex) => {
const sortKeysToMatch = sortKeys.slice(0, keyIndex);
const matchFilter = sortKeysToMatch.reduce((result: any, sortKeyToMatch) => {
result[sortKeyToMatch] = cursorObj[sortKeyToMatch];
return result;
}, {});

const cursorKeyFilter = getCursorKeyFilter(sortKey, sort, pagination, cursorObj);
matchFilter[sortKey] = cursorKeyFilter;
return matchFilter;
});

const filter = { $or: sortKeyFilters };
return filter as any as Filter<E>;
};