diff --git a/src/database/index.js b/src/database/index.js index b955f24..8407c78 100644 --- a/src/database/index.js +++ b/src/database/index.js @@ -1,15 +1,23 @@ const sequelize = require('./config/sequelize'); -const ArticleModel = require('./models/article'); -const CommentModel = require('./models/comment'); -const TagModel = require('./models/tag'); -const UserModel = require('./models/user'); +const ArticleModel = require('./models/mysql/Article'); +const ArticleTageModel = require('./models/mysql/ArticleTag'); +const CommentModel = require('./models/mysql/Comment'); +const FavoriteModel = require('./models/mysql/Favorite'); +const FollowerModel = require('./models/mysql/Follower'); +const TagModel = require('./models/mysql/Tag'); +const TagListModel = require('./models/mysql/TagList'); +const UserModel = require('./models/mysql/User'); const DB = { sequelize, User: UserModel(sequelize), Article: ArticleModel(sequelize), + ArticleTag: ArticleTageModel(sequelize), Comment: CommentModel(sequelize), + Favorite: FavoriteModel(sequelize), + Follower: FollowerModel(sequelize), Tag: TagModel(sequelize), + TagList: TagListModel(sequelize), }; module.exports = DB; diff --git a/src/database/models/article.js b/src/database/models/article.js deleted file mode 100644 index e7b2ee4..0000000 --- a/src/database/models/article.js +++ /dev/null @@ -1,43 +0,0 @@ -const { DataTypes, Model } = require("sequelize"); -const sequelize = require("../config/sequelize"); -const slug = require("slug"); - -class Article extends Model { } - -const ArticleModel = () => { - Article.init( - { - id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false }, - slug: { type: DataTypes.STRING, allowNull: false }, - title: { type: DataTypes.STRING, allowNull: false }, - description: { type: DataTypes.TEXT }, - body: { type: DataTypes.TEXT, allowNull: false } - }, - { - sequelize, - modelName: 'Article', - tableName: 'article', - timestamps: true, - createdAt: true, - updatedAt: true - } - ); - - Article.beforeValidate((article) => { - article.slug =slug(`${article.title}`) + '-' + (Math.random() * Math.pow(36, 6) | 0).toString(36); - }); - - Article.prototype.toJSONFor = function () { - return { - id: this.id, - slug: this.slug, - title: this.title, - description: this.description, - body: this.body - }; - }; - - return Article; -}; - -module.exports = ArticleModel; diff --git a/src/database/models/comment.js b/src/database/models/comment.js deleted file mode 100644 index c56352f..0000000 --- a/src/database/models/comment.js +++ /dev/null @@ -1,32 +0,0 @@ -const { DataTypes, Model } = require("sequelize"); -const sequelize = require("../config/sequelize"); - -class Comment extends Model { } - -const CommentModel = () => { - Comment.init( - { - id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false }, - body: { type: DataTypes.TEXT, allowNull: false } - }, - { - sequelize, - modelName: 'Comment', - tableName: 'comment', - timestamps: true, - createdAt: true, - updatedAt: true - } - ); - - Comment.prototype.toJSONFor = function () { - return { - id: this.id, - body: this.body - }; - }; - - return Comment; -}; - -module.exports = CommentModel; diff --git a/src/database/models/mysql/Article.js b/src/database/models/mysql/Article.js new file mode 100644 index 0000000..5350907 --- /dev/null +++ b/src/database/models/mysql/Article.js @@ -0,0 +1,82 @@ +const { DataTypes, Model } = require("sequelize"); +const sequelize = require("../../config/sequelize"); +const slug = require("slug"); + +class Article extends Model { } + +const ArticleModel = () => { + Article.init( + { + id: { + autoIncrement: true, + type: DataTypes.INTEGER, + allowNull: false, + primaryKey: true + }, + slug: { + type: DataTypes.STRING(255), + allowNull: false + }, + title: { + type: DataTypes.STRING(255), + allowNull: false + }, + description: { + type: DataTypes.TEXT, + allowNull: true + }, + body: { + type: DataTypes.TEXT, + allowNull: false + }, + authorId: { + type: DataTypes.STRING(24), + allowNull: true, + references: { + model: 'user', + key: 'id' + } + } + }, + { + sequelize, + tableName: 'article', + timestamps: true, + indexes: [ + { + name: "PRIMARY", + unique: true, + using: "BTREE", + fields: [ + { name: "id" }, + ] + }, + { + name: "authorId", + using: "BTREE", + fields: [ + { name: "authorId" }, + ] + }, + ] + } + ); + + Article.beforeValidate((article) => { + article.slug = slug(`${article.title}`) + '-' + (Math.random() * Math.pow(36, 6) | 0).toString(36); + }); + + Article.prototype.toJSONFor = function () { + return { + id: this.id, + slug: this.slug, + title: this.title, + description: this.description, + body: this.body + }; + }; + + return Article; +}; + +module.exports = ArticleModel; diff --git a/src/database/models/mysql/ArticleTag.js b/src/database/models/mysql/ArticleTag.js new file mode 100644 index 0000000..eaeeadc --- /dev/null +++ b/src/database/models/mysql/ArticleTag.js @@ -0,0 +1,63 @@ +const { DataTypes, Model } = require("sequelize"); +const sequelize = require("../../config/sequelize"); + +class ArticleTag extends Model { } + +const ArticleTagModel = () => { + ArticleTag.init( + { + tagName: { + type: DataTypes.STRING(255), + allowNull: false, + primaryKey: true, + references: { + model: 'tag', + key: 'name' + } + }, + articleId: { + type: DataTypes.INTEGER, + allowNull: false, + primaryKey: true, + references: { + model: 'article', + key: 'id' + } + } + }, + { + sequelize, + tableName: 'articleTag', + timestamps: false, + indexes: [ + { + name: "PRIMARY", + unique: true, + using: "BTREE", + fields: [ + { name: "tagName" }, + { name: "articleId" }, + ] + }, + { + name: "articleId", + using: "BTREE", + fields: [ + { name: "articleId" }, + ] + }, + ] + } + ); + + ArticleTag.prototype.toJSONFor = function () { + return { + tagName: this.tagName, + articleId: this.articleId + }; + }; + + return ArticleTag; +}; + +module.exports = ArticleTagModel; diff --git a/src/database/models/mysql/Comment.js b/src/database/models/mysql/Comment.js new file mode 100644 index 0000000..d7ad8f4 --- /dev/null +++ b/src/database/models/mysql/Comment.js @@ -0,0 +1,77 @@ +const { DataTypes, Model } = require("sequelize"); +const sequelize = require("../../config/sequelize"); + +class Comment extends Model { } + +const CommentModel = () => { + Comment.init( + { + id: { + autoIncrement: true, + type: DataTypes.INTEGER, + allowNull: false, + primaryKey: true + }, + body: { + type: DataTypes.TEXT, + allowNull: false + }, + authorId: { + type: DataTypes.STRING(24), + allowNull: true, + references: { + model: 'user', + key: 'id' + } + }, + articleId: { + type: DataTypes.INTEGER, + allowNull: true, + references: { + model: 'article', + key: 'id' + } + } + }, + { + sequelize, + tableName: 'comment', + timestamps: true, + indexes: [ + { + name: "PRIMARY", + unique: true, + using: "BTREE", + fields: [ + { name: "id" }, + ] + }, + { + name: "authorId", + using: "BTREE", + fields: [ + { name: "authorId" }, + ] + }, + { + name: "articleId", + using: "BTREE", + fields: [ + { name: "articleId" }, + ] + }, + ] + } + ); + + Comment.prototype.toJSONFor = function () { + return { + id: this.id, + body: this.body + }; + }; + + return Comment; +}; + +module.exports = CommentModel; diff --git a/src/database/models/mysql/Favorite.js b/src/database/models/mysql/Favorite.js new file mode 100644 index 0000000..8a97d49 --- /dev/null +++ b/src/database/models/mysql/Favorite.js @@ -0,0 +1,63 @@ +const { DataTypes, Model } = require("sequelize"); +const sequelize = require("../../config/sequelize"); + +class Favorite extends Model { } + +const FavoriteModel = () => { + Favorite.init( + { + userId: { + type: DataTypes.STRING(24), + allowNull: false, + primaryKey: true, + references: { + model: 'user', + key: 'id' + } + }, + articleId: { + type: DataTypes.INTEGER, + allowNull: false, + primaryKey: true, + references: { + model: 'article', + key: 'id' + } + } + }, + { + sequelize, + tableName: 'favorite', + timestamps: false, + indexes: [ + { + name: "PRIMARY", + unique: true, + using: "BTREE", + fields: [ + { name: "userId" }, + { name: "articleId" }, + ] + }, + { + name: "articleId", + using: "BTREE", + fields: [ + { name: "articleId" }, + ] + }, + ] + } + ); + + Favorite.prototype.toJSONFor = function () { + return { + userId: this.userId, + articleId: this.articleId + }; + }; + + return Favorite; +}; + +module.exports = FavoriteModel; diff --git a/src/database/models/mysql/Follower.js b/src/database/models/mysql/Follower.js new file mode 100644 index 0000000..1b09d4f --- /dev/null +++ b/src/database/models/mysql/Follower.js @@ -0,0 +1,63 @@ +const { DataTypes, Model } = require("sequelize"); +const sequelize = require("../../config/sequelize"); + +class Follower extends Model { } + +const FollowerModel = () => { + Follower.init( + { + userId: { + type: DataTypes.STRING(24), + allowNull: false, + primaryKey: true, + references: { + model: 'user', + key: 'id' + } + }, + followerId: { + type: DataTypes.STRING(24), + allowNull: false, + primaryKey: true, + references: { + model: 'user', + key: 'id' + } + } + }, + { + sequelize, + tableName: 'follower', + timestamps: false, + indexes: [ + { + name: "PRIMARY", + unique: true, + using: "BTREE", + fields: [ + { name: "userId" }, + { name: "followerId" }, + ] + }, + { + name: "followerId", + using: "BTREE", + fields: [ + { name: "followerId" }, + ] + }, + ] + } + ); + + Follower.prototype.toJSONFor = function () { + return { + userId: this.userId, + followerId: this.followerId + }; + }; + + return Follower; +}; + +module.exports = FollowerModel; diff --git a/src/database/models/mysql/Tag.js b/src/database/models/mysql/Tag.js new file mode 100644 index 0000000..c55bffb --- /dev/null +++ b/src/database/models/mysql/Tag.js @@ -0,0 +1,41 @@ +const { DataTypes, Model } = require("sequelize"); +const sequelize = require("../../config/sequelize"); + +class Tag extends Model { } + +const TagModel = () => { + Tag.init( + { + name: { + type: DataTypes.STRING(255), + allowNull: false, + primaryKey: true + } + }, + { + sequelize, + tableName: 'tag', + timestamps: true, + indexes: [ + { + name: "PRIMARY", + unique: true, + using: "BTREE", + fields: [ + { name: "name" }, + ] + }, + ] + } + ); + + Tag.prototype.toJSONFor = function () { + return { + name: this.name + }; + }; + + return Tag; +}; + +module.exports = TagModel; diff --git a/src/database/models/mysql/TagList.js b/src/database/models/mysql/TagList.js new file mode 100644 index 0000000..c1d7d93 --- /dev/null +++ b/src/database/models/mysql/TagList.js @@ -0,0 +1,63 @@ +const { DataTypes, Model } = require("sequelize"); +const sequelize = require("../../config/sequelize"); + +class TagList extends Model { } + +const TagListModel = () => { + TagList.init( + { + articleId: { + type: DataTypes.INTEGER, + allowNull: false, + primaryKey: true, + references: { + model: 'article', + key: 'id' + } + }, + tagName: { + type: DataTypes.STRING(255), + allowNull: false, + primaryKey: true, + references: { + model: 'tag', + key: 'name' + } + } + }, + { + sequelize, + tableName: 'tagList', + timestamps: false, + indexes: [ + { + name: "PRIMARY", + unique: true, + using: "BTREE", + fields: [ + { name: "articleId" }, + { name: "tagName" }, + ] + }, + { + name: "tagName", + using: "BTREE", + fields: [ + { name: "tagName" }, + ] + }, + ] + } + ); + + TagList.prototype.toJSONFor = function () { + return { + articleId: this.articleId, + tagName: this.tagName + }; + }; + + return TagList; +}; + +module.exports = TagListModel; diff --git a/src/database/models/user.js b/src/database/models/mysql/User.js similarity index 51% rename from src/database/models/user.js rename to src/database/models/mysql/User.js index 0ac429e..2d1ec78 100644 --- a/src/database/models/user.js +++ b/src/database/models/mysql/User.js @@ -1,54 +1,93 @@ const { DataTypes, Model } = require("sequelize"); var crypto = require('crypto'); var jwt = require('jsonwebtoken'), - secret = require('../../../config').secret, - session_lifetime = require('../../../config').session_lifetime; + secret = require('../../../../config').secret, + session_lifetime = require('../../../../config').session_lifetime; -const sequelize = require("../config/sequelize"); -//const bcrypt = require("bcrypt"); +const sequelize = require("../../config/sequelize"); class User extends Model { } const UserModel = () => { User.init( { - _id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false }, - email: { type: DataTypes.STRING, allowNull: false, unique: true }, - username: { type: DataTypes.STRING, allowNull: false, unique: true }, - firstname: { type: DataTypes.STRING }, - lastname: { type: DataTypes.STRING }, - bio: { type: DataTypes.TEXT, allowNull: true }, - image: { type: DataTypes.TEXT, allowNull: true }, - role: { type: DataTypes.STRING }, - salt: { type: DataTypes.STRING, allowNull: false }, - hash: { type: DataTypes.TEXT, allowNull: false } + id: { + type: DataTypes.STRING(24), + allowNull: false, + primaryKey: true + }, + email: { + type: DataTypes.STRING(255), + allowNull: false, + unique: "email" + }, + username: { + type: DataTypes.STRING(255), + allowNull: false, + unique: "username" + }, + firstname: { + type: DataTypes.STRING(255), + allowNull: true + }, + lastname: { + type: DataTypes.STRING(255), + allowNull: true + }, + bio: { + type: DataTypes.TEXT, + allowNull: true + }, + image: { + type: DataTypes.TEXT, + allowNull: true + }, + role: { + type: DataTypes.STRING(255), + allowNull: true + }, + salt: { + type: DataTypes.STRING(255), + allowNull: false + }, + hash: { + type: DataTypes.TEXT, + allowNull: false + } }, { sequelize, - modelName: 'User', tableName: 'user', timestamps: true, - createdAt: true, - updatedAt: true + indexes: [ + { + name: "PRIMARY", + unique: true, + using: "BTREE", + fields: [ + { name: "id" }, + ] + }, + { + name: "email", + unique: true, + using: "BTREE", + fields: [ + { name: "email" }, + ] + }, + { + name: "username", + unique: true, + using: "BTREE", + fields: [ + { name: "username" }, + ] + }, + ] } ); - /* - Model.prototype.matchPassword = async function (enteredPassword) { - return await bcrypt.compare(enteredPassword, this.password); - }; - - const DEFAULT_SALT_ROUNDS = 10; - - User.addHook("beforeCreate", async (user) => { - const encryptedPassword = await bcrypt.hash( - user.password, - DEFAULT_SALT_ROUNDS - ); - user.password = encryptedPassword; - }); - */ - User.prototype.validPassword = function (password) { let hash = crypto.pbkdf2Sync(password, this.salt, 10000, 512, 'sha512').toString('hex'); return this.hash === hash; @@ -64,7 +103,7 @@ const UserModel = () => { var exp = new Date(today.getTime() + (session_lifetime * 1000)); return jwt.sign({ - id: this._id, + id: this.id, username: this.username, exp: parseInt(exp.getTime() / 1000) }, secret, { algorithm: 'HS256' }); @@ -72,7 +111,7 @@ const UserModel = () => { User.prototype.toAuthJSON = function () { return { - id: this._id, + id: this.id, email: this.email, role: this.role, token: this.generateJWT(), @@ -89,7 +128,7 @@ const UserModel = () => { User.prototype.toJSONFor = function () { return { - id: this._id, + id: this.id, email: this.email, username: this.username, firstname: this.firstname, @@ -111,7 +150,7 @@ const UserModel = () => { username: this.username, image: this.image || '/assets/images/users/user.jpg', bg_image: this.bg_image || '/assets/images/users/bg_user.jpg', - following: user ? user.isFollowing(this._id) : false + following: user ? user.isFollowing(this.id) : false }; }; diff --git a/src/database/models/mysql/init-models.js b/src/database/models/mysql/init-models.js new file mode 100644 index 0000000..4eea8e7 --- /dev/null +++ b/src/database/models/mysql/init-models.js @@ -0,0 +1,65 @@ +var DataTypes = require("sequelize").DataTypes; +var _Article = require("./Article"); +var _ArticleTag = require("./ArticleTag"); +var _Comment = require("./Comment"); +var _Favorite = require("./Favorite"); +var _Follower = require("./Follower"); +var _Tag = require("./Tag"); +var _TagList = require("./TagList"); +var _User = require("./User"); + +function initModels(sequelize) { + var Article = _Article(sequelize, DataTypes); + var ArticleTag = _ArticleTag(sequelize, DataTypes); + var Comment = _Comment(sequelize, DataTypes); + var Favorite = _Favorite(sequelize, DataTypes); + var Follower = _Follower(sequelize, DataTypes); + var Tag = _Tag(sequelize, DataTypes); + var TagList = _TagList(sequelize, DataTypes); + var User = _User(sequelize, DataTypes); + + Article.belongsToMany(Tag, { as: 'tagNameTags', through: ArticleTag, foreignKey: "articleId", otherKey: "tagName" }); + Article.belongsToMany(Tag, { as: 'tagNameTagTagLists', through: TagList, foreignKey: "articleId", otherKey: "tagName" }); + Article.belongsToMany(User, { as: 'userIdUsers', through: Favorite, foreignKey: "articleId", otherKey: "userId" }); + Tag.belongsToMany(Article, { as: 'articleIdArticles', through: ArticleTag, foreignKey: "tagName", otherKey: "articleId" }); + Tag.belongsToMany(Article, { as: 'articleIdArticleTagLists', through: TagList, foreignKey: "tagName", otherKey: "articleId" }); + User.belongsToMany(Article, { as: 'articleIdArticleFavorites', through: Favorite, foreignKey: "userId", otherKey: "articleId" }); + User.belongsToMany(User, { as: 'followerIdUsers', through: Follower, foreignKey: "userId", otherKey: "followerId" }); + User.belongsToMany(User, { as: 'userIdUserFollowers', through: Follower, foreignKey: "followerId", otherKey: "userId" }); + ArticleTag.belongsTo(Article, { as: "article", foreignKey: "articleId"}); + Article.hasMany(ArticleTag, { as: "articleTags", foreignKey: "articleId"}); + Comment.belongsTo(Article, { as: "article", foreignKey: "articleId"}); + Article.hasMany(Comment, { as: "comments", foreignKey: "articleId"}); + Favorite.belongsTo(Article, { as: "article", foreignKey: "articleId"}); + Article.hasMany(Favorite, { as: "favorites", foreignKey: "articleId"}); + TagList.belongsTo(Article, { as: "article", foreignKey: "articleId"}); + Article.hasMany(TagList, { as: "tagLists", foreignKey: "articleId"}); + ArticleTag.belongsTo(Tag, { as: "tagNameTag", foreignKey: "tagName"}); + Tag.hasMany(ArticleTag, { as: "articleTags", foreignKey: "tagName"}); + TagList.belongsTo(Tag, { as: "tagNameTag", foreignKey: "tagName"}); + Tag.hasMany(TagList, { as: "tagLists", foreignKey: "tagName"}); + Article.belongsTo(User, { as: "author", foreignKey: "authorId"}); + User.hasMany(Article, { as: "articles", foreignKey: "authorId"}); + Comment.belongsTo(User, { as: "author", foreignKey: "authorId"}); + User.hasMany(Comment, { as: "comments", foreignKey: "authorId"}); + Favorite.belongsTo(User, { as: "user", foreignKey: "userId"}); + User.hasMany(Favorite, { as: "favorites", foreignKey: "userId"}); + Follower.belongsTo(User, { as: "user", foreignKey: "userId"}); + User.hasMany(Follower, { as: "followers", foreignKey: "userId"}); + Follower.belongsTo(User, { as: "follower", foreignKey: "followerId"}); + User.hasMany(Follower, { as: "followerFollowers", foreignKey: "followerId"}); + + return { + Article, + ArticleTag, + Comment, + Favorite, + Follower, + Tag, + TagList, + User, + }; +} +module.exports = initModels; +module.exports.initModels = initModels; +module.exports.default = initModels; diff --git a/src/database/models/tag.js b/src/database/models/tag.js deleted file mode 100644 index 11c5590..0000000 --- a/src/database/models/tag.js +++ /dev/null @@ -1,30 +0,0 @@ -const { DataTypes, Model } = require("sequelize"); -const sequelize = require("../config/sequelize"); - -class Tag extends Model { } - -const TagModel = () => { - Tag.init( - { - name: { type: DataTypes.STRING, primaryKey: true, allowNull: false } - }, - { - sequelize, - modelName: 'Tag', - tableName: 'tag', - timestamps: true, - createdAt: true, - updatedAt: true - } - ); - - Tag.prototype.toJSONFor = function () { - return { - name: this.name - }; - }; - - return Tag; -}; - -module.exports = TagModel; diff --git a/src/database/relationships/index.js b/src/database/relationships/index.js index 554a9d6..7be5a1c 100644 --- a/src/database/relationships/index.js +++ b/src/database/relationships/index.js @@ -12,18 +12,36 @@ const associate = () => { as: 'author', }); */ - DB.User.belongsToMany(DB.User, { as: "followers", through: "follower", foreignKey: "userId", timestamps: false }); - DB.User.belongsToMany(DB.User, { as: "following", through: "follower", foreignKey: "followerId", timestamps: false }); - DB.User.hasMany(DB.Article, { foreignKey: "authorId", onDelete: "CASCADE" }); + DB.Article.belongsToMany(DB.Tag, { as: 'tagNameTags', through: DB.ArticleTag, foreignKey: "articleId", otherKey: "tagName" }); + DB.Article.belongsToMany(DB.Tag, { as: 'tagNameTagTagLists', through: DB.TagList, foreignKey: "articleId", otherKey: "tagName" }); + DB.Article.belongsToMany(DB.User, { as: 'userIdUsers', through: DB.Favorite, foreignKey: "articleId", otherKey: "userId" }); + DB.Tag.belongsToMany(DB.Article, { as: 'articleIdArticles', through: DB.ArticleTag, foreignKey: "tagName", otherKey: "articleId" }); + DB.Tag.belongsToMany(DB.Article, { as: 'articleIdArticleTagLists', through: DB.TagList, foreignKey: "tagName", otherKey: "articleId" }); + DB.User.belongsToMany(DB.Article, { as: 'articleIdArticleFavorites', through: DB.Favorite, foreignKey: "userId", otherKey: "articleId" }); + DB.User.belongsToMany(DB.User, { as: 'followerIdUsers', through: DB.Follower, foreignKey: "userId", otherKey: "followerId" }); + DB.User.belongsToMany(DB.User, { as: 'userIdUserFollowers', through: DB.Follower, foreignKey: "followerId", otherKey: "userId" }); + DB.ArticleTag.belongsTo(DB.Article, { as: "article", foreignKey: "articleId" }); + DB.Article.hasMany(DB.ArticleTag, { as: "articleTags", foreignKey: "articleId" }); + DB.Comment.belongsTo(DB.Article, { as: "article", foreignKey: "articleId" }); + DB.Article.hasMany(DB.Comment, { as: "comments", foreignKey: "articleId" }); + DB.Favorite.belongsTo(DB.Article, { as: "article", foreignKey: "articleId" }); + DB.Article.hasMany(DB.Favorite, { as: "favorites", foreignKey: "articleId" }); + DB.TagList.belongsTo(DB.Article, { as: "article", foreignKey: "articleId" }); + DB.Article.hasMany(DB.TagList, { as: "tagLists", foreignKey: "articleId" }); + DB.ArticleTag.belongsTo(DB.Tag, { as: "tagNameTag", foreignKey: "tagName" }); + DB.Tag.hasMany(DB.ArticleTag, { as: "articleTags", foreignKey: "tagName" }); + DB.TagList.belongsTo(DB.Tag, { as: "tagNameTag", foreignKey: "tagName" }); + DB.Tag.hasMany(DB.TagList, { as: "tagLists", foreignKey: "tagName" }); DB.Article.belongsTo(DB.User, { as: "author", foreignKey: "authorId" }); - DB.User.hasMany(DB.Comment, { foreignKey: "authorId", onDelete: "CASCADE" }); + DB.User.hasMany(DB.Article, { as: "articles", foreignKey: "authorId" }); DB.Comment.belongsTo(DB.User, { as: "author", foreignKey: "authorId" }); - DB.Article.hasMany(DB.Comment, { foreignKey: "articleId", onDelete: "CASCADE" }); - DB.Comment.belongsTo(DB.Article, { foreignKey: "articleId" }); - DB.User.belongsToMany(DB.Article, { as: "favorites", through: "favorite", timestamps: false }); - DB.Article.belongsToMany(DB.User, { through: "favorite", foreignKey: "articleId", timestamps: false }); - DB.Article.belongsToMany(DB.Tag, { through: "tagList", as: "tagLists", foreignKey: "articleId", timestamps: false, onDelete: "CASCADE" }); - DB.Tag.belongsToMany(DB.Article, { through: "articleTag", uniqueKey: false, timestamps: false }); + DB.User.hasMany(DB.Comment, { as: "comments", foreignKey: "authorId" }); + DB.Favorite.belongsTo(DB.User, { as: "user", foreignKey: "userId" }); + DB.User.hasMany(DB.Favorite, { as: "favorites", foreignKey: "userId" }); + DB.Follower.belongsTo(DB.User, { as: "user", foreignKey: "userId" }); + DB.User.hasMany(DB.Follower, { as: "followers", foreignKey: "userId" }); + DB.Follower.belongsTo(DB.User, { as: "follower", foreignKey: "followerId" }); + DB.User.hasMany(DB.Follower, { as: "followerFollowers", foreignKey: "followerId" }); }; module.exports = associate;