-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadData.js
More file actions
42 lines (35 loc) · 1.09 KB
/
Copy pathloadData.js
File metadata and controls
42 lines (35 loc) · 1.09 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
const Tour = require('./model/tourModel');
const fs = require('fs');
const User = require('./model/userModel');
const Review = require('./model/reviewModel');
const importData = async ()=>{
try {
const tours = JSON.parse(fs.readFileSync(`${__dirname}/dev-data/data/tours.json`));
const users = JSON.parse(fs.readFileSync(`${__dirname}/dev-data/data/users.json`));
const reviews = JSON.parse(fs.readFileSync(`${__dirname}/dev-data/data/reviews.json`));
await Tour.create(tours);
await User.create(users, {validateBeforeSave: false});
await Review.create(reviews);
console.log('Files uploaded seccessfully');
process.exit();
} catch (error) {
console.log(error);
}
};
const deleteData = async ()=>{
try {
await Tour.deleteMany();
await User.deleteMany();
await Review.deleteMany();
console.log('data successfully deleted');
process.exit();
} catch (error) {
console.log(error);
}
}
if(process.argv[2]=== "--import"){
importData();
}
if(process.argv[2] === "--delete"){
deleteData();
}