-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (49 loc) · 1.26 KB
/
index.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
const axios = require("axios");
const {
users,
courses,
assignments,
submissions
} = require("./config/mongoCollection");
const {
userData,
courseData,
assignmentData,
submissionData
} = require("./data");
/**
* Given a collection function, array of documents, and a string describing
* the type of data we're inserting into Mongo, insert the data into the appropriate
* collection
*
* @param {function} collection
* @param {Array} data
* @param {String} collectionType
*/
const addToDb = async (collection, data, collectionType) => {
const coll = await collection();
const res = await coll.insertMany(data);
if (res.result.ok) {
console.log(`${res.insertedCount} documents added to Mongo`);
// return true;
} else {
console.log(`Something went wrong while adding ${collectionType}`);
// return false;
}
};
const main = async () => {
await addToDb(users, userData, "users");
await addToDb(courses, courseData, "courses");
await addToDb(assignments, assignmentData, "assignments");
await addToDb(submissions, submissionData, "submissions");
return true;
};
(async () => {
try {
const fin = await main();
console.log('Finished populating test db:', fin);
} catch(err){
console.log(err);
}
process.exit();
})();