I'm currently trying to build a cloud function to export my Firestore data to my Storage Bucket.
The only example I've found on the Firebase DOCs on how to do this:
https://googleapis.dev/nodejs/firestore/latest/v1.FirestoreAdminClient.html#exportDocuments
EXAMPLE
const firestore = require('@google-cloud/firestore');
const client = new firestore.v1.FirestoreAdminClient({
// optional auth parameters.
});
const formattedName = client.databasePath('[PROJECT]', '[DATABASE]');
client.exportDocuments({name: formattedName})
.then(responses => {
const response = responses[0];
// doThingsWith(response)
})
.catch(err => {
console.error(err);
});
From that example, it seems that I need to install @google-cloud/firestore
as a dependency to my cloud function.
But I was wondering if I can access these methods using only the firebase-admin
package.
I've thought of that because the firebase-admin
has the @google-cloud/firestore
as a dependency already.
> firebase-admin > package.json
"dependencies": {
"@firebase/database": "^0.4.7",
"@google-cloud/firestore": "^2.0.0", // <---------------------
"@google-cloud/storage": "^3.0.2",
"@types/node": "^8.0.53",
"dicer": "^0.3.0",
"jsonwebtoken": "8.1.0",
"node-forge": "0.7.4"
},
QUESTION:
Is it possible to get an instance of the FirestoreAdminClient
and use the exportDocuments
method using just the firebase-admin
?
Or do I really need to install the @google-cloud/firestore
as a direct dependency and work with it directly?
As of today, Firestore does not support automatic backups, but it DOES support exports via the gcloud CLI or REST API. Although not technically a backup in database jargon, an automatic export is valuable to have for disaster recovery because it can be re-imported to replace lost data.
With Cloud Functions, you can deploy Node. js code to handle events triggered by changes in your Cloud Firestore database. This allows you to easily add server-side functionality into your app without running your own servers.
The way you're accessing the admin client is correct as far as I can tell.
const client = new admin.firestore.v1.FirestoreAdminClient({});
However, you probably won't get any TypeScript/intellisense help beyond this point since the Firestore library does not actually define detailed typings for v1 RPCs. Notice how they are declared with any
types: https://github.com/googleapis/nodejs-firestore/blob/425bf3d3f5ecab66fcecf5373e8dd03b73bb46ad/types/firestore.d.ts#L1354-L1364
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With