- [Instructor] Before we start adding operations
to our database and the corresponding end points
we need to create a Schema.
The Schema is what dictates what types
of data and the structure
of your data your database takes.
In other words if you define the first name key
to take only strings as data
the database will not accept anything else.
Therefore the Schema defines the rules related
to what your database can accept.
So let's go ahead and do this.
So you want to be in the CRM model file
which sits in the model's folder.
And what we're going to do is first import mongoose
because this is what we're going to use
to define the Schema.
And then we create an object called Schema
with mongoose.Schema.
And then we're exporting an object
that we'll call ContactSchema with a new Schema.
So basically we're leveraging the Schema
from mongoose to create a new Schema like so.
Okay so in our Schema we can take a few things.
So we can take a type
and then we can also do a require
and we can do defaults.
So defaults is basically a way
to telling that if there's no data
then default to this.
So let's go ahead and create the first one.
So we're going to create firstName
as the first item in our Schema.
And then we take the type
and by the way there's multiple ways
to create a Schema.
So if you want to know all the different options
to create a Schema just take a look
at the documentation on mongoose.
But this is the way that I do it.
I like to have a clean way
of stating what are the types
and if we require something
in the defaults you could also do something
similar to this.
Let me just give you an example.
Let's do lastName and then simply say String like so
as the type.
But I like to do it with an object,
and then define a type,
and then with that object allow anything else
to be added if I need to.
Then we'll continue so let me just copy
what I have in here
and paste in inside of the last in the object.
Enter a last name
and then we can continue down this path
with all the other data points that we need.
So I'm going to copy this one, create a new one
which is going to be email is also a type string.
We don't need to require this one
so we can remove that.
And you can remove the comma if you want.
And then continue down the path
of the other data points.
So we have company as well
and I'll cover how to do the defaults
in a second.
And then we do an phone number.
Which this one is a type of number.
So if you want to have a quick reference
of all the other types that exist
you can do a search on mongoose.
So typically you have Booleans
and let me just show it to you.
So once you're on the https://mongoosejs.com
you can go on read the docs
and then you can go into Schema Types.
And you can see all the different Schema types
that are available.
So you have String, Number, Date, Buffer, Boolean,
Mixed, ObjectId, and so on and so forth.
So you can take a look at each one of em.
So if you're not sure which type covers
the one that you want to put
in there you can read through all of these.
In general you're going to do the String, Number,
and Boolean.
And if you need anything else you can take
a look at the other ones.
Okay so let's continue.
And after this last one we have
the created date.
So I'd like to keep in my database
when that specific object has been inserted
in my database.
So when I create a new contact I want
to know when it was created.
So this one what we're going to do
as opposed to do a type of string
we're going to do a type of date
and we're also going to have a default.
So this is another way
of entering a default
when you don't have any data
that is sent to your database.
So if for example you absolutely need something
on every field but you don't always have data
that has been brought in into your required body.
Then what you need to do is have a default
then you can add anything you want.
So in this case what I'm going to do
is do Date.now which will allow us
to have the date when this data has been created
in our database.
So I'm going to remove the last comma here
and then finish my Schema.
All right so let's save this.
Okay so now we have a MongoDB instance running.
We have mongoose connecting to it,
we have a Schema set up,
so right now if you look at the MongoDB instance running
on Robo 3T our making calls on the consult
to Mongo the database is empty
or least we don't have any items from our contacts yet.
We'll start adding new items
when we create the post end point
and we'll do this next.