FIRESTORE DATABASE

Cloud Firestore is a flexible and scalable database for server, mobile, and web development from Firebase and Google Cloud.

Firestore Database


Firestore Database is a cloud-hosted database service offered by Google. It is a NoSQL database, which means it does not follow the traditional schema of tables and rows. Instead, Firestore uses a data structure based on JSON, which allows storing data in the form of objects with key-value pairs.



JSON Objects Explained!



Advantages of Firestore Database:


Easy integration: Firestore Database natively integrates with other Firebase tools and services, such as authentication, cloud storage, and analytics. This makes it easy to build comprehensive applications with a single platform.


Scalability: Firestore Database is managed by Google, so you don't have to worry about the scalability of the database. Google handles the scaling and infrastructure issues.


Serverless: No need to configure or manage servers to use Firestore Database. This saves time and resources, as Google takes care of the underlying infrastructure.


Secure access: Firestore offers flexible security rules that allow you to define who has access to your data and what operations they can perform.


Disadvantages of Firestore Database:


Not suitable for all applications: While Firestore Database is suitable for many applications, it may not be the best choice for all of them. Highly complex applications or those with high scalability requirements may need a more customizable database solution.


Associated costs: Although Firestore offers a free plan, costs may increase as your application grows and reaches larger volumes of data and concurrent users.


Less flexibility: Firestore Database is NoSQL database. If you need to perform complex queries or advanced transactions, you may prefer a traditional SQL database.


When to use Firestore Database:


Firestore Database is an excellent choice for certain types of applications and use cases, such as:


Prototypes and quick projects: Firestore Database is ideal for quickly developing prototypes and small applications without worrying about server setup and management.


Mobile and web applications: Firestore Database integrates well with mobile and web applications, making it easy to develop cross-platform applications.


Small or medium-sized projects: For projects that do not require extreme complexity and can benefit from the ease of use and quick setup of Firestore. .

The Firestore database allows easy and fast integration with different languages, as you can see below, in a few lines you can do the basic operations of a database (CRUD).


  import { db } from "../configurations/fireBase.js";
  import { userSchema } from "../models/userModel.js";
  
  export default {
  
    findOne: async (req, res) => { // FILTER DATA
      try {
        let id = req.params.id;
        const userRef = await db.collection("user").doc(id)
        const data = await userRef.get()
        res.status(200).send({ data: data.data() || [], message: 'Ok' })
      } catch (error) {
        return res.status(400).send({ message: error })
      }
    },
  
    show: async (req, res) => { // READ ALL DATA
      try {
        let dataResponse = [];
        const userRef = await db.collection("user")
        const response = await userRef.get()
        response.forEach(doc => dataResponse.push({ ...doc.data(), id: doc.id }))
        res.status(200).send({ data: dataResponse, message: 'Ok' })
      } catch (error) {
        return res.status(400).send({ message: error })
      }
    },
  
    create: async (req, res) => { // CREATE DATA
      try {
        const data = await userSchema.validateAsync(req.body)
        await db.collection("user").add(data)
        res.status(200).send({ data, message: 'User Created' })
      } catch (error) {
        res.status(400).send({ message: error })
      }
    },
  
    remove: async (req, res) => { // DELETE DATA
      try {
        let id = req.params.id
        await db.collection("user").doc(id).delete()
        res.status(200).send({ message: 'User deleted' })
      } catch (error) {
        res.status(400).send(error)
      }
    },
  
    update: async (req, res) => { // UPDATE DATA
      try {
        let id = req.params.id
        const data = await userSchema.validateAsync(req.body)
        await db.collection("user").doc(id).update(data)
        res.status(200).send({ data, message: 'User updated' })
      } catch (error) {
        res.status(400).send(error)
      }
    }
  }


GitHub code https://github.com/vicho-99/api-express-firebase