-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseeder.js
More file actions
48 lines (38 loc) · 1.18 KB
/
seeder.js
File metadata and controls
48 lines (38 loc) · 1.18 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
const dotenv = require("dotenv");
const mongoose = require("mongoose");
const Product = require("./models/product.model");
const User = require("./models/user.model");
const Cart = require("./models/cart.model");
const Order = require("./models/order.model");
const Checkout = require("./models/checkout.model");
const products = require("./data/products");
dotenv.config();
mongoose.connect(process.env.MONGO_URI);
const seedData = async () => {
try {
// Clear Existing data
await Product.deleteMany();
await User.deleteMany({});
await Cart.deleteMany({});
await Order.deleteMany({});
await Checkout.deleteMany({});
// Create a default admin user
const createdUser = await User.create({
name: "Admin",
email: process.env.ADMIN_EMAIL,
password: process.env.ADMIN_PASSWORD,
role: "admin",
});
// Assign the default admin user to each product
const userID = createdUser._id;
const sampleProducts = products.map((product) => {
return { ...product, user: userID };
});
await Product.insertMany(sampleProducts);
process.exit();
} catch (error) {
console.log(error);
process.exit(1);
}
};
seedData();