-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
100 lines (90 loc) · 1.66 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
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
import Sequelize from 'sequelize';
const connection = new Sequelize('weddingsite', 'weddingdbuser', 'test', {
host: 'localhost',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
idle: 10000
}
});
const Post = connection.define('post', {
id: {
type: Sequelize.BIGINT,
primaryKey: true
},
uuid: {
type: Sequelize.STRING
},
title: {
type: Sequelize.STRING,
},
description: {
type: Sequelize.STRING,
},
votes: {
type: Sequelize.INTEGER
},
url: {
type: Sequelize.STRING
}
}, {paranoid: true});
const User = connection.define('user', {
id: {
type: Sequelize.BIGINT,
primaryKey: true
},
uuid: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING,
validate: {
isEmail: true
}
},
password: {
type: Sequelize.STRING
},
firstName: {
type: Sequelize.STRING
},
lastName: {
type: Sequelize.STRING
},
birthdate: {
type: Sequelize.DATE
},
type: {
type: Sequelize.INTEGER
},
deleted: {
type: Sequelize.BOOLEAN
},
});
const userPreference = connection.define('user_preference', {
id: {
type: Sequelize.INTEGER,
primaryKey: true
},
userId: {
type: Sequelize.BIGINT
},
theme: {
type: Sequelize.STRING
}
});
User.hasOne(userPreference, {foreignKey: 'userId'});
userPreference.belongsTo(User, {foreignKey: 'userId'});
// force: true will drop the table if it already exists
// User.sync({force: false}).then(() => {
// // Table created
// return User.create({
// firstName: 'John',
// lastName: 'Hancock'
// });
// });
// User.findAll().then(users => {
// console.log(users)
// });
export default connection;