简体   繁体   English

将 Firebase 实时数据库迁移到 Firestore

[英]Migrate Firebase Realtime Database to Firestore

I am looking for the best way to migrate my apps database which is using firebase realtime database to the new Cloud Firestore database.我正在寻找将使用 firebase 实时数据库的应用程序数据库迁移到新的 Cloud Firestore 数据库的最佳方法。 I am confident for the project I am working on I don't need to make any data schema changes, so I am pretty much just trying to 1-1 map it.我对我正在从事的项目充满信心,我不需要进行任何数据架构更改,所以我几乎只是尝试 1-1 map 它。 Firebase has suggested on their site to just write a script to do this, but I am not sure of the best way to go about that. Firebase 在他们的网站上建议只编写一个脚本来执行此操作,但我不确定 go 的最佳方法。 Has anyone already made a script that accomplishes this?有没有人已经制作了一个脚本来完成这个?

I wrote up a little node script that migrated things in a quick and dirty way and it worked quite nicely.我写了一个小节点脚本,它以一种快速而肮脏的方式迁移东西,它运行得非常好。

It is below if anyone else is interested.如果有人感兴趣,它在下面。

Note: This should only be used if your data model in the realtime database was completely flat and did not have much nested data, and you intend on keeping your data flat as well in Firestore注意:仅当实时数据库中的数据模型完全扁平且没有太多嵌套数据时才应使用此方法,并且您打算在 Firestore 中也保持数据扁平

To run this script just create a node file called index.js and throw it in a directory along with your service account file and raw json file from the realtime database export and run the following from command line.要运行此脚本,只需创建一个名为 index.js 的节点文件,并将其与您的服务帐户文件和来自实时数据库导出的原始 json 文件一起放入一个目录中,然后从命令行运行以下命令。

$ node index.js

Script implementation below.下面的脚本实现。

const admin = require('firebase-admin');

var serviceAccount = require("./config.json");
var database = require("./database.json");
var async = require ('async');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});

var db = admin.firestore();

var allEntityNames = Object.keys(database);

var asyncTasks = [];

for (var i in allEntityNames) {
  var entityName = allEntityNames[i];
  var entity = database[entityName];
  var entityKeys = Object.keys(entity);

  console.log("began migrating "+ entityName);
  for (var j in entityKeys) {
    var entityKey = entityKeys[j];
    var dict = entity[entityKey];
    asyncTasks.push(function(callback){
      db.collection(entityName).doc(entityKey).set(dict)
        .then(function() {
          callback();
        })
        .catch(function(error) {
          console.log(error);
          callback();
        });
    });
  }
  async.parallel(asyncTasks, function(){
    console.log("Finished migrating "+ entityName);
  });
}

Actually, I wrote a script in Node-Js that use batch in writing to Firestore (batch is super fast and suitable for write may items) here is my code, just change files name to your's name and run node YOUR_FILE_NAME.js实际上,我在Node-Js中编写了一个脚本,使用批处理写入 Firestore(批处理速度超快,适合写入可能的项目)这里是我的代码,只需将文件名更改为您的名称并运行node YOUR_FILE_NAME.js

const admin = require('firebase-admin');
var serviceAccount = require('./firestore-config.json');
var database = require('./database.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: 'YOUR_FILE_STORE_DB_URL',
});

var db = admin.firestore();
var allEntityNames = Object.keys(database);
var counter = 0;
var commitCounter = 0;
var batches = [];
batches[commitCounter] = db.batch();
var ref = db.collection('users');
allEntityNames.forEach(function(k, i) {
  if (counter <= 498) {
    var thisRef = ref.doc(k);
    batches[commitCounter].set(thisRef, database[k]);
    counter = counter + 1;
  } else {
    counter = 0;
    commitCounter = commitCounter + 1;
    batches[commitCounter] = db.batch();
  }
});
for (var i = 0; i < batches.length; i++) {
  batches[i].commit().then(function() {
    console.count('wrote batch');
  });
}
  1. if you don't have Node-Js on your machine, google to install it.如果你的机器上没有Node-Js ,谷歌安装它。 it is not so hard.这并不难。
  2. you can download firestore-config.json from your firebase console.您可以从 Firebase 控制台下载firestore-config.json

I just did this with a very basic node script and I hope it will serve as an example to the next one having this issue:我只是用一个非常基本的节点脚本来做这件事,我希望它可以作为下一个遇到这个问题的例子:

require('firebase/firestore')
const fs = require('fs')
const { initializeApp, firestore } = require('firebase/app')

const UID = 'asdfasdf' // UID of the user you are migrating

initializeApp({
    apiKey: process.env.API_KEY,
    projectId: process.env.PROJECT_ID
})

// db.json is the downloaded copy from my firebasedatabase
fs.readFile('db.json', (err, data) => {
    if (err) throw err
    const json = JSON.parse(data)
    const readings = json.readings[UID]
    const result = Object.values(readings)
    result.forEach(({ book, chapter, date }) =>
        // In my case the migration was easy, I just wanted to move user's readings to their own collection
        firestore().collection(`users/${UID}/readings`)
            .add({ date: firestore.Timestamp.fromDate(new Date(date)), chapter, book })
            .catch(console.error)
    )
    console.log('SUCCESS!')
})

Of course you can also iterate twice to do it for every user but in my case it was not needed :)当然,您也可以迭代两次为每个用户执行此操作,但在我的情况下不需要:)

There's a decent 3rd party npm package to help with importing, it boils down to basically one line:有一个不错的第 3 方 npm package 可以帮助导入,它基本上归结为一行:

await firestoreService.restore({ "my-table": myTable });

https://www.npmjs.com/package/firestore-export-import https://www.npmjs.com/package/firestore-export-import

Hi i have created a script for the same嗨,我已经为此创建了一个脚本

import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore'; 
import { AngularFireDatabase } from 'angularfire2/database';
constructor( private afs: AngularFirestore, private angularfire: AngularFireDatabase ) {}
convert() { 
this.itemsCollection = this.afs.collection('requests');//ref() 
this.angularfire.list('/requests/').auditTrail().subscribe((data: any) => { 
_.each(data, element =>{
this.itemsCollection.doc(element.key).set(element.payload.val()) .then((result) => { }); }); });}

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

相关问题 Firebase 实时数据库多路径更新与 Firestore 数据库批量写入 - Firebase realtime database multipath updates vs firestore database batched write Cloud Firestore 和 Firebase 实时数据库有什么区别? - What's the difference between Cloud Firestore and the Firebase Realtime Database? 实时数据库 - Firebase 不更新 - realtime database - Firebase not updating Firestore 实时数据库身份验证规则 - Firestore Realtime Database authentication rules Firebase 实时数据库规则 - Firebase realtime database rules 使用@PropertyName 更改 Firebase 实时数据库 POJO 属性,如何迁移现有数据 - Change Firebase Realtime Database POJO properties using @PropertyName, How to migrate existing data 如何将 MySql 数据库迁移到 Firestore - How to migrate MySql Database to Firestore 我如何将我的 firebase 实时数据库 function 转换为 firestore function - How can i convert my firebase realtime database function to firestore function 此代码适用于 Firestore,但我将如何在 flutter 中为实时数据库 firebase 编写此代码? - This code is for Firestore but how I'll write this code for realtime database firebase in flutter? Firebase 实时数据库定价计算 - Firebase Realtime Database pricing calculation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM