How to Connect Firebase Realtime NoSQL Cloud Database with Angular from Scratch

Article By Digamber Rawat on

Connect Angular 6 and Firebase Realtime Database

In this tutorial, I am going to talk about How to connect Firebase Realtime NoSQL cloud database with Angular from Scratch? + Basic CRUD operation using Firebase API.

We’ll be using AngularFire official library for setting up Firebase database services in the Angular project.

Firebase is a Google product, It is a real-time NoSQL cloud database that allows you to create faster and quicker web and mobile applications. It supports iOS, OS X, Android and web platforms. It doesn’t require server-side backend programming language.

Advantages of using AngularFire Library

  • Supports Push Notifications.
  • Offline Data Storage.
  • Allows ngrx API Implementation.
  • Based on the RxJS Observable Pattern.
  • Real-time Data Sync Across All the devices.
  • Support Download, Upload & Delete Images, Video & Blob Files.
  • Support user authentication (Google, Facebook, Twitter, Github, Email & password)

1. Technologies used

  • Node 8.11.1
  • Angular 6.0.0
  • Angular CLI 6.0.0
  • Firebase 5.5.1
  • Typescript 2.7.2

2. GitHub project files

3. Install and setup Angular project using Angular CLI.

Setup Angular CLI in your system. If already installed then ignore.

npm install @angular/cli

Run the cmd in Angular CLI to setup the Angular project.

ng new <angular-firebase>

cd <angular-firebase>

4. How to Setup Google Firebase Account?

Go to Firebase website login using your email id. When you see given below screen click on Add Project section.

Setup Google Firebase

Afterward, Enter your project name, accept the terms and condition and click on create a project button.

Firebase setup

Once your project is set up, then click on your project then you’ll see your admin dashboard navigate to Develop > Authentication > Web setup click on Web setup button, and a popup will appear along with your firebase credentials.

Copy your firebase credentials from here.

Firebase Details

5. How to setup Firebase (AngularFire library) in your project?

Once you are done with setting up the angular project and firebase account.  It’s time to install AngularFire and Firebase from NPM.

Run the given below cmd in Angular CLI.

npm install firebase @angular/fire --save

Let’s create the connection between firebase and your angular project.

Go to src/environments/enviorment.ts and files in your project folder. Then add your firebase configuration details in both the environment files as given below.

Firebase Credentials Setting

Now import AngularFireModule and environment in app.module.ts file, then declare AngularFireModule into the imports array.

You can optionally use your FirebaseApp name with the itializeApp method.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
// Firebase
import { AngularFireModule } from '@angular/fire';
import { environment } from '../environments/environment';
imports: [
AngularFireModule.initializeApp(environment.firebaseConfig, 'my-app-name')
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
export class AppModule {}

Now you are all set to use Firebase Realtime NoSQL cloud database with your Angular App.

In next step, I’ll discuss how to import various Firebase modules in app.module.ts

Import Firebase modules efficiently

There are various Firebase modules available for performing certain database tasks. But it is always advisable to use only required Firebase modules in your project.

If you are building authentication service in your app. Then only import AngularFireAuthModule in app.module.ts file. By this way, you’ll be able to keep your overall app size to low.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
// Firebase modules
import { AngularFireModule } from '@angular/fire';
import { AngularFirestoreModule } from '@angular/fire/firestore';
import { AngularFireStorageModule } from '@angular/fire/storage';
import { AngularFireAuthModule } from '@angular/fire/auth';
import { environment } from '../environments/environment';
imports: [
AngularFireModule.initializeApp(environment.firebaseConfig, 'my-app-name'), // Required for everything
AngularFirestoreModule, // Only required for database features
AngularFireAuthModule, // Only required for auth features,
AngularFireStorageModule // Only required for storage features
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
export class AppModule {}

6. How to create CRUD services using Firebase API?

In this last step, I’m going to explain how you can create CRUD operations in your project.

Create a separate shared folder, we are going to keep our CRUD service and Interface class file in it.
src > app > shared.

Go to terminal and run the given below command in Angular CLI to generate crud.service.ts and user.ts file.

ng generate interface shared/user   // Generate interface class file
ng generate service shared/crud    // Generate crud.service.ts file

Then go to src > app > shared > user.ts file and declare the data type in the user.ts file as given below.

export interface User {
$key: string;
name: string;
email: string;
contact: Number;

After that go to src > app > shared > crud.service.ts file and write the CRUD logic in your crud.service.ts file as given below.

import { Injectable } from '@angular/core';
import { AngularFireDatabase, AngularFireList, AngularFireObject } from '@angular/fire/database';
import { User } from '../shared/user';
providedIn: 'root'
export class CrudService {
usersRef: AngularFireList<any>;      // Reference to users list, Its an Observable
userRef: AngularFireObject<any>;     // Reference to user object, Its an Observable too
constructor(private db: AngularFireDatabase) { }   // Inject AngularFireDatabase dependency in constructor
// Create User
AddUser(user: User) {
// Read User
GetUser(id: string) {
this.userRef = this.db.object('users-list/' + id);
return this.userRef;
// Read Users List
GetUsersList() {
this.usersRef = this.db.list('users-list');
return this.usersRef;
// Update User
UpdateUser(user: User) {
// Delete User
DeleteUser(id: string) { 
this.userRef = this.db.object('users-list/'+id);

Now you’ve successfully created CRUD services in your project. I’m going to explain how you can consume CRUD services in my next detailed article.


In this tutorial, I’ve talked about Google Firebase database and its powerful features. You will learn to create an account in Google firebase. How to setup firebase services in your Angular project and How to create basic CRUD services using Firebase API. I hope you’d love this tutorial If you’ll find this article useful then let it reach to others.

Digamber Rawat

Feel free to contact me, If you are looking for a freelance full stack Developer, with the following skills: Mongo DB, Express JS, Angualr 2+, Node JS, Loopback JS, Firebase, Ionic Framework and WordPress. I also offer remote contracting service to the clients across the globe. Hire Me