-
Notifications
You must be signed in to change notification settings - Fork 2
/
populateCourses.js
57 lines (48 loc) · 1.32 KB
/
populateCourses.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
const courses = require("./public/JSON/courses");
const connectDB = require("./config/connect");
const Course = require("./models/courses.model");
const Author = require("./models/authors.test.model");
require("dotenv").config();
const videoLengths = [
"3 hrs 30 minutes",
"2 hrs 11 minutes",
"5 hrs 44 minutes",
"1 hr 17 minutes",
"5 hrs 19 minutes",
];
const randomDates = [
"December 7, 2021",
"January 17, 2020",
"August 5, 2021",
"September 10, 2020",
"March 5, 2021",
"July 12, 2020",
];
const popularArr = [true];
let i = 0;
for (let elem of courses) {
delete elem.author;
delete elem.authorJob;
delete elem.authorImg;
elem.previewImage = elem.previewImages;
delete elem.previewImages;
elem.description = elem.desc;
delete elem.desc;
elem.duration = videoLengths[Math.floor(Math.random()) * videoLengths.length];
elem.publishedAt =
randomDates[Math.floor(Math.random()) * randomDates.length];
if (i % 3 === 0)
elem.popular = popularArr[Math.floor(Math.random()) * popularArr.length];
i++;
}
const populate = async (courses) => {
await connectDB(process.env.MONGO_URI);
const authors = await Author.find().lean().exec();
for (let i = 0; i < courses.length; i++) {
courses[i].author = authors[i]._id;
}
await Course.deleteMany();
await Course.create(courses);
process.exit(0);
};
populate(courses);