如何将数据从Cloud Firestore导入到本地仿真器? [英] How to import data from cloud firestore to the local emulator?

查看:82
本文介绍了如何将数据从Cloud Firestore导入到本地仿真器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在本地运行云功能并针对生产数据中的副本进行调试. 有没有办法将在线数据复制到本地Firestore模拟器?

I want to be able to run cloud functions locally and debug against a copy from the production data. Is there a way to copy the data that is online to the local firestore emulator?

推荐答案

您可以使用 firestore-backup-restore 即可将生产数据作为JSON文件导出和导入.

You can use the firestore-backup-restore to export and import your production data as JSON files.

我写了一篇简短的文章,允许将这些JSON导入Firebase Simulator Firestore实例中.

I wrote a quick hack to allow for importing these JSON in the Firebase Simulator Firestore instance.

我提出了拉取请求,并制作了 npm模块同时.

I proposed a pull request and made this npm module in the meantime.

您可以通过以下方式使用它:

You can use it this way:

const firestoreService = require('@crapougnax/firestore-export-import')
const path = require('path')

// list of JSON files generated with the export service
// Must be in the same folder as this script
const collections = ['languages', 'roles']

// Start your firestore emulator for (at least) firestore
// firebase emulators:start --only firestore

// Initiate Firebase Test App
const db = firestoreService.initializeTestApp('test', {
   uid: 'john',
   email: 'john@doe.com',
})

// Start importing your data
let promises = []
try {
   collections.map(collection =>
      promises.push(
         firestoreService.fixtures(
            path.resolve(__dirname, `./${collection}.json`),
            [],
            [],
            db,
         ),
      ),
   )
   Promise.all(promises).then(process.exit)
} catch (err) {
   console.error(err)
}

显然,由于此数据不会在模拟器中保留,因此通常将它们注入测试套件的before()函数中,甚至在每次测试之前.

Obviously, since this data won't persist in the emulator, you'll typically inject them in the before() function of your test suite or even before every test.

这篇关于如何将数据从Cloud Firestore导入到本地仿真器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆