Prisma ORM crash course by building a RestAPI with Nodejs Express

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay guys so welcome back again so in this video let's see that how do we use the prisma orm and for that we'll be building a restful api using express as our backend and i think that an api is the best way to describe all the credit applications of prisma so without wasting any time let's dive into vs code and let's see that how it is done so here inside vs code i've created an empty directory called prisma api grid and if i do ls here we see that it is an empty directory right now so what we will do is that that will fold the express application inside this folder and to scaffold an express api we'll be using this package called express draft because it will provide us with the basic skeleton of our express api application so to install it globally you need to simply do npm i ifng and express draft to install it globally so let's do it here so i've already done this that is i have already done this that is npm i have ng express draft to install it globally but you might need to do it so now let me clear out the console and now to initialize the new express application what i simply need to do i need to simply do exp and dot to scaffold the express api application in the current folder which is in our case is prisma api grid so let me do exp dot and let me press enter and now it will create some files here inside this folder and now we see that we have this package.json file with some scripts here or fill with some dependencies here that is the dot env express and http errors to handle all the http errors and then we have morgan as the logger here and then we have a dev dependency called nodemon which is used to restart the application whenever it sees changes inside the dot js files by default and if we have a look at the dot env file we simply have a one environment variable that is called port and that is mapped to port 3000 and now if we have a look at the app.js file we see that it is already folded for our application use case here that is we have the default express application here and we have one endpoint here that is app.get and then this forward slash so let me try to start this application so if we have a look at the package.json file we have a dev script and we have the other start script so let me do npm run div to start the application in development mode so we see that the application is started and now if we go to local host port 3000 we see that or we get a response back from our api or from our server that is awesome it works so we know that our express application is working so now we have one more route here inside this folder uh or inside this application that is api route.js which is mapped to all the requests coming to forward slash api so if we go to this api route dot js and if we go to the index route of api we see that we should be getting this message here that is okay api is working so let's try that out so if we go to local sports 3000 forward slash api uh not hello but uh and we see that this is handled by the express error handler that we created inside the application which is done by default by the express draft package so this thing is also an additional use case here for using this package here so if we go to the forward slash api we see that so we see that the application is working and now what we need to do we need to create a couple of more routes here so before defining the routes here the model on which we are working upon is the products model that is we will be making api for the products thing here that is we should be having a route to get all the products we should be having a route to get an individual product around to delete and product a route to update a product so that's what we are going to build in this video and our product model would be linked to a category model which in itself is an api that is if you want you can create an api for the category also and in our case the model would be related like this that is a product would belong to a single category but one category can contain many products so this would be a one two many relationship though i could have chosen to make this api not to have any relations but like it would be more intuitive to work with prisma using relations so that's why i've chosen this thing here that is we should have our two models that is the product model and the category model so without wasting any time let's see that how do we install prisma so for that what we need to do we need to stop the api first and let me clear out the console first and now what i need to do i need to install a dev dependency called prisma which would give us the prisma cli so i'll do npm i hyphen d to install it as a dev dependency and it would it would be called prisma and it would take a moment or so and then we are going to install something else and that is called the prismaclient which would allow us the access to the prisma database so let me install the prismaclient so for that i need to do npmi at prisma forward slash client so this is the client which we are going to use in our application to make all the queries to the database and like it would make more sense once i'll do all of the stuff here so just wait for a second or so to install this prisma client and now let me clear up the console and now we see that we have these two dependencies installed that is the prismacline and the prisma as the div dependency and now to initialize a prisma project here inside the current project what we can do we can use this prisma cli which we get from this div dependency called prisma what we can do or we can simply do npx prisma in it or simply like this though i could have manually created all the files here inside the some folders here which are like by default but like it is more intuitive and it is more easy to create your schema using this npx prisma in it thing so it would initialize a new plasma project here inside the current project so now let me simply press enter and now it would do a couple of things here so we see that we got a folder here that is called prisma and we get something here inside the dot env file also so let me minimize the terminal once here so we see that we have these comments here that is this was inserted by prisma in it and that is what we already did and rather because of that this thing is here and we have these things here which you can simply ignore or the one thing to notice here is this database url and we see that this points to the postgres sql here and the beauty of prisma is this that you can use this prisma orm with any database you like if you like the sql database you can use it with sql databases uh like postgres like mysql database like the sqlite database or like mssql so whatever database you want to have as your backend or as a database you can use any database which you want or what you can also have you can also have the nosql database like mongodb to act as your database so this is the database url which you get here that is postgres sql and then forward slash something here but since so we do not have postgres installed locally on our system the database on which we are going to work is a relational database and that is a sqlite database which we are going to use so for that what i need to do i need to make a couple of changes here inside this database url which i am going to do in a moment so before changing this database url let's see that what's inside this prisma thing here so let me open this prisma folder here and we see that we have only one file here that is called schema.prisma so if you open this file we have two things here that is the database source which is currently set to postgresql and we would be changing it to sqlite in a moment and then we have this generator client and since we installed the prismaclient that is this package here that is this package prismaclient to query our database so this generator simply means that is to generate all the types for the model which we are going to make in a moment to be used with this prisma client so this is what is meant by this thing here but you can simply ignore this if you do not know you can simply keep it as it is and you should be only interested in this database source here or which is currently set to postgres sql and for this video i would be changing it to sqlite that is we want to use sqlite as our database here and now to use environment variables inside the schema.prisma file we can simply pass the environment variables like this we do not have to do process dot env and then dot something like this or we can simply put in a and v here and then we can say that in the parentheses we put in the key of our environment variable and that is the database url and since we are using sqlite we need to change the database url so let's go to the dot env file and instead of this thing here let me remove this thing out from here and so here let me simply provide here file dot forward slash and here what we can do we can simply call it like any database and let me call it let's say dev dot db uh that is the name of our database or the database file which would be creating for sqlite and that would be all what we need to do inside this env file so let's save the env file and let's go to the schema.prisma thing here and now what i need to do i need to define a model for a product so let's define a model like this so we can say model and then we need to give a name to our model and that would be called product in our case and now our product should have an id so let's pass in an id and then we need to provide in the type of this field here and in our case it would be an integer here and now what we need to do we need to make this field as the primary key so what we can say we can annotate it with at id that is this uh this field should be our primary key and then to provide in the default value for this field what we can do we can use one more uh like this annotation here that is at default and then we can provide in the default value and for this thing i can simply provide an auto increment so it would be like auto increment like this and then the id would be automatically generated whenever we create a new model or whenever we create a new product and now a product should have a name so let me say name and this would be of type of a string and now only to show you the different aspects of prisma or the different annotations which you can use inside a product model or inside any model i want to provide this field as unique it might not make sense but still i want to keep a product name to be unique so i can simply pass this annotation here that is at unique like this and then we should have a price for this product so let's call this field price and this should be an integer and then i would like to keep a default value for this price so we can pass in a default value like this that is 9.99 that would be the default price of this product whatever the product may be and then we need to create one more field here that is called created at and before we work ahead let me show you one thing here that is inside my vs code i've installed these two extensions that is prisma so if you go to this extensions things here and if you type in prisma you can install this extension on vs code that is this prisma by the prisma team so it gives you this beautiful ui or the the text is colored and then it would provide you with auto completion and the indentations of this model on this file so i would highly recommend that before working on upon prisma you should definitely install this extension called prisma and one more extension which i am going to use later on and that is called risk line to simply make a api request to our server that is this current server so this is the client which i am going to use to make api request from inside vs code and that is called risk line so this is also highly recommended you to install it so now let me close this thing here and let's go back to our thing here that is the model product and now we want to create one more field here that is called created ad that is when this product is created so for that let me call this field called created at and then this should be a date time field and then the default value should be also provided to this field and it should be now that is whenever this product could be created the current time would be saved whenever the product is created and now before defining the relation between this product and category uh let me save this thing here and let me define the model for category so let's define the model another model here called category so category and now this should have three fields that is the id of the category category the name of the category and which products it has so let me define the id here like the same as the product thing here so let me simply copy this thing from here and let me paste it here because the category should also have id so it is like this and then it also has a name field and this should be a string and now this category name should be unique so let me annotate it with add unique and then a category can contain many products so let me simply define this products here and this would be of type of product that is this product which we just created so now what i can do i can simply pass an array here that is a product product should contain many products and now we see that we get this red squiggly line here and now to fix this we have two things here we can simply save this file and this product model would be auto populated with the things which it would need to connect this category to this product and product to this category so let me simply save this thing here and now as soon as i save this thing here we see that we got two more fields inside this product model and that is the category and the category id so basically the relation is this that this field category is id that is that is this product belongs to a category which is which which can be nullable because there is a question mark here so it means that even if you want to create a product without a category this product can be created but if you do not want a product to be created without a category then you can simply remove the question mark here then we need to provide in the category whenever you want to create a product so this is this this works like this so now we see that we have two things here that is the category also and we also have the category id now to make a relation between this product and this category we have defined this category here and this relates to this thing here that is the fields and the reference id that is this field category id is reference to this id here inside the category model and now whenever we are going to create a new product we simply need to provide in the category id and we do not need to provide in the category here so this should be in a small small here not in caps so it would be linked to this category so like it it will make more sense when i'll do this inside the api but till now we have simply defined the product schema here and now before we can start working upon prisma let's go to this api route dot js and now here let me create a couple of more routes that is we should be having a route to get all the products so it would be like this so let's make a restful api first so this route would handle all the products so this route would be handling the individual product with an id and let me define one more route and this route should be a post route to make us create a new product so it would be a post route to this products route to create a new product and now we should have one more route and that would be to delete a product by an id so it would be called delete here and then we simply need to provide an id here that is the product we need to delete and then we need to have one more route and that would be a patch route here that is to update the products and then we also need to provide in the id here to update that is the product id to update so we see that we have all the five endpoints to our api that is getting all the products get a product id to create a product that is a post request to delete a product that is a delete request and then a patch request to update the product so firstly let me save this thing here and without and before working upon the api let me show you something here so let me clear out the console first so since we have installed prisma as a dev dependency so what we can do we can start the prisma studio to see that what all things are present inside our database using a gui interface so for that to install the prisma studio what i can do i can simply do npx prisma studio uh it would open the prisma studio and port i think double five double five so we see that we are getting these things here that is all the models here we have the category model also we have the product model also but if we try to go inside any of the model here we see that we won't be able to do this because we need to do a couple of more steps before we can do this stuff and even if we go to category we would be getting an error here because we have not created created any tables inside our database we have just defined the tables now to create a table inside any sql database you need to do migrations so let's go here and let's let me open another terminal here now to do all the migrations here inside the sql database using prisma what you can do you can simply do npx prisma migrate div that is since we are working in the development environment so i am passing in dev here and now if i do this thing here we see a couple of folders would be creating it created inside this prisma folder so we see that we need to provide in the name for this new migration so let's simply say that first whatever you want to provide it doesn't matter and now we see that our database is in sync with our schema which means that the necessary tables are generated and now if we go to our application here or this is not the application this is simply a gui interface to view the data what's inside the database so let me reload this thing here and now if i go to this category here we see that we are getting these fields here or these uh columns here that is for the id the name and the products which is currently empty and if you open another tab here and if we have a look at the products here we see that we have all the five columns here that is the id the name the price created at category and category id so this is a kind of a beautiful interface provided by prisma so that we can easily view our data but this is not the thing which we want to do we want to interact with prisma data but we want to interact with the database using the prismaclient so now since so we did this that is so we generated the migrations here so behind the scenes prisma also created one thing more so behind the scenes prisma did one thing more so what i did let me show it to you so if we have a look at the node modules folder and if we go to this dot prism of thing here that is this prisma we see that the client has been generated and what is meant by the client i would explain it to you in a moment so if we open this file here we see that all the type definitions for this uh for this for these models have been generated here that is we provided this id the name price and category id so what it would do it would provide us auto completion inside vs code or any other text editor and it would be super handy so i just wanted to show it to you so therefore i just opened this node modules folder and now if we have have a look at the migrations folder we see we have one more folder here inside the migration and if you open this migration.sql we see that prisma created also these things here that is to create a table inside a sql database it created this thing here so this is the beauty of prisma that you do not need to create your tables by your own you do not need to provide in this like ugly looking thing which is like writing sql statements is a nightmare in my case but not me but maybe not in your case but like i simply avoid writing sql statements by myself because you can have a lot of errors if you're writing it doing it by yourself so therefore it is very easy if you use prisma so if we go to this api route dot js and now instead of sending this response what we can do we can use the prisma client that we installed initially that is this prisma client and we can initialize the new prisma client inside our api router.js and then we can fetch all the products from our database so for that what we need to do we need to firstly require the prismaclient so we can say on something equal to require so let it be like this so let me make it larger here so at require at prismaclient like this and then what we need to do structure we simply need to destructure the prisma client here simply like this and now what i like to do i would like to initialize it that is the prisma client so what i can say i can simply say const prisma so since i'm in dev mode so i can initialize the prisma client here directly but in production you should only create a single instance of this prisma client so that it should not put overhead on your server so here i am simply going to do prisma equal to new prismaclient simply like this and now to get all the products i can simply say try catch because we are going to use a weight here and if there is an error i would simply call it next with the error as the only parameter and here what i will say i will say const products equal to await prisma dot products dot find many and then i can provide an empty object here simply like this and it would fetches all the products so and finally what we need to do we want to send back a json object containing all the products so we can simply say res.json and here we can simply pass in products simply like this so let me save this thing here and now what i'll do i'll simply create one more file here that is called risk client so let me simply call this create this file that is touch rest.http and now let me start the server here once again that is let me do npm run dev and now let's go to this rest.http and let me define in the first route to get all the products so it is running on localhost port 3000 and we need to go to this api route we need to get all the products simply like this and if we do make a request you should be getting an empty object back so we see that we are getting an empty response back and that is because we currently do not have any product inside our collection or inside our database not collections because collections are a part of mongodb so now why not let's create a product and a category here inside this uh inside this gui interface so let's go to the category first and now let me create a record here so firstly the id is automatic auto incremented the name of the product would be or the category would be mobiles and then currently it has zero products so let it be so now let me simply save one change here and we see that we have mobiles here as a category and now let me create one more one product by hand here so let me create add a record here so the id would be auto incremented the name of the product would be iphone 12 pro max or let it be like this the price i suppose maybe 11.99 not this much otherwise no one is going to buy it and now the created time is like now and the category we can select it here so since we created the mobile's category i can simply select it here and now let me simply save one change here and we see that this product is created and if we go back to our application and now if we try to make a request here we see that we should be getting one product here and that is because we have manually created this product inside the prisma gui interface or the prisma studio thing and we see here that in the response we are getting this response here that is the id of the product the name of the product the price the created ad time and the category id but what we want is not the category id we want to have the category name with the id included in that so that we can have the category object if we want to access it so what we can do we can go back to our api.js file and not in this thing here so let me open it here so we need to go to this api router.js and here what we can do we can pass in one more key here and that is include that is to include the category also so we can simply pass in category here and we see that we get this auto completions inside vs code and that is because of the prisma client that was generated when we did npx uh or migrate to npx prism migrate the client is automatically generated so we have these auto completions here and we can see that we can only pass in category here so here we can simply say true here that is to include the category also whenever we make a request and now if i go to this restart http and if i make a request here we see that this time we have the category id but we also have the category also a category also embedded inside here that is the name of the category or which this product is related to now if you are wondering that how to get a list of all the categories it is very simple let me show it to you though it is not a part of this api but let me show it to you that is const categories equal to await prisma dot category dot find many like this and i can put uh do it like this and then with the products i can also uh responds with categories here and this should be an object here like this and if i save this and if i go to make another request here that is to this url we see that we have the products here and we have the categories also but if we also wanted to include all the cat all the products inside this particular category or we can also modify our code here inside eparrow.js so let me close this thing here so let me save it and let me close it let me close it and here i can also do this thing here that is include and here i can simply say products to be true so that i can also get all the products inside a particular category so let me save this and if you go to this restaurant http and we should be getting a large object here right now so this is only for demonstration here so we see that we have the products here uh which we already saw what's what's inside it but now we see that we have this categories here and we have this first category name mobiles and then we have the products inside this category of ansys it this category only has one product so it is getting us a list of all the products that is inside this category so like you see that it is so easy to work with this prisma that uh making simple changes inside our query to the prisma database or the backend database using the prismaclient we can fetch all the categories with different products inside that category so this is like something easier for me to work with that is the prisma client and now let's see that how to get a product by id or firstly let's see that how to create a product and that would be a post request so let's go here and let me remove this line from here and now let me do everything inside try and catch because like uh i do it always this way that is so that i do not get any errors on the api so to create a product we need to pass in some request body and if we have a look at the schema.prisma file here to create a product we need a name for the product we need a price for a product otherwise the default price would be 9.99 and the created time we do not need to pass and then we also need to provide in the category id so that the product can be linked to a particular category id so let's go to our thing here this thing here that is api.js thing and we need to pass in the name the price and the category id so let's go to our api route and before working upon this api route let's create our uh request here so let me simply copy this path from here and let me paste it here and to separate these requests we need to provide in three hashes here and this was a get request by default let me uh let me explicitly express if i get here and this would be a post request and then we need to provide an adjacent kind of body here so let me provide in some headers here and that would be content type would be application json so application json would be the request body and then in the request body we need to provide in the name and let me provide in the name that is called iphone let's say 12 pro simply like this and then we also need to provide in the price of the product and this would be like 9.99 and then we need to provide in the category id so category id would be like say let's say one because we currently have only one category though in a moment i'm going to create a couple of more categories here so let's save this and now we can access this json object or this request body inside our api.js using request.body so let's go here so firstly what we can say we can say cons data equal to rec.body to access what's inside the body here inside this request and let me scroll up here and now to create a product we can simply say cons product equal to await prisma dot product dot create and then we need to provide an object here containing a key called data and here we can simply pass in request dot body or we can simply pass in this data here because that is also containing the requested body so let me remove this data thing from here so the data contains the requested body thing here and we do not need to manually specify all the fields but if you wanted to do so you can do it like this that is like this that is i would be rich dot body uh dot name like this and then you can specify all the different fields therefore for the price you can simply do it like this that is reg.body dot price like this and then we've then so on and so forth but since so we can simply use this body here request body so i can simply say red dot body like this and now that is the only thing which you need to do and finally you can simply rest.json and here you can simply pass in this product back to the client so product like this so let's save this and let's go to rest.http and now let me make a request we are going to make a post request with this body here and let's make a request and if everything goes well we see that we are getting an error here and that is because we need to provide this content type as application json and not in quotes here and this time i suppose that the product would be created because providing this application json in codes was not being able to pass this request body so it should be like this that is application json without the quotes and if we make a request here here we see that the product is created here with an idf2 and we have the category id also if we go back to our prisma studio and if we try to re refresh this products page here we see that now we have two products here and with we only have a single category so before creating more products let me add manual categories here so let me create tablets here uh like this and let me save this change and let me create one more category like uh maybe uh be like pcs uh let me save this and this should be like pc and not like this and now let me save this and now we have these three categories so let's go back and let's try to add some more products here so we see that in our product schema that is this product schema or this should be open inside this thing here so let me close this rest.http so let's save it and we see that two products cannot be created by the same name here because the name of the product should be unique so if we try to make the same request again we shouldn't be able to do so so let's go to this client here and let's try to make a request with this iphone 12 pro here and if we make a request here we see that we get this message here that is invalid product dot create because unique constraint is specified and that that was very true because the name needs needs to be unique inside the table and this is how it works that is a unique field so let's go to the api.js file and this is the way we create products here inside this router.post thing here and now what we need to look out is this that how do we get a product by id so let's handle this route here that is get product by id so let's remove this thing here and let's use try and catch a next error to handle the error like this and now to get a product by id what i can do i can simply say const id i need to destructure this id from the params equal to request dot prems like this and now i can say const product equal to avet prisma dot product dot find unique and here we need to provide in the query that is where id that is the id of the product would be equal to something and here let me simply provide in this id and this would give us an error because we see that we get a red squiggly line here and that is it says that a string is not assignable to type of number because whatever you send here as a request parameters they are all strings so what we need to do we need to downcast this string as a number here and that can be easily done in javascript using this number thing here and here we need to provide in this id and now this is valid that is whatever product id is coming here we can uh downcast it that as an id here and then we can simply fetch the product though for a typical production api you should be also validating this id here that whether this is a number or not that is whether this is not a number that is a nan here but this is this is the way we do it that is number and then id if the if this id string is a valid number and then finally we can simply say rest.json and here we can simply pass in product here not product but product so let's save this and let's so open this thing here and let me simply copy this query here or this request here and let me separate it by triple hashes and then we need to provide in the id of the product so let's simply pass in one because we know that we have a product with an id of one so let's go here and let's try to make a request here and we see that we are getting back this product here with an id of one so let's try to pass in an id which does not exist so let's pass in 23 and let's see that what happens so we see that we are getting back null because the no product exists here and if we pass in two so we should be getting the product with that id so we get iphone 12 pro max or only the iphone 12 and now if you also wanted to populate the category here what we can do we can simply go back to our api router.js and let's close this risk client and here what we can do we can pass in one more key here and that is include like in the products route to get all the product and here we can simply pass in category to be true to be populated here so let's save it and let's open the risk client here and now if you make a request to this route again that is to get the product with id 2 we see that we also have the category included in this response here so this is how we get a product by id so now let's see that how do we delete a product because that is almost similar as this getting a product by id so let's simply copy this thing from here that is this try and catch block and let's simply go to this product delete route here and let's remove this and let's paste in that try block here so we need to change this thing from here in a moment i'm going to do that but we do need to destructed this id using the request params here and then instead of using this thing here let me remove this thing from here so we'll say const deleted product equal to await prisma dot product dot delete and we see that we have many options here that is to delete an individual product or delete many products so we are simply interested in deleting one product as of now and here we need to pass in the object containing the where clause that is where id would be this number id and the reason is again the same as it was before that is we need to downcast this string as a number here because this id inside the product is an integer inside our model and then we simply respond back as the deleted product here so let's save this route or this file and let's go to the rest.http file and let me simply copy this route from here and let me paste it here at the bottom and this would be a delete request not the get request and then we need to pass in the number of the product or the id of the product which we want to delete so if we simply query this product that is product number two we get back this product that is iphone 12 pro and if we make our request here that is the delete request we should be getting back this product or this id that was deleted and if we go back to prisma studio or we reload we see that we only have one product remaining here and we can also verify it with our api to get all the products and we see that we are getting only a single product here that is with an id of one because product number two is deleted so let's quickly create the second product once again so let's create this product and this time the automatic increment id is 3 and we have created iphone 12 pro and now let's see that how do we update the product so let's say that if we want to update the product with this id that is id number three with the price of 12.99 so let's say if we want to do that so for that what i'm going to do i'm going to create one more route here and that would be a patch route and the url would be the same thing as this thing here but the id would be three so let's put in three here and then we need to provide in the content type for this request so let me close the sidebar here so the content type would be application json and not in code like the previous time and then we can provide in the body here and here we can simply say we want to change the price of this product number three with an id of three the price should be like 12.99 like this and let's also say that we want to change the category here for this project just for demonstration purposes so let's say that we want to change the product category here so the category would be let's say category ivd would be let's say two so if we go here and if we go to the categories here inside prisma studio oh we see that uh we have the id number two associated with tablets here and currently it has zero products and if we have a look here we see that we have two products here the iphone 12 pro which with an id of three and the price of 9.99 so we are going to change the price of 9.99 to 12.99 and we are going to change the category from this uh category from uh mobiles to tablet so that's what we are going to do so we need to pass in the category ids 2 here for the patch request so that's what we are doing here we are changing the price to be 12.99 we are changing the category id to be two and why not let's also change the name here because an iphone doesn't belongs to a tablet so let's also change the name here and let's put the name here that is iphone not iphone but ipad pro max like this so now we are completely changing this product to have the name of iphone ipad pro max the price 1299 and the category id2 and we are making a request to this route here so let's handle this request first inside our api router.js so for that we are going to use this route here that is the patch route so we again do try catch uh next error next error like this here firstly we are going to get the id from the request or params because that is the first thing which we need to do to simply find the product on which we are going to work upon and then we can simply say const product equal to await prisma dot product dot update and here we need to pass in an object so firstly we need to pass in the where clause so where the id would be this id and here for the same reasons we are going to pass in the number and then we need to pass in the data that is the new data and we can get that data from request.body thing here and then for obvious reasons we also want to include the category in the response so we can say include category to be true or like this and now what we can do we can simply rest.json that is product so let's save this and if everything goes well we should be updating this product here so let's go back here and if we firstly get the product with this id number three so let's get this product with this id number three so we see that we have this product here that is iphone 12 pro and now we are going to update this product with this request here that is this patch request so if we make a request here we see that we get back a product with an id3 the name has been updated the price has been updated and the category has also been updated as we can see here and if we try to get this product with an id of three again we see that we get back the same response and if we go back to our prisma studio and if we refresh this page we see that now inside the products collection we have ipad pro max price of 12.99 and the category is this category that is tablets here that is this product belongs to this category that is called tablets here and if we go to the categories here and if we reload we see that mobile contains one product tablets contain one product so this is all is working here and let's try to make a request to all the products again in this time we see that we get back an array of two products here with an id of one and id of three and as well as we are getting back a list of all the categories here that is firstly we get back an array of categories with an id of one which contains mobiles and it has only these products and then over the category id of two we have this tablets here and the products contained in this category is this and with an id of 3 that is the pcs we have no products here so this was about this thing here and now let me show you one more thing or the one more last thing here so let's say if you want to add one more field to this product here so let's say we want to create a field here that is whether it is the quantity of the product so let's say we wanted to add a quantity for this product and this should be an integer so let's save this so now if i go to api route dot js and if i want to access this quantity from this product model so this won't work and i will tell you why so let's go to this route here that is getting a product by id so we see that if we want to access the name we can simply say console.log so console.log product so it should automat our product dot or we see that we have these fields only that are populated by prisma generated client but we do not see quantity here it only has the id the name the price the created ad the category id and the category but not the quantity and that is because the prisma client that was generated when we did input migrations only generated these definitions for this model and now to again generate the definitions for the updated model what we need to do or let me comment out this line what we need to do we need to stop the application once again and we simply need to do issue one command and that is called npx prisma generate like this and it would generate the definitions for this product again so let's press enter and this time we see that the line has been generated again and if we do npm run dev again and if i do use this thing again here that is console.log product dot and this time we see that we get back the quantity as well here but now let me show you one more thing that is if i try to make a request here that is to get a product by id we see that we have this problem here and the reason is this the model has been changed and we have done generation that is the generation of the prisma client but that model has not migrated to our database the database only contains these fields here so if we refresh it we see that the database contains only these fields here not the quantity field and now to again validate our database with the new field what we need to do we need to stop the application once again and we need to do nps npx prisma migrate div again and now we need to wait for a couple of seconds and now we see that we cannot execute this thing here and that is because the quantity needs to have a default value because otherwise there are two rows in the table and it is not possible to execute this step or we can do so and so but the easiest thing to do here is this that you just go to your schema.prisma thing here and let's close this thing here and let's open it here so let's save it let's close it so what we can do we can simply provide in a default value here and the default value should be one like the one quantity or let's say zero quantity like this and let's save it and now if we do npsp does not migrate div this should uh go through and now we need to enter the new name for the migration and let's say like uh second migration and now the client is automatically automatically generated again though we have already manually generated this if i do npm run div and now if i go to this url to get all the products we see that we get the quantity as well here that is the quantity of zero so guys that was it so i guess that this is a pretty long video and i've done all almost all the crud operations about prisma here though this is only a brief overview overview about prisma and it is like uh it is like much more than to prisma than this video so you can have a look at the docs here and they have beautiful rocks here inside prisma documentation and like if you compare it with mongoose then like it depends on you that if you are like a fluent in mongoose then you can use mongoose already because like it's already a mature orm for mongodb but if you like to work with other databases like the sql databases and then you can definitely check out prisma because you can learn prisma once and then you can apply this on any database which you like to use either sql database or nosql database like mongodb so like it's a pretty good orm so that's all about this video so if you like this video do hit the like button and if you haven't subscribed to our channel till now do subscribe to our channel for much more exciting content so thank you bye tada take care and have a good day and have a great day you
Info
Channel: yoursTRULY
Views: 981
Rating: 5 out of 5
Keywords: yours truly, how to use prisma orm, prisma orm for Nodejs, how to build a rest api, prisma db, prisma tutorial
Id: HCJmlvgO2WY
Channel Id: undefined
Length: 47min 54sec (2874 seconds)
Published: Mon Sep 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.