-
Notifications
You must be signed in to change notification settings - Fork 0
/
seeder.js
59 lines (50 loc) · 1.4 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
59
const fs = require('fs');
const mongoose = require('mongoose');
const dotenv = require('dotenv');
//Bring the env variables here
dotenv.config({ path: './config/config.env' });
//BRing up th Models
const ngoData = require('./models/ngo_models/ngoData');
const emp_department = require('./models/admin_models/emp_department');
//bring up the database connection
mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: true,
});
//Read the JSON Files
const ngomockData = JSON.parse(
fs.readFileSync(`${__dirname}/mockData/ngoData.json`, 'utf-8')
);
const emp_deparrtmentmockData = JSON.parse(
fs.readFileSync(`${__dirname}/mockData/emp_departmentData.json`, 'utf-8')
);
//import into DB
const importData = async () => {
try {
await emp_department.create(emp_deparrtmentmockData);
await ngoData.create(ngomockData);
console.log('DATA DUMPED IN DATABASE');
process.exit();
} catch (err) {
console.log(err);
}
};
//TO delete all the data
const deleteData = async () => {
try {
await emp_department.deleteMany();
await ngoData.deleteMany();
console.log('DATA WIPED OUT FROM DATABASE');
process.exit();
} catch (err) {
console.log(err);
}
};
//command check and procees with seeding
if (process.argv[2] === '-add') {
importData();
} else if (process.argv[2] === '-delete') {
deleteData();
}