-
-
Save susthitsoft/bf20e0d9463afc1717be792545b4a11f to your computer and use it in GitHub Desktop.
firestoreDB to JSON to firestoreDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const admin = require('firebase-admin'); | |
const fs = require('fs'); | |
var serviceAccount = require('../../../../../../Private/myschool-data_transfer-key.json'); //how to setup your keys https://hackernoon.com/filling-cloud-firestore-with-data-3f67d26bd66e | |
admin.initializeApp({ credential: admin.credential.cert(serviceAccount) }); | |
const schema = { //only collection names | |
feedback: {}, | |
schools: { | |
califications: {}, | |
grades: {}, | |
} | |
}; | |
const db_2_JSON = (db, schema, current) => { | |
return Promise.all(Object.keys(schema).map(collection => { | |
return db.collection(collection).get() | |
.then(data => { | |
let promises = []; | |
data.forEach(doc => { | |
if (!current[collection]) current[collection] = { __type__: 'collection' }; | |
current[collection][doc.id] = doc.data(); | |
promises.push(db_2_JSON(db.collection(collection).doc(doc.id), schema[collection], current[collection][doc.id])); | |
}); | |
return Promise.all(promises); | |
}); | |
})).then(() => current); | |
}; | |
db_2_JSON(admin.firestore(), { ...schema }, {}) | |
.then(res => fs.writeFileSync('./src/native_web/utils/local_db.json', JSON.stringify(res, null, 2), 'utf8')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const admin = require('firebase-admin'); | |
const fs = require('fs'); | |
var serviceAccount = require('../../../../../../Private/myschool-data_transfer-key.json'); //how to setup your keys https://hackernoon.com/filling-cloud-firestore-with-data-3f67d26bd66e | |
admin.initializeApp({ credential: admin.credential.cert(serviceAccount) }); | |
const schema = { //only collection names | |
feedback: {}, | |
schools: { | |
califications: {}, | |
grades: {}, | |
} | |
}; | |
const JSON_2_db = (_JSON, db, schema) => { | |
return Promise.all(Object.keys(schema).map(collection => { | |
let promises = []; | |
Object.keys(_JSON[collection]).map(_doc => { | |
const doc_id = _doc; if(_doc === '__type__') return; | |
let doc_data = Object.assign({}, _JSON[collection][_doc]); | |
Object.keys(doc_data).map(_doc_data => { | |
if(doc_data[_doc_data] && doc_data[_doc_data]['__type__']) delete doc_data[_doc_data]; | |
}); | |
promises.push( | |
db.collection(collection).doc(doc_id) | |
.set(doc_data).then(() => { | |
return JSON_2_db(_JSON[collection][_doc], db.collection(collection).doc(doc_id), schema[collection]); | |
}) | |
); | |
}); | |
return Promise.all(promises); | |
})); | |
}; | |
JSON_2_db(JSON.parse(fs.readFileSync('./src/native_web/utils/local_db.json', 'utf8')), admin.firestore(), { ...schema }) | |
.then(() => console.log('imported')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment