-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseeder.js
48 lines (42 loc) · 1.17 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
const path = require("path");
const fs = require("fs");
require("dotenv").config({ path: path.join(__dirname, "configs", ".env") });
const mongoose = require("mongoose");
mongoose.connect(process.env.MONGO_URI);
const clients = JSON.parse(
fs.readFileSync(path.join(__dirname, "data", "clients.json"))
);
const projects = JSON.parse(
fs.readFileSync(path.join(__dirname, "data", "projects.json"))
);
const Client = require("./models/Client.model");
const Project = require("./models/Project.model");
const importData = async () => {
try {
await Client.create(clients);
await Project.create(projects);
console.log("Data imported ✅");
} catch (err) {
if(err.code==11000){
console.log(`Already data exists. First empty the database!\nrun: node seeder.js -d \nto delete db`)
}else{
console.error(err);
}
}
process.exit(0);
};
const deleteData = async () => {
try {
await Client.deleteMany();
await Project.deleteMany();
console.log("Data deleted ❌");
} catch (error) {
console.error(error);
}
process.exit(0);
};
if (process.argv[2] == "-i") {
importData();
} else if (process.argv[2] == "-d") {
deleteData();
}