问题描述
我希望能够在本地运行云功能并针对生产数据中的副本进行调试.有没有办法将在线数据复制到本地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导入到本地仿真器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!