Firebase / Cloud Firestore

Building a CRUD App with Firestore

This tutorial guides you through the process of building a web application that performs CRUD operations using Firestore.

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Covers real-time database and cloud storage with Cloud Firestore for scalable and secure apps.

1. Introduction

In this tutorial, we will learn how to build a CRUD (Create, Read, Update, Delete) web application using Firestore, a flexible, scalable database for mobile, web, and server development from Firebase and Google Cloud Platform.

Goals

By the end of this tutorial, you will:

  • Understand how to connect to Firestore
  • Be able to perform CRUD operations in Firestore
  • Have a working web application connected to a Firestore database

Prerequisites

You should have basic knowledge of HTML, CSS, and JavaScript. Familiarity with Firebase is helpful, but not necessary.

2. Step-by-Step Guide

Setting up Firestore

First, create a Firebase account and a new project in Firebase. Navigate to the Firestore Database section and create a new Firestore database.

Connecting to Firestore

In your HTML file, include the Firebase JavaScript library by adding these scripts in your HTML file:

<script src="https://www.gstatic.com/firebasejs/7.24.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.24.0/firebase-firestore.js"></script>

Then, initialize Firebase in your JavaScript file:

const firebaseConfig = {
  apiKey: "your-api-key",
  authDomain: "your-auth-domain",
  projectId: "your-project-id"
};

firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();

CRUD Operations

Create

To add a document to Firestore, use the .add() method with your data as an object:

db.collection('users').add({
  firstName: 'John',
  lastName: 'Doe',
  age: 30
});

Read

To get all documents in a collection, use the .get() method:

db.collection('users').get().then((snapshot) => {
  snapshot.docs.forEach(doc => {
    console.log(doc.data());
  });
});

Update

To update a document, use the .update() method:

db.collection('users').doc('document-id').update({
  age: 31
});

Delete

To delete a document, use the .delete() method:

db.collection('users').doc('document-id').delete();

3. Code Examples

Full Example

// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();

// Add a new document
db.collection('users').add({
  firstName: 'John',
  lastName: 'Doe',
  age: 30
});

// Get all documents
db.collection('users').get().then((snapshot) => {
  snapshot.docs.forEach(doc => {
    console.log(doc.data());
  });
});

// Update a document
db.collection('users').doc('document-id').update({
  age: 31
});

// Delete a document
db.collection('users').doc('document-id').delete();

This example shows how to connect to Firestore and perform CRUD operations. Make sure to replace 'document-id' with the actual ID of the document you want to update or delete.

4. Summary

In this tutorial, we've learned how to connect to Firestore and perform CRUD operations. You have now a grasp on how to create, read, update, and delete documents in Firestore.

5. Practice Exercises

Exercise 1: Create a new collection and add 5 documents to it, each with three different fields.

Exercise 2: Read all documents from the collection you created and log them to the console.

Exercise 3: Update one of the documents you created in Exercise 1.

Exercise 4: Delete one of the documents you created in Exercise 1.

Additional Resources

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Backlink Checker

Analyze and validate backlinks.

Use tool

Favicon Generator

Create favicons from images.

Use tool

Markdown to HTML Converter

Convert Markdown to clean HTML.

Use tool

Color Palette Generator

Generate color palettes from images.

Use tool

Time Zone Converter

Convert time between different time zones.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI 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 article

AI 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 article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help