-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseeders.js
More file actions
60 lines (49 loc) · 1.49 KB
/
seeders.js
File metadata and controls
60 lines (49 loc) · 1.49 KB
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
const fs = require('fs');
const moongose = require ('mongoose');
const color = require('colors');
const dotenv = require('dotenv');
// load env variables
dotenv.config({path: './config/config.env'});
// load models
const Bootcamps = require('./models/Bootcamp');
const Course = require('./models/course');
const User = require('./models/user');
// connect to db
moongose.connect(process.env.MONGO_URI,{
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true
});
// read json files
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'));
//import data into database
const importData = async() =>{
try {
await Bootcamps.create(bootcamps);
await Course.create(courses);
await User.create(user);
console.log('Data imported..'.green.inverse)
} catch (error) {
console.error(`${error}`)
}
}
//delete data
const deleteData = async() =>{
try {
await Bootcamps.deleteMany();
await Course.deleteMany();
await User.deleteMany();
console.log('Data destroyed..'.red.inverse);
process.exit();
} catch (error) {
console.error(`${error}`)
}
}
if(process.argv[2] ==='-i'){
importData();
}else if (process.argv[2] === '-d'){
deleteData();
}