-
Notifications
You must be signed in to change notification settings - Fork 1
/
updateFirestore.js
84 lines (75 loc) · 2.19 KB
/
updateFirestore.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const admin = require('firebase-admin');
let serviceAccount = require('./ukie-test-645eb0f78aef.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
let db = admin.firestore();
var json = require('../ukie-data/firestoreData.json');
//console.log(data)
// for (var collection in json) {
// console.log(collection)
// for (var doc in json[collection]) {
// console.log(doc)
// for (var data in json[collection][doc]) {
// console.log(data)
// for (var info in json[collection][doc][data]) {
// console.log(info)
// }
// }
// }
// }
Object.entries(json).map(collection => {
//console.log(collection[0])
//if(collection[0] === "courses") {
Object.entries(collection[1]).map(doc => {
//console.log(doc[0])
//console.log(doc[1])
let updateDoc = db.collection(collection[0]).doc(doc[0]);
updateDoc.set({}).then(something => {
Object.entries(doc[1]).map(data => {
//console.log("this is: ")
//console.log(data[0])
//console.log(data[1])
if(typeof(data[1]) === "object") {
//console.log("is object")
//console.log(data[1])
Object.entries(data[1]).map(arr => {
//console.log(arr[0])
//console.log(arr[1])
let dbData = {
};
Object.entries(arr[1]).map(final => {
//console.log(final[0])
//console.log(final[1])
dbData[`${final[0]}`] = final[1]
}
)
let tmp = {
};
tmp[`${data[0]}`] = admin.firestore.FieldValue.arrayUnion(dbData)
//console.log("this is what I want to see:" ,tmp)
console.log("dbData", dbData)
console.log("data", data[0])
console.log("doc", doc[0])
console.log("this is what I want to see:" ,tmp)
updateDoc.update(tmp);
}
)
}
else {
let tmp = {
};
tmp[`${data[0]}`] = data[1]
updateDoc.update(tmp);
}
}
)
})
.catch(err => {
console.log('Error getting document', err);
});
}
)
//}
}
)