-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.js
73 lines (56 loc) · 1.8 KB
/
seed.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
const faker = require('faker');
const { hash } = require ('bcryptjs');
const User = require ('./src/app/models/User');
const Product = require ('./src/app/models/Product');
const File = require ('./src/app/models/File');
let usersIds = [];
let totalProducts = 10;
let totalUsers = 3;
async function createUsers(){
const users = [];
const password = await hash('1111', 8);
while (users.length < totalUsers) {
users.push({
name: faker.name.firstName(),
email: faker.internet.email(),
password,
cpf_cnpj: faker.random.number(9999999),
cep: faker.random.number(9999999),
address: faker.address.streetName()
});
}
const usersPromise = users.map(user => User.create(user));
usersIds = await Promise.all(usersPromise);
}
async function createProducts(){
let products = [];
while(products.length < totalProducts) {
products.push({
category_id: Math.ceil(Math.random() * 3),
user_id: usersIds[Math.floor(Math.random() * totalUsers)],
name: faker.name.title(),
description: faker.lorem.paragraph(Math.ceil(Math.random() * 10)),
old_price: faker.random.number(9999),
price: faker.random.number(9999),
quantity: faker.random.number(99),
status: Math.round(Math.random())
});
}
const productsPromise = products.map(product => Product.create(product));
productsIds = await Promise.all(productsPromise);
let files = [];
while(files.length < 50){
files.push({
name: faker.image.image(),
path: `public/images/placeholder.png`,
product_id: productsIds[Math.floor(Math.random() * totalProducts)]
});
}
const filesPromise = await files.map(file => File.create(file));
await Promise.all(filesPromise);
}
async function init() {
await createUsers();
await createProducts();
}
init();