-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
105 lines (90 loc) · 2.54 KB
/
db.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const fs = require('fs');
const mongoose = require('mongoose');
const colors = require('colors');
const dotenv = require('dotenv');
// Load env vars
dotenv.config({ path: './config/config.env' });
// Load models
const Category = require('./models/Category');
const Location = require('./models/Location');
const User = require('./models/User');
const Place = require('./models/Place');
// Connect to DB
mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true
});
// Read JSON files
const category = JSON.parse(
fs.readFileSync(`${__dirname}/_data/category.json`, 'utf-8')
);
const location = JSON.parse(
fs.readFileSync(`${__dirname}/_data/location.json`, 'utf-8')
);
const users = JSON.parse(
fs.readFileSync(`${__dirname}/_data/users.json`, 'utf-8')
);
const place = JSON.parse(
fs.readFileSync(`${__dirname}/_data/place.json`, 'utf-8')
);
// Import into DB
const importData = async () => {
try {
await Category.create(category);
await Location.create(location);
await User.create(users);
await Place.create(place);
console.log('Data Imported Successfuly'.green.inverse);
process.exit();
} catch (err) {
console.error(err);
}
};
// Delete Data
const deleteData = async () => {
try {
await Category.deleteMany();
await Location.deleteMany();
await User.deleteMany();
await Place.deleteMany();
console.log('Data Destroyed Successfuly'.red.inverse);
process.exit();
} catch (err) {
console.error(err);
}
};
if (process.argv[2] === '-i') {
importData();
} else if (process.argv[2] === '-d') {
deleteData();
}
// const connectDB = async () => {
// try {
// if (process.env.NODE_ENV === 'production') {
// await mongoose.connect(
// `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@-.mongodb.net/tgr?retryWrites=true&w=majority`,
// {
// useNewUrlParser: true,
// useUnifiedTopology: true,
// useCreateIndex: true,
// useFindAndModify: false
// }
// );
// console.log('MongoDB Atlas Connected');
// } else {
// await mongoose.connect('mongodb://localhost:27017/tgr', {
// useNewUrlParser: true,
// useUnifiedTopology: true,
// useCreateIndex: true,
// useFindAndModify: false
// });
// console.log('MongoDB Localhost Connected');
// }
// } catch (err) {
// console.error(err.message);
// // Exit process with failure
// process.exit(1);
// }
// };