Queries
Query listeners are attached by using the firebaseConnect
higher order component. firebaseConnect
accepts an array of paths for which to create queries. When listening to paths, it is possible to modify the query with any of Firebase's included query methods.
NOTE:
By default the results of queries are stored in redux under the path of the query. If you would like to change where the query results are stored in redux, use storeAs
(more below).
Below are examples using Firebase query methods as well as other methods that are included (such as 'populate').
firebaseConnect
is a Higher Order Component (wraps a provided component) that attaches listeners to relevant paths on Firebase when mounting, and removes them when unmounting.
storeAs
Data is stored in redux under the path of the query for convince. This means that two different queries to the same path (i.e. todos
) will both place data into state.data.todos
even if their query parameters are different. If you would like to store your query somewhere else in redux, use storeAs
:
@firebaseConnect([
{
path: 'todos',
storeAs: 'myTodos', // place in redux under "myTodos"
queryParams: ['orderByChild=createdBy', 'equalTo=123someuid'],
}
{
path: 'todos',
queryParams: ['limitToFirst=20'],
}
])
@connect(({ firebase }) => ({
myTodos: dataToJS(firebase, 'myTodos'), // state.firebase.data.myTodos due to storeAs
allTodos: dataToJS(firebase, 'todos') // state.firebase.data.todos since no storeAs
}))
once
To load a firebase location once instead of binding, the once option can be used:
Internally Uses Firebase Method: orderByPriority
@firebaseConnect([
{ type: 'once', path: '/todos' }
])
orderByChild
To order the query by a child within each object, use orderByChild.
Internally Uses Firebase Method: orderByChild
Example
Ordering a list of todos by the text parameter of the todo item (placing them in alphabetical order).
@firebaseConnect([
'/todos#orderByChild=text'
// { path: '/todos', queryParams: [ 'orderByChild=text' ]} // object notation
])
orderByKey
Order a list by the key of each item. Since push keys contain time, this is also a way of ordering by when items were created.
Internally Uses Firebase Method:
orderByKey
Example
Ordering a list of todos based on their key (puts them in order of when they were created)
@firebaseConnect([
'/todos#orderByKey'
// { path: '/todos', queryParams: [ 'orderByKey' ]} // object notation
])
orderByValue
Order a list by the value of each object. Internally runs
Internally Uses Firebase Method: orderByValue
Example
Ordering a list of score's based on score's value
@firebaseConnect([
`scores#orderByValue`
// { path: '/scores', queryParams: [ 'orderByValue' ] } // object notation
])
orderByPriority
Order a list by the priority of each item.
Internally Uses Firebase Method: orderByPriority
Example
Ordering a list based on priorities
@firebaseConnect([
`scores#orderByPriority`
// { path: '/scores', queryParams: [ 'orderByPriority' ] } // object notation
])
limitToFirst
Limit query results to the first n number of results.
Internally Uses Firebase Method: limitToFirst
Examples
Displaying only the first todo item
@firebaseConnect([ '/todos#limitToFirst' // { path: '/todos', queryParams: [ 'limitToFirst=1' ] } // object notation ])
Displaying only the first 10 todo items
@firebaseConnect([ '/todos#limitToFirst=10' // { path: '/todos', queryParams: [ 'orderByChild=createdBy', 'equalTo=123' ] } // object notation ])
limitToLast
Limit query results to the last n number of results
Internally Uses Firebase Method: limitToLast
Examples
Only the last todo item
@firebaseConnect([ '/todos#limitToLast' // { path: '/todos', queryParams: [ 'limitToLast' ] } // object notation ])
Only the last 10 todo items
@firebaseConnect([ '/todos#limitToLast=10' // { path: '/todos', queryParams: [ 'limitToLast=10' ] } // object notation ])
startAt
Start query at a specific location by providing the specific number or value
Internally Uses Firebase Method: startAt
Examples
- Starting at the fifth item
@firebaseConnect([ 'todos#startAt=5&limitToFirst=2' // { path: '/todos', queryParams: [ 'startAt=5', 'limitToFirst=2' ] } // object notation ])
- Paginate results
@firebaseConnect([ 'todos#startAt=5&limitToFirst=10' // { path: '/todos', queryParams: [ 'startAt=5', 'limitToFirst=10' ] } // object notation ])
- Non-number values
@firebaseConnect([ 'todos#startAt=val1&limitToFirst=10' // { path: '/todos', queryParams: [ 'startAt=5', 'limitToFirst=10' ] } // object notation ])
endAt
End query at a specific location by providing the specific number or value
Internally Uses Firebase Method: endAt
Examples
- Usage with startAt
@firebaseConnect([ 'todos#orderByChild=added&startAt=1&endAt=5' // { path: '/todos', queryParams: [ 'orderByChild=added', 'startAt=1', 'endAt=5' ] } // object notation ])
equalTo
Limit query results with parameter equal to previous query method (i.e when used with orderByChild, it limits results with child equal to provided value).
Internally Uses Firebase Method: equalTo
Parsing
The following are internally parsed:
null
boolean
number
This means the actual value will be parsed instead of the string containing the value. If you do not want this to happen, look at the notParsed
query parameter below.
Examples
- Order by child parameter
@firebaseConnect([ 'todos#orderByChild=createdBy&equalTo=ASD123', // { path: '/todos', queryParams: [ 'orderByChild=createdBy', 'equalTo=ASD123' ] } // object notation ])
notParsed
Can be used to keep internal parsing from happening. Useful when attempting to search a number string using equalTo
Examples
- Order by child parameter equal to a number string. Equivalent of searching for
'123'
(where as not usingnotParsed
would search for children equal to123
)@firebaseConnect([ { path: '/todos', queryParams: [ 'orderByChild=createdBy', 'notParsed', // keeps equalTo from automatically parsing 'equalTo=123' ] } ])
storeAs
By default the results of queries are stored in redux under the path of the query. If you would like to change where the query results are stored in redux, use storeAs
:
Examples
- Querying the same path with different query parameters
@firebaseConnect(props => [
{ path: 'projects' }
{ path: 'projects', storeAs: 'myProjects', queryParams: ['orderByChild=uid', '123'] }
])
@connect(({ firebase }, props) => ({
projects: populatedDataToJS(firebase, 'projects'),
myProjects: populatedDataToJS(firebase, 'myProjects'), // use storeAs path to gather from redux
}))
ordered
In order to get ordered data, use orderedToJS
Examples
- Get list of projects ordered by key
@firebaseConnect(props => [
{ path: 'projects', queryParams: ['orderByKey'] }
])
@connect(({ firebase }, props) => ({
projects: orderedToJS(firebase, 'projects'),
}))
Populate
Populate allows you to replace IDs within your data with other data from Firebase. This is very useful when trying to keep your data flat. Some would call it a join, but it was modeled after the mongo populate method.
Visit Populate Section for full documentation.