Reading & Writing Data
Learn to perform basic create, read, update, and delete (CRUD) operations on your Realtime Database.
CRUD: Create, Read, Update, Delete
Welcome to working with Firebase Realtime Database! Today, we'll master the fundamental operations known as CRUD:
- Create: Adding new data
- Read: Retrieving existing data
- Update: Modifying existing data
- Delete: Removing data
These operations are the building blocks for almost any application that stores information.
Getting a Database Reference
Before we can perform any CRUD operations, we need to get a reference to our Realtime Database. This reference points to a specific location in your database's JSON tree.
First, ensure Firebase is initialized in your app. Then, you can get the database instance and a reference to a specific path.
import { initializeApp } from "firebase/app";
import { getDatabase, ref } from "firebase/database";
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
databaseURL: "https://YOUR_PROJECT_ID.firebaseio.com",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_PROJECT_ID.appspot.com",
messagingSenderId: "YOUR_SENDER_ID",
appId: "YOUR_APP_ID"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
// Get a reference to the database service
const db = getDatabase(app);
// Get a reference to the 'users' path
const usersRef = ref(db, 'users');
console.log("Database reference obtained.");All lessons in this course
- Realtime Database Fundamentals
- Reading & Writing Data
- Structuring Your Data
- Listening for Realtime Changes