-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.js
120 lines (89 loc) · 2.84 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
const { hash } = require('bcrypt')
const faker = require('faker')
const User = require('./src/app/models/User')
const Chef = require('./src/app/models/Chef')
const File = require('./src/app/models/File')
const Recipe = require('./src/app/models/Recipe')
const Base = require('./src/app/models/Base')
let usersIds = []
let totalUsers = 5
let totalChefs = 4
let totalRecipes = 20
let totalFiles = totalChefs + totalRecipes
async function createUsers() {
const users = []
const password = await hash('1', 8)
while (users.length < totalUsers) {
users.push({
name: faker.name.firstName(),
email: faker.internet.email(),
password,
is_admin: Math.round(Math.random()),
})
}
const usersPromise = users.map(user => User.create(user))
usersIds = await Promise.all(usersPromise)
}
async function createChefs() {
const chefs = []
for (let i = 1; chefs.length < totalChefs; i++) {
chefs.push({
name: faker.name.firstName(),
file_id: i
})
}
const chefsPromise = chefs.map(chef => Chef.create(chef))
chefsIds = await Promise.all(chefsPromise)
}
async function createFiles() {
let files = []
while (files.length < totalFiles) {
files.push({
name: faker.image.image(),
path: `/images/placeholder.png`,
})
}
const filesPromise = files.map(file => File.create(file))
filesIds = await Promise.all(filesPromise)
}
async function createRecipes() {
let recipes = []
while (recipes.length < totalRecipes) {
recipes.push({
chef_id: Math.ceil(Math.random() * 3),
user_id: usersIds[Math.floor(Math.random() * totalUsers)],
title: faker.name.title(),
ingredients: `{${faker.lorem.paragraph(Math.ceil(Math.random() * 1)).split(" ")}}`,
preparation: `{${faker.lorem.paragraph(Math.ceil(Math.random() * 1)).split(" ")}}`,
information: faker.lorem.paragraph(Math.ceil(Math.random() * 5))
})
}
const recipesPromise = recipes.map(recipe => Recipe.create(recipe))
recipesIds = await Promise.all(recipesPromise)
}
async function createPivotTableRelations() {
const relations = []
let recipesCount = 0
for (let i = totalChefs + 1; relations.length < totalRecipes; i++) {
recipesCount++
relations.push({
recipe_id: recipesCount,
file_id: i
})
}
const relationsPromises = relations.map(relation => {
Base.init({
table: 'recipes_files'
})
Base.create(relation)
})
await Promise.all(relationsPromises)
}
async function init() {
await createUsers()
await createFiles()
await createChefs()
await createRecipes()
await createPivotTableRelations()
}
init()