-
Notifications
You must be signed in to change notification settings - Fork 0
/
seeder.js
53 lines (42 loc) · 1.1 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
import mongoose from "mongoose";
import dotenv from "dotenv";
import users from "./data/users.js";
import notes from "./data/notes.js";
import User from "./models/userModel.js";
import Note from "./models/noteModel.js";
import connectDB from "./config/db.js";
dotenv.config();
connectDB();
const importData = async () => {
try {
await Note.deleteMany();
await User.deleteMany();
const createdUsers = await User.insertMany(users);
const adminUser = createdUsers[0]._id;
const sampleNotes = notes.map((note) => {
return { ...note, user: adminUser };
});
await Note.insertMany(sampleNotes);
console.log("Data Imported!".green.inverse);
process.exit();
} catch (error) {
console.error(`${error}`.red.inverse);
process.exit(1);
}
};
const destroyData = async () => {
try {
await Note.deleteMany();
await User.deleteMany();
console.log("Data Destroyed!".red.inverse);
process.exit();
} catch (error) {
console.error(`${error}`.red.inverse);
process.exit(1);
}
};
if (process.argv[2] === "-d") {
destroyData();
} else {
importData();
}