-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
66 lines (57 loc) · 1.76 KB
/
Copy pathtest.js
File metadata and controls
66 lines (57 loc) · 1.76 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
const mongoose = require("mongoose");
// MongoDB URI
const MONGO_URI = "mongodb+srv://test:testuser123@cluster0.ln0so.mongodb.net/maindb?retryWrites=true&w=majority&appName=Cluster0";
// Define the ShippingRate schema inline
const shippingRateSchema = new mongoose.Schema({
minDistance: {
type: Number,
required: true,
},
maxDistance: {
type: Number,
required: true,
},
charge: {
type: Number,
required: true,
},
estimatedDelivery: {
type: Number,
required: true,
},
});
// Create the ShippingRate model
const ShippingRate = mongoose.model("ShippingRate", shippingRateSchema);
// Connect to MongoDB
mongoose.connect(MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const rates = [
{ minDistance: 0, maxDistance: 2, charge: 0, estimatedDelivery: 1 }, // Free delivery under 2 km
{ minDistance: 2, maxDistance: 5, charge: 50, estimatedDelivery: 2 },
{ minDistance: 5, maxDistance: 10, charge: 75, estimatedDelivery: 3 },
{ minDistance: 10, maxDistance: 20, charge: 100, estimatedDelivery: 4 },
{ minDistance: 20, maxDistance: 50, charge: 150, estimatedDelivery: 5 },
];
async function populateRates() {
try {
// Connect to the database
await mongoose.connection;
console.log("Connected to MongoDB");
// Clear existing shipping rates
await ShippingRate.deleteMany({});
console.log("Cleared existing shipping rates");
// Insert new rates
await ShippingRate.insertMany(rates);
console.log("Shipping rates populated successfully");
} catch (error) {
console.error("Error populating shipping rates:", error);
} finally {
// Close the connection
await mongoose.connection.close();
console.log("MongoDB connection closed");
}
}
// Run the function
populateRates();