-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseeds.js
148 lines (117 loc) · 3.44 KB
/
seeds.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const faker = require('faker-br');
const { hash } = require('bcryptjs');
const { arrayDB, validationOfRecipeInputs } = require('./src/lib/utils');
const User = require('./src/app/models/User');
const Recipe = require('./src/app/models/Recipe');
const File = require('./src/app/models/File');
const RecipesFile = require('./src/app/models/RecipesFiles');
const Chef = require('./src/app/models/Chef');
let usersId = [];
let chefsId = [];
let recipesId = [];
async function createUsers() {
let users = [];
// the password for all the users is '123'
const password = await hash('123', 8);
while (users.length < 3) {
users.push({
name: faker.name.firstName(),
email: faker.internet.email(),
password: password,
is_admin: false,
});
}
//Creating a user that is Admin
users.push({
name: faker.name.firstName(),
email: faker.internet.email(),
password: password,
is_admin: true,
});
const usersPromise = users.map((user) => User.create(user));
usersId = await Promise.all(usersPromise);
}
async function createFiles() {
let files = [];
//let count = 1;
/*
If you change the number of files generated you'll have to change the
functions 'createChefs' and 'createRecipes' cause they can't have
the same file_id values.
*/
while (files.length < 20) {
files.push({
name: faker.image.image(),
path: `public/img-app/placeholder1.png`,
});
//count++;
}
const filesPromise = files.map((file) => File.create(file));
filesId = await Promise.all(filesPromise);
}
async function createChefs() {
let chefs = [];
/*
Now I need to guarantee that each chef has different file_id
*/
let count = 1;
while (chefs.length < 5) {
chefs.push({
name: faker.name.firstName(),
file_id: count,
});
count++;
}
const chefsPromise = chefs.map((chef) => Chef.create(chef));
chefsId = await Promise.all(chefsPromise);
}
async function createRecipes() {
let recipes = [];
let ingredient = [];
let preparation = [];
ingredient.push(faker.random.word());
preparation.push(faker.random.word());
//creates 20 recipes
while (recipes.length < 15) {
const randomChefId = Math.ceil(Math.random() * chefsId.length);
const randomUserId = Math.ceil(Math.random() * usersId.length);
recipes.push({
chef_id: randomChefId,
user_id: randomUserId,
title: faker.name.title(),
//with ArrayDB the return result will be in the format of array for the Postgres
ingredients: arrayDB(ingredient),
preparation: arrayDB(preparation),
information: faker.lorem.paragraph(Math.ceil(Math.random() * 10)),
});
}
const recipesPromise = recipes.map((recipe) => Recipe.create(recipe));
recipesId = await Promise.all(recipesPromise);
}
async function createRecipeFiles() {
let recipeFiles = [];
let count = 6;
while (recipeFiles.length < recipesId.length) {
recipesId.forEach((recipeId) => {
//This way each recipe will have a different file_id for them and the chefs
const obj = {
recipe_id: recipeId,
file_id: count,
};
recipeFiles.push(obj);
count++;
});
}
const recipesPromise = recipeFiles.map((recipeFileObj) =>
RecipesFile.create(recipeFileObj)
);
await Promise.all(recipesPromise);
}
async function init() {
await createUsers();
await createFiles();
await createChefs();
await createRecipes();
await createRecipeFiles();
}
init();