Angular 8 Firestore tutorial with CRUD application example – @angular/fire

Cloud Firestore helps us store data in the cloud. It supports offline mode so our app will work fine (write, read, listen to, and query data) whether device has internet connection or not, it automatically fetches changes from our database to Firebase Server. We can structure data in our ways to improve querying and fetching capabilities. This tutorial shows you how to work with Firebase Firestore along with an Angular app that can do CRUD Operations.

Related Posts:
Angular 8 Firebase tutorial: Integrate Firebase into Angular 8 App with @angular/fire
Angular 8 Firebase CRUD operations with @angular/fire
Angular 8 – Upload/Display/Delete files to/from Firebase Storage using @angular/fire

Technology

– Angular 8
– @angular/fire 5.1.2
– firebase 5.10.1

Video

Firebase Firestore with @angular/fire

Set up the Firebase Project & Install @angular/fire

Please visit this post to know step by step.

The only different for this tutorial is that, we use Firebase Firestore instead of Firebase Realtime Database:

angular-8-firestore-turorial-create-database

Firebase Firebase CRUD operations for Object
Create an object binding/ Retrieve

Create

Update

Delete

Firebase Firebase CRUD operations for List of Objects
Create a list binding/ Retrieve

– Returns an Observable of data as a synchronized array of JSON objects without snapshot metadata. It is simple to render to a view:

– Returns an Observable of data as a synchronized array of DocumentChangeAction<>[] with metadata (the underlying data reference and snapshot id):

Create

Update

Delete

Configure offline persistence

By default, offline persistence is disabled. To enable it, call enablePersistence() method:

Firebase Firestore CRUD in Angular 8 App using @angular/fire

Project Overview
Goal

We will build an Angular 8 Firebase App using @angular/fire that can:
– add/remove Customer
– show all Customers
– update Customer’s status
to Firebase Firestore.

angular-8-firestore-turorial-demo-goal

Project Structure

angular-8-firestore-turorial-project-structure

environment.ts configure information to connect with Firebase Project.
customer.ts defines Customer class data model.
customer.service.ts defines CustomerService that uses @angular.fire to interact with Firebase.
– 3 Angular components:
+ create-customer for creating new item.
+ customer-details shows item detais
+ customers-list contains list of items, this is parent component of customer-details
app-routing.module.ts defines how we route Angular components.
app.module.ts declares and imports necessary environment & modules.

Step by Step
Set up the Firebase Project & Install @angular/fire

Please visit this post to know step by step.

angular-8-firestore-turorial-create-database

Add Firebase config to environments variable

Open /src/environments/environment.ts, add your Firebase configuration that we have saved when Popup window was shown:

angular-8-firebase-tutorial-integrate-angular-fire-popup

Create Service & Components

Run the commands below:
ng g s customers/Customer
ng g c customers/CustomerDetails
ng g c customers/CustomersList
ng g c customers/CreateCustomer

Setup @NgModule

app.module.ts

Create Model Class

customer.ts

The key field is important for updating item.

Create Data Service

customer.service.ts

Create Component for item Details

customer-details.component.ts

customer-details.component.html

Create Component to show List of Items

customers-list.component.ts

In the code above, we use snapshotChanges() with RxJS map() operator to get the id of each item and assign to key field.

customers-list.component.html:

We pass each customer item data to customer-details component.

Create Component to save item

create-customer.component.ts

create-customer.component.html:

Define App Routing Module

app-routing.module.ts

app.component.html:

To understand how to use Angular Routing Module, please visit:
How to work with Angular Routing – Spring Boot + Angular 4

Run & Check Result

– Run Angular App with command: npm start.
– Open browser with url http://localhost:4200/.

angular-8-firestore-turorial-result-browser

Result in Firestore console:

angular-8-firestore-turorial-result-firestore-console

Source Code

Angular8Firestore

Conclusion

Now we’ve known how to connect Angular App with Firebase Firestore, how to make CRUD operations with Firestore for Object or List of Objects. We also created an Angular 8 Application that can save, receive, update, delete items with Firestore Database.

Happy learning!



By grokonez | April 30, 2019.


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*