- Mongoose uses schemas to define
the structure and data types within a document.
To create and manipulate data within a defined schema,
Mongoose gives you a the concept of models.
A model is the programming interface
that lets you create and
manipulate data based on the schema.
We will now create a schema that stores users.
For that I will first create a new folder.
"Models inside server" and inside that folder,
I will create a new file "UserModel.js".
On top of this file I will bring in mongoose
because mongoose require mongoose
and we also want to add some functionality
to validate the data and validate
the email address is a solved problem
so we will install a module for that.
So in the terminal I run npm install --save
enter module is called email-validator
and while this is running let's
bring it in email validator=require
Email-validator
Now let's start creating the actual user schema
For that add const UserSchema =
mongoose.Schema (); first argument
which is an object that contains
the data definition for this module.
So if we want to store users, first
of all a user has a username and
this username should be from type string.
It should be required so we
won't except users without username.
We will trim the included data to remove
trailing spaces or white spaces
We want usernames to be unique
so I add index unique true and
the min length for any username should be 3.
We now see here this arrow indicators
so it is complaining that the indents are wrong.
If this happens just click on the bottom bar
on spaces and select indent using spaces.
And then just 2 because that's the default
of our configuration and then we
can alter them and it's fixed.
Next we will add a property for email
because every user should also
have an email address.
For that I can basically copy the
configuration for the username and modify
that and so it is still a string.
It should be required, it should be trimmed.
Additionally, it should also be lowercase
because email addresses are always case insensitive.
We don't need a minlength, but what we
want to do now is we want to validate this email
address so I add validate and
I can define the validator function here.
So I add validator and we use this
email validator module that we used before
and there we pass in the function validate.
If the email address is wrong then we will
give the user a message which should be
${props.value} is not a valid email address.
Next users should also have passwords
so add a new property password
and I will again copy over everything
we have here for the username.
It's string, it's required, it should be trimmed
and the minlength should be 8 in case of a password.
In the end, I also want mongoose
to automatically create time stamps for each document.
The second argument I add now
the configuration time stamps, true
so that's a configuration option that makes mongoose
create and add and updated time stamp
for each document.
And in the end add module.exports
=mongoose.model('User') and I want
to use userschema so the model
is called user and uses the userschema.
So let's try that out for that I already
created the test so I simply run
npx mocha test/server/models/UserModel.test.js
and we see that it runs through
so what did we test here?
We tested if we can create a user,
if user with a too short username is rejected,
if a too short password is rejected,
if an invalid email format is rejected,
and that a user that was created
is also findable in the database.