-
Notifications
You must be signed in to change notification settings - Fork 0
/
seeder.js
64 lines (57 loc) · 1.67 KB
/
seeder.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
const fs = require('fs');
const mongoose = require('mongoose');
const colors = require('colors');
const dotenv = require('dotenv');
// Load env vars
dotenv.config();
// Load Models
const Bootcamp = require('./models/Bootcamp');
const Course = require('./models/Course');
const User = require('./models/User');
const Review = require('./models/Review');
// Connect to DB
mongoose.connect(
`mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}@cluster0.hcqem.mongodb.net/${process.env.DB_NAME}?retryWrites=true&w=majority`,
{
useCreateIndex: true,
useFindAndModify: false,
useNewUrlParser: true,
useUnifiedTopology: true,
}
);
const bootcamps = JSON.parse(
fs.readFileSync(`${__dirname}/_data/bootcamps.json`)
);
const courses = JSON.parse(fs.readFileSync(`${__dirname}/_data/courses.json`));
const users = JSON.parse(fs.readFileSync(`${__dirname}/_data/users.json`));
const reviews = JSON.parse(fs.readFileSync(`${__dirname}/_data/reviews.json`));
// Import into DB
const importData = async () => {
try {
await Bootcamp.create(bootcamps);
await Course.create(courses);
await User.create(users);
await Review.create(reviews);
console.log('Data imported...'.cyan.bold);
process.exit();
} catch (error) {
console.log(error.message);
}
};
const deleteData = async () => {
try {
await Bootcamp.deleteMany();
await Course.deleteMany();
await User.deleteMany();
await Review.deleteMany();
console.log('Data destroyed...'.red.bold);
process.exit();
} catch (error) {
console.log(error.message);
}
};
if (process.argv[2] === '-i') {
importData();
} else if (process.argv[2] === '-d') {
deleteData();
}