This repository was archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseeder.js
More file actions
136 lines (105 loc) · 3.57 KB
/
seeder.js
File metadata and controls
136 lines (105 loc) · 3.57 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*=======================================================
Data Seeder
Usage :
node seeder <method> <type>
Example :
(1) node seeder -i bootcamp
this command will import bootcamp data in server
(2) node seeder -d course
this command will destroy data of course from server
=======================================================*/
const fs = require('fs');
const mongoose = require('mongoose');
const colors = require('colors');
const dotenv = require('dotenv');
// Load env vars
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,
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 users = JSON.parse(fs.readFileSync(`${__dirname}/_data/users.json`, "utf-8"))
const reviews = JSON.parse(fs.readFileSync(`${__dirname}/_data/reviews.json`, "utf-8"))
// Import into DB
const importData = async (model, name, data) => {
try {
await model.create(data);
console.log(`${name} Data Imported...`.green.inverse);
process.exit();
} catch (err) {
console.log(err);
}
}
// Delete data
const deleteData = async (model, name) => {
try {
await model.deleteMany();
console.log(`${name} Data Destroyed...`.red.inverse);
process.exit();
} catch (err) {
console.log(err);
}
}
// Import everything into DB
const importAll = async (name, data) => {
try {
await Bootcamp.create(bootcamps);
await Course.create(courses);
await User.create(users);
await Review.create(reviews);
console.log(`All Data Imported...`.green.inverse);
process.exit();
} catch (err) {
console.log(err);
}
}
// Delete all data
const deleteAll = async (model) => {
try {
await Bootcamp.deleteMany();
await Course.deleteMany();
await User.deleteMany();
await Review.deleteMany();
console.log(`All Data Destroyed...`.red.inverse);
process.exit();
} catch (err) {
console.log(err);
}
}
// Error
const showError = async () => {
console.log("Error".red.inverse + " Check command again");
process.exit();
}
if (process.argv[2] === "-i" && process.argv[3] === "course") {
importData(Course, "Course", courses);
} else if (process.argv[2] === "-d" && process.argv[3] === "course") {
deleteData(Course, "Course");
} else if (process.argv[2] === "-i" && process.argv[3] === "bootcamp") {
importData(Bootcamp, "Bootcamp", bootcamps);
} else if (process.argv[2] === "-d" && process.argv[3] === "bootcamp") {
deleteData(Bootcamp, "Bootcamp");
} else if (process.argv[2] === "-i" && process.argv[3] === "user") {
importData(User, "User", users);
} else if (process.argv[2] === "-d" && process.argv[3] === "user") {
deleteData(User, "User");
} else if (process.argv[2] === "-i" && process.argv[3] === "review") {
importData(Review, "Review", reviews);
} else if (process.argv[2] === "-d" && process.argv[3] === "review") {
deleteData(Review, "Review");
} else if (process.argv[2] === "-i" && process.argv[3] === "all") {
importAll();
} else if (process.argv[2] === "-d" && process.argv[3] === "all") {
deleteAll();
} else {
showError();
}