Skip to content

Commit

Permalink
fix: Allow numbers and objects in filters
Browse files Browse the repository at this point in the history
For example: `filter={["thing", "==", db.doc("things/" + thing)]}`

fix #16
  • Loading branch information
bfirsh committed Jun 7, 2018
1 parent 455a47b commit 3192249
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,22 @@ Passing in an array of arrays creates a compound query to filter the collection
/>
```

Passing in document references allows you to filter by reference fields:

```jsx
<FirestoreCollection
path={'users'}
filter={[
'organization',
'==',
firestore.collection('organizations').doc('foocorp'),
]}
render={() => {
/* render stuff*/
}}
/>
```

##### render

> function({}) | _required_
Expand Down
8 changes: 7 additions & 1 deletion src/FirestoreCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ class FirestoreCollection extends Component {
sort: PropTypes.string,
limit: PropTypes.number,
filter: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.string),
PropTypes.arrayOf(
PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.object,
]),
),
PropTypes.arrayOf(PropTypes.array),
]),
children: PropTypes.func,
Expand Down
22 changes: 22 additions & 0 deletions src/__tests__/collection.filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,25 @@ test('filters the documents returned with a compound filter', () => {
}),
);
});

test('filter accepts objects and numbers', () => {
const { firestoreMock } = createMocksForCollection();
const renderMock = jest.fn().mockReturnValue(<div />);
const collectionName = 'users';
mount(
<FirestoreCollection
path={collectionName}
filter={['number', '==', 5]}
render={renderMock}
/>,
{ context: { firestoreDatabase: firestoreMock, firestoreCache: {} } },
);
mount(
<FirestoreCollection
path={collectionName}
filter={['ref', '==', firestoreMock.doc('things/foobar')]}
render={renderMock}
/>,
{ context: { firestoreDatabase: firestoreMock, firestoreCache: {} } },
);
});

0 comments on commit 3192249

Please sign in to comment.