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.
60 lines
1.6 KiB
60 lines
1.6 KiB
import { Sequelize, DataTypes, Optional, Model } from "sequelize"
|
|
|
|
interface UserAttributes {
|
|
id: number
|
|
username: string
|
|
password: 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
|
|
|
|
// timestamps!
|
|
public readonly createdAt!: Date
|
|
public readonly updatedAt!: Date
|
|
public readonly deletedAt!: Date
|
|
}
|
|
User.init(
|
|
{
|
|
id: {
|
|
type: DataTypes.INTEGER,
|
|
autoIncrement: true,
|
|
primaryKey: true,
|
|
},
|
|
username: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false,
|
|
},
|
|
password: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false,
|
|
}
|
|
},
|
|
{
|
|
modelName: "User",
|
|
sequelize,
|
|
timestamps: true,
|
|
paranoid: true, // 对模型施加了一个软删除
|
|
},
|
|
)
|
|
// 覆盖User的toJSON方法
|
|
interface User {
|
|
toJSON: () => UserOuput
|
|
}
|
|
User.associate = function (models) {
|
|
models["User"].hasOne(models["UserInfo"])
|
|
}
|
|
return User
|
|
}
|
|
|