-
Notifications
You must be signed in to change notification settings - Fork 8
/
db.js
64 lines (58 loc) · 1.47 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
const conn = require('./conn');
const Product = require('./product.model.js');
const User = conn.define('user', {
name: {
type: conn.Sequelize.STRING,
unique: true,
allowNull: false
},
position: {
type: conn.Sequelize.JSONB
},
isAdmin: {
type: conn.Sequelize.BOOLEAN,
defaultValue: false
},
password: conn.Sequelize.STRING,
googleUserId: conn.Sequelize.STRING,
githubUserId: conn.Sequelize.STRING,
githubAccessToken: conn.Sequelize.STRING,
facebookUserId: conn.Sequelize.STRING,
facebookAccessToken: conn.Sequelize.STRING
});
const sync = ()=> conn.sync({ force: true });
const seed = ()=> {
const products = ['foo', 'bar', 'bazz'];
const users = ['moe', 'larry', 'curly'];
let foo, bar, bazz, moe, larry, curly;
return sync()
.then(()=> {
const promises = products.map(name => Product.create({ name }))
.concat(users.map( name => User.create( { name, password: name.toUpperCase()})));
return Promise.all(promises);
})
.then( result => [ foo, bar, bazz, moe, larry, curly ] = result )
.then( ()=> {
if(process.env.ADMIN_USERNAME && process.env.ADMIN_PASSWORD){
User.create({ name: process.env.ADMIN_USERNAME, password: process.env.ADMIN_PASSWORD, isAdmin: true});
}
})
.then(()=> {
return {
moe,
larry,
curly,
foo,
bar,
bazz
};
});
};
module.exports = {
models: {
Product,
User
},
sync,
seed
};