You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.6 KiB
64 lines
1.6 KiB
import { Sequelize, DataTypes, Optional, Model } from "sequelize"
|
|
|
|
interface UserAttributes {
|
|
id: number;
|
|
username: string;
|
|
password: string;
|
|
nickname: string;
|
|
email: string;
|
|
|
|
createdAt?: Date;
|
|
updatedAt?: Date;
|
|
deletedAt?: Date;
|
|
}
|
|
|
|
export interface UserInput extends Optional<UserAttributes, 'id'> { }
|
|
export interface UserOuput extends Required<UserAttributes> { }
|
|
export type TUserModel = ReturnType<typeof UserModel>
|
|
|
|
type DT = typeof DataTypes
|
|
export default function UserModel(sequelize: Sequelize, DataTypes: DT) {
|
|
class User extends Model<UserAttributes, UserInput> implements UserAttributes {
|
|
public id: number;
|
|
public username: string;
|
|
public password: string;
|
|
public nickname: string;
|
|
public email: string;
|
|
|
|
// timestamps!
|
|
public readonly createdAt!: Date;
|
|
public readonly updatedAt!: Date;
|
|
public readonly deletedAt!: Date;
|
|
}
|
|
User.init({
|
|
id: {
|
|
type: DataTypes.INTEGER.UNSIGNED,
|
|
autoIncrement: true,
|
|
primaryKey: true,
|
|
},
|
|
username: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false
|
|
},
|
|
password: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false
|
|
},
|
|
nickname: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false
|
|
},
|
|
email: {
|
|
type: DataTypes.STRING,
|
|
}
|
|
}, {
|
|
sequelize,
|
|
timestamps: true,
|
|
paranoid: true // 对模型施加了一个软删除
|
|
})
|
|
// 覆盖User的toJSON方法
|
|
interface User{
|
|
toJSON: ()=> UserOuput
|
|
}
|
|
return User
|
|
};
|
|
|