This file defines the User model for MongoDB using mongoose. We start with a TypeScript interface so the fields stay typed in the project. The schema tells MongoDB what each field should look like. timestamps adds createdAt and updatedAt automatically. The model export checks if a model already exists, which avoids errors when Next.js reloads during development.
import mongoose from "mongoose";

interface IUser {
  _id?: mongoose.Types.ObjectId;
  name: string;
  email: string;
  password: string;
  image: string;
  createdAt?: Date;
  updatedAt?: Date;
}

const userSchema = new mongoose.Schema<IUser>(
  {
    name: { type: String, required: true },
    email: { type: String, required: true, unique: true },
    password: { type: String, required: true },
    image: { type: String }
  },
  { timestamps: true }
);

const UserModel = mongoose.models.User || mongoose.model("User", userSchema);
export default UserModel;