Implementing CRUD Operations in Firebase Firestore

Implementing CRUD Operations in Firebase Firestore



Implementing CRUD Operations in Firebase Firestore

Implementing CRUD Operations in Firebase Firestore

Introduction

Firebase Firestore is a NoSQL document database provided by Google Cloud Platform. It's a real-time database that offers excellent scalability and ease of use, making it a popular choice for web and mobile applications. In this blog series, we'll explore how to perform CRUD (Create, Read, Update, Delete) operations on a Firestore database.

Setting Up

Before we begin, make sure you have the following set up:

  • A Firebase project
  • A Firestore database
  • The Firebase SDK for your programming language (JavaScript, Node.js, etc.)

1. Create a Firebase Project

If you haven't already, create a new Firebase project in the Firebase console.

2. Enable Firestore

Navigate to your project's settings and enable Firestore. You'll need to choose a location for your database.

3. Install the Firebase SDK

Install the Firebase SDK for your chosen platform using your package manager. For example, in Node.js, you would use:

npm install firebase

CRUD Operations

1. Create

Creating a new document in Firestore is simple. Let's say we have a collection called "users". To create a new user document, we would use the following code:

import firebase from 'firebase/app'; import 'firebase/firestore'; const db = firebase.firestore(); const user = { name: 'John Doe', email: 'john.doe@example.com' }; db.collection('users').add(user) .then(docRef => { console.log('Document written with ID:', docRef.id); }) .catch(error => { console.error('Error adding document:', error); });

This code will create a new document in the "users" collection with the provided data and will generate a unique ID for the document.

2. Read

To read data from a document, we can use the get() method.

const docRef = db.collection('users').doc('user-id'); docRef.get() .then(doc => { if (doc.exists) { console.log('Document data:', doc.data()); } else { console.log('No such document!'); } }) .catch(error => { console.error('Error getting document:', error); });

Replace 'user-id' with the actual ID of the document you want to read. This code will fetch the document and display its data.

3. Update

Updating a document is similar to creating. We use the update() method.

const docRef = db.collection('users').doc('user-id'); docRef.update({ email: 'john.doe.updated@example.com' }) .then(() => { console.log('Document updated successfully!'); }) .catch(error => { console.error('Error updating document:', error); });

This code will update the "email" field of the document with the specified value.

4. Delete

Deleting a document is straightforward. We use the delete() method.

const docRef = db.collection('users').doc('user-id'); docRef.delete() .then(() => { console.log('Document deleted successfully!'); }) .catch(error => { console.error('Error deleting document:', error); });

This code will delete the specified document.