-
Notifications
You must be signed in to change notification settings - Fork 24
/
seed.js
69 lines (56 loc) · 1.83 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
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 usersId = [];
let totalUsers = 3;
let totalProducts = 10;
async function createUsers() {
const users = [];
const password = await hash('123456', 8);
while (users.length < totalUsers) {
users.push({
name: faker.name.firstName(),
email: faker.internet.email().toLocaleLowerCase(),
password,
cpf_cnpj: faker.datatype.number(99999999),
cep: faker.datatype.number(99999999),
address: faker.address.streetName()
});
}
const usersPromise = users.map(user => User.create(user));
usersId = await Promise.all(usersPromise);
}
async function createProducts() {
const products = [];
while (products.length < totalProducts) {
products.push({
category_id: Math.ceil(Math.random() * 2),
user_id: usersId[Math.floor(Math.random() * totalUsers)],
name: faker.name.title(),
description: faker.lorem.paragraph(Math.ceil(Math.random() * 10)),
old_price: faker.datatype.number(9999),
price: faker.datatype.number(9999),
quantity: faker.datatype.number(99),
status: Math.round(Math.random())
});
}
const productsPromise = products.map(product => Product.create(product));
let 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 = files.map(file => File.create(file));
await Promise.all(filesPromise);
}
async function init() {
await createUsers();
await createProducts();
}
init();