Welcome back to “Learn Firebase: The Data Engineer Way” !! Hope you were looking forward to this. Happy Learning…
In previous blog, we have learned to create base template for our app using Firebase authentication. Please note that you can use blog-1 source code as base template for any Angular4 project which requires common functionalities like sign up, sign in & logout.
In this blog, we will learn to implement and manage features of inventory app which includes add/update/delete of an inventory item.
MOCKUP

DATA MODELING
Lets create data model as per mock up. Lets list down fields of single item.- Name : String
- Serial Number : String
- Category: String
- Cost: Numeric
- Currency: String
- Available: Items
- Purchase Date: Date
- Comments: Text
Basic rule of Firebase, which is also mentioned in Firebase documentation, is to keep data structure flat i.e. no or minimal nesting. I haven’t tried deep nesting in Firebase but as per my experience, I can say it will make data retrieval slower.
Note: Ignore ‘Key’ field for now, we will cover that shortly.
Again, Firebase stores this data as JSON object. Firebase database is nothing but a large nested json object. The rows shown in mock up are stored in Firebase database as shown below –

Inventory-app-726af is a root node. I am storing all inventory item objects under node named ‘items’. So we have to insert any object in Firebase under either root node in our case ‘Inventory-app-726af’ or under any parent node like ‘items’.
Whenever we add/insert or in Firebase’s terms push object into Firebase database, Firebase assign an unique key to object (you can think of it as ObjectID in MongoDB or Auto Increment PK in Mysql).
TYPESCRIPT OBJECT
The corresponding typescript object would be:
export class Item { public name: string; public cost: number; public totalItems: number; public availableItems: number; public purchaseDate: Date; public $key: string; public serialNum: string; public category: string; public comments: string; public currency: string; constructor(serialNum: string,name: string, cost: number, totalItems: number , purchaseDate: Date, category: string, currency: string, comments: string) { this.name = name; this.cost = cost; this.totalItems = totalItems; this.serialNum = serialNum; this.purchaseDate = purchaseDate; this.category = category; this.currency = currency; this.comments = comments; }}
INVENTORY SERVICE
import { Injectable } from '@angular/core'; import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database'; import { AngularFireAuth } from 'angularfire2/auth'; import { Item } from './inventory.model'; @Injectable() export class InventoryService{ item: Item ; items: FirebaseListObservable<any[]>; constructor(private db: AngularFireDatabase) { this.items = this.db.list('/items'); } getAllItems() { return this.items.map( (data) => data.map(x => x as Item) ); } deleteItemByKey($key: string) { this.items.remove($key); } addItem(item: Item) { this.items.push(item); } editItem(key: string,item: Item) { this.db.object('items/'+key).update(item); } }
AngularFireDatabase (variable db) provides two methodes to refer firebase database object.
db.list – It means we are refering to an array list i.e. node which is referred in list method is going to return array list.
e.g. db.list(‘/items’) – have list of objects db.object – It means we are referring to single object i.e. node which is referred in object method is going to return an object.
e.g. db.object(‘/items/-Kmb-ZXfao7VdfenhfYj’) – refers to Notepads-4 object
Using above two methods we can then accomplish any CRUD operation as shown below.
Create Item
Think of an array and you want to add an element. So you will use push method
db.list(‘/items’).push(itemObject);
Read Items
db.list(‘/items’)
This will return items. I generally use map method to cast raw object into Item object.
Update Item
db.object(‘/items/-Kmb-ZXfao7VdfenhfYj’).update(newItem)
To update specific item, you first have to locate that item and then update with new value.
Delete Item
Similar to push, if we want to delete an item for array list, we can use remove method.HTML CODE
Kindly refer manage.component.html file. This is just a boiler plate code to add/edit forms and pass those values to services. I have install two npm modulesAbove modules makes life easier while handling form requests.
npm install ng2-bootstrap-modal npm install ng2-bootstrap
TYPESCRIPT CODE
export class ManageComponent implements OnInit { itemList: Item[] = []; selectedItem: Item; @ViewChild('addItemModal') public addItemModal: ModalDirective ; @ViewChild('editItemModal') public editItemModal: ModalDirective ; constructor(private inventoryService: InventoryService) { inventoryService.getAllItems().subscribe( data => this.itemList = data ); } .. .. }
Major things to notice in above typescript code are:
@ViewChild
To get access to a component and its methods, we can use the @ViewChild decorator. We are accessing both input forms ‘addItemModal’ and ‘editItemModal’ through @ViewChild feature.
Observable
Observables open up a continuous channel of communication in which multiple values of data can be emitted over time.
If you check getAllItems method code in inventory.service.ts, it returns observable. So to get data from obseravable, we have to subscribe it.
This is where Firebase becomes real-time. Whenever any user add or update or delete inventory item, getAllItems returns updated list of items through websocket.
Hope this blog helps you learn to implement and manage the features of inventory app.If you have any queries or need further clarification on any section of the blog , please write us in comment section.
In next blog, we will implement Request and Approve functionality where we will learn how to perform Firebase joins. Keep checking this space for new Blog.