-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathseed.js
122 lines (102 loc) · 3.5 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const faker = require('faker');
const { hash } = require('bcryptjs');
const User = require('./src/app/models/User');
const File = require('./src/app/models/File');
const Chef = require('./src/app/models/Chef');
const Recipe = require('./src/app/models/Recipe');
const RecipeFile = require('./src/app/models/RecipeFile');
function createFiles(num, placeholder) {
const files = [];
while (files.length < num) {
files.push({
name: faker.image.image(),
path: `public/images/${placeholder}.png`
});
}
return files;
}
let totalUsers = 6,
totalChefs = 8,
totalRecipes = 9,
usersId,
chefsId;
async function createUsers() {
const users = [];
const password = await hash('rocket', 8);
while (users.length < totalUsers) {
users.push({
name: faker.name.firstName(),
email: faker.internet.email().toLowerCase(),
password,
is_admin: Math.round(Math.random())
});
}
const usersPromise = users.map(user => User.create(user));
usersId = await Promise.all(usersPromise);
}
async function createChefs() {
const chefs = [];
const files = createFiles(totalChefs, 'chef_placeholder');
const filesPromise = files.map(file => File.create(file));
const filesId = await Promise.all(filesPromise);
for (let fileIndex = 0; chefs.length < totalChefs; fileIndex++) {
chefs.push({
name: faker.name.firstName(),
file_id: filesId[fileIndex]
});
}
const chefsPromise = chefs.map(chef => Chef.create(chef));
chefsId = await Promise.all(chefsPromise);
}
async function createRecipes() {
try {
const recipes = [];
const ingredients = [];
const preparation = [];
for (let i = 0; i < 5; i++) {
ingredients.push(faker.lorem.words(Math.ceil(Math.random() * 6)));
preparation.push(faker.lorem.words(Math.ceil(Math.random() * 6)));
}
while (recipes.length < totalRecipes) {
recipes.push({
chef_id: chefsId[Math.floor(Math.random() * totalChefs)],
user_id: usersId[Math.floor(Math.random() * totalUsers)],
title: faker.commerce.productName(),
ingredients,
preparation,
information: faker.lorem.paragraph(Math.ceil(Math.random() * 10))
});
}
const recipesPromise = recipes.map(recipe => Recipe.create(recipe));
const recipesId = await Promise.all(recipesPromise);
const files = createFiles(45, 'recipe_placeholder');
const filesPromise = files.map(file => File.create(file));
const filesId = await Promise.all(filesPromise);
const recipeFiles = [];
let fileIndex = 0,
recipeIndex = totalRecipes - 1,
recipeImageLimit = 5;
while (recipeFiles.length < 45) {
for (let i = 0; i < recipeImageLimit; i++) {
recipeFiles.push({
recipe_id: recipesId[recipeIndex],
file_id: filesId[fileIndex]
});
fileIndex++;
}
recipeIndex--;
}
const recipeFilesPromise = recipeFiles.map(recipe_file =>
RecipeFile.create(recipe_file)
);
await Promise.all(recipeFilesPromise);
} catch (error) {
console.error(error);
}
}
async function init() {
await createUsers();
await createChefs();
await createRecipes();
}
init();