Skip to content

Commit

Permalink
fix: A bug fix
Browse files Browse the repository at this point in the history
Description:
Store the fields that have been ordered by with (and throw if you try to
orderBy twice in the same field) instead of just checking if any fields
have been ordered by.
An orderBy function cannot be called more than once in the same query
expression, except on different fields.

Fixing issue #266
  • Loading branch information
pepe authored and pepe committed Nov 11, 2021
1 parent 10d78dd commit f4041ed
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/BaseFirestoreRepository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,16 @@ describe('BaseFirestoreRepository', () => {
albumsSubColl.orderByAscending('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');
}).toBeTruthy();
});
});
});

describe('orderByDescending', () => {
it('must order repository objects', async () => {
Expand Down Expand Up @@ -164,8 +173,15 @@ describe('BaseFirestoreRepository', () => {
albumsSubColl.orderByAscending('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');
}).toBeTruthy();
});
});
});

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 f4041ed

Please sign in to comment.