79474109

Date: 2025-02-27 22:58:06
Score: 1.5
Natty:
Report link

I am a bit late but i think this is good approach (source: https://medium.com/@agentwhs/complete-guide-for-typescript-for-mongoose-for-node-js-8cc0a7e470c1)

mongoose Schema:

  gender: {
    type: Number,
    enum: [0, 1],
    default: 0,
    required: true
  },

TS enum:

enum Gender {
  Male = 1,
  Female = 0
}

Then use of virtual or method:

// Virtuals
UserSchema.virtual("fullName").get(function(this: UserBaseDocument) {
  return this.firstName + this.lastName
})

// Methods
UserSchema.methods.getGender = function(this: UserBaseDocument) {
  return this.gender > 0 ? "Male" : "Female"
}
Reasons:
  • Blacklisted phrase (0.5): medium.com
  • Probably link only (1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Marek Javůrek