-
Notifications
You must be signed in to change notification settings - Fork 0
/
seeder.js
65 lines (57 loc) · 1.86 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
65
const fs = require('fs');
const mongoose = require('mongoose');
const colors = require('colors');
const dotenv = require('dotenv');
// Load env variables
dotenv.config({ path: './config/config.env'});
// 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(process.env.MONGO_URI, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true
});
// Read JSON files
// dirname = curernt directory name
const bootcamps = JSON.parse(fs.readFileSync(`${__dirname}/_data/bootcamps.json`, 'utf-8'));
const courses = JSON.parse(fs.readFileSync(`${__dirname}/_data/courses.json`, 'utf-8'));
const user = JSON.parse(fs.readFileSync(`${__dirname}/_data/users.json`, 'utf-8'));
const review = JSON.parse(fs.readFileSync(`${__dirname}/_data/reviews.json`, 'utf-8'));
// Import into DB
const importData = async () => {
try {
await Bootcamp.create(bootcamps);
await Course.create(courses);
await User.create(user);
await Review.create(review);
console.log('Data Imported...'.green.inverse);
process.exit();
} catch (err) {
console.error(err);
}
}
// Delete Data
const deleteData = async () => {
try {
await Bootcamp.deleteMany();
await Course.deleteMany();
await User.deleteMany();
await Review.deleteMany();
console.log('Data Destroyed...'.red.inverse);
process.exit();
} catch (err) {
console.error(err);
}
}
// When seeder is run, allow argument to know whether to us Import or Delete function
if (process.argv[2] === '-i') { // terminal: node seeder -i
importData();
}
else if (process.argv[2] === '-d'){ // terminal: node seeder -d
deleteData();
}