0
0
Firebasecloud~30 mins

Data aggregation patterns in Firebase - Mini Project: Build & Apply

Choose your learning style9 modes available
Data Aggregation Patterns with Firebase
📖 Scenario: You are building a simple Firebase database to track sales data for a small store. Each sale records the product name and the quantity sold. You want to organize this data so you can easily find the total quantity sold for each product.
🎯 Goal: Create a Firebase Realtime Database structure to store sales data, add a configuration variable to filter products, write a query to aggregate total quantities sold per product, and finalize the database rules to allow read access.
📋 What You'll Learn
Create a Firebase Realtime Database node called sales with exact entries for three sales.
Add a configuration variable called filterProduct to select a product name.
Write a Firebase query to sum the quantities sold for the selected product.
Add database rules to allow read access to the sales node.
💡 Why This Matters
🌍 Real World
Stores and small businesses often track sales data in Firebase to analyze product performance and inventory needs.
💼 Career
Understanding data aggregation and Firebase rules is essential for cloud developers managing real-time databases and ensuring secure data access.
Progress0 / 4 steps
1
Create initial sales data in Firebase
Create a Firebase Realtime Database node called sales with these exact entries: sale1 with product: 'apple' and quantity: 10, sale2 with product: 'banana' and quantity: 5, and sale3 with product: 'apple' and quantity: 7.
Firebase
Need a hint?

Use a JavaScript object named sales with keys sale1, sale2, and sale3. Each key holds an object with product and quantity.

2
Add a filter configuration variable
Add a variable called filterProduct and set it to the string 'apple' to select the product to aggregate.
Firebase
Need a hint?

Declare a constant named filterProduct and assign it the string 'apple'.

3
Aggregate total quantity for the filtered product
Write code to calculate the total quantity sold for the product stored in filterProduct. Use a for loop with variables saleKey and sale to iterate over Object.entries(sales). Sum the quantity of sales where sale.product === filterProduct into a variable called totalQuantity.
Firebase
Need a hint?

Initialize totalQuantity to 0. Use a for loop with saleKey and sale from Object.entries(sales). Inside the loop, add sale.quantity to totalQuantity only if sale.product matches filterProduct.

4
Add Firebase database rules for read access
Add Firebase Realtime Database rules to allow read access to the sales node for all users. The rules should have a rules object with a sales child that has .read set to true.
Firebase
Need a hint?

Define a constant databaseRules with a rules object. Inside rules, add a sales object with '.read' set to true.