Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass only where clause to the count query #684

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@
},
"version": "3.1.3",
"dependencies": {
"chalk": "^5.0.0"
"chalk": "^4.1.2 <5.0.0"
}
}
9 changes: 4 additions & 5 deletions src/__tests__/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { async } from 'rxjs';
import { Repository, FindManyOptions } from 'typeorm';

export class MockRepository extends Repository<any> {
Expand All @@ -14,17 +13,17 @@ export class MockRepository extends Repository<any> {
return [await this.find(options), await this.count(options)];
};

find = async (options?: FindManyOptions<any>): Promise<any[]> => {
find = jest.fn(async (options?: FindManyOptions<any>): Promise<any[]> => {
const startIndex = options.skip;
const endIndex = startIndex + options.take;

const localItems = this.items.slice(startIndex, endIndex);
return localItems;
};
});

count = async (options?: FindManyOptions<any>): Promise<number> => {
count = jest.fn(async (options?: FindManyOptions<any>): Promise<number> => {
return this.items.length;
};
});
}

export class Entity {}
37 changes: 37 additions & 0 deletions src/__tests__/paginate.repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,43 @@ describe('Test paginate function', () => {
expect(results).toBeInstanceOf(Pagination);
});

it('Calls to `find` & `count` should be correct (with explicit `where` clause)', async () => {
const mockRepository = new MockRepository(0);
const paginateOpts = {
limit: 8,
page: 2,
};
const findCondition = {
where: {foo: 'bar'},
orderBy: {
foo: 'ASC'
}
}

paginate<any>(mockRepository, {...paginateOpts}, findCondition);

expect(mockRepository.find).toHaveBeenCalledTimes(1);
expect(mockRepository.find).toHaveBeenCalledWith(expect.objectContaining({skip: 8, take: 8, ...findCondition}));
expect(mockRepository.count).toHaveBeenCalledTimes(1);
expect(mockRepository.count).toHaveBeenCalledWith(expect.objectContaining(findCondition.where));
});

it('Calls to `find` & `count` should be correct (with implicit `where` clause)', async () => {
const mockRepository = new MockRepository(0);
const paginateOpts = {
limit: 8,
page: 2,
};
const findCondition = {foo: 'bar'};

paginate<any>(mockRepository, {...paginateOpts}, findCondition);

expect(mockRepository.find).toHaveBeenCalledTimes(1);
expect(mockRepository.find).toHaveBeenCalledWith(expect.objectContaining({skip: 8, take: 8, where: findCondition}));
expect(mockRepository.count).toHaveBeenCalledTimes(1);
expect(mockRepository.count).toHaveBeenCalledWith(expect.objectContaining(findCondition));
});

it('Item length should be correct', async () => {
const mockRepository = new MockRepository(10);

Expand Down
22 changes: 14 additions & 8 deletions src/paginate.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
Repository,
FindManyOptions,
SelectQueryBuilder,
ObjectLiteral,
FindConditions,
FindOneOptions,
} from 'typeorm';
import { Pagination } from './pagination';
import {
Expand All @@ -19,7 +19,7 @@ const DEFAULT_PAGE = 1;
export async function paginate<T, CustomMetaType = IPaginationMeta>(
repository: Repository<T>,
options: IPaginationOptions<CustomMetaType>,
searchOptions?: FindConditions<T> | FindManyOptions<T>,
searchOptions?: FindConditions<T> | FindOneOptions<T>,
): Promise<Pagination<T, CustomMetaType>>;
export async function paginate<T, CustomMetaType = IPaginationMeta>(
queryBuilder: SelectQueryBuilder<T>,
Expand All @@ -29,7 +29,7 @@ export async function paginate<T, CustomMetaType = IPaginationMeta>(
export async function paginate<T, CustomMetaType = IPaginationMeta>(
repositoryOrQueryBuilder: Repository<T> | SelectQueryBuilder<T>,
options: IPaginationOptions<CustomMetaType>,
searchOptions?: FindConditions<T> | FindManyOptions<T>,
searchOptions?: FindConditions<T> | FindOneOptions<T>,
) {
return repositoryOrQueryBuilder instanceof Repository
? paginateRepository<T, CustomMetaType>(
Expand Down Expand Up @@ -150,10 +150,17 @@ function resolveNumericOption(
return defaultValue;
}

const normalizeSearchOptions = <T>(searchOptions?: FindConditions<T> | FindOneOptions<T>): FindOneOptions<T> => {
if(searchOptions && typeof searchOptions === 'object' && ('where' in searchOptions || 'order' in searchOptions)){
return searchOptions;
} else {
return {where: searchOptions};
}
}
async function paginateRepository<T, CustomMetaType = IPaginationMeta>(
repository: Repository<T>,
options: IPaginationOptions<CustomMetaType>,
searchOptions?: FindConditions<T> | FindManyOptions<T>,
searchOptions?: FindConditions<T> | FindOneOptions<T>,
): Promise<Pagination<T, CustomMetaType>> {
const [page, limit, route, paginationType, countQueries] =
resolveOptions(options);
Expand All @@ -170,19 +177,18 @@ async function paginateRepository<T, CustomMetaType = IPaginationMeta>(
});
}

const normalizedOpts = normalizeSearchOptions(searchOptions);
const promises: [Promise<T[]>, Promise<number> | undefined] = [
repository.find({
skip: limit * (page - 1),
take: limit,
...searchOptions,
...normalizedOpts,
}),
undefined,
];

if (countQueries) {
promises[1] = repository.count({
...searchOptions,
});
promises[1] = repository.count(normalizedOpts.where as any);
}

const [items, total] = await Promise.all(promises);
Expand Down
7 changes: 1 addition & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1274,19 +1274,14 @@ chalk@^2.0.0:
escape-string-regexp "^1.0.5"
supports-color "^5.3.0"

chalk@^4.0.0, chalk@^4.1.0:
chalk@^4.0.0, chalk@^4.1.0, "chalk@^4.1.2 <5.0.0":
version "4.1.2"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.2.tgz#aac4e2b7734a740867aeb16bf02aad556a1e7a01"
integrity sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==
dependencies:
ansi-styles "^4.1.0"
supports-color "^7.1.0"

chalk@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-5.0.0.tgz#bd96c6bb8e02b96e08c0c3ee2a9d90e050c7b832"
integrity sha512-/duVOqst+luxCQRKEo4bNxinsOQtMP80ZYm7mMqzuh5PociNL0PvmHFvREJ9ueYL2TxlHjBcmLCdmocx9Vg+IQ==

char-regex@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/char-regex/-/char-regex-1.0.2.tgz#d744358226217f981ed58f479b1d6bcc29545dcf"
Expand Down