-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
32 lines (25 loc) · 1.1 KB
/
db.js
File metadata and controls
32 lines (25 loc) · 1.1 KB
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
const Sequelize = require('sequelize')
const PG_HOST = process.env.PG_HOST || 'localhost'
const PG_DB = process.env.PG_DB || 'tweet-db'
const PG_USERNAME = process.env.PG_USERNAME || 'postgres'
const PG_PASSWORD = process.env.PG_PASSWORD || 'password'
const PG_PORT = process.env.PG_PORT || 5432
const PG_DIALECT = process.env.PG_DIALECT || 'postgres'
const sequelize = new Sequelize(PG_DB, PG_USERNAME, PG_PASSWORD, {
port: PG_PORT,
host: PG_HOST,
dialect: PG_DIALECT,
})
const db = {};
db.Sequelize = Sequelize;
db.sequelize = sequelize;
//Models/tables
db.tweets = require('./models/Tweet')(sequelize, Sequelize);
db.likes = require('./models/Like')(sequelize, Sequelize);
db.retweets = require('./models/ReTweet')(sequelize, Sequelize);
// Models Relations/Associations
db.likes.belongsTo(db.tweets, { foreignKey: "TweetId", targetKey: "id" });
db.retweets.belongsTo(db.tweets, { foreignKey: "TweetId", targetKey: "id" });
db.tweets.hasMany(db.likes, { foreignKey: "TweetId", targetKey: "id" });
db.tweets.hasMany(db.retweets, { foreignKey: "TweetId", targetKey: "id" });
module.exports = db;