hello guys welcome to this brand new tutorial on Prisma you might have come to this video because you might have seen Prisma being used with nextjs and react server components or even from a backend framework like Express whichever it is by the end of this tutorial you should have learned what is Prisma why use it how to set it up and then later on we will practically use it to interact with our database okay so let's get started so what is Prisma and why use it let's look at the first point it is built for simplifying database interactions Prisma is an orm or in other words object relational mapper or it can also be thought of as a modern-day database toolkit for web developers that simplifies database interactions with an orm like Prisma instead of interacting with databases using raw difficult to understand SQL queries you have an easy to ous objectoriented interface this makes working with SQL as well as nosql databases like MySQL post SQL and even mongodb much more enjoyable to work with some of the other examples of orm like Prisma would be and SQL light let us look at the Second Use case for Prisma Prisma provides type safety and catches data related errors it catches errors during compile time so it helps you catch issues well in advance before your code even runs you can use Prisma with regular JavaScript but it is better suited for typescript as Prisma is built with typescript in mind let us look at the next use case support for various databases it is typically used with relational databases like MySQL post SQL SQL light however you can also use it with no SQL databases like mongodb with Prisma the code that you write be it the models the schemas the apis will not change based on your database for example example if you have used my SQL for your project at the start and on a later date you want to switch to post SQL you can easily do so with almost no code change let us look at the last use case automatically generates code based on your database schema if you have used mongodb or sqlite you create schemas which represent the structure of the database tables for example to create a user schema you might have to specify all of the fields like the ID email and name if they are optional or mandatory what their data types and also their relationship with another table etc etc before orms like Prisma we had to do it on the database level before we even started coding but with Prisma you just have to specify the models and by running it it will automatically create all the database tables rows columns primary Keys Auto increments etc etc so now that we've got a pretty good idea of why use Prisma let us actually see what are the components of Prisma so there are three parts to Prisma let us look at the first one Prisma client it is the autogenerated and types safe query builder for nodejs and typescript basically whenever you write models and schemas Prisma client helps you interact with the database much more easier and also make sure that the database queries that you write are type safe it can be used with arest apis graphql apis or even xj's full stack applications or anything else it helps with autoc correction and type checking in your IDE an exciting use case for Prisma is for next year's apps with a new rsse architecture where Prisma CLI can be used on react server components let us look at the second part of Prisma Prisma migrate it is formally defined as the data modeling and the migration system in Prisma if you do not understand this don't worry essentially it is designed to simplify and automate the process of managing your database schema as your application grows for example you can start off with the user model with email and name at a later date you might have a requirement to add an is admin field to the model in which case by running Prisma migrate it will automatically make the necessary changes to your database while retaining existing data let us look at the last one Prisma Studio it is a GUI or a graphical user interface to view and edit your data in the database this is the only part of Prisma that is not open source the rest of it is completely open source it is very useful to have but absolutely not a requirement so if you're somebody who's never worked with any of the other orms like sqlite for example then it is possible that you're not aware of what data modeling is it is absolutely crucial that you understand this as this forms the basis for a lot of things that we are going to be covering in this video data modeling is the process of defining the data requirements and the structure of a system it usually involves describing the database schema including tables Fields data types and the relationships between them effective data modeling in Prisma is crucial as it forms the foundation on which effective database queries are built on it also assures that the data of your application is structured efficiently for saving retrieving and updating activities so going forward in the rest of this video to understand everything practically we will be creating a user schema and an article schema we will also form a relationship between a user and articles so essentially a user can have multiple articles but every article should have just one user so now let us get started with the coding so these are the four commands that you will have to run to set up your entire Dev project let's go to the vs code so let us actually first go ahead and initialize a note project so to do that you can say npm in it and then put a y flag so you can see that a package.json file is created for us so now let us run the first command so the first command is I'm installing typescript so npm install typescript and along with that I'm also installing the TS node package and I'm also installing the types for nodejs and I'm also telling npm that these are all going to be developer dependencies so let's press enter so now that this is done let us come to the second command where we're initializing the typescript so npx TSC in it so you can see that this creates a TS config file but we are not going to be focusing too much on typescript in this tutorial now let us come to the third command which is npmi Prisma so let us go ahead and install Prisma right now and let's also specify that this is going to be a developer dependency press enter all right great so now you can see that we have the Prisma installed as well now let us actually go ahead and initialize the Prisma so let's say npx Prisma in it and then along with this I also want to specify the data source so let me type it out and explain it to you data source provider so basically this data source provider could be any of the databases that you want to work with so it could be postes it could be MySQL it could be mongodb but for the sake of this tutorial I want to keep this very simple for you guys because I don't want you to go installing my SQL or post SQL or mongodb and that is going to take a lot of time so instead I suggest we work with SQL light SQL light is as the name suggests is a lightweight SQL database it is going to be just a single file but the code or the operations that you will be performing on it will be very similar to how you would on MySQL or post SQL the code that you will be writing is going to be the exact same so don't worry about it so let's press enter now you can see that there's a couple of files that are generated the first one is the EnV file you can actually see that we've got the database URL and the location of the database file so this is going to be created later on when we run Prisma migrate which we will talk about in a little bit but this is the place where you put in the connection string so for example if you using MySQL the connection string will go right here if you're using mongodb the connection string of that mongodb will go right here because we are using SQL light this is going to be the location we don't see it right now but soon when we run Prisma migrate it will show up so if you see right here Prisma supports the native connection string format for postgress MySQL SQL light so if you want to know more about it you can go to this particular link all right and now let us look at the other folder that has been created and you can find the schema. Prisma file so essentially the schema. Prisma file comprises of three different sections all right so the first one could be the data source which we have already seen we have used SQL light to be our database and then the connection string has been already pointed to this particular environment file and this is where we are defining the Prisma client this is what is going to help us interact with the database much more easier we will talk more about it but you don't have to worry too much about this because this is just standard boiler plate now what is the third part this is where we write the models so let us actually go ahead and create a user table and an articles table and build a relationship between them as well all right so to create a user model you can say model user and inside of this you will want the ID so this basically just represents the user table all right so this is the schema for the user table so in a user table we will ideally want the ID right so we will want a unique identifier so let us call it ID and after this we have to specify the data type so we can say int all right because this needs to be an integer and we also want to specify that this ID is not going to repeat right so no two users can have the same ID right so for that you can say at ID all right so this defines a single field ID on a Model all right so there is one more functionality that I want to add to this ID I want this ID to autoincrement right every time there is a new user getting added I want the ID to get Auto incremented so to do that you can say at default and then inside of this I can say Auto increment all right now let us go to the second one I also want the user to have a name right so the name could be a string and you can actually see that it suggests a lot of different things so if you choose the first one the name is going to be mandatory if you opt for the question mark so the name might or might not come all right and obviously the third one is going to be an array of strings so I'm going to use the second one so I don't want the name field to be a mandatory field for the user all right I want the email and I want the email to be absolutely mandatory all right and lastly I want the Articles field and this is going to be an array of Articles all right now you see that this is showing red squiggly lines because it says that it does not refer to another model because we have not created an articles model yet right so let us actually go ahead and create that uh sorry this is not going to be a plural all right now so this is going to be article all right so this model basically signifies the structure of the article table right so even article should have IDs right so we can actually use the same line here as well anytime there is a new article getting added we would want the IDS to Auto increment we would always want the IDS to be unique as well we can also add a title right here and we can have it as a string and the body of the article could be something like an optional string because we don't want the body to be mandatory all right and also we can have another field right here called author ID right and we want the author ID to be an end okay now this is where we actually form a relationship between the article and the user models so let us build that relationship right now so to do that I can say the author and then we can say user basically meaning that this author needs to be referring to a particular user now let's create the relation to create a relation just say add relation and this method takes in two properties one is the fields so this takes in the author ID and the second parameter is references and this points to the ID of the user model right here so basically this means that the author ID of this particular model which is the article model should be referring to the ID of this particular user model so this is what this relation means if you do not understand it do watch it a few times and I'm sure you will understand it so now that we've written the user model as well as the article model let us now run Prisma migrate so to run this command you can say npx Prisma migrate and because we are in development you can say Dev and we can also give it a name called in it pressing enter and you can now see that a new file has been created right so if you go inside of this migrations and go inside of this folder you can see that SQL commands to create both of these tables are written right here how beautiful is that right so we don't ever have to write these ugly looking difficult to understand SQL queries by ourselves because Prisma does it automatically for us right so we just have to write these models and run a migration and Prisma is automatically going to generate all of the SQL queries for us along with this we can also see that the SQL light database has been created as well we will not be able to see it but there are some thirdparty vs code extensions that we can use to see the data but we are not going to be doing that right now later on in the video I will be showing you how to use Prisma GUI which is the graphical user interface for Prisma so now let us actually go ahead and start interacting with this database so so let's go ahead and create a file called index.ts so to interact with the database you will need Prisma client okay so let's import that Prisma client and let's initialize the class as well so new Prisma client so because we are working with a Note file let us actually go ahead and create a main function right here we can also specify this to be an async function okay so coming down here let's invoke Main and at the same time let's do a then and a catch so instead of then let's write a callback function so essentially whenever the main is done executing we want to disconnect from Prisma so to do that we can say Prisma do dollar disconnect okay so one more thing that I forgotten is that the disconnection is in async operation so we will have to add async and a weight right here so let us do the same thing here as well and here we will be getting the error message and you can actually console or console. error and then put it right here and then we will also have to disconnect from Prisma and in addition to that because this is the catch block it would also be helpful if we exit the node process with the proper exit code which is one now for the first query let us actually try creating a user so to do that we can actually say that const user and let's await it and we can say Prisma do user so you can see that it automatically Auto suggests the user model right because a Prisma client is connecting us to the Prisma models that we've written right here so that is why it autocorrects us okay so we can create a user by saying dot create add an object inside of it so I'm going to say data an object here as well and inside of this I can actually specify the name I can mention Harish right here and we also know that the property email is missing because we've made that mandatory so let us add the email here as well so I can say Harish gmail.com so now that we've entered all of the necessary properties you can see that there is no red Squigly lines all right now let us actually console the user and then see if it has actually been added or not so let us actually go ahead and run this file so to do that we can say npx and we know that this is a typescript file right so you have to say TS node and then specify the name of the file right here so let's press enter you can actually see that it prints it out it says the ID is going to be one because this is the first user that we've created it starts from one and the name and the email is added as well so this is the data that is sitting as the first field inside of our database right here we will not be able to see it still so let us do one thing let us actually fetch all of the users that are Pres in the database file right now okay so fetch users all right so to do that we can say const users and then await Prisma user so to fetch all users you can use the method find many all right so to print this out we can add users right here and I want to comment this out and the reason why I want to command this out is that every single time I run this command this file is going to get executed and multiple entries with the name Harish is going to get created which I do not want okay now let me run this great you can actually see that we are getting an array of objects right here right so it works fine now let me commment this out as well so the next thing that I want to do is I want to create an article associate with a user okay because we know that articles always need to have a user right so let us do that so we have article and let's await this Prisma article create inside of this as usual we have to specify the data and an article should absolutely have a title right so we can say Harish is first blog or article or something okay we don't want to have the body because we have mentioned that that needs to be an optional thing only right so the next thing is we have to associate this article to a particular user so to do that you can actually specify the author right here okay so the author and then I can use the connect object right here and then I can specify the ID to one so basically what this is saying is connect this particular article to the user with ID one so let us run it let me replace the Articles right here and if I run this again let's make sure that everything else is commented out okay so if I run this you can see that the article has an ID of one we've got the title here as well and here this is the most important part here you can see that the author ID is one right it is successfully mapped to the user with the ID of one right here okay so that is what this means so let us now test it out see if this article has actually been created in this database okay so let me comment this out now and we can write another command or a query rather right here saying that fetch articles okay so we have articles Prisma article and find many okay so we have the Articles right here let's run this now perfect you can see that we are getting an array of objects and we have the first article inside of it now you might have a question so right here we mentioned the author and not the author ID right so when we fetch that shouldn't we also be able to see the author object right here we can all right we just have to explicitly tell Prisma that we just have to say include and then create an object author and true let us run this again great you can actually see that along with the ID title and body we're also getting the entire object as well so so far we've created a user we saw how to fetch those users as well and then we saw how to create an article and Associate that with a user right so we first creating an article and we associating that article with an existing user in this particular case but right now I want to do something else I want to create a new user and a new article at the same time okay so let us see how to do that let me comment it out so I can call this create user and article at the same time okay so let's do that const user a wait Prisma do user dot create and I can create an object inside of it and then I can say data and we know that every user should absolutely have an email so we can call this yamini at gmail.com and we can give her a name as well yamini all right so we know that this code is going to create the user but at the same time we also want to create an article and Associate all right so to do that you can say articles and then create and inside of this we know that every article should absolutely have a title so we can say yamini's first blog and let's also give it a body why not so we can say this is yamin's first blog okay now let us actually log this user out so if I run this you can see that a user has been created with the ID of two you can see that it has been Auto incremented as well and we've got the name and we've got the email as well right so let us now actually fetch all of the users and see if this item has been added so let me comment this entire thing out and I can go back to the fetch users right here okay so let's uncomment it and log this out so ideally we should have two users right now beautiful you can see that we've got Harish and we've got yamini right here and this is an array if you also want to see the associated articles you can do so by by mentioning the include keyword right here so you can actually mention the Articles to be true let me save this and if I try to run this again you can see that we're getting the Articles array as well so because we're logging it out in the terminal because there is not a lot of space you might not be able to see the full nested object so to help with that there's a small code snipper that I've created so let me actually comment out this particular line right here so if I run this command again you can see that it beautifully lists out so you can see that this is the user the user's email and this is the article okay similarly this is the second user and we've got the article and the body so essentially I'm just taking the users array looping through it and then I'm consoling logging it in a very neat way that's it so here Harish has got one article yam has got one article right so let us now try to add another article to Yami all right right so what we can do is come down here you can say create another article for a user okay so we can say article await Prisma do article. create and inside of this data assign it to an object we know that the title is mandatory let us say yamin's second article and associated with the user so we can say author and then connect and then inside of this we want the ID to be the second user right this article needs to be associated with the second user ID Let's uh run this one let's make sure that everything else is commented out we can comment this part out as well let us run this you can see that a third article has been created so it says ID is equal to 3 but then it is associated with the second author ID so if I now uncomment both of this code fetch all of the users and let me comment this code here as well we don't want to create another article so users so let's run this again perfect here you can see that yamini has two articles right now now there are a couple of things pending let us now see how to up upate certain operations all right so let me comment it out here we can now try to update a users's data okay so we can say user chrisma update so here you might have to use the update method right here okay so what to update right so to mention that we can use where right here and we can say the ID needs to be one or two or three whichever user that we want to update so let us say I want to update the first user right here so let us say I want to update the user's name so I can mention data right here and then I can say the name needs to be now ton all right let's print it out again so essentially what we're doing is that we're updating the users's name right here to this name all right so let us actually run it so it says that it has been updated we'll actually test it out but by going up here let us uh uncomment the fetch users and let's print it out see if it has actually worked all right let's run it again perfect you can see that there is no more Harish anymore we've updated this particular field name now let us see how we can delete something so we'll try deleting an article okay so we can say article and then await Prisma article delete so instead of this particular update keyword we're now going to use the delete method so what do we want to delete we can just mention where and inside of this object we can say we want to delete the first article or the second article or the third article so we can say article number two so let me console this out see if the delution actually succeeds and here you can see that this article has been deleted so the second article belongs to the author ID to which has now been deleted so if we were to now fetch all of the users so if I were to go to this users right here if I put this here let me comment this out so if I run this again you should effectively see that this second user yam mini only has one object inside okay so the second article has been successfully deleted so one more thing that I want to show you guys is the Prisma GOI so to open it up type npx Prisma studio and you can see that it automatically creates a different tab right here right so inside of this you should be able to visually see all of the database tables so right here you can see that we've got the article table and the user table right here so inside of user we had the as well as yamani right and each of those users also have one article associated with them so as you can see tarun is associated with this particular article and yamani is associated with the yamani second article title similarly you can go back and you can click on the article table and here you can see that we've got two articles right here you can also go change the author of each of those articles so for example you can actually go here instead of yamani I can actually change it to tarun so now this particular article is going to be associated with the user tarun right here to save this particular change you can go ahead and click on this button you can also add a new article by clicking on this add record button and you can see that this ID is going to be Auto incremented so you're not going to be able to change that and for title you can actually say something like Hello World right and body we can just leave it as empty because this is optional and for the author ID you can either specify it to be one or two so I'm just going to go ahead and specify the user ID 2 we can just go ahead and save it so now because of this change right here we've got three articles in total so you can actually fetch it from our code as well so you can actually run the same Command right here and you can actually see that we now have an additional article right here right we've got a total of three articles right now and that is it guys I hope you learned how to use Prisma effectively Prisma is a very popular choice for backend especially if you're working with NEX Chas so if you're interested in learning NEX Chas and react server components and effectively use Prisma in it I have a dedicated nextjs 14 playlist if you want to learn that as well so do drop a comment and consider subscribing to the channel and I'll see you in the next video