serverless / serverless-google-cloudfunctions

Serverless Google Cloud Functions Plugin – Adds Google Cloud Functions support to the Serverless Framework
https://www.serverless.com
MIT License
271 stars 126 forks source link

Is Firestore Event Triggers supported? #146

Closed dsjacobsen closed 5 years ago

dsjacobsen commented 5 years ago

Can Firestore Event Triggers be used with the Serverless framework?

I want to use Cloud Firestore triggers, but unsure if it’s support, and if it is, then how I correctly specify the event in the serverless.yml file?

An example of a function that should trigger event when any changes to a document happens. From here: https://firebase.google.com/docs/functions/firestore-events

Note: I am importing functions from firebase in a separate file and then importing it in my index.js.

exports.firestoreEvents = functions.firestore
   .document(‘users/marie’).onWrite((change, context) => {
     // ... Your code here
   });

If supported, how do I configure it in serverless.yml?

 firestoreEvents:
   handler: firestoreEvents
   events:
     - event:
       ????
pmuens commented 5 years ago

Hey @dsjacobsen thanks for opening :+1:

The event event can be any event which is in turn handled by a Background Function.

According to the event docs those could be firebase events as well.

I haven't tried it but the definition should look something like this:

firestoreEvents:
  handler: firestoreEvents
  events:
    - event:
        resource: google.firebase.foo
        eventType: providers/google.firebase.foo/eventTypes/bar.baz
marianofino commented 5 years ago

Hello @dsjacobsen @pmuens For triggering a function upon a Firestore document change you should write something like this in your serverless.yml file:

myFunction:
  handler: myFunction
  events:
    - event:
        eventType: providers/cloud.firestore/eventTypes/document.update
        resource: projects/<project-id>/databases/(default)/documents/<path-to-document>

I have written an article on how to use Serverless Framework with Firebase triggers: https://medium.com/ponce-agtech/using-firebase-triggers-in-serverless-framework-ad99594b86fa

Hope it helps ;)

dsjacobsen commented 5 years ago

Great article @marianofino thanks for sharing it here! :)