-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.js
42 lines (33 loc) · 980 Bytes
/
import.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var admin = require("firebase-admin");
var fs = require('fs');
var serviceAccount = require("./serviceAccountKey.json");
var fileName = process.argv[2];
// You should replae databaseURL with your own
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://<your-project-name>.firebaseio.com"
});
var db = admin.firestore();
var collectionName = '';
fs.readFile(fileName, 'utf8', function(err, data){
if(err){
return console.log(err);
}
// Turn string from file to an Array
dataArray = JSON.parse(data);
for(var index in dataArray){
collectionName = index;
for(var doc in dataArray[index]){
if(dataArray[index].hasOwnProperty(doc)){
db.collection(collectionName).doc(doc)
.set(dataArray[index][doc])
.then(() => {
console.log('Import success, adding to firestore..');
})
.catch(error => {
console.log(error);
});
}
}
}
})