import { Sequelize, DataTypes, Optional, Model } from "sequelize" interface UserInfoAttributes { id: number user_id: number nickname: string email: string avatar: string tel: string created_at?: Date updated_at?: Date deleted_at?: Date } export interface UserInfoInput extends Optional { } export interface UserInfoOuput extends Required { } export type TUserInfoModel = ReturnType type DT = typeof DataTypes export default function UserInfoModel(sequelize: Sequelize, DataTypes: DT) { class UserInfo extends Model implements UserInfoAttributes { public id: number public user_id: number public nickname: string public email: string public avatar: string public tel: string // timestamps! public readonly created_at!: Date public readonly updated_at!: Date public readonly deleted_at!: Date } UserInfo.init( { id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true, }, user_id: { type: DataTypes.INTEGER, allowNull: false, onDelete: "CASCADE", references: { model: "user", key: 'id', }, }, nickname: { type: DataTypes.STRING, allowNull: false, }, email: { type: DataTypes.STRING, }, avatar: { type: DataTypes.STRING, }, tel: { type: DataTypes.STRING, }, }, { modelName: "user_info", sequelize, underscored: true, deletedAt: true, timestamps: true, paranoid: true, // 对模型施加了一个软删除 }, ) // 覆盖User的toJSON方法 UserInfo.prototype.toJSON = function () { const values = Object.assign({}, this.get()) as UserInfoOuput delete values.deleted_at return values } UserInfo.associate = function (models) { // User删除时对应的UserInfo同步删除 models["user_info"].belongsTo(models["user"], { foreignKey: "user_id" }) } return UserInfo }