app2-api/modules/blog/category.model.js
2019-08-21 13:21:15 +02:00

36 lines
746 B
JavaScript
Executable File

module.exports = function (sequelize, DataTypes) {
const Category = sequelize.define('Category', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: {
type: DataTypes.STRING,
allowNull: false
},
sort: {
type: DataTypes.INTEGER,
defaultValue: 0,
allowNull: false,
}
}, {
tableName: 'categories',
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, {
as: 'posts',
through: models.PostCategory,
foreignKey: 'categoryId'
});
};
return Category;
};