20250912084440-create-news.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. 'use strict';
  2. /** @type {import('sequelize-cli').Migration} */
  3. module.exports = {
  4. async up (queryInterface, Sequelize) {
  5. await queryInterface.createTable('news', {
  6. id: {
  7. type: Sequelize.INTEGER,
  8. primaryKey: true,
  9. autoIncrement: true
  10. },
  11. title: {
  12. type: Sequelize.STRING,
  13. allowNull: false,
  14. comment: '新闻标题'
  15. },
  16. content: {
  17. type: Sequelize.TEXT,
  18. allowNull: false,
  19. comment: '新闻内容'
  20. },
  21. summary: {
  22. type: Sequelize.TEXT,
  23. allowNull: true,
  24. comment: '新闻摘要'
  25. },
  26. author: {
  27. type: Sequelize.STRING,
  28. allowNull: true,
  29. comment: '作者'
  30. },
  31. source: {
  32. type: Sequelize.STRING,
  33. allowNull: true,
  34. comment: '新闻来源'
  35. },
  36. coverImage: {
  37. type: Sequelize.STRING,
  38. allowNull: true,
  39. comment: '封面图片URL'
  40. },
  41. category: {
  42. type: Sequelize.STRING,
  43. allowNull: true,
  44. comment: '新闻分类'
  45. },
  46. tags: {
  47. type: Sequelize.STRING,
  48. allowNull: true,
  49. comment: '标签,用逗号分隔'
  50. },
  51. viewCount: {
  52. type: Sequelize.INTEGER,
  53. defaultValue: 0,
  54. comment: '浏览次数'
  55. },
  56. isPublished: {
  57. type: Sequelize.BOOLEAN,
  58. defaultValue: true,
  59. comment: '是否发布'
  60. },
  61. publishedAt: {
  62. type: Sequelize.DATE,
  63. allowNull: true,
  64. comment: '发布时间'
  65. },
  66. createdAt: {
  67. type: Sequelize.DATE,
  68. allowNull: false
  69. },
  70. updatedAt: {
  71. type: Sequelize.DATE,
  72. allowNull: false
  73. }
  74. });
  75. // 添加索引
  76. await queryInterface.addIndex('news', ['title']);
  77. await queryInterface.addIndex('news', ['category']);
  78. await queryInterface.addIndex('news', ['isPublished']);
  79. await queryInterface.addIndex('news', ['publishedAt']);
  80. },
  81. async down (queryInterface, Sequelize) {
  82. await queryInterface.dropTable('news');
  83. }
  84. };