Cloud function to export Firestore backup data. Using firebase-admin or @google-cloud/firestore?

firestore export to csv
cloud firestore export json
firestore automated backups
gcloud firestore export
firestore import json
firebase console
firebase cloud firestore backup
firebase-functions

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?

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

Schedule data exports - Firebase, This is part 4 of a series of devops 101 with firebase cloud functions. This would expensive af; Run a cron job on my computer to export the data 🤯; Use the service account , you will need to add the role Cloud Datastore Import Export Admin auth } from 'google-auth-library' import { backupSlackNotification } from '. Cloud Firestore is a flexible, scalable database for mobile, web, and server development from Firebase and Google Cloud Platform. Like Firebase Realtime Database, it keeps your data in sync across client apps through realtime listeners and offers offline support for mobile and web so you can build responsive apps that work regardless of network latency or Internet connectivity.

Here is an implementation I'm using that allows you to do whatever operations you need to do, based on the template provided by firebase here https://firebase.google.com/docs/firestore/solutions/schedule-export

In my case I'm filtering out collections from firestore I don't want the scheduler to automatically backup

const { Firestore } = require('@google-cloud/firestore')

const firestore = new Firestore()
const client = new Firestore.v1.FirestoreAdminClient()
const bucket = 'gs://backups-user-data'

exports.scheduledFirestoreBackupUserData = async (event, context) => {
  const databaseName = client.databasePath(
    process.env.GCLOUD_PROJECT,
    '(default)'
  )

  const collectionsToExclude = ['_welcome', 'eventIds', 'analyticsData']

  const collectionsToBackup = await firestore.listCollections()
    .then(collectionRefs => {
      return collectionRefs
        .map(ref => ref.id)
        .filter(id => !collectionsToExclude.includes(id))
    })


  return client
    .exportDocuments({
      name: databaseName,
      outputUriPrefix: bucket,
      // Leave collectionIds empty to export all collections
      // or define a list of collection IDs:
      // collectionIds: ['users', 'posts']
      collectionIds: [...collectionsToBackup]
    })
    .then(responses => {
      const response = responses[0]
      console.log(`Operation Name: ${response['name']}`)
      return response
    })
    .catch(err => {
      console.error(err)
    })
}

Extend Cloud Firestore with Cloud Functions - Firebase, NPM package for backup and restore Firebase Firestore You can read it here: https://firebase.google.com/docs/firestore/manage-data/export-import. However, you You should replae databaseURL with your own admin.initializeApp({ writeFile("firestore-export.json", JSON.stringify(dt), function(err) { I'm an Android developer and recently I've started working on a project based on firebase cloud functions and firestore database. I'm writing an HTTP trigger function that will take two parameters and compare that parameter value with the firestore data value and if the value matches then return a response of true or else false. Duplicate Question:

firebase-admin just wraps the Cloud SDK and re-exports its symbols. You can use the wrapper, or use the Cloud SDK directly, or even a combination of the two if you want. If you want to use both, you have to declare an explicit dependency on @google-cloud/firestore in order to be able to import it directly into your code.

Cloud function to export Firestore backup data. Using firebase , When you work with Google Cloud products pretty much everything that like node-firestore-backup-restore or firestore-export-import etc which are The data is stored in Documents that contain field mapping to values. .firebaserc and firebase.json along with the folder named functions at the root level. We are preparing a release of @google-cloud/firestore v2.3.0 that will increase the timeout for all of our operations to 60s. This applies to all operations issued by the SDK, and is independent of the timeout set by any runtime environment (such as GCF).

Here is the full explanation with code (I use it and it works very well) on how to do automated Firestore backups by mixing Cloud Scheduler, PubSub and Firebase Function https://firebase.google.com/docs/firestore/solutions/schedule-export

Firestore Automated Backups, This is the Node.js Server SDK for Google Cloud Firestore. The Cloud Firestore Server SDKs are designed to manage the full set of data in your Cloud that accesses Cloud Firestore on behalf of end users, use the firebase Client SDK. a new client const firestore = new Firestore(); async function quickstart() { // Obtain a� The Firebase Admin Node.js SDK enables access to Firebase services from privileged environments (such as servers or cloud) in Node.js. For more information, visit the Firebase Admin SDK setup guide. Installation. The Firebase Admin Node.js SDK is available on npm as firebase-admin: $ npm install --save firebase-admin

How to backup and restore Cloud Firestore | by Dale Nguyen, Contribute to jloosli/node-firestore-import-export development by creating an is home to over 50 million developers working together to host and review code, - a , --accountCredentials <path> - path to Google Cloud account credentials from 'node-firestore-import-export'; import * as firebase from 'firebase-admin';� The Firebase Admin Node.js SDK enables access to Firebase services from privileged environments (such as servers or cloud) in Node.js. For more information, visit the Firebase Admin SDK setup guide. Installation. The Firebase Admin Node.js SDK is available on npm as firebase-admin: $ npm install --save firebase-admin

How to perform and schedule firestore backups with Google Cloud , How to Export CSV from a Firebase Cloud Function. Firestore data converted to CSV via background Cloud Function npm install fs-extra --save npm install json2csv --save npm install @google-cloud/storage --save fileName); // Reference report in Firestore const db = admin.firestore() const reportRef� Anytime one of the document changes, I run a cloud function to update and check the other documents. Running a “maintainer” cloud function that runs periodically and check whether all the documents are in sync. I would love to know your opinion or a new method to do this which is more reliable. Thanks a lot!

Package, I am using Node 8 as well. My app was down for pretty much the entire day yesterday (IST) and I was glued to my laptop trying to manually do everything that cloud function would do until I had to ask my clients to stop using the app for the last few hours.

Comments
  • It does work! No intellisense (as per the reason you've pointed out). But it works. Thanks.
  • Note it seems on Node10 runtime the variable process.env.GCLOUD_PROJECT is not available by default and the script won't run.
  • @Vojtěch could be the case, yes, as I currently have my functions running still on Node 8... if you find a fix please let me know as well.
  • i just pass the cloud project id as env var manually and it works,
  • If you could, would you mind showing me an example of how to get a hold of the FirestoreAdminClient using the firebase-admin ? I've tried: const client = new admin.firestore.v1.FirestoreAdminClient({}); and it was all auto-completed by VSCode. But I'm not getting any auto-completion on my client variable after that. Am I doing something wrong? Thanks!
  • I've never used FirestoreAdminClient. Typical access is going to happen through the Firestore object. googleapis.dev/nodejs/firestore/latest/Firestore.html