Skip to content
关注公众号,获取新课通知

sequelize数据库配置


  1. 安装并配置egg-sequelize插件(它会辅助我们将定义好的 Model 对象加载到 app 和 ctx 上)和mysql2模块:
shell
npm install --save egg-sequelize mysql2
  1. config/plugin.js中引入 egg-sequelize 插件
js
exports.sequelize = {
  enable: true,
  package: "egg-sequelize",
};
  1. config/config.default.js
js
config.sequelize = {
  dialect: "mysql",
  host: "127.0.0.1",
  username: "root",
  password: "root",
  port: 3306,
  database: "egg-video",
  // 中国时区
  timezone: "+08:00",
  define: {
    // 取消数据表名复数
    freezeTableName: true,
    // 自动写入时间戳 created_at updated_at
    timestamps: true,
    // 字段生成软删除时间戳 deleted_at
    // paranoid: true,
    createdAt: "created_time",
    updatedAt: "updated_time",
    // deletedAt: 'deleted_time',
    // 所有驼峰命名格式化
    underscored: true,
  },
};