2017-10-04 97 views
14

我正在寻找将正在使用Firebase实时数据库的应用程序数据库迁移到新的Cloud Firestore数据库的最佳方法。我对我正在研究的项目非常有信心,我不需要对数据模式进行任何更改,所以我几乎只是试图对它进行映射。 Firebase在他们的网站上建议只写一个脚本来做到这一点,但我不确定最好的解决方法。有没有人已经完成了这个脚本?将Firebase实时数据库迁移到Firestore

+0

https://firebase.google.com/docs/firestore/firestore-for-rtdb –

+0

是的,知道链接的,但他们基本上只是建议写一个脚本,但这不是这个问题所要求的。 – Luke

+0

不太可能任何人都会为此使用通用脚本,因为它可能会根据每个人的特定用例和要求定制。 –

回答

5

我写了一个小节点脚本,它以快速和肮脏的方式迁移事物,并且工作得非常好。

如果其他人感兴趣,则在下面。

注:这应该只有在实时数据库中的数据模型是完全平坦的,并没有太多的嵌套的数据,你打算使用,并保持你的数据持平,以及在公司的FireStore

运行此脚本只需创建一个名为index.js的节点文件,并将其与实时数据库导出中的服务帐户文件和原始json文件一起放入目录中,然后从命令行运行以下命令。

$ node index.js 

下面的脚本实现。

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); 
    }); 
} 
-1

嗨,我已经创建了一个脚本为同

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) => { }); }); });} 
+0

你可以添加步骤来运行这个? – Luke

+0

是 1)配置您的Firebase项目,然后 2)添加到TS文件 从'angularfire2/firestore'导入{AngularFirestore,AngularFirestoreCollection}; 从'angularfire2/database'导入{AngularFireDatabase}; convert(){this.itemsCollection = this.afs.collection('requests'); // ref() this.angularfire.list('/请求/')。auditTrail()。subscribe((data:any)=> {ee(data,element => {this.itemsCollection.doc(element.key).set(element.payload.val )).then((result)=> {});});});} –