如何将cloud firestore中的数据导入本地模拟器? [英] How to import data from cloud firestore to the local emulator?

查看:24
本文介绍了如何将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.

我编写了一个快速技巧来允许在 Firebase Simulator Firestore 实例中导入这些 JSON.

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.

你可以这样使用:

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天全站免登陆