Performing CRUD Operations with Realtime Database

Tutorial 2 of 5

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

  1. Go to the Firebase website and sign in with your Google account.
  2. Click on "Go to console" on the top right corner and create a new project.
  3. In the Firebase console, click on "Database" in the sidebar, then click on "Create database" under the Realtime Database section.
  4. Choose "Start in test mode" and click "Done".

Set up your application

  1. Initialize a new Node.js project by running npm init -y in your terminal.
  2. Install Firebase by running npm install firebase.
  3. Create a new file index.js and 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

  1. Exercise: Create a new user in the database with a unique user ID, name, and email.
  2. Solution: Similar to the 'Create Data' example above, but you should generate a unique ID for each user.
  3. Tip: You can use db.ref('users').push().key to generate a unique ID.

  4. Exercise: Read the data of the user you just created and log it to the console.

  5. Solution: Similar to the 'Read Data' example above, but replace 'users/user1' with the path to your new user.
  6. Tip: Make sure your database rules allow read access to the data you're trying to read.

  7. Exercise: Update the email of the user you just created.

  8. Solution: Similar to the 'Update Data' example above, but replace 'users/user1' with the path to your new user.

  9. Exercise: Delete the user you just created.

  10. Solution: Similar to the 'Delete Data' example above, but replace 'users/user1' with the path to your new user.