-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseeder.js
47 lines (43 loc) · 1.17 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
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const fs = require("fs");
const Tour = require("./models/tourModel");
const User = require("./models/userModel");
const Review = require("./models/reviewModel");
const connectDB = require("./db/config");
const tours = require("./dev-data/data/tours");
const users = require("./dev-data/data/users");
const reviews = require("./dev-data/data/reviews");
dotenv.config();
connectDB();
const importData = async () => {
try {
await Tour.create(tours);
await User.create(users, { validateBeforeSave: false });
await Review.create(reviews);
console.log("Data Imported");
process.exit();
} catch (error) {
console.error(error);
process.exit(1);
}
};
const destroyData = async () => {
try {
await Tour.deleteMany();
await User.deleteMany();
await Review.deleteMany();
console.log("Data Destroyed");
process.exit();
} catch (error) {
console.error(error);
process.exit(1);
}
};
if (process.argv[2] === "-i") {
importData();
} else if (process.argv[2] === "-d") {
destroyData();
} else {
console.log("Please give correct inputs -i/-d to import/destroy data");
}