Sorting and filtering

Using the realtime database

All three read methods (database.read, database.channel and database.sync) accepts references as strings, Reference objects or Query objects.

Using the last two options we can filter, sort and limit the results using the Firebase API methods (orderByChild, equalTo, limitToLast, etc).

Filtering

// Will only get users for which the `isAdmin` key is `true`:
const admins = yield call(
  rsf.database.read,
  firebase.database().reference('users').orderByChild('isAdmin').equalTo(true)
)

Sorting

// Will get all users ordered by age:
const usersOrderedByAge = yield call(
  rsf.database.read,
  firebase.database().reference('users').orderByChild('age')
)

Limiting

// Will synchronize the five youngest users:
yield fork(
  rsf.database.sync,
  firebase.database().reference('users').orderByChild('age').limitToFirst(5),
  action
)

Using firestore

A similar approch works for firestore methods: getCollection, syncCollection and channel.

They also accept firebase.firestore.CollectionReference and firebase.firestore.Query as argument.

Filtering

// Will only synchronise users for which the `isAdmin` key is `true`:
yield fork(
  rsf.firestore.syncCollection,
  firestore.collection('users').where('isAdmin', '==', true),
  action
)

Sorting, limiting

// Get the 10 youngest users:
const users = yield call(
  rsf.firestore.getCollection,
  firestore.collection('users').orderBy('age').limit(10)
)