Easy way to build API | NestJS PostgreSQL and TypeORM

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in today's episode we're gonna take a look how to use nest js pay attention that it's not a next it's nest js you can mis interpret them so it's a just framework for backend of course you could use express or what do you like more but what is the coolness of this one because it has the similar structure as angular and we walk through and create a simple api just enough to use for your application so it's gonna be five requests five different type actually it's four types but we're gonna do five requests and also for this we're gonna use postgresql database it's possible to use with mongoose on sjs but i just prefer the sql types of database so that's why i chose this one but of course you can use any kind of db that you like and so we're going to create a post get patch and delete and to get requests to get all data and get by id so it's going to be really simple and as i told it's going to be enough for you to start working with it if you like what i am doing please leave a comment subscribe and like button of course but it's better to leave a comment i would like it more so enjoy the episode and see you in a second let's start from setup in our environment uh for back-end we're gonna use it's a really popular framework right now why i choose this one and not other because it has really kind of common uh bacon structure not back-end but the same almost as angular looks like and it's also possibilities type script so for me it's easier to work with it and also for our feedback app i was needed to have an api so i decided why not first thing that we need to do it's to install sjs globally i already did this so that's why let me find the right folder fit like this one after installing it globally you need to do this command you will have a nest available in the command line but do not forget that you need to kind of throw open it and then we're gonna do nest so it has a cli support already useful and let's call it server so it's automatically defined the structure for your app and whatever you need also for backend or db i'm gonna use postgre sql and omegas as a package because sql structure is more common for me instead of not sql dbs so i just use this one as an example to use it you need to go to elephant scale it's a website where you could online service where you could use postgresql to use it you need but it's gonna it could be another section of our video if we set up the basic structure for application so let's just here it was installed successfully and we just jump over so what do we have here it has a basic structure with app module yes if you know something about angular it's going to be pretty the same so it's app module is here we kind of bootstrap in our mini module so not bad to use also i decided to use type oram uh it's for sql databases uh and also it could be used with monohulls but with but it's not a stable version right now what is the why i like this because it has support of entities and uh providers and connection kind of to so what what the usage of this when you want to have a relationship between objects you need to use some ram provider and typo one of them it has a good documentation uh with it type ram and a pretty wide usage so it supports entities a different relationship between entities one to one one to many many to one support of validations etc etc so pretty good thing to use uh also what we're gonna need else so we need to install type ram and support for post gray type ram and postgre type rm postgres scale so let's grass as you can see uh everything is here so we just gonna install pg package another one which we needed and diaper ram and we gonna be good to go first let's just set up this one okay and to run our application uh here are already some commands available if you do not need to watch right now you just run start and it's gonna build your app and start it if you need to watch kind of for development you just type npm uh run start dev so all these commands available here and our app gonna be builded and in watch mod so here should be available and you can find this hello world where where where is it get hello so sp hello and as you can see in app server the the method which is we triggered from get hello and cp hello basic incidentalization is done so let's connect our db so first of all uh what i need to do it's uh i'm using elephant sql it's kind of provider for postgre to sign up uh you can use your github or google account i already created one for me so and once you log in you can create a new instance uh choose a name fit uh db let's call it uh there a free plan just for development it's gonna be enough text you don't need this and choose the closest region to you for me it's going to be and you could choose amazon cluster or whatever you want i will choose what is ah here's sao paulo new azure what is what do we have northern stockholm stockholm is okay for us because i'm from ukraine so create instance it takes several seconds and here you have all information that you will need then it's our application uh the first thing that we're gonna do is create our entity uh so as i explained before nest gs supports entities and on type oram website you could find the example with entity it's here kind of straightforward that you need to create class and just add decorator entity from typo and create it and it's gonna be a primary column other stuff there are also some options available uh that you need to call it like these some validations rules types and etc etc so we're gonna use our uh angular app as a reference is it it's it's it's somewhere here so we have a mock for our feedback entity we're gonna use this as an example but one thing that we don't do today it's create a relationship between entities i'm just put comments count kind of basis for this and later on we could change this because uh here we're requiring to have that one feedback could have many commands but each command could have many replies and also we need map them to the user who put this so let's create our first entity for this we're gonna create a folder just to enter this entity just to keep them in one place then we're gonna call it fit back dot entity dot yes and as before we need to create class so it's going to be export class feedback like this and also we need to have entity decorator here from type ram this one we're gonna fix what do you look like okay let's move further and here we need to have some so let's define our columns one of them is going to be id and as all we might know that ide once we post the entity or adding new object into our database we need to at least be unique and generate them automatically so primary we need to add here decorators that it's primary generated column like this a lot of some things what you don't like pixel approach here prettier it's marks everything what do you want okay we need definitely to do a fix for this prepare and furniture whites and i'll just disable preacher and our file not gonna be pretty anymore but it's okay it's not the main point so let's move further let's change that okay like this for tabs prefer the next uh column is gonna be what is it so title category and so on so on uh so title it's gonna be string and we need to put that it's just a simple column our decorator also the same thing gonna be with description description uh what else is string string okay nothing else at the string also let's define that in column we could pass some options here we could define the type of this so it's going to be just text and one of the option is default value and let's put it like this so there are a lot of options that you a bunch of types that you could define and also a lot of [Music] options to move on with the mods not the most difficult let's define first of all simple types so upwards also gonna be a number and comments in our case gonna be a number so here of what's number and column decorator and by default it's going to be zero because from the beginning we do not have anything and also the same thing is going to be for comments for now comments okay uh one thing that i want to share with you its types that we can use here so let's take a look at our mock so status here it's a num and category also a num so let's define them we could create here also another folder uh called models and inside of it we're gonna create uh in the same thing as we did in angular we're gonna create data model dot yes and inside of it let's define our status here now for example so here we're gonna have uh in angular project janups and we just could copy this from angular and pass it here and what do you you don't like prettier four top bits fix all preacher problems we need to have why you not trying not sure what the issue with this will have but yes blind it's possible to okay let's disable for now so we basically copied our vietnam from uh angular project it's a one cool thing which is really awesome to have and then we're gonna define our column here it's gonna be column and the name for this is uh status status then we define it as a string because we're gonna store string value but here in options we're gonna define some of them so type is gonna be enum it's possible to have it here and if you provide wrong value you get an error then if it's typing now we need to provide it stay to cnm and also the default value default what we're gonna choose state to see a num suggestion because it's not gonna happen anywhere so we defined just our entity model let's move further since i kind of fix it the problem with the eslint i just hear left and just rerun the command here this one and also i ran the command restart clean service it seems so far we're good so the next thing that we're gonna do is to define a new module it's gonna be database so let's here create database just to keep everything in the same place database module.ts and here what we're going to do is define a new model like this and also we need to do an export class database module like this and here we're gonna have uh providers we do not have anything so far but we're gonna have and also exports to be able to export one of them so if we don't have let's create it and provider basically need to be able to connect to our database here providers and database provider dot yes here we're gonna do the next thing we're gonna define a variable export const the base provider which is equal to you could have here several it should have provide option and let's define the name the base connection and it's not good to have it as i put it right now it's better to have vietnam in a variable some or nothing but environment variable to keep you this unique and just do not change it by mistake also here we do use factory and this one is going to be it's an example just for provider so as you can see here we need to provide provider and this kind of an example so all database providers use factory and it's going to be a sync a sync and uh error function like this and here we're gonna do await create connection so by this we creating a connection to our database but create connection we need to import it from type or not from net so keep in mind that you here we could do a mistake and to connect to our db we need to provide uh several things like these these and here we should have a commando wait here okay so type which type of db we're gonna be using and also you could find all this information in connection options so what is connection options using concat fee so as you can see there are several ways to connect to it but i find out these the most which i'm showing the most useful for my self but you could go in another way and just to try find out something else so type type is gonna be postgresql then we gonna have here several things host let me copy some of this thing postgres ah it's not in config way uh okay okay here's some just to avoid typing a lot because this is a duplicate port we need to keep it we need username password database support also let's go step by step okay so fix all preacher problems yes to find a host host is the server so you need to copy this line cost port i'm not sure how to find it here but i was using 54 32 as far as i seen in guidelines and different documentation it's the most convenient i'm not really sure we'll see what does it mean then username we need to use this one user and default database also keep in mind the database here needs to be the same as your username do not put feed db the instance name here so it's one thing database and username that you need to keep the same and password you could copy from here so as you can see connection in place and we should be able successfully connect to db as far as i thin we connected but there are no other information another one thing that we need to provide here is entities so we just need to have the list of entities here and one of them is our entity feedback comma so we should be connected also i install pg admin to be able to connect to our database how to do it it's just uh after install here you will see servers and postgre 14 you create new server feed db servers and you need to provide a connection almost the same thing as we did here it's host as you can see port maintenance database means the same as your username and password just the same thing save password save so we should be able to connect and as you can see here uh our connection and there you will see a bunch of databases how to find here by your username it's a huge huge list of the names but one of them should be yours it's a strange thing with an vh wh it's a bit strange but it is what it is wh here it is and actually we would be able to copy to open it and to find our tables we need to go to schemas and here tables but last one here and to auto generate it we need to put here synchronize and if you put true it's going to be automatically synchronized and created so right now we could refresh it we should see our table but it's not here why and why it's not here so anything let's refresh schemas ah stupid me we didn't use our providers okay so we first of all we need to use it yes yes to use it we just put here database provider import and the same way we put it here and we should be able to connect to it i hope so but we didn't use our module to use our module we need to create a feedback folder here and actually use it and import let's create a folder for our feedbacks bunch of folders let's say fit box it's gonna be the one place where we could keep module controller and service for feedbacks so let's call it feedbacks dot module dot yes uh the same way we're gonna define our module here we provide the imports and we provide here uh database module yes also we're gonna need controllers a bit later and providers also we're gonna need a bit later also we need to export class feedbacks module here a few things that we need to fix fix operator problems okay and the last step almost to be able to generate our uh table in database we need to provide our feedbacks module into app module kind of structure like this we have so to do this we just do this and we good should be but but yeah should we connect yep dependency initialize it let's take a look what we're gonna have in our pg admin tables our feedback table yeah and it's a bit slow but as you can see here all our structures status and also the one kill scene that as you remember we have here vietnam and enums automatically created in uh well it should be somewhere here to do i just forgot its primary key types should be in types feedback status you know as you can see it's here that we have a independence it so our database table was automatically created also let's add another one filled here we're gonna copy category you know and then we put it in the same place back all all we do not need here and let's add one more column it's going to be category degree here we're gonna use category and num also here we're gonna define what is gonna be by default let's put enhancement and after a lot we should be able to see the new column category here it's a bit slow but it's what it is columns what do we have here category just happened automatically because our tables synchronized start working with our uh books so the next thing that we're gonna define another provider and why do we need this is to be able to work with a repository repository it's a scene which allows you to work with with what with database so if you need to find out one just provides several methods to use uh ability to delete uh find find all file by id and etc etc etc pretty useful thing so let's define our uh feedback provider so and we need uh provider per entity and table so basically it's the same thing entity and table so here let's create feedback provider dot yes and it's gonna have almost the same thing as uh for database but in a bit other way so feedback provider type of it provider equal but yeah it should be like this then it's equal and like this and here we're gonna have provide uh also in caps feedbacks feedbacks repository and here use factory and factory actually we need to define here connection type of this also connection from type of ram and here we need connection let's type here should be double n connection dot connect connection get repository and we should provide here our entity feedback entity like this comma and inject and why i said that we need to be careful with this because inject it's going to be the same name that we used here so we need to be really careful or put this into our and or into some vietnam to store all providers name so yeah and after this we can move further and this one thing we're gonna let's also create no well we don't need no no no no no we can inject our feedback provider here uh another thing that we need to do into a module it's define uh the router that we have here so for this it's a net gs thing we need to define router module and register and just to have a url common for all path and let's do it api feedbacks so all our api calls gonna start uh once we want to receive uh for feedbacks gonna start with this and also here we need to uh define module which gonna provide all this information and it's gonna be for the back module like this so we're done with app module we still need feedback we don't need this one thing okay so let's create our service because we and can control the most interesting part starts from here so we need one hour one more file feedbacks dot controller dot yes and also fit box dot service yes like this um about service let's just create a class here export class feedbacks service this also class should be injectable and fix preacher for now we need to inject our service into feedback service into our module and uh regarding controller uh we need to do the following so we need to also put a decorator controller and export class fit box controller this fix this and provide it here the box controller and we are ready to go and create our first empirical so what we need to do here constructor we need to provide our feedback service here and as you can see it's almost the same as in angular application so in component for example we also have a constructor we need to provide it and so on so on so looks pretty the same for me like this and what we're gonna do first let's create get api call to get all feedbacks for now it's empty but this is what it is get all feedbacks we do not have any parameters here we're gonna return promise with type feedback it's gonna be our entity and return on these service and get feedbacks we do not have this method so far but we're gonna have it soon and inside of our service we're gonna just define it like this also we need to do to use our repository we need to inject it here so we need in our constructor we use an inject here also we need to provide the provider name so it's gonna be this as a stream and then we just need to define a variable it's going to be private so it's feedback uh repository and the type of fleet repository from diaper ram and feedback here pics and what you don't like not sure but here we're gonna do return these feedback repository find and find and we do not have if we want to receive all just use find and that's it what is what you don't like properly out implementation private blah blah blah like this yeah forgot and what is going on yeah we need to specify types that we're gonna return promise and feedback it's gonna be a collection of them and here i forgot to specify this and this uh get from yes yes yes so right now get repository cannot uh blah blah blah what is going on very stupid i find out what is the problem i put brackets here but didn't put a return and that's why i had an issue yeah because of it stupid what to say so we need to get feedbacks and this one here so right now in the postman we will be able i assume to get api feedbacks empty yes so let's quickly try to add post to add feedback in service you can do it really fast and this also should be a thing so here we're gonna do a sync it feedback we can receive feedback here type feedback we're gonna return promise and after insert it's insert result it's a typo ram type so and here we need to return these feedback repository third if you don't need to do any validation or check something so just like that and in controller what we're gonna deal with is we need to do post for this we here use another decorator and create let's call it like this so what we're gonna receive here here we're gonna have in body on decorator we should get feedback type feedback and then please service it like this and we need return we should be able to build everything uh let's do [Music] post here let's keep it and do post here feedbacks post body and raw and here we need let's grab something from uh mock data and here comments one like this send journal server okay now value in column title what does it mean well it's now now constrained okay let's take a look what we console lock feedback let's take a look what we receive here how to object because here we should application json send okay everything is fine this we can see uh uh raw title something wrong with the title title title white title is not here what we have in our entity get i know title is here but why it's without title i don't know three three okay okay so get this working host is working it just to add few more api calls but it's not gonna be difficult because we can uh do one by one so i would like to add all methods in the service for now and then just use them so the next one is going to be find one we're gonna receive a number here and gonna return promise and we should return here feedback like this and return please facebook repository find one and just provide an id and by this requirement it's gonna work as it is also i want to do here a sync update and we need to update by id and also another parameter will be feedback here for update and the type is gonna be promise and we're gonna return feedback as before and here just few things to deal with so i'm gonna add what a kind of validation to do so feedback to update first of all we're gonna try to find if a weight point one is exist if yes if feedback to update equal define it then we need to throw new not found exception there in is just a lot of exceptions like this but yep i just want to make sure that these uh feedback resuppository update and by id our feedback like this and here return [Music] these right one id like this and the next one is gonna be delete it's smaller but still so we don't need to update nothing we just use here to find feedback repository and delete id and here we don't have this one thing delete and this is going to be delete result only not like this but we'll type our yes let's add all this scene in our control so the next one get by d is gonna be that we need in our integrator provide getaway d let's call it get one feedback and here promise feedback it's gonna be just one thing and here get what is what's the name find one point one and uh because it's gonna be by d we need to use param decorator here uh that type the name of problem is gonna be id and here is going to be string by default because we get it's going to be just url and you cannot uh get from url non-string type and we just convert it to number like this and we should be able to find one so we'll hit s3 yeah let's try to add one more thing here title two 11 okay 84 if we use it like this okay and should receive four four yeah working as expected uh also here somewhere console lock i forgot to remove yeah another one on which we're gonna work is patch uh also receive an id here update feedback let's call it also we're gonna have param just copy paste uh then and also we're gonna have as for post we're gonna have feedback here and we can return promise feedback so here we're gonna do return please service update and just boss number id and feedback that's it much and the last one is gonna be delete also by id delete id and delete feedback program id promise no and here delete okay so let's do here instead of post we're gonna do patch let's just for id4 updates title title to update it after a patch this copy [Music] send comma and let's get it title to update after patch almost patch but and i'll go and delete it so we want to delete the fourth item affected 200 affected one and we got only one item left it wasn't easy we faced some issues but yes as you can see we did it so we just finished development for our easy and simplest api you ever needed i hope this short tutorial will be helpful and if we go just one by one uh let's start from post uh just to review blah blah blah attack send so added new thing then we're gonna do get all of them is here get by the fifth also working patch let's update fifth item title five blah blah blah okay also updated successfully and let's delete the fifth item sends affected one and get all no fifth item with id5 so we just did five apis call which you might need it in any kind of your project we faced some issues but it was connected to my physics in which i forgot to add something or miss kind of return so and so on uh i hope it was helpful uh keep in mind that we're gonna have one more video on this topic at least one more we're gonna work with connections one to one one too many because we need to extend a bit our entities so if you like the video please put comments it's really important for me to move on it increases engagement so please do this just simple plus sign hello in comments and i would be happy also do not forget to subscribe and press the like button and see you in the next episode bye bye
Info
Channel: Competent Programming
Views: 571
Rating: undefined out of 5
Keywords: nestjs, typeorm, postgresql, nest, nest.js, node, node.js, guide, typeorm nestjs, typeorm tutorial, nestjs tutorial, node js, nestjs vs express, nestjs mongodb, nestjs authentication, nestjs typeorm, next js crash course, typeorm relation, typeorm one to many example, typeorm express tutorial, typeorm relationship, tutorial, nestjs api, nestjs api rest, nestjs api example, nestjs api tutorial, nestjs course free, postgresql pgadmin 4, typeorm javascript, typeorm crud, typescript
Id: SkDHvfyXdsQ
Channel Id: undefined
Length: 65min 39sec (3939 seconds)
Published: Mon Nov 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.