Everything you need to know about Google Cloud's Firestore API
Everything you need to know about Google Cloud's Firestore API

The Complete Guide for Developers, Product Owners, and Serverless Enthusiasts

Everything you need to know about Google Cloud's Firestore API

Google Firestore is one of the most popular cloud NoSQL databases in the world, provided by Firebase and Google Cloud. Firestore is suitable for building real-time applications, is scalable, does not require server management, and is very fast. This database is available with REST API, gRPC, and various SDKs and is very popular with mobile, web, and backend developers. In this article, we will introduce Firestore in a complete and comprehensive way, with practical examples, projects that can be built with it, successful application examples, architecture, security, and best practices.
0 Shares
0
0
0
0

What is Firestore?

Firestore is a Document/Collection-based NoSQL cloud database that has the following features:

  • Structured and flexible storage
  • Real-time Synchronization capability
  • High-level security with Firebase Security Rules
  • Automatic scalability
  • Very low latency globally
  • Ability to execute complex queries
  • Suitable for small to enterprise projects

Firestore in two modes Native Mode (Firebase specific) and Datastore Mode (Provided for GCP Enterprise services).


Data Structure in Firestore

Firestore uses a simple but powerful architecture:

  • Collection → A collection of Documents

  • Document → A JSON-like structure

  • Subcollection → Collection inside each Document

  • Field → Fields of each Document

Sample structure:

users (collection)
   └── userId123 (document)
    ├── name: "Ali"
    ├── age: 28
    └── posts (subcollection)
      └── postId981 (document)
        ├── title: "My First Post"
        └── likes: 52

Sample code examples using Firestore API

1. Use with JavaScript (Web App / Node.js)

Installation

npm install firebase

Connecting to Firestore

import { initializeApp } from "firebase/app";
import { getFirestore, doc, setDoc, getDoc } from "firebase/firestore";
const firebaseConfig = {
apiKey: "...",
authDomain: "...",
projectId: "...",
};
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);

Add Document

await setDoc(doc(db, "users", "user1"), {
name: "Ali",
age: 28,
job: "Developer"
});

Reading Document

const docRef = doc(db, "users", "user1");
const snap = await getDoc(docRef);
if (snap.exists()) {
console.log("Data:", snap.data());
}

2. Firestore REST API

Send a POST request to create a Document

curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-d '{
"fields": {
"title": {"stringValue": "New Article"},
"views": {"integerValue": "102"}
}
}' \
"https://firestore.googleapis.com/v1/projects/YOUR_PROJECT/databases/(default)/documents/posts"


Projects that can be built with Firestore

1. Chat and messaging apps (Real-time Chat App)

Due to its instant synchronization capability, Firestore is one of the best options for building chat.

2. Content Management Systems (Blog, CMS)

Especially for small teams or startups that don't want to deal with servers.

3. Mobile Apps (iOS / Android / Flutter)

Due to:

  • Low Latency

  • Simultaneous Sync

  • Internal Offline Mode

4. Real-Time Management Dashboards (Admin Panels)

Such as online store dashboard, user statistics, order management.

5. Small and medium-sized online store

Add products, categories, shopping carts, orders, etc.

6. IoT applications

Fast storage of sensor data.


Examples of successful projects using Firestore

  • Alibaba Cloud Mobile – Using Firestore for Real-Time Data Management

  • The New York Times Crossword – Use for online multiplayer

  • Todoist – Synchronize user tasks across multiple devices

  • Shazam – Managing some cloud data

  • Hotstar – Save user data and profile settings

(Many services use Firestore behind the scenes without officially announcing it)


Firestore Benefits

Automatic scalability

No need to manage the server.

High reading speed

Given the Global Edge architecture.

Security with Firestore Security Rules

Example Rule that only the account owner has read/write permission:

service cloud.firestore {
match /databases/{database}/documents {
match /users/{uid} {
allow read, write: if request.auth.uid == uid;
}
}
}
Offline Mode Support

On mobile and web.

Powerful Queries

Filtering, sorting, range query, indexing.


Challenges and limitations (which are best to know before using)

❌ No Join (it's NoSQL)
❌ Complex queries require indexes.
❌ Cost is calculated based on the number of Read/Write/Storage
❌ Not suitable for very large datasets (TB and above)
❌ Transactions are limited.


Firestore vs Realtime Database

FeatureFirestoreRealtime DB
Data structureDocument-basedJSON Tree
QueryVery strongVery limited
ScalabilityTopMedium
PriceOrdinaryCheaper
UsesModern appsSimple Chat, IoT

Firestore is a newer and more optimized version of Realtime DB.


Firestore best practices for professional production

1. Choose the right data structure

Preferably, do not create large documents.

2. Use Subcollection to segment data

Example: posts → comments → likes

3. Cache heavy queries

To reduce the cost of reading.

4. Use Cloud Functions for backend processing

Such as sending notifications, creating indexes, and summarizing data.

5. Logging in Firestore Logging

To avoid unwanted costs.


Example of a complete blog system with Firestore

Proposed structure:

posts (collection)
  └── postId
    ├── title
    ├── content
    ├── authorId
    ├── createdAt
    └── comments (subcollection)
      └── commentId
        ├── userId
        └── message

Example of adding a post:

import { collection, addDoc, serverTimestamp } from "firebase/firestore";
await addDoc(collection(db, "posts"), {
title: "First Blog Post",
content: "This is the content...",
authorId: "u123",
createdAt: serverTimestamp()
});

Conclusion

Firestore is a powerful, real-time cloud database suitable for building fast, scalable, and serverless services.
This service is suitable for:

✅ Website
✅ Mobile app
✅ Content management system
✅ Chat apps
✅ Realtime dashboards
✅ IoT projects
✅ Online stores

If you are building a product that requires Instant sync, high speed, and manageable cost Yes, Firestore is one of the best options.

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like