Building a CRUD REST API with Nest js and Prisma

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign hello everyone back at lemoncode 21 in this video we are making a simple crud Appian Nest Jazz let's get started before we start building a nest application you need to install the nest CLI with the command below [Music] wait for the installation to finish once the installation is complete create a new Nest application with the command below [Music] once npm has installed all the packages required to run the application change the directory to the project folder and run the server [Music] thank you [Music] install the prismac LI as a development dependency [Music] thank you we will remove three files that are not needed in this project you can simply delete them to clean up the project structure [Music] next we will configure prettier to avoid any code formatting errors [Music] once the installation is finished invoke the prismacli locally using mpex with the command below now create your initial Prisma setup using the Prisma and a command first open the NV file and change the database connection according to your configuration make sure to fill in the required information such as host Port username password and database name next navigate to the project folder and locate the schema Prisma file in this file add the model definition for the book table you can specify the columns needed such as ID title author and others thank you [Music] after adding the models in the schema Prisma file don't forget to run the migrate command to apply the changes to the database schema you can use the command MPX Prisma migrate Dev to run the migration [Music] create a new folder named book inside your project folder in this folder create a file called book model import the necessary modules such as Prisma client and implement the book model according to the structure defined in the Prisma schema [Music] next create the Prisma service file in your project folder this file will be responsible for establishing the connection between the application and the database using Prisma client import the Prisma client module and create an instance of Prisma client to be used in the application [Music] now we will add the implementation function of on module on it in the service or controller that requires it this function will be executed after the module is initialized we can use this function to perform initial tasks such as initializing the database connection or loading configurations [Music] [Music] [Music] create the book service tees file in your project folder this file will contain ceud functions create read update delete for the book entity import the Prisma client module and use the previously created Prisma client instance to execute queries to the database [Music] [Music] thank you [Music] [Music] foreign [Music] thank you [Music] [Music] next create the book controller it's file in your project folder this file will be responsible for defining all the API routes related to the book entity make sure to import the book service module that you created earlier and add an instance of book service to the class Constructor inside book controller each route will call the corresponding function inside book service to perform the necessary operations [Music] thank you [Music] thank you [Music] [Music] foreign [Music] file in your project folder in this file import all the controllers and providers you have created [Music] don't forget to add the at controller decorator to the book controller class to indicate that it is the controller related to the book entity [Music] at this point you have successfully created Your Nest Prisma r-e-s-t-a-p-i now let's test the application using Postman let's do testing [Music] [Music] if you want to modify the reek request and Reese response objects in your router functions you can do so by adding the at Rec and at rest decorators to the function parameters by adding at Rec to a parameter Nest Jess will inject the current request object into that parameter similarly adding at rest will inject the response object in the above example the at Rec queue decorator injects the reek object of type request and The at-res Decorator injects the res object of type response you can then use these objects to modify the request and response as required such as setting headers accessing query parameters and more [Music] remember to import the request and response objects from the appropriate module usually Express to ensure the correct typings are used by utilizing these decorators you have the flexibility to modify and manipulate the request and response objects within your root Handler functions in nestjs [Music]
Info
Channel: lemoncode21
Views: 11,311
Rating: undefined out of 5
Keywords: lemoncode21, backend enginner, frontend engineer, programming, javascript, typescript, nodejs, express, OOP, python, fastapi, nestjs, prisma
Id: t_8Ez7vjDdQ
Channel Id: undefined
Length: 12min 58sec (778 seconds)
Published: Tue Jun 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.