Firebase / Realtime Database
Comparing Firestore vs Realtime Database
Firebase offers two cloud-based, client-accessible database options to cater for your app needs: Firestore and Realtime Database. This tutorial will help you understand the differ…
Section overview
5 resourcesCovers Firebase Realtime Database for managing and syncing data across devices.
1. Introduction
Tutorial's Goals
The main goal of this tutorial is to compare two of Firebase's powerful database options: Firestore and Realtime Database. We will explore the key differences, advantages, disadvantages, and ideal use cases for each.
Learning Outcomes
By the end of this tutorial, you will understand:
- The core concepts of Firestore and Realtime Database
- When to use Firestore or Realtime Database
- How to interact with both databases using code examples
Prerequisites
A basic understanding of Firebase, JavaScript and databases will be helpful for this tutorial.
2. Step-by-Step Guide
Firestore
Firestore is a NoSQL document database that lets you easily store, sync, and query data for your mobile and web apps at a global scale. It organizes data as collections of documents, and can even have nested sub-collections.
Key Features:
- Document-oriented: Data is saved in documents, which are organized into collections.
- Expressive querying: You can perform complex queries over your data.
- Offline support: Firestore caches data that your app is actively using, so the app can write, read, listen to, and query data even when offline.
- Scalability: Firestore scales automatically to handle your app's load.
Realtime Database
Realtime Database is Firebase's original database. It's a NoSQL database that stores data as JSON and synchronizes it in realtime to every connected client.
Key Features:
- Realtime: Realtime Database updates in real time. You don't have to refresh your browser to get the latest updates.
- Offline support: Like Firestore, it allows mobile and web apps to remain responsive even when offline.
- Scalability: While it can scale, it requires sharding to scale beyond 100,000 concurrent connections.
3. Code Examples
Firestore
// Initialize Firestore
const db = firebase.firestore();
// Add a new document
db.collection("users").add({
first: "Alan",
last: "Turing",
born: 1912
})
.then((docRef) => {
console.log("Document written with ID: ", docRef.id);
})
.catch((error) => {
console.error("Error adding document: ", error);
});
Realtime Database
// Get a reference to the database service
const database = firebase.database();
// Write new post
function writeNewPost(uid, username, picture, title, body) {
const postData = {
author: username,
uid: uid,
body: body,
title: title,
starCount: 0,
authorPic: picture
};
const newPostKey = firebase.database().ref().child('posts').push().key;
const updates = {};
updates['/posts/' + newPostKey] = postData;
updates['/user-posts/' + uid + '/' + newPostKey] = postData;
return firebase.database().ref().update(updates);
}
4. Summary
In this tutorial, we compared Firestore and Realtime Database. Firestore is more scalable and provides more robust querying, but Realtime Database can be more straightforward for simple data needs.
Next steps for learning could include diving deeper into the Firebase platform and exploring other services it offers, like authentication and cloud functions.
5. Practice Exercises
- Exercise: Create a Firestore database and write a function to add a new document to a collection.
- Exercise: Create a Realtime Database and write a function to add a new child to a node.
- Exercise: For both databases, write a function to retrieve data and display it in your app.
Remember, practice is the key to mastering any skill. Continue experimenting with Firestore and Realtime Database to understand their potential. Happy coding!
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Random Password Generator
Create secure, complex passwords with custom length and character options.
Use toolLatest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article