Hey @AmaFB
To solve the issue of triggering a Firestore function on a document creation in a non-default Firestore database, here is a step-by-step, human-friendly guide:
1. Initialize the Other Firestore Database
First, you need to initialize the Firestore database you want to access. This is done by setting up Firebase Admin SDK to point to your non-default Firestore database:
const admin = require(‘firebase-admin’);
admin.initializeApp({
credential: admin.credential.applicationDefault(),
databaseURL: ‘https://.firebaseio.com’
});
const otherDb = admin.firestore();
2. Set Up Firestore Trigger on Default Database
Firestore triggers like onDocumentCreated can only listen to events on the default Firestore database. You can use these triggers to detect changes and then manually perform operations on the non-default database.
Here’s how you can set up a trigger to listen to document creations in the default Firestore database and then copy the data to the other Firestore database:
const functions = require(‘firebase-functions’);
const admin = require(‘firebase-admin’);
admin.initializeApp();
const otherDb = admin.firestore();
exports.onSurveyCreated = functions.firestore
.document(‘surveys/{surveyId}’)
.onCreate(async (snap, context) => {
const newSurvey = snap.data();
const surveyId = context.params.surveyId;
// Write to the other Firestore database
await otherDb.collection(‘surveys’).doc(surveyId).set(newSurvey);
console.log(‘Survey written to other database’);
});
3. Note About Cross-Databases Operations
As of now, Firestore triggers do not support direct operations on non-default databases. The approach is to use the default database to detect changes and then perform the required operations on the other database manually, as shown above.
4. Clarifying the Error and Documentation
You mentioned finding documentation suggesting you can specify a different database directly in the trigger. However, if you’re encountering errors like:
3. Note About Cross-Databases Operations
As of now, Firestore triggers do not support direct operations on non-default databases. The approach is to use the default database to detect changes and then perform the required operations on the other database manually, as shown above.
4. Clarifying the Error and Documentation
You mentioned finding documentation suggesting you can specify a different database directly in the trigger. However, if you’re encountering errors like:
Error: 3 INVALID_ARGUMENT: The request was for database ‘projects/project-id/databases/(default)’ but was attempting to access database ‘projects/project-id/databases/otherDb’
This means the feature might not be fully supported or there could be a misconfiguration. Stick to using the default database for triggers and then manually interacting with other databases.
Summary
- Initialize the non-default Firestore database properly.
- Set up triggers in the default database.
- Manually handle data operations to/from the non-default database in your trigger functions.
By following these steps, you should be able to achieve the desired functionality of accessing and manipulating data in a non-default Firestore database.
If you have any query then feel free to connect with me.