Skip to content

Commit

Permalink
Merge pull request #284 from jomendez/orderby-function-called-more-th…
Browse files Browse the repository at this point in the history
…an-once
  • Loading branch information
wovalle authored Nov 11, 2021
2 parents 10d78dd + f94aa04 commit 3732cf1
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
50 changes: 49 additions & 1 deletion src/BaseFirestoreRepository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,24 @@ describe('BaseFirestoreRepository', () => {
albumsSubColl.orderByAscending('releaseDate').orderByDescending('releaseDate');
}).toThrow();
});

it('must throw an Error if an orderBy* function is called more than once in the same expression ascending', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const albumsSubColl = pt.albums;
expect(() => {
albumsSubColl.orderByAscending('releaseDate').orderByAscending('releaseDate');
}).toThrow();
});

it('must succeed when orderBy* function is called more than once in the same expression with different fields', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const albumsSubColl = pt.albums;
expect(() => {
albumsSubColl.orderByAscending('releaseDate').orderByDescending('name');
}).not.toThrow();
});
});
});

describe('orderByDescending', () => {
it('must order repository objects', async () => {
Expand Down Expand Up @@ -164,8 +181,39 @@ describe('BaseFirestoreRepository', () => {
albumsSubColl.orderByAscending('releaseDate').orderByDescending('releaseDate');
}).toThrow();
});

it('must throw an Error if an orderBy* function is called more than once in the same expression descending', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const albumsSubColl = pt.albums;
expect(() => {
albumsSubColl.orderByDescending('releaseDate').orderByDescending('releaseDate');
}).toThrow();
});

it('must succeed when orderBy* function is called more than once in the same expression with different fields', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const albumsSubColl = pt.albums;
expect(() => {
albumsSubColl.orderByAscending('releaseDate').orderByDescending('name');
}).not.toThrow();
});

it('must succeed when orderBy* function is called more than once in the same expression with different fields ascending', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const albumsSubColl = pt.albums;
expect(() => {
albumsSubColl.orderByAscending('releaseDate').orderByAscending('name');
}).not.toThrow();
});

it('must succeed when orderBy* function is called more than once in the same expression with different fields descending', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const albumsSubColl = pt.albums;
expect(() => {
albumsSubColl.orderByDescending('releaseDate').orderByDescending('name');
}).not.toThrow();
});
});
});

describe('findById', () => {
it('must find by id', async () => {
Expand Down
19 changes: 17 additions & 2 deletions src/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class QueryBuilder<T extends IEntity> implements IQueryBuilder<T> {
protected limitVal: number;
protected orderByObj: IOrderByParams;
protected customQueryFunction?: ICustomQuery<T>;
protected orderByFields: Set<string> = new Set();

constructor(protected executor: IQueryExecutor<T>) {}

Expand Down Expand Up @@ -144,12 +145,19 @@ export class QueryBuilder<T extends IEntity> implements IQueryBuilder<T> {
}

orderByAscending(prop: IWherePropParam<T>) {
if (this.orderByObj) {
const fieldProp: string = typeof prop == 'string' ? prop : '';
const alreadyOrderedByField = this.orderByFields.has(fieldProp);

if (this.orderByObj && alreadyOrderedByField) {
throw new Error(
'An orderBy function cannot be called more than once in the same query expression'
);
}

if (!alreadyOrderedByField && fieldProp) {
this.orderByFields.add(fieldProp);
}

this.orderByObj = {
fieldPath: this.extractWhereParam(prop),
directionStr: 'asc',
Expand All @@ -159,12 +167,19 @@ export class QueryBuilder<T extends IEntity> implements IQueryBuilder<T> {
}

orderByDescending(prop: IWherePropParam<T>) {
if (this.orderByObj) {
const fieldProp: string = typeof prop == 'string' ? prop : '';
const alreadyOrderedByField = this.orderByFields.has(fieldProp);

if (this.orderByObj && alreadyOrderedByField) {
throw new Error(
'An orderBy function cannot be called more than once in the same query expression'
);
}

if (!alreadyOrderedByField && fieldProp) {
this.orderByFields.add(fieldProp);
}

this.orderByObj = {
fieldPath: this.extractWhereParam(prop),
directionStr: 'desc',
Expand Down

0 comments on commit 3732cf1

Please sign in to comment.