User.js 709 B

123456789101112131415161718192021222324252627282930313233
  1. import mongoose from "mongoose";
  2. const ROLES = ["user", "admin"];
  3. const UserSchema = new mongoose.Schema(
  4. {
  5. username: {
  6. type: String,
  7. required: true,
  8. unique: true,
  9. },
  10. password: {
  11. type: String,
  12. required: true,
  13. },
  14. points: { type: Number, default: 0 },
  15. role: { type: String, enum: ROLES, default: "user" },
  16. securityQuestion: {
  17. type: String,
  18. },
  19. securityAnswer: { type: String },
  20. lastLoginAt: { type: Date },
  21. token: {
  22. type: String,
  23. default: null,
  24. },
  25. },
  26. { timestamps: true }
  27. );
  28. UserSchema.index({ "username": 1 }); // username 索引
  29. export default mongoose.models.User || mongoose.model("User", UserSchema);