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.
Section overview
5 resourcesCovers 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.
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