Cloud Functions — Firestore Triggers

Cloud Functions — Firestore Triggers

Cloud Functions is a feature provided by Firebase for running server codes at Google infrastructures in response to events triggered by Firebase events and HTTPS requests.

Cloud Functions is a feature provided by Firebase for running server codes at Google infrastructures in response to events triggered by Firebase events and HTTPS requests. Developer does not have to handle the maintenance and scalability issues. Firebase automatically scales up computing resources to match the usage patterns of your users. Each function runs in isolation in its own environment with its own configuration.Developer just has to write the business logic and let Google handle the server for us.

There are a lots of available Firebase events and one of them is Firestore event. Firestore notifies Cloud Functions once any document is created, updated and deleted within the specific collection. It is useful for adding extra information at document initialisation, validating information when document is updated and notifying admins / users once their information is deleted.

The implementation is not difficult but with some tricks. Lets see here!

1. onCreate

const functions = require("firebase-functions")
    /*
    Example of new data: {
     'fooKey': 'fooValue'
    }
    */
    exports.onCreateTrigger = functions.firestore
        .document('fooCollection/{docId}')
        .onCreate((queryDocumentSnapshot, eventContext) => {
            // ID of newly created document
            const newlyCreatedDocumentID = eventContext.params.docId

            // Retrieve value from data
            const createdData = queryDocumentSnapshot.data();
            const fooValueSyntax1 = createdDate.fooKey;
            const fooValueSyntax2 = createdDate["fooKey"];
            const fooValueSyntax3 = queryDocumentSnapshot.get("fooKey");

            return 0
        });
view raw
OnCreateTrigger.js hosted with ❤ by GitHub

onCreate is triggered once a new document in a specific collection ( fooCollection at line 8) is created. The callback contains a QueryDocumentSnapshot and an EventContext. Document id and document data can be retrieved from them for further checking (See line 11 to line 17).

google firebase-cloud-functions firestore firebase gcp function

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Multi-cloud Spending: 8 Tips To Lower Cost

Mismanagement of multi-cloud expense costs an arm and leg to business and its management has become a major pain point. Here we break down some crucial tips to take some of the management challenges off your plate and help you optimize your cloud spend.

Overview of Google Cloud Essentials Quest

If you looking to learn about Google Cloud in depth or in general with or without any prior knowledge in cloud computing, then you should definitely check this quest out.

Google Cloud Training | Google Cloud Platform Course | Google Cloud for Beginners

**Link: https://www.youtube.com/watch?v=gud65lqebrc** In this [**Google Cloud Training**](https://www.youtube.com/watch?v=gud65lqebrc "Google Cloud Training") live session, you will know everything about google cloud from basic to advance level...

Services of Google Cloud

What is Google Cloud? The Google cloud platform provides its users with stable and highly scalable cloud computing services. These services help customers compute and store information and help developers...

Google Cloud's Elena Felder on Spring Cloud GCP, Reactor, and More

Hi, Spring fans! In this episode, Josh Long (@starbuxman) talks to Google Cloud’s Elena Felder (@hellata) about building Reactor/Spring-based integrations for Google Cloud technologies like Google Cloud Spanner. Bootiful Podcast: Google Cloud's Elena Felder on Spring Cloud GCP, Reactor, and More