-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseeder.js
53 lines (46 loc) · 1.27 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
const fs = require('fs');
const mongoose = require('mongoose');
const colors = require('colors');
const dotenv = require('dotenv');
// Load env variables
dotenv.config();
// Load models
const Bootcamp = require('./models/Bootcamp');
const Course = require('./models/Course');
// Connect to db
mongoose.connect(process.env.MONGO_URI,{
useCreateIndex: true,
useFindAndModify:false,
useNewUrlParser:true,
useUnifiedTopology: true
});
// Read the jSON files
const bootcamps = JSON.parse(fs.readFileSync(`${__dirname}/_data/bootcamps.json` , `utf-8`));
const courses = JSON.parse(fs.readFileSync(`${__dirname}/_data/bootcamps.json` , `utf-8`));
// Import into db
const importData = async()=>{
try {
await Bootcamp.create(bootcamps);
await Course.create(courses);
console.log('Data Imported ...'.green.inverse);
process.exit();
} catch (err) {
console.log(err);
}
}
// Delete data
const deleteData = async()=>{
try {
await Bootcamp.deleteMany();
await Course.deleteMany();
console.log('Data Destroyed ...'.red.inverse);
process.exit();
} catch (err) {
console.log(err);
}
}
if(process.argv[2] === '-i'){
importData();
}else if(process.argv[2] === '-d'){
deleteData();
}