Brahm Bind Singh
Brahm Bind Singh

Reputation: 33

interface for model and general in ts

I am stuck in a situation please help.

I created a interface for User Model in mongoose and ts. the model has profile pic field which consists of a reference to document model. so my user schema model interface consists profilePic: mongoose.Schema.Types.ObjectId. but when i fetch user details from db using .lean() this interface fails as it removes mongoose properties. how to resolve this?

Basically whats the best way such that i can create a interface for mongoose schema and alos can use a interface for lean scenarios?

interface UserDocument extends Document{
    _id: mongoose.Schema.Types.ObjectId
    firstName: string,
    lastName: string,
    email: string,
    phone: string,
    countryCode: string,
    isEmailVerified: boolean,
    isPhoneVerified: boolean,
    password: string,
    isActive: boolean,
    gender: EGender,
    profilePic: mongoose.Schema.Types.ObjectId,
    createdAt: string,
    updatedAt: string
}

const UserSchema: mongoose.Schema<UserDocument> = new mongoose.Schema<UserDocument>({
    firstName: {
        type: String,
        required: true
    },
    lastName: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true,
        unique: true,
        lowercase: true
    },
    phone: {
        type: String,
        required: true
    },
    countryCode: {
        type: String,
        required: true
    },
    isEmailVerified: {
        type: Boolean,
        default: false
    },
    isPhoneVerified: {
        type: Boolean,
        default: false
    },
    password: {
        type: String,
        required: true
    },
    isActive: {
        type: Boolean,
        default: true
    },
    gender: {
        type: String,
        enum: Object.values(EGender),
        required: true
    },
    profilePic: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "CustomerDocument"
    }
}, { timestamps: true })

Upvotes: 0

Views: 15

Answers (0)

Related Questions