-
Notifications
You must be signed in to change notification settings - Fork 0
/
seeder.js
67 lines (57 loc) · 1.58 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
66
67
const dotenv = require('dotenv');
dotenv.config({ path: './config/config.env' });
const fs = require('fs');
const mongoose = require('mongoose');
const course = require('./models/Courses');
const bootcamp = require('./models/Bootcamps');
const user = require('./models/users');
const review = require('./models/Review');
const colors = require('colors');
colors.setTheme({ success: 'rainbow', error: 'red' });
mongoose.connect(process.env.DB, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false,
});
const cData = JSON.parse(
fs.readFileSync(`${__dirname}\\_data\\courses.json`, 'utf-8')
);
const bData = JSON.parse(
fs.readFileSync(`${__dirname}\\_data\\bootcamps.json`, 'utf-8')
);
const uData = JSON.parse(
fs.readFileSync(`${__dirname}\\_data\\users.json`, 'utf-8')
);
const rData = JSON.parse(
fs.readFileSync(`${__dirname}\\_data\\reviews.json`, 'utf-8')
);
const importData = async () => {
try {
await bootcamp.create(bData);
await course.create(cData);
await user.create(uData);
await review.create(rData);
console.log('successful'.success);
process.exit();
} catch (error) {
console.log(error);
}
};
const deleteData = async () => {
try {
await course.deleteMany();
await bootcamp.deleteMany();
await user.deleteMany();
await review.deleteMany();
console.log('database deleted successfully'.success);
process.exit();
} catch (error) {
console.log(error);
}
};
if (process.argv[2] === '-i') {
importData();
} else if (process.argv[2] === '-d') {
deleteData();
}