🚀 Beta mode be careful of the bugs
Mongo-db

Model

Model for authentication and store data in the database.

Overview

The model is a collection of data that is used to store information about a user. It is used to store information about a user, such as their name, email, and password.

Note

Please setup the mongodb database first.

Model Setup

To setup the model, you need to create a new file called model.ts in your project root.

import mongoose, { Schema } from "mongoose";
 
export interface User {
  _id?: string;
  username: string;
  email: string;
  password?: string;
  role: "user" | "admin";
  createdAt?: Date;
}
 
const UserSchema: Schema<User> = new Schema({
  username: {
    type: String,
    required: [true, "Username is Required"],
  },
  email: {
    type: String,
    required: [true, "Email is Required"],
    unique: true,
    match: [/.+\@.+\..+/, "please use a valid email address"],
  },
  password: {
    type: String,
  },
  role: {
    type: String,
    enum: ["user", "admin"],
    default: "user",
  },
  createdAt: {
    type: Date,
    default: Date.now,
  },
});
 
const UserModel =
  (mongoose.models.User as mongoose.Model<User>) ||
  mongoose.model<User>("User", UserSchema);
export default UserModel;
  

On this page