chapter.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. const { DataTypes } = require('sequelize');
  2. const connect = require('./connect');
  3. /*
  4. type: DataTypes.STRING,
  5. allowNull: false,
  6. defaultValue:'',
  7. comment: '名',
  8. */
  9. function format(m) {
  10. m.href = `/course/${m.cid}/${m.id}`;
  11. m.coverimg = `${m.video}?vframe/jpg/offset/1`;
  12. }
  13. const model = connect.define(
  14. __filename.slice(__dirname.length + 1).replace('.js', ''),
  15. {
  16. id: {
  17. type: DataTypes.INTEGER,
  18. autoIncrement: true,
  19. primaryKey: true,
  20. comment: 'id',
  21. },
  22. cid: {
  23. type: DataTypes.INTEGER,
  24. allowNull: false,
  25. comment: '课程id',
  26. },
  27. title: {
  28. type: DataTypes.STRING,
  29. allowNull: false,
  30. comment: '标题',
  31. },
  32. status: {
  33. type: DataTypes.TINYINT,
  34. defaultValue: 0,
  35. comment: '状态',
  36. },
  37. video: {
  38. type: DataTypes.STRING,
  39. allowNull: false,
  40. comment: '视频',
  41. },
  42. weight: {
  43. type: DataTypes.INTEGER,
  44. defaultValue: 100,
  45. comment: '权重',
  46. },
  47. },
  48. {
  49. comment: '章节',
  50. hooks: {
  51. beforeFind(option) {
  52. if (!option.order) {
  53. option.order = [['weight', 'asc']];
  54. }
  55. },
  56. afterFind(m) {
  57. m && (Array.isArray(m) ? m.forEach(item => format(item)) : format(m));
  58. },
  59. },
  60. }
  61. );
  62. module.exports = model;