UNPKG

create-bar-project

Version:

This module helps create a base for web application projects.

37 lines (33 loc) 877 B
import cryptPassword from 'Common/Utils/Bcrypt'; import mongoose from 'mongoose'; const Schema = mongoose.Schema; const UserSchema = new Schema( { firstName: { type: String, required: true }, lastName: { type: String, required: true }, email: { type: String, required: true, unique: true }, password: { type: String, required: true }, phoneNumber: { type: String, required: true }, birthDate: { type: Date, required: false }, role: { type: mongoose.Schema.Types.ObjectId, ref: 'Role', }, }, { toJSON: { transform(_doc, ret) { delete ret.password; delete ret.__v; }, }, }, ); UserSchema.pre('save', function (next) { const user = this as any; if (this.isModified('password')) { user.password = cryptPassword(user.password); } next(); }); export default mongoose.model('User', UserSchema);