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/firestore
import 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 rsf
firestore.addDocument(collectionRef, data)
Adds a new document to this collection with the specified data, assigning it a document ID automatically.
Type | Description | |
---|---|---|
collectionRef Optional | String or firebase.firestore.CollectionReference | If using a string, it is a slash-separated path to a collection. |
data Optional | 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 | |
---|---|---|
pathOrRef Optional | String, firebase.firestore.CollectionReference, firebase.firestore.DocumentReference or firebase.firestore.Query | To filter, order or limit data, pass a firebase.firestore.Query (eg. |
type Optional | A string | Either |
buffer Optional | A Buffer object | Defaults to |
snapshotListenOptions Optional | 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 | |
---|---|---|
documentRef Optional | 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 | |
---|---|---|
collectionRef Optional | 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 | |
---|---|---|
documentRef Optional | 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 | |
---|---|---|
documentRef Optional | String or firebase.firestore.DocumentReference. | If using a string, it is a slash-separated path to a document. |
data Optional | Object | An object of the fields and values for the document. |
options Optional | 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 | |
---|---|---|
pathOrRef Optional | String or firebase.firestore.CollectionReference or firebase.firestore.Query | To filter, order or limit data, pass a Query (eg. |
options Optional | 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 | |
---|---|---|
pathOrRef Optional | String or firebase.firestore.DocumentReference | If using a string, it is a slash-separated path to a document. |
options Optional | 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 | |
---|---|---|
documentRef Optional | String or firebase.firestore.DocumentReference | If using a string, it is a slash-separated path to a document. |
args Optional | 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');
}