-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed.js
More file actions
56 lines (48 loc) · 1.55 KB
/
seed.js
File metadata and controls
56 lines (48 loc) · 1.55 KB
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
const faker = require('faker');
const { Cart, Category, Product } = require('./models');
const db = require('./db');
const createCategory = () => Category.create({
name: faker.commerce.department(),
});
const createCategories = (n) => {
const categoriesPromises = [];
for (let i = 0; i < n; i += 1) {
categoriesPromises.push(createCategory())
}
return Promise.all(categoriesPromises);
}
const createProduct = (categoryId) => Product.create({
name: faker.commerce.productName(),
description: faker.lorem.paragraph(),
image: faker.image.image(),
categoryId,
availability: !!Math.round(Math.random()),
price: faker.commerce.price(),
});
const createCatalogue = (n, categories) => {
const productPromises = [];
for (let i = 0; i < n; i += 1) {
const categoryId = faker.random.arrayElement(categories).id;
productPromises.push(createProduct(categoryId));
}
return Promise.all(productPromises);
}
const createCartItem = (productId) => Cart.create({
quantity: faker.random.number(9) + 1,
productId,
});
const createCart = (n, products) => {
const cartItemPromises = [];
for (let i = 0; i < n; i += 1) {
const productId = faker.random.arrayElement(products).id;
cartItemPromises.push(createCartItem(productId));
}
return Promise.all(cartItemPromises);
}
const generateShop = (nCat, nProd, nCart) =>
db.sync({ force: true })
.then(() => createCategories(nCat))
.then(categories => createCatalogue(nProd, categories))
.then(products => createCart(nCart, products))
.catch(console.log);
generateShop(5, 100, 5);