简体   繁体   English

用于导出 Firestore 备份数据的云功能。 使用 firebase-admin 还是 @google-cloud/firestore?

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

I'm currently trying to build a cloud function to export my Firestore data to my Storage Bucket.我目前正在尝试构建一个云函数来将我的 Firestore 数据导出到我的存储桶。

The only example I've found on the Firebase DOCs on how to do this:我在 Firebase DOC 上找到的关于如何执行此操作的唯一示例:

https://googleapis.dev/nodejs/firestore/latest/v1.FirestoreAdminClient.html#exportDocuments 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.从那个例子来看,我似乎需要安装@google-cloud/firestore作为我的云功能的依赖项。

But I was wondering if I can access these methods using only the firebase-admin package.但我想知道我是否可以使用firebase-admin包访问这些方法。

I've thought of that because the firebase-admin has the @google-cloud/firestore as a dependency already.我想到了这一点,因为firebase-admin已经将@google-cloud/firestore作为依赖项。

> firebase-admin > package.json > 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 ?是否可以仅使用exportDocuments firebase-admin获取FirestoreAdminClient的实例并使用exportDocuments方法?

Or do I really need to install the @google-cloud/firestore as a direct dependency and work with it directly?还是我真的需要将@google-cloud/firestore作为直接依赖项安装并直接使用它?

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.但是,除此之外,您可能不会获得任何 TypeScript/intellisense 帮助,因为 Firestore 库实际上并未为 v1 RPC 定义详细的类型。 Notice how they are declared with any types: https://github.com/googleapis/nodejs-firestore/blob/425bf3d3f5ecab66fcecf5373e8dd03b73bb46ad/types/firestore.d.ts#L1354-L1364注意它们是如何用any类型声明的: https : //github.com/googleapis/nodejs-firestore/blob/425bf3d3f5ecab66fcecf5373e8dd03b73bb46ad/types/firestore.d.ts#L1354-L1364

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这是我正在使用的实现,它允许您根据 firebase 此处提供的模板执行您需要执行的任何操作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在我的情况下,我从 firestore 中过滤掉集合我不希望调度程序自动备份

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)
    })
}

firebase-admin just wraps the Cloud SDK and re-exports its symbols. firebase-admin 只是包装 Cloud SDK 并重新导出其符号。 You can use the wrapper, or use the Cloud SDK directly, or even a combination of the two if you want.您可以使用包装器,也可以直接使用 Cloud SDK,如果需要,甚至可以将两者结合使用。 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.如果您想同时使用两者,则必须声明对 @google-cloud/firestore 的显式依赖关系,以便能够将其直接导入到您的代码中。

这是关于如何通过混合 Cloud Scheduler、PubSub 和 Firebase 函数来进行自动 Firestore 备份的完整代码说明(我使用它并且效果很好) https://firebase.google.com/docs/firestore/solutions/schedule -出口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 构建失败:npm ERR! @google-cloud/firestore 无法从 firebase-admin 访问 - Build failed: npm ERR! @google-cloud/firestore not accessible from firebase-admin Firestore 在 google-cloud firestore 上查询“IN” - Firestore where 'IN' query on google-cloud firestore 使用 Google Cloud Function 将数据从 firebase firestore 导出到 MeiliSearch - Exporting data from firebase firestore to MeiliSearch using Google Cloud Function 在 Cloud Function 中使用 Cloud Firestore 数据 - Using Cloud Firestore data in a Cloud Function 从云中的 Firebase Firestore 获取数据 Function - Get Data from Firebase Firestore in Cloud Function 本地云功能Firebase将数据添加到Firestore - Local Cloud Function Firebase add data to Firestore 无法使用 Google 云将数据保存到 Firestore function - Unable to save data to Firestore using Google cloud function 如何通过 getUserByPhoneNumber 使用 google-cloud firestore 身份验证获取用户记录? - How to get the user Record using the google-cloud firestore auth by getUserByPhoneNumber? 使用 Firebase 云从 Cloud Firestore 读取数据 function - Read data from Cloud firestore with Firebase cloud function 使用 node.js 标准环境在 AppEngine 上找不到模块 @google-cloud/firestore - Cannot find module @google-cloud/firestore on AppEngine using node.js standard environment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM