forked from wecode-bootcamp-korea/38-1st-JustWeDoIt-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
faker.js
90 lines (78 loc) · 2.22 KB
/
faker.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
require("dotenv").config();
const { faker } = require('@faker-js/faker');
const { DataSource } = require('typeorm');
const database = new DataSource({
type: process.env.TYPEORM_CONNECTION,
host: process.env.TYPEORM_HOST,
port: process.env.TYPEORM_PORT,
username: process.env.TYPEORM_USERNAME,
password: process.env.TYPEORM_PASSWORD,
database: process.env.TYPEORM_DATABASE
})
function createRandomUser() {
return {
name: faker.name.fullName(),
email: faker.internet.email(),
password: faker.internet.password(),
phone_number: faker.phone.number('010-####-####'),
address: faker.address.streetAddress(),
resident_number_front: faker.random.numeric(6),
resident_number_back: faker.random.numeric(1, { bannedDigits: ['0', '5', '6', '7', '8', '9']}),
point: faker.random.numeric(7)
}
}
function createRandomProduct() {
return {
name: faker.commerce.productName(),
description: faker.commerce.productDescription(),
price : faker.commerce.price(10000, 100000, 0)
}
}
const postMockUserData = async (name, email, password, phone_number, address, resident_number_front, resident_number_back, point) => {
return await database.query(`
INSERT INTO users(
name,
email,
password,
phone_number,
address,
resident_number_front,
resident_number_back,
point
) VALUES (?, ?, ?, ?, ?, ?, ?, ?);`,
[ name, email, password, phone_number, address, resident_number_front, resident_number_back, point ]
);
}
const postMockProductData = async (name, description, price) => {
return await database.query(`
INSERT INTO products(
name,
description,
price
) VALUES (?, ?, ?);`,
[ name, description, price ]
);
}
const makeMockData = async() => {
await database.initialize()
for (let i = 0; i < 100; i++) {
const user = createRandomUser();
const product = createRandomProduct();
postMockUserData(
user.name,
user.email,
user.password,
user.phone_number,
user.address,
user.resident_number_front,
user.resident_number_back,
user.point,
)
postMockProductData(
product.name,
product.description,
product.price
)
}
}
makeMockData();