-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathseed.js
161 lines (126 loc) · 4.16 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
const faker = require('faker')
faker.locale= 'pt_BR'
const { hash } = require('bcryptjs')
const { chefs: dataChefs, recipes: dataRecipes } = require('./backupfoodfy.json')
const Users = require('./src/app/models/users')
const Recipes = require('./src/app/models/recipes')
const RecipeFiles = require('./src/app/models/filesRecipes')
const Chefs = require('./src/app/models/chefs')
const ChefFiles = require('./src/app/models/filesChefs')
const limitChefs = 3
const limitRecipes = 8
const limitUsers = 3
const chefsIds = []
let usersIds = []
async function createChef(chef, file) {
const fileId = await ChefFiles.create(file)
chef.file_id = fileId
const chefId = await Chefs.create(chef)
return chefId
}
async function createRecipe(recipe, file) {
const recipeId = await Recipes.create(recipe)
const fileId = await RecipeFiles.create(file)
await RecipeFiles.createRecipeFiles(recipeId, fileId)
return recipeId
}
async function createUsers() {
let iteration = 0
const password = await hash('123', 8)
let user = {
name: faker.name.findName().replace(/(')/g, "$1'"),
email: 'adminfoodfy@gmail.com',
password,
is_admin: true
}
let userId = await Users.create(user)
usersIds.push(userId)
while (iteration < limitUsers) {
const name = faker.name.findName().replace(/(')/g, "$1'")
user = {
name,
email: faker.internet.email(name),
password,
is_admin: faker.random.boolean()
}
userId = await Users.create(user)
usersIds.push(userId)
iteration++
}
}
async function createOriginalChefs() {
let iteration = 0
while (iteration < dataChefs.length) {
const { name, path } = dataChefs[iteration]
const file = {
name: `${Date.now().toString()}-${name}`,
path
}
const chef = { name }
const chefId = await createChef(chef, file)
chefsIds.push(chefId)
iteration++
}
}
async function createOriginalRecipes() {
let iteration = 0
while (iteration < dataRecipes.length) {
const { title, path, ingredients, preparation, information } = dataRecipes[iteration]
const recipe = {
title,
chef_id: chefsIds[Math.floor(Math.random() * chefsIds.length)] || 1,
user_id: usersIds[Math.floor(Math.random() * usersIds.length)] || 1,
ingredients,
preparation,
information
}
const file = {
name: `${Date.now().toString()}-${title}`,
path
}
await createRecipe(recipe, file)
iteration++
}
}
async function createChefs() {
let iteration = 0
while (iteration < limitChefs) {
const name = faker.name.findName().replace(/(')/g, "$1'")
const file = {
name: `${Date.now().toString()}-${name}`,
path: 'public/images/recipes-and-chefs/chefs.jpg'
}
const chef = { name }
const chefId = await createChef(chef, file)
chefsIds.push(chefId)
iteration++
}
}
async function createRecipes() {
let iteration = 0
while (iteration < limitRecipes) {
const title = faker.name.title().replace(/(')/g, "$1'")
const recipe = {
title,
chef_id: chefsIds[Math.floor(Math.random() * chefsIds.length)] || 1,
user_id: usersIds[Math.floor(Math.random() * usersIds.length)] || 1,
ingredients: faker.lorem.paragraph(Math.ceil(Math.random() * 5)).split('. '),
preparation: faker.lorem.paragraph(Math.ceil(Math.random() * 4)).split('. '),
information: faker.lorem.paragraph(Math.floor(Math.random() * 3))
}
const file = {
name: `${Date.now().toString()}-${title}`,
path: 'public/images/recipes-and-chefs/recipes.png'
}
await createRecipe(recipe, file)
iteration++
}
}
async function init() {
await createUsers()
await createOriginalChefs()
await createOriginalRecipes()
await createChefs()
await createRecipes()
}
init()