Working with Firebase Firestore and the Ignite UI for Angular Grid

Dhananjay Kumar / Thursday, September 13, 2018

In this article, w’ll learn to use Firebase Firestore and the Ignite UI for Angular Grid. The Firestore is Firebase NoSQL database offering so, before you start, I recommend that you to learn more about both Firestore and Ignite UI for Angular.

In this article, we’ll learn to read data from Firestore and bind that data to the Ignite UI for Angular Grid. In next article for this topic, w’ll move through Create, Delete, and Update operations.

So, let’s begin with setting up Firestore, creating Angular project to work with Ignite UI for Angular, and then fetching data from Firestore.

Setting up Firestore

 

You’re going to navigate to the Firebase console. When you’re there, click on the “Add project” option.

After clicking on “Add project”, you will get a dialog window to provide project information. Enter into that to create project as shown in the image below. Here, I have given the project name iggridemo.

Once project is created, you need to add an app to the project. We’ll add a web project, as we’re going to use Firestore data collection inside Angular, which is a web project.

As you click on the web option, you’ll get snippet to add to your project. Copy this snippet, since you may need it later to add into an Angular project.  

Note:  We will add the below setting in Angular project environment.prod.ts file.

Next, click on “Database” in the side menu options and create a collection. Keep in mind that Firestore is NoSQL based database. Here we create collections to work with data. To create a database, go to the side menu and click on database, then creating the database. You’ll be asked to select your security rules. Select option “Start in test mode” and click on “Enable”.

After setting up security rules, click on “Add collection” to add a collection to the database.

Give a name to the collection. For this example, I gave the name here “products”.

After creating the collection, add a document. I’m adding a document in the products collection, as shown in the image below:

After adding first document, the database should look like this: 

Now, let’s add a few more documents to the collection. For that action, click on “Add document” in the products collection. 

 

So, I’ve added five documents to the products collection. We’re going to work with the products collection now in the Ignite UI for Angular Grid. So far, we’ve only created the collection in Firebase Firestore.

Setting up an Angular project with Ignite UI

We have three options for setting up anAngular project with Ignite UI for Angular.

  1. Use Ignite UI CLI to create a new Angular project configured with Ignite UI for Angular.
  2. Use Ignite UI for Angular in an existing Angular project.
  3. Use Ignite UI for Angular Toolbox Extension for Visual Studio Code.

 From this point forward, I’ll assume that you already have an Angular project configured to work with Ignite UI for Angular.  

Setting up AngularFire

We’re going to use the AngularFire library to work with Firebase in an Angular projectTo start, install AngularFire in the Angular project using npm.

 npm install firebase @angular/fire --save 

After installing Angularfire, we need to setup a Firebase environment. To do that, open environment.prod.ts and modify it, as shown below:

export const environment = {
    production: false,
    firebase: {
        apiKey: "yourkey",
        authDomain: "iggriddemo.firebaseapp.com",
        databaseURL: "https://iggriddemo.firebaseio.com",
        projectId: "iggriddemo",
        storageBucket: "iggriddemo.appspot.com",
        messagingSenderId: "1055912852453"
    }
};

If you recall from setting up Firestore, we added a web project. We need to copy that setting from there to environment.prod.ts file. If you’re working in a development environment, you may want to add the above entry in the environment.ts file. 

Next, in AppModule, import Firestore and AngularFire modules. Import in app.module.ts, as below:

import { AngularFirestoreModule } from '@angular/fire/firestore';
import { AngularFireModule } from '@angular/fire';
import { environment } from '../environments/environment';

Modify AppModule:

@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        IgxGridModule.forRoot(),
        AngularFireModule.initializeApp(environment.firebase),
        AngularFirestoreModule
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule {
}

Now, we’ve added AngularFireModule and AngularFireStoreModule in imports array.

Read Data from Firestore Collections

We’re going to read data from Firestore collections in AppComponent. First, import AngularFirestore and Observable in AppComponent.

import { AngularFirestore } from '@angular/fire/firestore';
import { Observable } from 'rxjs';

Next, let’s fetch data in the constructor of the component:

items: Observable<any[]>;
constructor(private db: AngularFirestore) {
    
    this.items = db.collection('/products').valueChanges();
 
}

If you remember, we have created a products collection in Firestore. Putting everything together, AppComponent will look like below:

import { Component } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
import { Observable } from 'rxjs';
 
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    title = 'Ignite UI for Angular Grid + Firestore';
    items: Observable<any[]>;
    constructor(private db: AngularFirestore) {
 
        this.items = db.collection('/products').valueChanges();
 
    }
}

On the template, let’s add Ignite UI for Angular Grid:

<h1>
    Welcome to {{ title }}!
</h1>
<igx-grid [data]="items | async" [autoGenerate]="false" height="400px">
    <igx-column field="Id" [sortable]="true" header="Id" [filterable]="true"></igx-column>
    <igx-column field="Title" [sortable]="true" header="Title" [filterable]="true"></igx-column>
    <igx-column field="Price" [sortable]="true" header="Price" [filterable]="true"></igx-column>
    <igx-column field="Quantity" [sortable]="true" header="Quantity" [filterable]="true"></igx-column>
    <igx-column field="inStock" [sortable]="true" header="Stock" [filterable]="true"></igx-column>
</igx-grid>

So, let’s talk through the code, since we:

  • Added igxGrid
  • Set data through property binding to items. Since it is an observable, we are using async pipe.
  • Set autoGenerate property to false because we are going to add columns manually.
  • Configured columns and bound it to the Firestore collection products columns.

On running the application you will find the Ignite UI for Angular Grid is displaying data from Firestore. For products, collection igxGrid should be rendered, as shown in the image below:

In next post, we will see how we can perform Create, Update, and Delete operations. I hope you find this post useful, and now you should able to work with Firebase Firestore and Ignite UI for Angular Grid with ease.

If you like this post, please share it. Also, if you have not checked out Infragistics Ignite UI for Angular Components, be sure to do so! They have 50+ Material-based Angular components to help you code web apps faster.