This repository has been archived by the owner on Dec 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.js
136 lines (128 loc) · 2.52 KB
/
models.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
"use strict";
var Sequelize = require('sequelize');
var sequelize = new Sequelize(process.env.DATABASE_NAME, 'postgres', process.env.DATABASE_PASSWORD, {
dialect: 'postgres'
});
sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
// MODELS GO HERE
const User = sequelize.define('user', {
username: {
type: Sequelize.STRING,
allowNull: false,
unique: true
},
password: {
type: Sequelize.STRING,
allowNull: false
},
karma: {
type: Sequelize.INTEGER,
defaultValue: 0
}
});
const Post = sequelize.define('post', {
fk_author_id: {
type: Sequelize.INTEGER,
references: {
model: User,
key: 'id',
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE
}
},
title: {
type: Sequelize.STRING,
allowNull: false
},
content: {
type: Sequelize.STRING(1023),
allowNull: false
},
points: {
type: Sequelize.INTEGER,
defaultValue: 0
},
is_link: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValie: 0
},
image_url: {
type: Sequelize.STRING(511),
defaultValue: 'https://goo.gl/PrZTpL'
}
});
const Comment = sequelize.define('comment', {
fk_author_id: {
type: Sequelize.INTEGER,
references: {
model: User,
key: 'id',
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE
}
},
fk_post_id: {
type: Sequelize.INTEGER,
references: {
model: Post,
key: 'id',
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE
}
},
parent_id: {
type: Sequelize.INTEGER,
allowNull: true
},
content: {
type: Sequelize.STRING(1023),
allowNull: false
},
points: {
type: Sequelize.INTEGER,
defaultValue: 0
}
});
const Vote = sequelize.define('vote', {
fk_voter_id: {
type: Sequelize.INTEGER,
references: {
model: User,
key: 'id',
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE
}
},
fk_post_id: {
type: Sequelize.INTEGER,
references: {
model: Post,
key: 'id',
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE
},
allowNull: true
},
fk_comment_id: {
type: Sequelize.INTEGER,
references: {
model: Comment,
key: 'id',
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE
},
allowNull: true
},
type: {
type: Sequelize.STRING
}
});
module.exports = {
sequelize,
User,
Post,
Comment,
Vote
};