egg结合sequelize--模型定义中去除 createAt、updateAt字段,驼峰

楚嘉胜
2023-12-01
module.exports = app => {

  const { STRING, INTEGER, DATE } = app.Sequelize;

  const Announces = app.model.define('announces', {
    id: { type: INTEGER, primaryKey: true, autoIncrement: true },
    title: { type: STRING, allowNull: false },
    content: { type: STRING, allowNull: false },
    isTop: { type: INTEGER, defaultValue: 1, field: 'is_top' },
    createDate: { type: DATE, field: 'create_date' },
    createBy: { type: STRING, field: 'create_by' },
    updateDate: { type: DATE, field: 'update_date' },  //field 转驼峰
    updateBy: { type: STRING, field: 'update_by' },

  }, {
    timestamps: false,  //去除createAt updateAt
    freezeTableName: true,  使用自定义表名
    // 实例对应的表名
    tableName: 'user',
    // 如果需要sequelize帮你维护createdAt,updatedAt和deletedAt必须先启用timestamps功能
    // 将createdAt对应到数据库的created_at字段
    createdAt: 'created_at',
    // 将updatedAt对应到数据库的updated_at字段
    updatedAt: 'updated_at',
    //And deletedAt to be called destroyTime (remember to enable paranoid for this to work)
    deletedAt: false, //'deleted_at',
    //删除数据时不删除数据,而是更新deleteAt字段 如果需要设置为true,则上面的deleteAt字段不能为false,也就是说必须启用
    paranoid: false
  });

  return Announces;

};

 

 类似资料: