Skip to content

Commit

Permalink
feat: adding not-in 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 3f253e5 commit 8766b3b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/AbstractFirestoreRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,21 @@ export abstract class AbstractFirestoreRepository<T extends IEntity> extends Bas
return new QueryBuilder<T>(this).whereIn(prop, val);
}

/**
* Returns a new QueryBuilder with a filter specifying that the
* field @param prop matches none of the comparison values in @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[] array of values to compare in the filter (max 10 items in array)
* @returns {QueryBuilder<T>} A new QueryBuilder with the specified
* query applied.
* @memberof AbstractFirestoreRepository
*/
whereNotIn(prop: IWherePropParam<T>, val: IFirestoreVal[]): IQueryBuilder<T> {
return new QueryBuilder<T>(this).whereNotIn(prop, val);
}

/**
* Returns a new QueryBuilder with a maximum number of results
* to return. Can only be used once per query.
Expand Down
11 changes: 11 additions & 0 deletions src/BaseFirestoreRepository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,11 @@ describe('BaseFirestoreRepository', () => {
expect(list.length).toEqual(3);
});

it('must filter with whereNotIn', async () => {
const list = await bandRepository.whereNotIn('formationYear', [1965]).find();
expect(list.length).toEqual(2);
});

it('should throw with whereArrayContainsAny and more than 10 items in val array', async () => {
expect(async () => {
await bandRepository
Expand All @@ -457,6 +462,12 @@ describe('BaseFirestoreRepository', () => {
}).rejects.toThrow(Error);
});

it('should throw with whereNotIn and more than 10 items in val array', async () => {
expect(async () => {
await bandRepository.whereNotIn('formationYear', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]).find();
}).rejects.toThrow(Error);
});

it('must filter with two or more operators', async () => {
const list = await bandRepository
.whereLessOrEqualThan('formationYear', 1983)
Expand Down
19 changes: 17 additions & 2 deletions src/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export default class QueryBuilder<T extends IEntity> implements IQueryBuilder<T>
if (val.length > 10) {
throw new Error(`
This query supports up to 10 values. You provided ${val.length}.
For details please visit: https://firebase.google.com/docs/firestore/query-data/queries#in_and_array-contains-any
For details please visit: https://firebase.google.com/docs/firestore/query-data/queries#in_not-in_and_array-contains-any
`);
}
this.queries.push({
Expand All @@ -105,7 +105,7 @@ export default class QueryBuilder<T extends IEntity> implements IQueryBuilder<T>
if (val.length > 10) {
throw new Error(`
This query supports up to 10 values. You provided ${val.length}.
For details please visit: https://firebase.google.com/docs/firestore/query-data/queries#in_and_array-contains-any
For details please visit: https://firebase.google.com/docs/firestore/query-data/queries#in_not-in_and_array-contains-any
`);
}
this.queries.push({
Expand All @@ -116,6 +116,21 @@ export default class QueryBuilder<T extends IEntity> implements IQueryBuilder<T>
return this;
}

whereNotIn(prop: IWherePropParam<T>, val: IFirestoreVal[]) {
if (val.length > 10) {
throw new Error(`
This query supports up to 10 values. You provided ${val.length}.
For details please visit: https://firebase.google.com/docs/firestore/query-data/queries#in_not-in_and_array-contains-any
`);
}
this.queries.push({
prop: this.extractWhereParam(prop),
val,
operator: FirestoreOperators.notIn,
});
return this;
}

limit(limitVal: number) {
if (this.limitVal) {
throw new Error(
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export enum FirestoreOperators {
arrayContains = 'array-contains',
arrayContainsAny = 'array-contains-any',
in = 'in',
notIn = 'not-in'
}

export interface IFireOrmQueryLine {
Expand Down Expand Up @@ -45,6 +46,7 @@ export interface IQueryable<T extends IEntity> {
whereArrayContains(prop: IWherePropParam<T>, val: IFirestoreVal): IQueryBuilder<T>;
whereArrayContainsAny(prop: IWherePropParam<T>, val: IFirestoreVal[]): IQueryBuilder<T>;
whereIn(prop: IWherePropParam<T>, val: IFirestoreVal[]): IQueryBuilder<T>;
whereNotIn(prop: IWherePropParam<T>, val: IFirestoreVal[]): IQueryBuilder<T>;
find(): Promise<T[]>;
findOne(): Promise<T | null>;
}
Expand Down

0 comments on commit 8766b3b

Please sign in to comment.