-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-data.js
101 lines (82 loc) · 2.33 KB
/
generate-data.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
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
const faker = require("faker");
const fs = require("fs");
// Set locale to use Vietnamese
faker.locale = "vi";
const randomCategoryList = n => {
if (n <= 0) return [];
const categoryList = [];
// loop and push category
Array.from(new Array(n)).forEach(() => {
const category = {
id: faker.datatype.uuid(),
name: faker.commerce.department(),
createdAt: Date.now(),
updatedAt: Date.now()
};
categoryList.push(category);
});
return categoryList;
};
const randomProductList = (categoryList, numberOfProducts) => {
if (numberOfProducts <= 0) return [];
const productList = [];
// random data
for (const category of categoryList) {
Array.from(new Array(numberOfProducts)).forEach(() => {
const product = {
categoryId: category.id,
id: faker.datatype.uuid(),
name: faker.commerce.productName(),
color: faker.commerce.color(),
price: Number.parseFloat(faker.commerce.price()),
description: faker.commerce.productDescription(),
createdAt: Date.now(),
updatedAt: Date.now(),
thumbnailUrl: faker.image.imageUrl(400, 400)
};
productList.push(product);
});
}
return productList;
};
const randomReferralList = n => {
if (n <= 0) return [];
const referralList = [];
// loop and push category
Array.from(new Array(n)).forEach(() => {
const referral = {
id: faker.datatype.uuid(),
businessName: faker.company.companyName(),
ownerName: faker.name.findName(),
ownerNumber: faker.phone.phoneNumber(),
status: "pending",
isPaid: faker.datatype.boolean(),
amount: Number.parseFloat(faker.finance.amount()),
rejectNote: faker.lorem.sentence(),
createdAt: Date.now(),
updatedAt: Date.now()
};
referralList.push(referral);
});
return referralList;
};
// IIFE
(() => {
// random data
const categoryList = randomCategoryList(4);
const productList = randomProductList(categoryList, 5);
const referralList = randomReferralList(10);
// prepare db object
const db = {
referrals: referralList,
categories: categoryList,
products: productList,
profile: {
name: "Po"
}
};
// write db object to db.json
fs.writeFile("db.json", JSON.stringify(db), () => {
console.log("Generate data successfully!");
});
})();