Created
April 11, 2020 13:07
-
-
Save HunterHeston/705865138df86c48a201199c0e1147fd to your computer and use it in GitHub Desktop.
bulk upload json data to firestore
This file contains 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('./node_modules/firebase-admin'); | |
const collectionKey = 'ExampleCollection'; /* 1. The name of your collection here, case-sensitive. */ | |
const data = require('./data.json'); /* 2. A path to your data in JSON format here. */ | |
admin.initializeApp({ | |
credential: admin.credential.cert({ | |
/* 3. Your ServiceAccount details here. */ | |
'type': 'service_account', | |
'project_id': '...', | |
'private_key_id': '...', | |
'private_key': '...', | |
'client_email': '...', | |
'client_id': '...', | |
'auth_uri': 'https://accounts.google.com/o/oauth2/auth', | |
'token_uri': 'https://oauth2.googleapis.com/token', | |
'auth_provider_x509_cert_url': 'https://www.googleapis.com/oauth2/v1/certs', | |
'client_x509_cert_url': '...' | |
}), | |
databaseURL: '', /* 4. Your database URL here. */ | |
}); | |
const firestore = admin.firestore(); | |
const settings = { timestampsInSnapshots: true }; | |
firestore.settings(settings); | |
if (data && (typeof data === 'object')) { | |
Object.keys(data).forEach(docKey => { | |
firestore.collection(collectionKey).doc(docKey).set(data[docKey]).then((res) => { | |
console.log('Document ' + docKey + ' successfully written!'); | |
}).catch((error) => console.error('Error writing document: ', error)); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment