const { DataTypes } = require('sequelize'); const connect = require('./connect'); /* type: DataTypes.STRING, allowNull: false, defaultValue:'', comment: '名', */ function format(m) { m.href = `/course/${m.cid}/${m.id}`; m.coverimg = `${m.video}?vframe/jpg/offset/1`; } const model = connect.define( __filename.slice(__dirname.length + 1).replace('.js', ''), { id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true, comment: 'id', }, cid: { type: DataTypes.INTEGER, allowNull: false, comment: '课程id', }, title: { type: DataTypes.STRING, allowNull: false, comment: '标题', }, status: { type: DataTypes.TINYINT, defaultValue: 0, comment: '状态', }, video: { type: DataTypes.STRING, allowNull: false, comment: '视频', }, weight: { type: DataTypes.INTEGER, defaultValue: 100, comment: '权重', }, }, { comment: '章节', hooks: { beforeFind(option) { if (!option.order) { option.order = [['weight', 'asc']]; } }, afterFind(m) { m && (Array.isArray(m) ? m.forEach(item => format(item)) : format(m)); }, }, } ); module.exports = model;