2017-10-03 11:03:16 +00:00
# Firestore (Beta)
2017-10-04 21:49:46 +00:00
RNFirebase mimics the [Firestore Web SDK ](https://firebase.google.com/docs/reference/js/firebase.firestore ) by providing a bridge to the native Android and iOS Firestore SDK's.
2017-10-03 11:03:16 +00:00
2017-10-04 21:49:46 +00:00
All Firestore operations are accessed via `firebase.firestore()` . For detailed documentation of the supported methods demonstrated below please use the official firestore web sdk guides.
2017-10-03 11:03:16 +00:00
2017-10-04 20:18:20 +00:00
?> Please note that Persistence (offline support) is enabled by default with Firestore on iOS and Android.
2017-10-03 11:03:16 +00:00
## Add and Manage Data
### Collections
Read information about a collection example:
```javascript
2017-10-04 20:18:20 +00:00
import firebase from 'react-native-firebase';
2017-10-03 11:03:16 +00:00
firebase.firestore()
.collection('posts')
.get()
.then(querySnapshot => {
// Access all the documents in the collection
const docs = querySnapshot.docs;
// Access the list of document changes for the collection
const changes = querySnapshot.docChanges;
// Loop through the documents
querySnapshot.forEach((doc) => {
const value = doc.data();
})
})
```
Add to a collection example (generated ID):
```javascript
2017-10-04 20:18:20 +00:00
import firebase from 'react-native-firebase';
2017-10-03 11:03:16 +00:00
firebase.firestore()
.collection('posts')
.add({
title: 'Amazing post',
})
.then(() => {
// Document added to collection and ID generated
// Will have path: `posts/{generatedId}`
})
```
Add to a collection example (manual ID):
```javascript
2017-10-04 20:18:20 +00:00
import firebase from 'react-native-firebase';
2017-10-03 11:03:16 +00:00
firebase.firestore()
.collection('posts')
.doc('post1')
.set({
title: 'My awesome post',
content: 'Some awesome content',
})
.then(() => {
// Document added to collection with path: `posts/post1`
})
```
### Documents
There are multiple ways to read a document. The following are equivalent examples:
```javascript
2017-10-04 20:18:20 +00:00
import firebase from 'react-native-firebase';
2017-10-03 11:03:16 +00:00
firebase.firestore()
.doc('posts/posts1')
2017-10-11 00:59:11 +00:00
.get()
.then((documentSnapshot) => {
2017-10-03 11:03:16 +00:00
const value = documentSnapshot.data();
});
firebase.firestore()
.collection('posts')
.doc('posts1')
2017-10-11 00:59:11 +00:00
.get()
.then((documentSnapshot) => {
2017-10-03 11:03:16 +00:00
const value = documentSnapshot.data();
});
```
Create a document example:
```javascript
2017-10-04 20:18:20 +00:00
import firebase from 'react-native-firebase';
2017-10-03 11:03:16 +00:00
firebase.firestore()
.doc('posts/posts1')
.set({
title: 'My awesome post',
content: 'Some awesome content',
})
.then(() => {
// Document created
});
```
Updating a document example:
```javascript
2017-10-04 20:18:20 +00:00
import firebase from 'react-native-firebase';
2017-10-03 11:03:16 +00:00
firebase.firestore()
.doc('posts/posts1')
.update({
title: 'My awesome post',
})
.then(() => {
// Document created
});
```
Deleting a document example:
```javascript
2017-10-04 20:18:20 +00:00
import firebase from 'react-native-firebase';
2017-10-03 11:03:16 +00:00
firebase.firestore()
.doc('posts/posts1')
.delete()
.then(() => {
// Document deleted
});
```
### Batching document updates
Writes, updates and deletes to documents can be batched and committed atomically as follows:
```javascript
2017-10-04 20:18:20 +00:00
import firebase from 'react-native-firebase';
2017-10-03 11:03:16 +00:00
const ayRef = firebase.firestore().doc('places/AY');
const lRef = firebase.firestore().doc('places/LON');
const nycRef = firebase.firestore().doc('places/NYC');
const sfRef = firebase.firestore().doc('places/SF');
firebase.firestore()
.batch()
.set(ayRef, { name: 'Aylesbury' })
.set(lRef, { name: 'London' })
.set(nycRef, { name: 'New York City' })
.set(sfRef, { name: 'San Francisco' })
.update(nycRef, { population: 1000000 })
.update(sfRef, { name: 'San Fran' })
.set(lRef, { population: 3000000 }, { merge: true })
.delete(ayRef)
.commit()
.then(() => {
// Would end up with three documents in the collection: London, New York City and San Francisco
});
```
### Transactions
Coming soon
## Realtime Updates
### Collections
Listen to collection updates example:
```javascript
2017-10-04 20:18:20 +00:00
import firebase from 'react-native-firebase';
2017-10-03 11:03:16 +00:00
firebase.firestore()
.collection('cities')
.where('state', '==', 'CA')
.onSnapshot((querySnapshot) => {
querySnapshot.forEach((doc) => {
// DocumentSnapshot available
})
})
```
The snapshot handler will receive a new query snapshot every time the query results change (that is, when a document is added, removed, or modified).
### Documents
Listen to document updates example:
```javascript
2017-10-04 20:18:20 +00:00
import firebase from 'react-native-firebase';
2017-10-03 11:03:16 +00:00
firebase.firestore()
.doc('posts/post1')
.onSnapshot((documentSnapshot) => {
// DocumentSnapshot available
})
```
The snapshot handler will receive the current contents of the document, and any subsequent changes to the document.