import mongoose from "mongoose"; const ROLES = ["user", "admin"]; const UserSchema = new mongoose.Schema( { username: { type: String, required: true, unique: true, }, password: { type: String, required: true, }, points: { type: Number, default: 0 }, role: { type: String, enum: ROLES, default: "user" }, securityQuestion: { type: String, }, securityAnswer: { type: String }, lastLoginAt: { type: Date }, token: { type: String, default: null, }, }, { timestamps: true } ); UserSchema.index({ "username": 1 }); // username 索引 export default mongoose.models.User || mongoose.model("User", UserSchema);