-
Notifications
You must be signed in to change notification settings - Fork 0
/
import-dev-data.js
63 lines (54 loc) · 1.35 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
55
56
57
58
59
60
61
62
63
const fs = require('fs');
const mongoose = require('mongoose');
const dotenv = require('dotenv');
// eslint-disable-next-line import/no-dynamic-require
const Tour = require(`${__dirname}/models/tourModel`);
dotenv.config({ path: './config.env' });
// console.log(process.env);
const DB = process.env.DATABASE.replace(
'<password>',
process.env.DATABASE_PASSWORD
);
mongoose
.connect(DB, {
useUnifiedTopology: true,
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
})
.then(() => {
console.log('DB connection successful!');
});
// READ JSON FILE
const tours = JSON.parse(
fs.readFileSync(
`${__dirname}/starter/dev-data/data/tours-simple.json`,
'utf-8'
)
);
// IMPORT DATA INTO DB
const importData = async () => {
try {
await Tour.create(tours);
console.log('Data successfully loaded');
} catch (err) {
console.log(err);
}
process.exit();
};
// DELETE ALL DATA FROM DB
const deleteData = async () => {
try {
await Tour.deleteMany();
console.log('Data successfully deleted');
process.exit();
} catch (err) {
console.log(err);
}
};
if (process.argv[2] === '--import') {
importData();
} else if (process.argv[2] === '--delete') {
deleteData();
}
console.log(process.argv);