You have unlimited access as a PRO member
You are receiving a free preview of 3 lessons
Your free preview as expired - please upgrade to PRO
Contents
Recent Posts
- Object Oriented Programming With TypeScript
- Angular Elements Advanced Techniques
- TypeScript - the Basics
- The Real State of JavaScript 2018
- Cloud Scheduler for Firebase Functions
- Testing Firestore Security Rules With the Emulator
- How to Use Git and Github
- Infinite Virtual Scroll With the Angular CDK
- Build a Group Chat With Firestore
- Async Await Pro Tips
Ngrx Effects With Firestore and AngularFire V5
Episode 61 written by Jeff DelaneyIn this lesson, I will show you how to integrate AngularFire v5 with ngrx to handle documents and collections in the Firestore database. We will create an app that keeps data in-sync between the ngrx store frontend and the Firestore database backend.
This tutorial uses code directly from my previous Ngrx Entity tutorial. It updates the simple pizza app to use Firestore for backend data persistence. I highly recommend that you start there first.
Full Source Code
Adding Effects to the Modules
In the previous lesson, we created an ngrx feature module. We need to (1) add firebase and (2) add ngrx effects to the existing modules.
First, let’s update the app module with firebase and an empty effects array.
// app.module |
Then update the pizza module with the actual effects that will be created later.
// pizza.module |
Actions
We need to add a few new actions to deal with the asynchronous reading and writing of data in Firestore. Here’s a breakdown of the new actions, all the others will stay exactly the same.
- QUERY - Makes the initial request for a firestore collection.
- ADD_ALL - Takes the returned array of documents and adds them to ngrx store.
- SUCCESS - Generic action that represents successful write operation in Firestore.
import { Action } from '@ngrx/store'; |
Reducer
The reducer is going to get much simpler. All of our CRUD operations are now asynchronous and handled via effects. The only thing we need our reducer to do is update the collection with new objects when the data changes. Firestore always returns the full array of documents when a change occurs, so using addAll()
seems to be the best approach when working with Firstore collections and @ngrx/entity.
I also removed the default data because we will now be retrieving all persistent data from Firestore.
import * as actions from './pizza.actions'; |
Ngrx Effects with Firestore
All of the important work happens in the effects, starting with the first $query
effect that retrieves a Firestore collection.
Our interaction with Firebase is asynchronous, so we are going to listen to certain initial actions with the @effect
decorator, then map them to a different action after performing an async operation. The effects code is very RxJS heavy and may look intimidating if you’re new to this stuff, but what it does is actually quite simple:
- It listens to an action type that triggers an async event, i.e
CREATE
. - Makes the request with AngularFire, which returns a
Promise
(or Observable). - Maps the result to an action, i.e
SUCCESS
orERROR
.
Just remember, all of the effects below follow this same three-step process.
@Injectable() |
import 'rxjs/add/operator/map'; |
Component
Lastly, we need to update the component. The only difference is that we need to run the QUERY action to load the initial data. Once this happens, our ngrx store and Firebase will be synced up with a realtime connection.
Only one line has changed from the previous lesson this.store.dispatch( new actions.Query() )
, which queries the initial data when the component is initialized.
import { Component, OnInit } from '@angular/core'; |
The End
Having a persistent backend is required for almost all apps and Firestore makes it easy. Let me know what you think in the comments or on Slack.