-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed.js
More file actions
51 lines (44 loc) · 1.38 KB
/
seed.js
File metadata and controls
51 lines (44 loc) · 1.38 KB
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
'use strict'
// const db = require('../server/db')
const {db, User, Product, Order, Review} = require('../server/db/models')
const tests = [
{
content: 'hello anna',
questionId: 2
}
]
async function seed() {
await db.sync({force: true})
console.log('db synced!')
await Promise.all(users.map(user => User.create(user)))
console.log(
`seeded ${users.length} users, seeded ${orders.length} orders, seeded ${
products.length
} products, seeded ${reviews.length} reviews,`
)
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