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
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');
});
}
Node-Js
on your machine, google to install it. it is not so hard.firestore-config.json
from your firebase console.