-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulate.ts
85 lines (74 loc) · 2.05 KB
/
populate.ts
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
import { faker } from "@faker-js/faker";
import mongoose from "mongoose";
const mongoUrl =
"mongodb+srv://taskapp:[email protected]/React-Ecommerce";
const dbConnect = async () => {
if (mongoose.connection.readyState >= 1) {
return;
}
try {
await mongoose.connect(mongoUrl);
console.log("Connected to DB");
} catch (e) {
console.log(
`Initial Distribution API Database connection error occur -`,
e
);
}
};
dbConnect();
const couponSchema = new mongoose.Schema({
code: { type: String, required: true, unique: true, trim: true },
description: { type: String },
discountType: { type: String, required: true },
amount: { type: Number, required: true },
expiryDate: { type: Date, required: true },
minimumAmount: { type: Number, required: true },
// Add other properties as needed
});
const CouponModel = mongoose.model("Coupon", couponSchema);
async function generateFakeData() {
const numberOfCoupons = 50;
for (let i = 0; i < numberOfCoupons; i++) {
const code = faker.string.alphanumeric(8);
console.log(code, "這是code");
const description = faker.lorem.sentence();
const discountType = faker.helpers.arrayElement([
"Percentage",
"Fixed Amount",
]);
const amount = faker.number.int({ min: 5, max: 50 });
const expiryDate = faker.date.future();
const minimumAmount = faker.number.int({ min: 100, max: 500 });
console.log(
{
code,
description,
discountType,
amount,
expiryDate,
minimumAmount,
relatedCoupons: [],
},
"這是果"
);
const coupon = new CouponModel({
code,
description,
discountType,
amount,
expiryDate,
minimumAmount,
relatedCoupons: [],
});
await coupon.save();
}
console.log("Fake data generated successfully.");
}
// generateFakeData();
const deleteType = async () => {
await CouponModel.deleteMany({
discountType: { $in: ["debate", "percentage", "rebate"] },
});
};
deleteType();