-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: WHERE filters were not behaving as expected
- Loading branch information
Sam Jackson
committed
Feb 4, 2019
1 parent
12b3313
commit d2a0b1b
Showing
5 changed files
with
156 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,55 @@ | ||
/** | ||
* Parses GraphQL AST to get complex parameters to GraphQL queries. | ||
* @param objectFields Set of key-value pairs in GraphQL AST form. | ||
*/ | ||
export const parseObjectValue = (objectFields: any): any => { | ||
return objectFields.map((field: any) => { | ||
const { name, value } = field; | ||
if (value.kind === 'IntValue') value.value = parseInt(value.value); | ||
|
||
return { | ||
key: name.value, | ||
value: value.value | ||
}; | ||
}); | ||
} | ||
|
||
/** | ||
* Applies filters to a Firestore query. Basically chains a series of | ||
* calls to `firestore.collection#where`. | ||
* @param collectionQuery The query to be made against some collection. | ||
* @param where Set of filters formatted as `KEY_COMPARATOR: VALUE` pairs | ||
*/ | ||
export const setQueryFilters = ( | ||
collectionQuery: any, | ||
where: any[] | ||
): firebase.firestore.Query => { | ||
where.forEach((filter: any) => { | ||
const key: string = filter['key']; | ||
const value: any = filter['value']; | ||
const splitKey: string[] = key.split('_'); | ||
const whereOp = splitKey[splitKey.length - 1]; | ||
const actualKey = key.slice(0, -1 * (whereOp.length + 1)); | ||
switch (whereOp) { | ||
case 'eq': | ||
collectionQuery = collectionQuery.where(actualKey, '==', value); | ||
break; | ||
case 'gt': | ||
collectionQuery = collectionQuery.where(actualKey, '>', value); | ||
break; | ||
case 'gte': | ||
collectionQuery = collectionQuery.where(actualKey, '>=', value); | ||
break; | ||
case 'lt': | ||
collectionQuery = collectionQuery.where(actualKey, '<', value); | ||
break; | ||
case 'lte': | ||
collectionQuery = collectionQuery.where(actualKey, '<=', value); | ||
break; | ||
default: | ||
collectionQuery = collectionQuery.where(key, '==', value); | ||
break; | ||
} | ||
}); | ||
return collectionQuery; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters