-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathseeder.js
More file actions
51 lines (43 loc) · 1.31 KB
/
seeder.js
File metadata and controls
51 lines (43 loc) · 1.31 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
43
44
45
46
47
48
49
50
51
const fs = require('fs');
const colors = require('colors');
const dotenv = require('dotenv').config();
const Database = require('./config/db');
// Load models
const Student = require('./models/studentModel');
const User = require('./models/userModel');
const Incharge = require('./models/inchargeModel');
const Score = require('./models/scoreModel');
// Read JSON files
const students = JSON.parse(fs.readFileSync(`${__dirname}/resources/data/students.json`, 'utf-8'));
const users = JSON.parse(fs.readFileSync(`${__dirname}/resources/data/users.json`, 'utf-8'));
const incharges = JSON.parse(fs.readFileSync(`${__dirname}/resources/data/incharges.json`, 'utf-8'));
// Import into DB
const importData = async () => {
try {
await Student.create(students);
await User.create(users);
await Incharge.create(incharges);
console.log('Data Imported...'.green);
process.exit();
} catch (err) {
console.log(err);
}
};
// Delete Data
const deleteData = async () => {
try {
await Student.deleteMany();
await User.deleteMany();
await Incharge.deleteMany();
await Score.deleteMany();
console.log('Data Destroyed...'.red);
process.exit();
} catch (err) {
console.log(err);
}
};
if (process.argv[2] === '-i') {
importData();
} else if (process.argv[2] === '-d') {
deleteData();
}