Migrate Firebase Realtime Database to Firestore

后端 未结 4 547
甜味超标
甜味超标 2020-12-28 15:48

I am looking for the best way to migrate my apps database which is using firebase realtime database to the new Cloud Firestore database. I am confident for the project I am

4条回答
  •  萌比男神i
    2020-12-28 15:55

    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

    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. it is not so hard.
    2. you can download firestore-config.json from your firebase console.

提交回复
热议问题