diff --git a/src/models/PostCategory.js b/src/models/PostCategory.js new file mode 100644 index 0000000..32f7b2d --- /dev/null +++ b/src/models/PostCategory.js @@ -0,0 +1,43 @@ +const { DataTypes } = require("sequelize"); + +module.exports = (sequelize, _DataTypes) => { + + const PostCategory = sequelize.define( + 'PostCategory', + { + postId: { + type: DataTypes.INTEGER, + field: 'post_id', + primaryKey: true, + }, + categoryId: { + type: DataTypes.INTEGER, + field: 'category_id', + primaryKey: true, + }, + }, + { + timestamps: false, + underscored: true, + tableName: 'posts_categories', + }, + ); + + PostCategory.associate = (models) => { + models.BlogPost.belongsToMany(models.Category, { + as: 'categories', + through: PostCategory, + foreignKey: 'post_id', + otherKey: 'category_id', + }); + + models.Category.belongsToMany(models.BlogPost, { + as: 'blogPosts', + through: PostCategory, + foreignKey: 'category_id', + otherKey: 'post_id', + }); + }; + + return PostCategory; +};