-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
73 lines (50 loc) · 1.71 KB
/
db.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
const Sequelize = require('sequelize');
const conn = new Sequelize(process.env.DATABASE_URL);
const Product = conn.define('product', {
name: conn.Sequelize.STRING
});
const User = conn.define('user', {
name: conn.Sequelize.STRING,
password: conn.Sequelize.STRING
});
User.belongsTo(Product, { as: 'favoriteProduct'});
User.belongsTo(Product, { as: 'worstProduct'});
const sync = ()=> conn.sync({ force: true });
const seed = ()=> {
const products = ['fish', 'lettuce', 'ketchup'];
const users = ['Nancy', 'Leo', 'Corny'];
let fish, lettuce, ketchup, nancy, leo, corny;
return sync()
.then(()=> {
const productPromises = products.map(name => Product.create({ name }));
const userPromises = users.map((name, index)=> User.create({ name, password: products[index]}));
return Promise.all( [...productPromises, ...userPromises ])
.then( results => [fish, lettuce, ketchup, nancy, leo, corny] = results )
.then( () => {
return Promise.all([
nancy.setFavoriteProduct(fish),
nancy.setWorstProduct(lettuce),
leo.setWorstProduct(lettuce),
leo.setFavoriteProduct(ketchup),
corny.setWorstProduct(lettuce),
corny.setFavoriteProduct(ketchup)
])
})
});
};
// return Promise.all(productPromises.concat(userPromises))
// .then(results => [foo, bar, bazz, moe, larry, curly] = results)
// .then( ()=> Promise.all([
// moe.setBestProduct(foo),
// moe.setWorstProduct(bar),
// curly.setBestProduct(bazz)
// ]))
// });
module.exports = {
models: {
Product,
User
},
sync,
seed
};