-
Notifications
You must be signed in to change notification settings - Fork 0
/
init-db.ts
95 lines (52 loc) · 1.86 KB
/
init-db.ts
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
85
86
87
88
89
90
91
92
93
import {COURSES, findLessonsForCourse} from './db-data';
import * as firebase from 'firebase';
var config = {
apiKey: 'AIzaSyDAdoefqX5OqjkD3BkW25ZAL6XYZMo4Vz8',
authDomain: 'fir-course-17549.firebaseapp.com',
databaseURL: 'https://fir-course-17549.firebaseio.com',
projectId: 'fir-course-17549',
storageBucket: 'fir-course-17549.appspot.com',
messagingSenderId: '170806523820'
};
console.log("Uploading data to the database with the following config:\n");
console.log(JSON.stringify(config));
console.log("\n\n\n\nMake sure that this is your own database, so that you have write access to it.\n\n\n");
firebase.initializeApp(config);
const db = firebase.firestore();
const settings = {timestampsInSnapshots: true};
db.settings(settings);
async function uploadData() {
var batch = db.batch();
const courses = db.collection('courses');
Object.values(COURSES)
.sort((c1:any, c2:any) => c1.seqNo - c2.seqNo)
.forEach(async (course:any) => {
const newCourse = removeId(course);
const courseRef = await courses.add(newCourse);
const lessons = courseRef.collection("lessons");
const courseLessons = findLessonsForCourse(course.id);
//console.log(`Adding ${courseLessons.length} lessons to ${course.description}`);
courseLessons.forEach(async lesson => {
const newLesson = removeId(lesson);
await lessons.add(newLesson);
});
});
return batch.commit();
}
function removeId(data:any) {
const newData: any = {...data};
delete newData.id;
return newData;
}
uploadData()
.then(() => {
console.log("Writing data, exiting in 10 seconds ...\n\n");
setTimeout(() => {
console.log("\n\n\nData Upload Completed.\n\n\n");
process.exit(0);
}, 10000);
})
.catch(err => {
console.log("Data upload failed, reason:", err, '\n\n\n');
process.exit(-1);
});