-
Notifications
You must be signed in to change notification settings - Fork 0
/
populateDB.js
44 lines (39 loc) · 1.31 KB
/
populateDB.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
const fs = require("fs");
const mongoose = require("mongoose");
require("dotenv").config({ path: "./configs/config.env" });
const bootcampModel = require("./Models/Bootcamp");
const courseModel = require("./Models/Courses");
const userModel = require("./Models/user");
const reviewModel = require("./Models/Reviews");
mongoose.connect(process.env.DATABASE_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: true,
});
const bootcamps = JSON.parse(
fs.readFileSync(`${__dirname}/data/bootcamps.json`)
);
const courses = JSON.parse(fs.readFileSync(`${__dirname}/data/courses.json`));
const users = JSON.parse(fs.readFileSync(`${__dirname}/data/users.json`));
const reviews = JSON.parse(fs.readFileSync(`${__dirname}/data/reviews.json`));
const populateDB = async () => {
await bootcampModel.create(bootcamps);
await courseModel.create(courses);
await userModel.create(users);
await reviewModel.create(reviews);
};
const deleteDB = async () => {
await bootcampModel.deleteMany();
await courseModel.deleteMany();
await userModel.deleteMany();
await reviewModel.deleteMany();
};
if (process.argv[2] === "-i") {
populateDB();
console.log("inserted");
} else if (process.argv[2] === "-d") {
deleteDB();
console.log("Deleted");
} else if (process.argv[2] == "-t") {
console.log(bootcamps);
}