Hi everyone.
In the previous video we already defined the schema.
And in this video, let's see how we can use the schema.
Okay, to create some data inside the database.
Okay.
First let me reminder for you the concept of single responsibility.
Okay.
In the first section we already know the controller.
It will go to service right.
And from the service we will call into the repo.
And now let's understand what is the repo.
Right now when we work with the ORM.
The ORM right here will be the repo and the ORM object relational mapping.
Right here will be the Mongoose.
Mongoose
Right here.
Okay.
The moongose.
Right here is the.
Object relational mapping.
And so for that reason, we will treat as a "repo".
And from the service we will call inside the "repo".
Okay.
We will use dependency injection to call the object relational mapping.
And now let me show you how we can do that.
Let's scroll down a little bit.
And right here inside the "service" we will inject okay.
We will inject the model.
Now instead of
Remember I will copy also and try to understand what happened.
I will copy and go to the service right here.
And I will paste it inside the constructor right here.
And I will import InjectModel from nestjs/mongoose and cat.
I will rename to post okay.
And the model.
Model and the post class
Now let me see oh sorry.
Sorry.
Because we must import it from the schema okay.
And replace with schema.
And with this syntax, we will inject okay.
We inject the repository.
Now let remember the how the DI work okay.
When we bootstrapped the application.
It has a IOC container, right?
With IOC container right here, the framework already create the object of the cat of the post model.
You can say, when we run the application it already have a post model inside here.
And when from the service.
We will look at the IOC container and go to the post model and look.
That's, right?
And we will inject the post model into the service.
So for that reason this code right here it try to do that.
Okay.
Go.
That's enough for you.
Don't try to remember us.
Is that of?
Understand how things work is very important.
Okay.
Don't try to understand.
What's the InjectModel
Okay.
What is the post name?
What is the model?
Right here.
Model right here.
It's not important at all.
Okay.
Understand how things work?
Right here.
Is not important.
Instead of show, instead of.
Let's try to understand why we do that
Okay, now we already define
Constructor.
I will create public method.
Oh, sorry for that.
We will create a create method to create some data.
Okay.
And now let's see how we can create a data, let's see.
Inside here we will use the syntax like like this right.
It will go to the model and use something called create cat dto
DTO okay.
The term "DTO" right here I will show you what is "DTO" later.
But right now don't worry about that.
Let's see how we can create us.
I will define the created.
Right.
createdPost and new this.postModel and instead of
I will pass it with different object.
And in the object right here we have a title.
Right?
Because in the post schema we have a title.
So right here is suggest me.
For example "post 1" and description
Description.
One.
Okay.
And I will return createdPost
And inside the post.
Controller.
Okay.
I will create a private.
Sorry.
We must create a constructor to inject the service post.
Post Service
Okay.
That is, we can mark it.
Read only if you want.
With this I will create.
Sorry guy.
Okay, I will call.
data = await
Or we can return directly because we will return this.
To use async await right here
Okay, I will return.
I will return this.postService.create
Okay.
That is we will return for the user.
And with the controller we will rename it to the posts.
Okay posts, this is a common way to do the rest API.
And we will use the @Post decorator because we will create a.
We will create data, right?
For that reason we use the @Post method.
And with the @Post method we can define some property.
Sorry some URL like this.
Because with the controller we already maked with the "posts".
So right now we don't need it all.
If you add, for example, like something like this, it will concatenation from the "posts" right here.
Okay.
It will concat it with with my string inside the @Post.
It will concat is something like this.
Okay.
Hopefully you already understand what is the string right here inside the controller.
But for now let me remove that.
So it will use.
Default will be "posts"
Right.
Now let's go to here and open the post man and create a collection.
I will create a collection and I will rename.
This will be the nestjs tutorial
And I will add request.
Okay.
And change the methods to POST and use the http://localhost:3000
Okay.
And right now I will use /posts
Okay.
Because we already defined a "posts" over here.
So we use /posts
And right now we don't need to body or something like this because we already hardcode.
We already fixed the title and description.
Right.
Let me send now it will show the created and something like title description.
And because we work with MongoDB so we have a default will be underscore ID right here.
And because we use the @Post the @Post right here, it will automatic create a status, right?
Very cool.
Let's check the document inside here.
Let's check the collection.
Let me refresh.
Or you can click on refresh right here.
Now you can see we don't have anything inside the posts.
Why why that
Because we forget return .save() method right.
So for that reason, go to the right here and return.
We will return .save()
Okay.
And .save() right here is the promise.
So we can use a async await if you
Know, but it's not important because we return.
Directly right.
It is just the normal JavaScript knowledge.
I will not explain that.
So because we already save, let me send again.
Now you can see because when we save, it has one more flag right here.
But let let's ignore that and go to the database and I will refresh again.
Okay.
We already have a data right.
Very cool.
And that's how we can work with the database.
So we can findAll, try to do that
I will pause the video right here.
And I will see you in the next one.