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
+1 -1
View File
@@ -1,4 +1,4 @@
const { DataTypes, Model } = require("sequelize");
const { DataTypes } = require("sequelize");
const sequelize = require("../util/database");
const slugify = require("slugify");
+1 -1
View File
@@ -1,4 +1,4 @@
const { DataTypes, Model } = require("sequelize");
const { DataTypes } = require("sequelize");
const sequelize = require("../util/database");
const Comment = sequelize.define("Comment", {
+1 -1
View File
@@ -1,4 +1,4 @@
const { DataTypes, Model } = require("sequelize");
const { DataTypes } = require("sequelize");
const sequelize = require("../util/database");
const Tag = sequelize.define("Tag", {
+1 -8
View File
@@ -1,15 +1,8 @@
const { DataTypes, Model } = require("sequelize");
const sequelize = require("../util/database");
const bcrypt = require("bcryptjs");
/**
* "email": "jake@jake.jake",
"token": "jwt.token.here",
"username": "jake",
"bio": "I work at statefarm",
"image": null
*/
const User = sequelize.define("User", {
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false },
email: { type: DataTypes.STRING, allowNull: false, unique: true },
+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 });