且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将数据从Cloud Firestore导入到本地仿真器?

更新时间:2021-09-02 09:40:03

您可以使用 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.