繁体   English   中英

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

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

我目前正在尝试构建一个云函数来将我的 Firestore 数据导出到我的存储桶。

我在 Firebase DOC 上找到的关于如何执行此操作的唯一示例:

https://googleapis.dev/nodejs/firestore/latest/v1.FirestoreAdminClient.html#exportDocuments

例子

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

从那个例子来看,我似乎需要安装@google-cloud/firestore作为我的云功能的依赖项。

但我想知道我是否可以使用firebase-admin包访问这些方法。

我想到了这一点,因为firebase-admin已经将@google-cloud/firestore作为依赖项。

> 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"
  },

题:

是否可以仅使用exportDocuments firebase-admin获取FirestoreAdminClient的实例并使用exportDocuments方法?

还是我真的需要将@google-cloud/firestore作为直接依赖项安装并直接使用它?

据我所知,您访问管理客户端的方式是正确的。

const client = new admin.firestore.v1.FirestoreAdminClient({});

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

这是我正在使用的实现,它允许您根据 firebase 此处提供的模板执行您需要执行的任何操作https://firebase.google.com/docs/firestore/solutions/schedule-export

在我的情况下,我从 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 只是包装 Cloud SDK 并重新导出其符号。 您可以使用包装器,也可以直接使用 Cloud SDK,如果需要,甚至可以将两者结合使用。 如果您想同时使用两者,则必须声明对 @google-cloud/firestore 的显式依赖关系,以便能够将其直接导入到您的代码中。

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM