firebasegoogle-cloud-platformgoogle-cloud-firestorefirebase-realtime-databasegoogle-cloud-functions

Is there a way to connect cloud firestore to realtime database using Cloud functions?


I'm designing my firebase database in such a way whereby certain document fields in the firestore are linked to the realtime database fields; So that if there's a change in the real-time database, it changes the corresponding firestore field.

Let me give my question more context..
I'm designing a mobile app which has error-reporting chat rooms. From these chat rooms, the users are able to write their errors and their errors go into the real-time database as well as update the firestore.
On the admin side, he should be able to read all of their errors.
I haven't really delved that much into the Cloud Functions, so I wanted to know if it's possible to link the two in such a way.
Below is the structure of the firestore collection: firestore


Solution

  • Using the Firebase Admin SDK you can very well, in a Cloud Function, write from one Firebase database service to the other database service, i.e. from the Realtime Database to Cloud Firestore or vice-versa.

    You write in your question "(If) there's a change in the Realtime Database, it changes the corresponding Firestore field.".

    Here is a simple example of code that shows how to write to a specific Firestore document upon change in the Realtime Database. We take the case where you write to a city/radio node in the Realtime Database and you want to update the corresponding radio document in Firestore.

    It's up to you to adapt it to your exact case, in particular adapting the path that triggers the Cloud Function and the Firestore document and field(s) you want to update.

    const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    
    admin.initializeApp();
    
    exports.updateFirestore = functions.database.ref('cities/{cityId}/{radioId}')
        .onWrite((change, context) => {
    
            const city = context.params.cityId;
            const radio = context.params.radioId;
    
            // Exit when the data is deleted -> to confirm that this is needed in your case....
            if (!change.after.exists()) {
                return null;
            }
            // Grab the current value of what was written to the Realtime Database.
            const data = change.after.val();
    
            //Write to Firestore: here we use the TransmitterError field as an example
            const firestoreDb = admin.firestore();
            const docReference = firestoreDb.collection(city).doc(radio);
    
            return docReference.set(
                {
                    TransmitterError: data.TransmitterError
                },
                { merge: true }
            );
    
        });
    

    Since you "haven't really delved that much into the Cloud Functions", I would suggest you watch the 3 videos about "JavaScript Promises" from the Firebase video series: https://firebase.google.com/docs/functions/video-series/

    Diving into the documentation is also a must!