How to mock a firestore DeltaDocumentSnapshot in firebase functions - firebase

According to this documentation, you can create a mock DeltaSnapshot for firebase realtime database. But I am trying to test a firebase function using firestore, and here the event has this type:
event: firebase.Event<firebase.firestore.DeltaDocumentSnapshot>
So how do you mock this event, so you can unit test functions utilizing firestore triggers?

Related

Cloud functions for firebase generation 2

How can I implement firestore database trigger (onCreate, onUpdate) with Cloud functions for firebase gen 2 ?
As in Firestore cloud function gen1 we can use Cloud firestore database triggers, But how can we implement this in Firestore cloud function gen 2
As mentioned in the documentation, Cloud functions gen 2 do not support Cloud Firestore triggers at the moment.
You'll have to use Gen 1 functions if your application rely on Firestore triggers.

What is the appropriate way to load data from Firestore into BigQuery?

I was looking at a few ways to export data out from Firestore without using export (expensive operation in the long term as it doesn't support incremental backups) to use in BigQuery and Data Studio.
1) Using Google Pub/Sub.
This will probably require function to both write to pub/sub and then another to trigger to BQ.
2) Using Cloud Functions to trigger from an onCreate event to write directly to a BigQuery dataset and table.
(This is using table.insert)
What would the advantage be to use Pub/Sub - other than it would appear that it will cost more in the long term?
Or is there another way I am unaware of to do this?
I'm new at this. Some advise and pro and cons of the above scenarios are much appreciated.
The official solution is here.
In case of using Cloud Functions to trigger from an onCreate event, what will you create? Create File on Cloud Storage or create Firestore Document?
I think that in case of using Cloud Functions you should use PubSub trigger.
I recommend asynchronous architecture like Pub/Sub. Because rerun is easy and the scope of influence is limited.
I developed sample is here. I'm using Cloud Scheduler not cron.yaml. The cost of Cloud Scheduler is here.
(If you want) Export Firebase Authentication users to Cloud Firestore Collection. Use Firestore, Cloud Functions(PubSub) and Cloud Scheduler.
Export All Cloud Firestore Collections and Specified collections to Cloud Storage. Use AppEngine and Cloud Scheduler.
Export Specified Cloud Firestore Collections to BigQuery( as Partitioned tables). Use AppEngine and Cloud Scheduler.

Connect firebase function from realtime DB to Spanner

I have an app with tens of thousands of users where every second we are capturing data (location data). For analysis we are trying to allow this data to be queryable to generate reports for the user.
The idea is to pass the data in the Realtime database to Spanner so that I can query the data in SQL format and generate reports based on that.
I want to be able to do this from my trigger everytime that I update the realtime database.
So data follows this flow:
iOS / Android location data -> writes Firebase Realtime DB -> Function trigger based on write event -> Add to Spanner
Is this possible and if so how do you suggest it being done?
I am already using firebase functions to trigger notifications and keep database consistency, but not sure how to connect to spanner. The documentation that I have found is all about Cloud functions and when I do
const Spanner = require('#google-cloud/spanner');
I get the "Error: Error parsing triggers: Cannot find module '#google-cloud/spanner'"
My first limitation is: Are firebase functions limited to firebase itself or can I integrate with other cloud tools such as spanner?
Thanks,
Ricardo
I don't have experience with Realtime Database, PubSub and Functions, but I'm also interested :)
I imagine 2 steps: [1] Firestore triggers can publish message to PubSub, and [2] Functions can subscribe and handle PubSub events which interact with Spanner.
[1] How to publish message in Google Pub/Sub from Firebase Cloud Function?
[2] Google Cloud Pub/Sub Triggers  |  Cloud Functions
[2] Using Cloud Spanner with Cloud Functions  |  Cloud Functions
The error means you haven't (correctly) installed the Cloud Spanner client library. To fix this, in your functions folder run:
npm install #google-cloud/spanner
Unless you actually need the data in RTDB, you can avoid that step entirely and call a Cloud Function directly: See HTTP Functions.
There is a handy 'How-to Guide' on writing a Cloud Function for interacting with Cloud Spanner in the documentation, just modify it for put instead of get: See Using Cloud Spanner with Cloud Functions

Firestore + App Engine for realtime updates possible?

This documentation link for App Engine outlines an example where app engine listens to a RealTime Database reference.
I was wondering if
This can be done equivalently with Firestore using .onSnapshot
Whether I can specify wildcards in the reference as I do with cloud function triggers
Yes
No
There are no wildcard listeners with the Firestore SDK. Cloud Functions is responding to events generated by Firestore, which is very different than how a listener in the SDK works.

Firebase Cloud Function trigger

As I know currently Cloud Functions doesn't support triggering functions from Firebase.
For now I'm planing to use an basic Engine instance to trigger the functions based on the queue.
Is this the right way to go? or should I trigger the cloud function directly from the clients device after the data is inserted in the db?
thank you
Cloud Functions for Firebase was just launched today! You can use the SDK to trigger cloud functions from your Firebase database, storage bucket, authentication, and analytics events. https://firebase.google.com/docs/functions/

Resources