Part 1 - NodeJS Clean Architecture API with PostgreSQL, Sequelize, Express

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
good day guys what we will be discussing or what we'll be doing today is the clean architecture when you code your api so uh for daddy for the discussion part of this architecture is there's this guy in youtube that i see i will just put the descript the link in the description so if you want to know the discussion of this architecture just see the link below so what i like about this architecture so it's very flexible so when you will revise your code in your api so there's not much of a change you have to do and also in when you will change your database for example for from postgre to my sql so there's only a minimal changes not uh only uh there will be no big changes in your code so just minimal so i already have the the finished api here but we will be starting from scratch so i just uh downloaded the the packages because i don't have internet here no wi-fi so sad so the packages we'll be using is basic auth for our basic authentication of four hour doubts so of course for us to allow to access a close the genes so that can be to read that that a b file this file so express for our server then pg and i will be using the sqlize for efficient making of the tables and node1 for it to watch our main file so that's for our dependencies so we will make our source folder i will just copy the enb file that i have used so this is for the port where our api will run so part 24 nodemb the current environment is development for the credentials for our database and for the encryption data encryption algorithm encryption and the [Music] i4 for uh i mean for the manual and then for the credentials for our basic class first now we will make our source folder and in the clean architecture there is just uh five main [Music] folders or yeah folders first is for that controller so if you need a discussion just see the link below what's the purpose of this i mean the controller is it will connect to the app so it will make the will call our use cases functions or use cases then express callback will be using express then empty piece i mean data access so that's it that's the four admin five main folders in the clean architecture so in the entities folder this is where you will put the business logic data validation like that so example in employee so let's say an employee needs uh must have a first name or last name and last name so you will put that validation here so data access this is for our database connection and query express callback is the express server which but we did we didn't use the express package which is a substitute for that yeah where our use case is where we will process the data from our controller and the controller is uh we the controller will get the data from the client so our client might be a web app a mobile app like that then we will make our main file the objects so let's modify our package json i'll be using nodemon i use nodemon or it will constantly check our main file for changes then it will run start so if you're done developing so just npm start so the node will run your file so let's try so we don't have any code here so sample test main file for any changes so it will when i make any changes then save the file it will display support support example there so i will just copy the the main file here in disparate file as even separate project so i will comment this first we will make it later so this we will just bring the packages and then config the randisconfig.nb.config for the api to read the variables here in rnb file then declare our express app then enable course this is just the body parser so it will read uh the json files that was sent from by the client in this port variable if there is no port in our nb file it will have an import 3000 but here in amd file it will then import 24 this 424 so up listen in the port then just control log this one then we did we do not have routes yet so comment date comment comment this line first for now then up use if the route is not existing so it will display this so let's try in the postman make collection i will also push in the api my the postman collection so first now let's make a variable here name it are locals or 24 and let's put an api here then just save and close let's try out is nowhere to be found because we don't have our reloads yet so we display this so also let's make the routes folder for our routes you can put it here in the source folder or in the directory but i want to make it here i want to put it here so it will be organized just one folder make it clean so next in the sqlized documentation in the migration part you need this file that's equalized file for dynamic migration so just copy this file place it here there so when you run the sqlize init command it will generate this in this directory so let's try to run it pxx is so it worked so we it must be here there there sqlize folder sorry config js so let's just if you didn't uh in default it is a config.json but i want if it is in json file you can't read the variables here in rnb file so i used config.js so let's just copy here here so require the dot nv then config right enable to read this file because the nv file is i will um it is a best practice that you will not push this in the repository so you'll put the confidential data here in the enb file so when we are running in the development when we are running in our node nvs in development so it will be using these connections or credentials for our database the node.nb variability is also mentioned there in the sqlize page so you use this variable so when in production you just replace here production then so it will follow these credentials for your database here then you will change the value of the variable here so we will try it later we will connect in the different database so for now we will just use the development that's for this config.js file also i forgot let's put in the models let's require that env here so that it will read what's the value of the process node e and we are in the variable if you will not put this uh require the tnb package so it will just always run in the development environment so excuse me first okay so this is just uh auto generated by the sqlize so you just don't have to just don't touch anything here just this file at this before this so let's make our first one on one model on the employees model so our employees will have these fields so just a first name and last name and age so let's make let's make it here [Music] forget the command so i will pause for now i will research first don't have internet i will back later so let's continue uh i forgot that equalizers help function so let's just try that and so if you want to know the commands so let's just write this equalize slide and up so what we will be doing is this part model generate so let's exfoliate silly so we will be naming our model employees oh also i mean you need this part attributes for the fields so attributes first thing the field then the data type so first name string last name string and then age integer missing required argument name hmm oh there there so in the migrations here it will generate the sqlites will generate the fields for our migrations i mean the file i mean the file then here in the models this so let's try to migrate it in our database so npx police [Music] will be my it will use the database sample forgot to create the database so this is my local postgre then let's create new i'm using navicat premium so let's create database example so in our sample database there's no table yet so let's try to migrate again so it's successful so let's just refresh there's our table place table that created that and updated that it is automat generated by this equalize automatic so that's for the our database so let's continue in the next part because the video is already long see you guys there
Info
Channel: Rodney Lingganay
Views: 3,147
Rating: 4.75 out of 5
Keywords:
Id: PZ5PRv1DzMQ
Channel Id: undefined
Length: 18min 16sec (1096 seconds)
Published: Sat Jan 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.