-
Notifications
You must be signed in to change notification settings - Fork 0
/
dummy-generator.js
52 lines (45 loc) · 1.48 KB
/
dummy-generator.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
const fs = require("fs");
const dotenv = require("dotenv");
const connectDatabase = require("./helpers/database/connect-database");
const Question = require("./models/Question");
const Answer = require("./models/Answer");
const User = require("./models/User");
const path = "./dummy/";
const users = JSON.parse(fs.readFileSync(path + "users.json").toString());
const questions = JSON.parse(fs.readFileSync(path + "questions.json").toString());
const answers = JSON.parse(fs.readFileSync(path + "answers.json").toString());
// Environment Variables
dotenv.config({ path: "./config/env/config.env" });
// MongoDb Connection
connectDatabase();
const importAllData = async function () {
try {
await User.create(users);
await Question.create(questions);
await Answer.create(answers);
console.log("Import Process Successful");
} catch (err) {
console.log(err);
console.err("There is a problem with import process");
} finally {
process.exit();
}
};
const deleteAllData = async function () {
try {
await User.deleteMany();
await Question.deleteMany();
await Answer.deleteMany();
console.log("Delete Process Successful");
} catch (err) {
console.log(err);
console.err("There is a problem with delete process");
} finally {
process.exit();
}
};
if (process.argv[2] === "--import") {
importAllData();
} else if (process.argv[2] === "--delete") {
deleteAllData();
}