Mongoose database connection
const mongoose = require ( "mongoose" ) ;
const connectDB = async ( ) => {
try {
mongoose . set ( "strictQuery" , true ) ;
const password = "password" ;
const database = "database_name" ;
const conn = await mongoose . connect (
`mongodb+srv://aadilmallick:${ password } @cluster0.kqerb.mongodb.net/${ database } `
) ;
console . log ( "connected!" ) ;
} catch ( e ) {
console . log ( e ) ;
}
} ;
module . exports = { connectDB } ;
const mongoose = require ( "mongoose" ) ;
const userSchema = new mongoose . Schema ( {
name : {
type : String ,
required : true ,
lowercase : true ,
minLength : 2 ,
maxLength : 25 ,
} ,
createdAt : {
type : Date ,
immutable : true ,
default : ( ) => Date . now ( ) ,
} ,
age : {
type : Number ,
min : 1 ,
max : 100 ,
} ,
email : {
type : String ,
validate : {
validator : ( e ) => e . includes ( "@" ) ,
message : ( props ) => `${ props . value } is not a valid email` ,
} ,
} ,
bestFriend : {
type : mongoose . Schema . Types . ObjectId ,
ref : "User" ,
} ,
} ) ;
const User = mongoose . model ( "User" , userSchema ) ;
module . exports = User ;