Skip to content

Commit

Permalink
feat: adding not-equal support
Browse files Browse the repository at this point in the history
  • Loading branch information
JingWangTW authored and wovalle committed Jan 12, 2021
1 parent 770dd8a commit 3f253e5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/AbstractFirestoreRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ export abstract class AbstractFirestoreRepository<T extends IEntity> extends Bas
return new QueryBuilder<T>(this).whereEqualTo(prop, val);
}

/**
* Returns a new QueryBuilder with a filter specifying that the
* value in @param prop must not be equal to @param val.
*
* @param {IWherePropParam<T>} prop field to be filtered on, where
* prop could be keyof T or a lambda where T is the first parameter
* @param {IFirestoreVal} val value to compare in the filter
* @returns {QueryBuilder<T>} A new QueryBuilder with the specified
* query applied.
* @memberof AbstractFirestoreRepository
*/
whereNotEqualTo(prop: IWherePropParam<T>, val: IFirestoreVal): IQueryBuilder<T> {
return new QueryBuilder<T>(this).whereNotEqualTo(prop, val);
}

/**
* Returns a new QueryBuilder with a filter specifying that the
* value in @param prop must be greater than @param val.
Expand Down
6 changes: 6 additions & 0 deletions src/BaseFirestoreRepository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,12 @@ describe('BaseFirestoreRepository', () => {
expect(list[0].name).toEqual('Porcupine Tree');
});

it('must filter with whereNotEqualTo', async () => {
const list = await bandRepository.whereNotEqualTo('name', 'Porcupine Tree').find();
expect(list.length).toEqual(1);
expect(list[0].formationYear).toEqual(1983);
});

it('must filter with whereGreaterThan', async () => {
const list = await bandRepository.whereGreaterThan('formationYear', 1983).find();
expect(list.length).toEqual(1);
Expand Down
9 changes: 9 additions & 0 deletions src/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ export default class QueryBuilder<T extends IEntity> implements IQueryBuilder<T>
return this;
}

whereNotEqualTo(param: IWherePropParam<T>, val: IFirestoreVal) {
this.queries.push({
prop: this.extractWhereParam(param),
val,
operator: FirestoreOperators.notEqual,
});
return this;
}

whereGreaterThan(prop: IWherePropParam<T>, val: IFirestoreVal) {
this.queries.push({
prop: this.extractWhereParam(prop),
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type IFirestoreVal = string | number | Date | boolean | DocumentReference

export enum FirestoreOperators {
equal = '==',
notEqual = '!=',
lessThan = '<',
greaterThan = '>',
lessThanEqual = '<=',
Expand All @@ -36,6 +37,7 @@ export type IWherePropParam<T> = keyof T | ((t: T) => unknown);

export interface IQueryable<T extends IEntity> {
whereEqualTo(prop: IWherePropParam<T>, val: IFirestoreVal): IQueryBuilder<T>;
whereNotEqualTo(prop: IWherePropParam<T>, val: IFirestoreVal): IQueryBuilder<T>;
whereGreaterThan(prop: IWherePropParam<T>, val: IFirestoreVal): IQueryBuilder<T>;
whereGreaterOrEqualThan(prop: IWherePropParam<T>, val: IFirestoreVal): IQueryBuilder<T>;
whereLessThan(prop: IWherePropParam<T>, val: IFirestoreVal): IQueryBuilder<T>;
Expand Down

0 comments on commit 3f253e5

Please sign in to comment.