app2-api/modules/blog/category.model.js
2019-04-24 23:01:54 +02:00

34 lines
704 B
JavaScript
Executable File

module.exports = function (sequelize, DataTypes) {
const Category = sequelize.define('Category', {
id: {
type: DataTypes.INTEGER,
primaryKey: true
},
name: {
type: DataTypes.STRING,
allowNull: false
},
sort: {
type: DataTypes.INTEGER,
defaultValue: 0,
allowNull: false,
}
}, {
tableName: 'category',
freezeTableName: true,
timestamps: false
});
Category.beforeCreate((category) => {
category.dataValues.id = Math.floor(Math.random() * (999 - 8)) + 8;
})
Category.associate = function (models) {
Category.Posts = Category.belongsToMany(models.Post, {
through: models.PostCategory,
foreignKey: 'categoryId'
});
};
return Category;
};