Mise à jour des models mysql

This commit is contained in:
Rampeur
2025-08-15 15:54:13 +02:00
parent 8a011af73a
commit bd3d1ade49
14 changed files with 633 additions and 156 deletions
+28 -10
View File
@@ -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;