Luk*_*uke 20 firebase firebase-realtime-database google-cloud-firestore
我正在寻找将使用firebase实时数据库的应用数据库迁移到新的Cloud Firestore数据库的最佳方法.我对我正在进行的项目充满信心,我不需要进行任何数据模式更改,因此我只是尝试1-1映射它.Firebase在他们的网站上建议只编写一个脚本来执行此操作,但我不确定最佳方法.有没有人已经制作了一个完成此任务的脚本?
我编写了一个小节点脚本,以快速和肮脏的方式迁移事物,并且它运行得非常好.
如果其他人有兴趣,它在下面.
注意:如果在实时数据库数据模型是完全平坦的,并没有太多的嵌套的数据这应该只被使用,并且你打算让您的数据持平,以及在公司的FireStore
要运行此脚本只创建一个节点文件名为index.js并把它在一个目录,从实时数据库导出服务帐户文件和原始JSON文件一起并运行以下命令行.
$ node index.js
Run Code Online (Sandbox Code Playgroud)
下面的脚本实现.
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);
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5430 次 |
| 最近记录: |