-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed.js
More file actions
143 lines (133 loc) · 3.51 KB
/
seed.js
File metadata and controls
143 lines (133 loc) · 3.51 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import mongoose from "mongoose";
import dotenv from "dotenv";
import { category as Category } from "./models/category.js";
import { products as Product } from "./models/products.js";
dotenv.config();
const MONGO_URL = process.env.MONGO_URL;
const categories = [
{
name: "Topwear",
discription: "Stylish tops and shirts",
url: "/models/1.avif",
},
{
name: "Bottomwear",
discription: "Comfortable pants and skirts",
url: "/models/7.avif",
},
{
name: "Accessories",
discription: "Essential fashion accessories",
url: "/models/16.avif",
},
{
name: "New Arrivals",
discription: "Latest fashion trends",
url: "/models/9.avif",
},
];
const products = [
{
name: "SKIN LONG SLEEVE",
price: 45.0,
category_id: "Topwear",
discription: "A comfortable and stylish long sleeve skin-fit top.",
url: "/models/10.avif",
public_id: "prod_1",
},
{
name: "HONGOR UNISEX DRESS",
price: 125.0,
category_id: "Topwear",
discription: "Elegant unisex dress for all occasions.",
url: "/models/11.avif",
public_id: "prod_2",
},
{
name: "DOROTA TANK TOP",
price: 65.0,
category_id: "Topwear",
discription: "Lightweight tank top for summer days.",
url: "/models/12.avif",
public_id: "prod_3",
},
{
name: "STEVE BLACK",
price: 78.0,
category_id: "Bottomwear",
discription: "Classic black pants with a modern fit.",
url: "/models/6.avif",
public_id: "prod_4",
},
{
name: "KOTTI PANT",
price: 100.97,
category_id: "Bottomwear",
discription: "Premium quality pants for everyday wear.",
url: "/models/7.avif",
public_id: "prod_5",
},
{
name: "MESH BLAZER",
price: 150.0,
category_id: "Topwear",
discription: "Modern mesh blazer for a bold look.",
url: "/models/4.avif",
public_id: "prod_6",
},
{
name: "URBAN JACKET",
price: 95.0,
category_id: "Topwear",
discription: "Versatile jacket for urban exploration.",
url: "/models/13.avif",
public_id: "prod_7",
},
{
name: "SILK SCARF",
price: 35.0,
category_id: "Accessories",
discription: "Luxurious silk scarf to complement any outfit.",
url: "/models/14.avif",
public_id: "prod_8",
},
{
name: "LEATHER BELT",
price: 55.0,
category_id: "Accessories",
discription: "Genuine leather belt with a minimalist buckle.",
url: "/models/15.avif",
public_id: "prod_9",
},
{
name: "FLORAL MAXI DRESS",
price: 110.0,
category_id: "New Arrivals",
discription: "Beautiful floral maxi dress for spring.",
url: "/models/8.avif",
public_id: "prod_10",
},
];
async function seedDatabase() {
try {
await mongoose.connect(MONGO_URL);
console.log("Connected to MongoDB for seeding...");
// Clear existing data (optional, but good for demo)
await Category.deleteMany({});
await Product.deleteMany({});
console.log("Cleared existing categories and products.");
// Insert categories first
const createdCategories = await Category.insertMany(categories);
console.log(`Inserted ${createdCategories.length} categories.`);
// Insert products
const createdProducts = await Product.insertMany(products);
console.log(`Inserted ${createdProducts.length} products.`);
console.log("Database seeded successfully!");
} catch (error) {
console.error("Error seeding database:", error);
} finally {
await mongoose.connection.close();
console.log("MongoDB connection closed.");
}
}
seedDatabase();