Firebase / Realtime Database
Performing CRUD Operations with Realtime Database
In this tutorial, we'll delve into the core operations you can perform with Firebase Realtime Database: Create, Read, Update, and Delete (CRUD). We'll go through each operation st…
Section overview
5 resourcesCovers Firebase Realtime Database for managing and syncing data across devices.
1. Introduction
In this tutorial, we will explore how to perform CRUD operations - Create, Read, Update, and Delete, with Firebase Realtime Database. The Firebase Realtime Database is a cloud-hosted NoSQL database that lets you store and sync data between your users in realtime.
You will learn how to:
- Set up a Firebase project and integrate it with your application.
- Create, Read, Update, and Delete data from a Firebase Realtime Database.
Prerequisites:
- Basic knowledge of JavaScript.
- Node.js and NPM installed on your computer.
- A Google Account to access the Firebase console.
2. Step-by-Step Guide
Set up your Firebase project
- Go to the Firebase website and sign in with your Google account.
- Click on "Go to console" on the top right corner and create a new project.
- In the Firebase console, click on "Database" in the sidebar, then click on "Create database" under the Realtime Database section.
- Choose "Start in test mode" and click "Done".
Set up your application
- Initialize a new Node.js project by running
npm init -yin your terminal. - Install Firebase by running
npm install firebase. - Create a new file
index.jsand include the Firebase configuration object which you can find in your Firebase project settings.
const firebase = require('firebase');
const config = {
// Your Firebase configuration object
};
firebase.initializeApp(config);
const db = firebase.database();
3. Code Examples
Create (Write) Data
To write data to Firebase, we use the set() function:
const userRef = db.ref('users/user1');
userRef.set({
name: 'John Doe',
email: 'john.doe@example.com'
}).then(() => console.log('Data written successfully'));
In this example, we're creating a new user with the user ID of user1, and setting their name and email.
Read Data
To read data from the database, we use the once() function:
const userRef = db.ref('users/user1');
userRef.once('value', snapshot => {
console.log(snapshot.val());
});
Here, we're reading the user data we just created. once() triggers a one-time read from the database, and snapshot.val() returns the data.
Update Data
To update data, we use the update() function:
const userRef = db.ref('users/user1');
userRef.update({
email: 'new.email@example.com'
}).then(() => console.log('Data updated successfully'));
In this example, we're updating the email of user1.
Delete Data
To delete data, we use the remove() function:
const userRef = db.ref('users/user1');
userRef.remove()
.then(() => console.log('Data removed successfully'));
Here, we're deleting the user user1 from our database.
4. Summary
In this tutorial, we learned how to perform CRUD operations with Firebase Realtime Database. We learned how to write, read, update, and delete data from our database.
Next, you might want to learn how to listen for data changes in realtime, handle user authentication with Firebase, or structure your data for scalability.
5. Practice Exercises
- Exercise: Create a new user in the database with a unique user ID, name, and email.
- Solution: Similar to the 'Create Data' example above, but you should generate a unique ID for each user.
-
Tip: You can use
db.ref('users').push().keyto generate a unique ID. -
Exercise: Read the data of the user you just created and log it to the console.
- Solution: Similar to the 'Read Data' example above, but replace
'users/user1'with the path to your new user. -
Tip: Make sure your database rules allow read access to the data you're trying to read.
-
Exercise: Update the email of the user you just created.
-
Solution: Similar to the 'Update Data' example above, but replace
'users/user1'with the path to your new user. -
Exercise: Delete the user you just created.
- Solution: Similar to the 'Delete Data' example above, but replace
'users/user1'with the path to your new user.
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.
Latest 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