-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.js
50 lines (42 loc) · 1.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
"use strict";
var Sequelize = require('sequelize');
require('sequelize-hierarchy')(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);
});
const User = sequelize.define('user', {
id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
username: { type: Sequelize.STRING, allowNull: false, unique: true },
password: { type: Sequelize.STRING, allowNull: false },
img_url: Sequelize.STRING
});
const Post = sequelize.define('post', {
id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
title: Sequelize.STRING,
content: { type: Sequelize.STRING(1234), allowNull: false }
});
Post.isHierarchy();
const Vote = sequelize.define('vote', {
id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
up: { type: Sequelize.ENUM, values: [-1, 1], allowNull: false }
});
Post.belongsTo(User, { foreignKey: { allowNull: false} });
User.hasMany(Post, { foreignKey: { allowNull: false} });
Vote.belongsTo(Post, { foreignKey: { allowNull: false } });
Post.hasMany(Vote, { foreignKey: { allowNull: false } });
Vote.belongsTo(User, { foreignKey: { allowNull: false } });
// Post.belongsTo(SubReddit, { foreignKey: { allowNull: false } });
module.exports = {
User,
Post,
Vote,
sequelize
};