UNPKG

@diagramers/api

Version:

Diagramers API - A comprehensive Node.js API template with TypeScript, Firebase Functions, and Socket.io

94 lines (90 loc) 1.77 kB
import mongoose, { Schema, Document } from 'mongoose'; import { IUser } from '../entities/user.entity'; export interface IUserDocument extends Omit<IUser, '_id'>, Document { password: string; } const userSchema = new Schema({ email: { type: String, required: true, unique: true, lowercase: true, trim: true, match: [/^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/, 'Please enter a valid email'] }, username: { type: String, required: true, unique: true, trim: true, minlength: 3, maxlength: 30 }, password: { type: String, required: true, minlength: 6 }, firstName: { type: String, trim: true, maxlength: 50 }, lastName: { type: String, trim: true, maxlength: 50 }, phone: { type: String, trim: true, match: [/^\+?[\d\s-()]+$/, 'Please enter a valid phone number'] }, avatar: { type: String, trim: true }, isActive: { type: Boolean, default: true }, lastLoginAt: { type: Date }, emailVerified: { type: Boolean, default: false }, phoneVerified: { type: Boolean, default: false }, role: { type: String, default: 'user', enum: ['user', 'admin', 'moderator'] }, permissions: [{ type: String, trim: true }], status: { type: Number, default: 1, enum: [1, 0, -1] // 1: active, 0: deleted, -1: suspended } }, { timestamps: true, toJSON: { transform: function(doc, ret) { delete ret.password; return ret; } } }); // Indexes userSchema.index({ email: 1 }); userSchema.index({ username: 1 }); userSchema.index({ status: 1 }); userSchema.index({ role: 1 }); export const UserModel = mongoose.model<IUserDocument>('User', userSchema);