-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseeders.js
57 lines (52 loc) · 1.41 KB
/
seeders.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
const mongoose = require("mongoose");
const user = require("./data/user");
const products = require("./data/products");
const User = require("./modals/userModal");
const Product = require("./modals/productsModal");
const Order = require("./modals/orderModal");
const db='mongodb+srv://amel1234:[email protected]/chitralishop?retryWrites=true&w=majority'
await mongoose.connect(db, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
compress:true
});
const importData = async () => {
try {
await Order.deleteMany();
await Product.deleteMany();
await User.deleteMany();
const createdUsers = await User.insertMany(user);
const AdminUser = createdUsers[0]._id;
const sampleProducts = products.map((product) => {
return {
...product,
user: AdminUser,
};
});
await Product.insertMany(sampleProducts);
console.log("Data Imported");
process.exit();
} catch (err) {
console.error(`${err}`);
process.exit(1);
}
};
const destroyData = async () => {
try {
await Order.deleteMany();
await Product.deleteMany();
await User.deleteMany();
console.log("Data Destroyed");
process.exit();
} catch (err) {
console.error(`${err}`);
process.exit(1);
}
};
if (process.argv[2] === "-i") {
importData();
} else if (process.argv[2] === "-d") {
destroyData();
}