-
Notifications
You must be signed in to change notification settings - Fork 28
/
seeder.js
58 lines (48 loc) · 1.14 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
48
49
50
51
52
53
54
55
56
57
58
const fs = require('fs');
const mongoose = require('mongoose');
// Load env vars
require('./src/utils/env');
// Load models
const Request = require('./src/models/Request');
const User = require('./src/models/User');
// Connect to DB
mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
});
// Read JSON files
const requests = JSON.parse(
fs.readFileSync(`${__dirname}/src/_data/requests.json`, 'utf-8')
);
const users = JSON.parse(
fs.readFileSync(`${__dirname}/src/_data/users.json`, 'utf-8')
);
// Import into DB
const importData = async () => {
try {
await Request.create(requests);
await User.create(users);
console.log('Data Imported...');
process.exit();
} catch (err) {
console.error(err);
}
};
// Delete data
const deleteData = async () => {
try {
await Request.deleteMany();
await User.deleteMany();
console.log('Data Destroyed...');
process.exit();
} catch (err) {
console.error(err);
}
};
if (process.argv[2] === '-i') {
importData();
} else if (process.argv[2] === '-d') {
deleteData();
}