Mise à jour des 'models'

This commit is contained in:
Rampeur
2025-08-13 18:47:41 +02:00
parent e741081966
commit 9bce9b714e
5 changed files with 27 additions and 72 deletions
+23 -61
View File
@@ -15,15 +15,6 @@ const { errorHandler } = require("./middlewares/errorHandler");
var isProduction = appEnv === 'production';
/*
var db = require("./models");
*/
// Import Models
const User = require("./models/User");
const Article = require("./models/Article");
const Tag = require("./models/Tag");
const Comment = require("./models/Comment");
const app = express();
// Body parser
@@ -64,6 +55,29 @@ app.use((req, res, next) => {
next();
});
/*
var db = require("./models");
*/
// Import Models
const User = require("./models/User");
const Article = require("./models/Article");
const Tag = require("./models/Tag");
const Comment = require("./models/Comment");
// Relations
User.belongsToMany(User, { as: "followers", through: "Followers", foreignKey: "userId", timestamps: false });
User.belongsToMany(User, { as: "following", through: "Followers", foreignKey: "followerId", timestamps: false });
User.hasMany(Article, { foreignKey: "authorId", onDelete: "CASCADE" });
Article.belongsTo(User, { as: "author", foreignKey: "authorId" });
User.hasMany(Comment, { foreignKey: "authorId", onDelete: "CASCADE" });
Comment.belongsTo(User, { as: "author", foreignKey: "authorId" });
Article.hasMany(Comment, { foreignKey: "articleId", onDelete: "CASCADE" });
Comment.belongsTo(Article, { foreignKey: "articleId" });
User.belongsToMany(Article, { as: "favorites", through: "Favorites", timestamps: false });
Article.belongsToMany(User, { through: "Favorites", foreignKey: "articleId", timestamps: false });
Article.belongsToMany(Tag, { through: "TagLists", as: "tagLists", foreignKey: "articleId", timestamps: false, onDelete: "CASCADE" });
Tag.belongsToMany(Article, { through: "ArticleTags", uniqueKey: false, timestamps: false });
// Route files
const users = require("./routes/api/users");
@@ -84,58 +98,6 @@ app.use(tags);
app.use(errorHandler);
//console.log(`User: ${User}`);
// Relations
User.belongsToMany(User, {
as: "followers",
through: "Followers",
foreignKey: "userId",
timestamps: false,
});
User.belongsToMany(User, {
as: "following",
through: "Followers",
foreignKey: "followerId",
timestamps: false,
});
User.hasMany(Article, {
foreignKey: "authorId",
onDelete: "CASCADE",
});
Article.belongsTo(User, { as: "author", foreignKey: "authorId" });
User.hasMany(Comment, {
foreignKey: "authorId",
onDelete: "CASCADE",
});
Comment.belongsTo(User, { as: "author", foreignKey: "authorId" });
Article.hasMany(Comment, {
foreignKey: "articleId",
onDelete: "CASCADE",
});
Comment.belongsTo(Article, { foreignKey: "articleId" });
User.belongsToMany(Article, {
as: "favorites",
through: "Favorites",
timestamps: false,
});
Article.belongsToMany(User, {
through: "Favorites",
foreignKey: "articleId",
timestamps: false,
});
Article.belongsToMany(Tag, {
through: "TagLists",
as: "tagLists",
foreignKey: "articleId",
timestamps: false,
onDelete: "CASCADE",
});
Tag.belongsToMany(Article, {
through: "ArticleTags",
uniqueKey: false,
timestamps: false,
})
//Article.sync();
/*
const sync = async () => await sequelize.sync({ force: true });