-
Notifications
You must be signed in to change notification settings - Fork 0
/
testSeedPractice.js
154 lines (135 loc) · 4.1 KB
/
testSeedPractice.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
const db = require('../server/db')
const {
User,
Camper,
Campgound,
Campsite,
Amenity,
Reservation
} = require('./server/db/models/index')
const {campSiteSeedData} = require('./campSiteSeedData')
const Chance = require('chance')
const chance = new Chance(95698435)
const amenityQuantity = 3
const reservationQuantity = 500
const campsiteReservationsQuantity = 500
const numberOfCampers = 500
const numberOfCampsites = 300
// pickset, shuffle
const startTimeRes = chance.date()
chance.mixin({
reservation: () => ({
startTime: chance.date(),
endTime: startTimeRes + chance.natural({min: 1, max: 14}),
partyNumber: chance.integer({min: 1, max: 7}),
campsiteId: chance.unique(
chance.natural({min: 1, max: reservationQuantity})
)
})
})
chance.mixin({
amenity: () => ({
category: chance.pickset(['Power', 'Sewege', 'Water'])
})
})
chance.mixin({
camper: () => ({
firstName: chance.first(),
lastName: chance.last(),
email: chance.email()
})
})
chance.mixin({
amenityId: chance.natural({min: 1, max: 3}),
campsiteId: chance.unique(chance.natural({min: 1, max: numberOfCampsites}))
})
chance.mixin({
campsiteReservations: () => ({
campsiteId: chance.unique(
chance.natural({min: 1, max: campsiteReservationsQuantity})
),
reservationId: chance.unique(
chance.natural({
min: 1,
max: campsiteReservationsQuantity
})
)
})
})
// const productCategoryAssociations = []
// for (let i = 1; i <= numOfProducts; i++) {
// const numOfAssocs = chance.natural({min: 1, max: 3})
// const assocs = chance.unique(
// () => chance.natural({min: 1, max: numOfCategories}),
// numOfAssocs
// )
// for (let j = 0; j < numOfAssocs; j++) {
// productCategoryAssociations.push({productId: i, categoryId: assocs[j]})
// }
// }
// const lineItemAssociations = []
// for (let i = 1; i <= numOfOrders; i++) {
// const numOfAssocs = chance.natural({min: 1, max: 5})
// const assocs = chance.unique(
// () => chance.natural({min: 1, max: numOfProducts}),
// numOfAssocs
// )
// for (let j = 0; j < numOfAssocs; j++) {
// lineItemAssociations.push({
// orderId: i,
// productId: assocs[j],
// quantity: chance.natural({min: 1, max: 15}),
// itemPrice: chance.natural({min: 400, max: 150000})
// })
// }
// }
// async function seed() {
// await db.sync({force: true})
// console.log(`db ${db.config.database} synced!`)
// await Category.bulkCreate(
// chance.unique(chance.word, numOfCategories).map(w => ({name: w}))
// )
// await Artist.bulkCreate(chance.n(chance.artist, numOfArtists))
// await Product.bulkCreate(chance.n(chance.product, numOfProducts))
// await User.bulkCreate(chance.n(chance.user, numOfUsers), {
// individualHooks: true
// })
// await Review.bulkCreate(chance.n(chance.review, numOfReviews), {
// individualHooks: true
// })
// await Order.bulkCreate(chance.n(chance.order, numOfOrders))
// await ProductCategory.bulkCreate(productCategoryAssociations)
// await LineItem.bulkCreate(lineItemAssociations, {
// individualHooks: true
// })
//}
async function seed(){
await db.sync({force: true})
console.log(`db ${db.config.database} synced!`)
await Campsite.bulkCreate(campSiteSeedData)
console.log(`seeded successfully`)
}
// We've separated the `seed` function from the `runSeed` function.
// This way we can isolate the error handling and exit trapping.
// The `seed` function is concerned only with modifying the database.
async function runSeed() {
console.log('seeding...')
try {
await seed()
} catch (err) {
console.error(err)
process.exitCode = 1
} finally {
console.log('closing db connection')
await db.close()
console.log('db connection closed')
}
}
// Execute the `seed` function, IF we ran this module directly (`node seed`).
// `Async` functions always return a promise, so we can use `catch` to handle
// any errors that might occur inside of `seed`.
if (module === require.main) {
runSeed()
}
// we export the seed function for testing purposes (see `./seed.spec.js`)
module.exports = seed