Nestjs Mongoose Tutorial #4 | Schema

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we will learn how to create a resource in njs and how to uh give access of model to a particular resource so next comes with n CLI uh you know uh you have it installed before you work with njs project so if you click Nest help it will give you certain set of commands like this uh you can see there are certain commands next new is used to uh create a new project since we are already using a reposit so we didn't we didn't do this uh the most important thing that I want to show you is Nest uh generate and it will help you generate boiler plate code for whatever you want to generate it for right so right now uh you can create a controller a service you know stuff like that uh but you can also choose to create an entire CR Resource you know so you don't have to do anything and everything manually so if I do something like Nest G resource and name it user uh it will ask me what kind of resource I want to create for example rest API graphql there are two types of cql here um and the third one is I think microservices so let it uh load and you will see the options see R API yeah there is web sockets as well so I want R API I would uh press enter would you like to generate credit entry points I would say yes press Y and hit enter and it will now create all of those files you can see if you come to the source this user directory is created and here Services service spec controller controller spec spe file is for testing these all files are created so similarly user module is also created like this where all of these things are registered you know controller as well as service and most importantly you know whenever you create a new module you have to register it into app module and that is also registered here as well so hit save so that the BR problem is resolved see it is just some sort of a problem I would say fix it so that to the next line and this is perfectly fixed so d means data transfer objects you know it defines how your you know request should be look likeing you know how the your request should look like so dto can be used for transformation as well as validation purpose in N CH so we don't need entities because we are not working with typm or any other sort of thing so let me just delete that part now how about I go here and I create a schema so let's create schemas folder you know the short is you uh do something like this user. schema. TS if I do schema SL it will automatically create that folder for me and the file inside of it so that is pretty much cool to create a schema uh you would need to import some stuff like prop schema uh schema Factory from n j import document from and then Define the schema something like this let GitHub goer do the work so I think it has defined it I don't want to extend this here so this is mostly uh like you know boiler plate code I want this to be required uh not username but rather email and I also want the password to be required as well like this so what is the error that I have done here so if I do something like um Quick Fix solve R problems so it will fix those I think I have matched up you know the control S button for automatically CR to activate so if I go here now um I can do you know some sort of optional property as well you know these two are required so for for example option option means some documents may have that property and some Doc and some documents may not have that propert property so something like um first name String and prop last name String something like this and I guess I I would have to keep doing uh these pror fixes and the next uh step you have to do is you have to define the type of the document something like this and then the schema I think it is doing the job for me so this is kind of a uh you know like a boiler plate code that you would have to do you know if you want to create um some other document uh you know some other schema for example by the name of course you would name the class as as course you would define all of your properties here the type whatever the type you want for example you could name it to number or all of the other schema types that we uh learn in previous video it could be any of those types that you can Define here and this is how you define the schema in a form of a class this decorator means that this particular class is a schema file and then you have imported uh you have you know used schema Factory to create a schema from the class which is by the name of the user and then you have exported this user schema so the reason we have exported all of this stuff is because uh we would be importing it in other important files as well as well so what we are going to be doing now is creating some sort of relationship between this schema and our entire user module here and you know um we would be doing Sy in our Imports just like we did in our main application module so something like we have to import um module obviously and then we have to import user schema that we just created user and user schema from the file right now we have to do something like this perfect so it should be Imports okay hit save now what we have done so far is we have created a schema called user schema and we have created a link between user schema and user module so we are saying that register this the module by the name of user in this user module right so that is what we have done so that we can use the user module uh user model of our database in this particular module okay so we would be doing this with the with the help of injection so dependency injection is a very broad Topic in njs it simply means that uh there are certain objects which are already resolved and they are currently inside ioc container of njs and they are ready to be used by any other uh you know controller which intends to use them controller or service or whatever uh whatever the thing that wants to use this okay thank you for watching
Info
Channel: Shameel Uddin
Views: 56
Rating: undefined out of 5
Keywords: nestjs, nestjs mongodb, nestjs mongoose, mongodb, mongoose, nestjs mongoose tutorial, nestjs mongodb tutorial, nestjs tutorial, mongodb tutorial, mongoose tutorial, nestjs vs express, nestjs microservices, nest.js, nest.js mongodb, nestjs crash course, nestjs api, nestjs rest api, nestjs jwt, nestjs authentication, nestjs access token, nestjs jwt guard, nestjs passport, nestjs passport jwt, nestjs prisma, nestjs e2e, Shameel Uddin, nestjs/microservices, nestjs course
Id: tTnx7yq5Ack
Channel Id: undefined
Length: 7min 41sec (461 seconds)
Published: Fri Feb 16 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.