When using firestore don't forget to install and load the @firebase/firestore dependency into your project (as it's not part of the firebase package yet):
yarn add @firebase/firestoreimport it in your project:
import firebase from 'firebase'
import '@firebase/firestore' // 👈 Don't forget this!
import ReduxSagaFirebase from 'redux-saga-firebase'
const firebaseApp = firebase.initializeApp({ ... })
const rsf = new ReduxSagaFirebase(firebaseApp)
export default rsffirestore.addDocument(collectionRef, data)Adds a new document to this collection with the specified data, assigning it a document ID automatically.
| Type | Description | |
|---|---|---|
collectionRefOptional | String or firebase.firestore.CollectionReference | If using a string, it is a slash-separated path to a collection. |
dataOptional | Object | The data to store. |
function* addDocument() {
const doc = yield call(
rsf.firestore.addDocument,
'users',
{
firstName: 'Elon',
lastName: 'Musk'
}
);
}
firestore.channel(pathOrRef, type, buffer)Returns a redux-saga Channel which emits every time the data at pathOrRef in firestore changes.
| Type | Description | |
|---|---|---|
pathOrRefOptional | String, firebase.firestore.CollectionReference, firebase.firestore.DocumentReference or firebase.firestore.Query | To filter, order or limit data, pass a firebase.firestore.Query (eg. |
typeOptional | A string | Either |
bufferOptional | A Buffer object | Defaults to |
snapshotListenOptionsOptional | A SnapshotListenOptions object | Options to control the circumstances when the channel will emit events. |
A redux-saga Channel which emits every time the data at pathOrRef in firestore changes.
function* syncTodosSaga() {
const channel = rsf.firestore.channel('todos');
while(true) {
const todos = yield take(channel);
yield put(syncTodos(todos));
}
}
firestore.deleteDocument(documentRef)Deletes the document referred to by this DocumentReference.
| Type | Description | |
|---|---|---|
documentRefOptional | String or firebase.firestore.DocumentReference. | If using a string, it is a slash-separated path to a document. |
function* deleteDocument() {
yield call(rsf.firestore.deleteDocument, 'users/elonm');
}
firestore.getCollection(collectionRef)Reads the collection referred to by this collectionRef.
| Type | Description | |
|---|---|---|
collectionRefOptional | String or firebase.firestore.CollectionReference or firebase.firestore.Query | To filter, order or limit data, pass a Query (eg. |
function* getCollection() {
const snapshot = yield call(rsf.firestore.getCollection, 'users');
let users;
snapshot.forEach(user => {
users = {
...users,
[user.id]: user.data()
}
});
yield put(gotUsers(users));
}
firestore.getDocument(documentRef)Reads the document referred to by this documentRef.
| Type | Description | |
|---|---|---|
documentRefOptional | String or firebase.firestore.DocumentReference. | If using a string, it is a slash-separated path to a document. |
function* getDocument() {
const snapshot = yield call(rsf.firestore.getDocument, 'users/1');
const user = snapshot.data();
yield put(gotUser(user));
}
firestore.setDocument(documentRef, data, options)Writes to the document referred to by this DocumentReference. If the document does not exist yet, it will be created. If you pass options, the provided data can be merged into the existing document.
| Type | Description | |
|---|---|---|
documentRefOptional | String or firebase.firestore.DocumentReference. | If using a string, it is a slash-separated path to a document. |
dataOptional | Object | An object of the fields and values for the document. |
optionsOptional | Object | An object to configure the set behavior. Pass |
function* setDocument() {
yield call(
rsf.firestore.setDocument,
'users/1',
{ firstName: 'Leonardo' }
);
}
firestore.syncCollection(pathOrRef, options)Automatically dispatches a redux action every time the collection at pathOrRef changes.
| Type | Description | |
|---|---|---|
pathOrRefOptional | String or firebase.firestore.CollectionReference or firebase.firestore.Query | To filter, order or limit data, pass a Query (eg. |
optionsOptional | Object | An object to configure how the collection should be synchronised. It must contain at least the |
import { syncTodos } from '../actionCreators/firestore';
function* todosRootSaga() {
yield fork(
rsf.firestore.syncCollection,
'todos',
{ successActionCreator: syncTodos }
);
}
firestore.syncDocument(pathOrRef, options)Automatically dispatches a redux action every time the document at pathOrRef changes.
| Type | Description | |
|---|---|---|
pathOrRefOptional | String or firebase.firestore.DocumentReference | If using a string, it is a slash-separated path to a document. |
optionsOptional | Object | An object to configure how the document should be synchronised. It must contain at least the |
import { syncTodo } from '../actionCreators/firestore';
function* todosRootSaga() {
yield fork(
rsf.firestore.syncDocument,
'todos/1',
{ successActionCreator: syncTodo }
);
}
firestore.updateDocument(documentRef, ...args)Updates fields in the document referred to by this DocumentReference. The update will fail if applied to a document that does not exist.
| Type | Description | |
|---|---|---|
documentRefOptional | String or firebase.firestore.DocumentReference | If using a string, it is a slash-separated path to a document. |
argsOptional | Object | Either an object containing all of the fields and values to update, or a series of arguments alternating between fields (as string or firebase.firestore.FieldPath objects) and values. |
function* updateDocument() {
yield call(rsf.firestore.updateDocument, 'users/1', 'lastName', 'Da Vinci');
}