-
Notifications
You must be signed in to change notification settings - Fork 0
/
import-dev-data.js
54 lines (44 loc) · 1.15 KB
/
import-dev-data.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
const dotenv = require("dotenv");
const mongoose = require("mongoose");
const fs = require("fs");
const Tour = require("./Models/tourModel");
// Reading environment variables
dotenv.config({ path: "./config.env" })
// Connecting with the database
mongoose.connect(process.env.DATABASE, {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log("*** Connected to the database ***");
}).catch((err) => {
console.log("Error : ", err)
})
// Reading file
const tours = JSON.parse(fs.readFileSync(`${__dirname}/dev-data/data/tours.json`, "utf-8"));
const importData = async () => {
try {
await Tour.create(tours);
console.log("Data successfully loaded ");
}
catch (err) {
console.log("Error : ", err)
}
process.exit();
}
// Delete all data from the database
const deleteData = async () => {
try {
await Tour.deleteMany();
console.log("Data successfully deleted ! ");
}
catch (err) {
console.log("Error : ", err)
}
process.exit();
}
if (process.argv[2] == "--import") {
importData();
}
else if (process.argv[2] == "--delete") {
deleteData();
}