Build a Node.js App With Sequelize [1] - Connection & Model

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey what's going on guys in this series we're gonna be building an application using nodejs Express and sequel eyes which is an ORM that works with any relational database such as MySQL Postgres motion say any it works with MySQL PostgreSQL light and Microsoft SQL alright so we're gonna be using postgrads but you can just as well use any of the others I actually created this application on a Windows machine using MySQL with xampp but in this video I'm on my Mac and I always have problems with xampp on a Mac so I'm gonna be using Postgres alright so either one is fine if you want to do exactly what I did you just want to go to PostgreSQL dot or go to download and I'm gonna Mac so I'm gonna click that and I want to click on the download installer and I just grabbed the version 11.10 it for Windows if you want and this should come with PG admin which is an admin tool similar to how PHP myadmin works with with MySQL so again either if you want to use either that's fine and I just want to show you I don't have the the full application built on this machine but I do have the the HTML template so I want it to look decent so I'm including this code gig HTML folder as a download in the description for you guys if you're gonna follow along because you're gonna need the interface so if I open up the index.html file you'll see the home page which is just a search box where we can find a coding gig or a coding job by keywords so JavaScript PHP rails whatever it may be we also have the all gigs page where it lists all the gigs with the title description budget the apply now is just a mail to link to the contact email and then to add a gig we have this link here that has a form to add all the the information ok now we're not going to do authentication just yet I may add it if this series does well and you guys like it I might add authentic with Passport but I do have other videos that that use you know authentication and nodejs including my nodejs course so if you guys already know how to do that then you may want to implement that yourselves but I may do it later on but for now we're going to just kind of get this functionality going so first thing I want to do is create our database and our one table we're just going to have a gigs table and you can do that however you want if you're using MySQL you might want to use PHP myadmin or you can use the terminal or some other program I'm using PG admin so I'm just going to go over to databases here and say create database and I'm gonna call this code gig I'll use the postgrads user alright so let's now go to our code gig database we want to go to schemas and let's go to tables and create table okay so we'll call this gigs and I'm just gonna I'm going to use the Postgres as the owner this should all be fine let's go to columns and let's click the little plus sign here and add a column so this is gonna be ID and the type is actually going to be serial which is basically an auto increment integer and we want this to be our primary key so you want to click this right here alright and if you're using MySQL just create an int and give it auto increment and primary key next one is going to be the title and we're going to use a varying character or character varying and for title for length let's do 200 okay we also want technologies okay technologies same thing we're gonna do character varying and as far as length let's do 100 no it will do 200 okay we also want a budget do character varying and let's see we'll do 20 for the length and next one is going to be description for a description let's do text okay we also want a contact email and for that we'll do varying character and we also want two fields that sequel eyes is going to look for that's created at and updated that so you want created at spell just like that camelcase and this is going to be a date field and then we also want and update it at so let's create that updated at and that will be date as well and that should do it let's go ahead actually let me just check yeah I think that should do it let's click Save and now we have our database we have our gigs table with all of our columns okay alright so now we can actually start to create our application so let's jump into vs code and I just have an empty folder called code gig and first thing we want to do is in our terminal I'm using my integrated vs code terminal I'm gonna do NPM in it so that we can create a package.json file so we're gonna enter through this for a description will just say simple job fine app for coders and the entry point is gonna be app dot J s and for author you guys can put your own name if you want and I'm gonna do an MIT license all right so that's our package jason now we need to install our dependencies so let's do npm install we need Express which is our web framework body parser because we have a form that we need to get data from sequel eyes of course and if we look at the docs for sequel eyes or the getting started page right here oh this isn't it I want to go to get started we need to install sequel eyes which I just added but we also need to add the database driver if you're using MySQL you want to install MySQL - like that we're using Postgres so we actually need to install PG + PG - H store we need both of those things so let's add PG and PG PG - H store and then the last thing I want is the Express - handlebars template engine because we need a way to display our HTML and also include dynamic content we need to be able to loop through the gigs and help with them and stuff like that and handlebars is my favorite template engine for 4 Express or 4 node so I think that's it let's go ahead and run that and we'll install those dependencies and those will get added to our package JSON next thing I want to do is just add a dev dependency so I'm gonna say npm install - uppercase D and then node Mon okay and as a lot of you guys know node Mon is it'll constantly refresh the server when we make a change so we don't have to do it manually and then once that's installed we just want to add a couple scripts here one is gonna be our regular start script which is just going to run node and it's going to run the app dot J's file alright and then we want a node Mon app J s and we're gonna call this dev so that we can do npm run dev and that will start up our server with a node mod alright so now we can close that up and let's create a file called app dot J s so this is obviously our entry point we're gonna go ahead and bring in some stuff here we need to bring in Express and we need to bring in a couple other things like Express - handlebars okay let's call this exp HBS for express handlebars and we also want to bring in our body parser and we also want to bring in the path module which we didn't install that's actually a core nodejs module just to work just to deal with file paths let's since let's create our app variable and set that to Express so we want to initialize our app and let's say app dot listen which will run our server and for a port let's create a variable called port and set that to process dot e NV dot port or on our local machine we're gonna run it on 5000 alright and we'll put that pour it right in here and let's do a shortcut for an arrow function and we'll just do console.log put in some back ticks and we'll say server started on ports and then we can insert a variable with this syntax okay so that should run the server now we don't have any routes so let's create our index route so apt-get because we're catching a get request whoops to slash which is just the index page and then second parameter will be a function with request and response as a parameter okay and all I'm gonna do here is just res dot send to the browser the text of let's just say index okay so we should be able to run this let's do NPM run dev and let's go ahead and open it up so we'll go to localhost port 5000 and there we go so we know that that's working so now I just want to start to implement sequel eyes before we do anything just to make sure we can actually connect to our database so let's go ahead and do that now we're altima we're going to have our database connection in a different file but now I'm just gonna do it in here so I'm gonna create a sea let's create a variable called sequel eyes and actually we can just take a look at the the docks right here so setting up a connection we just need to bring it in and then create a variable and set it to new sequel eyes passing all of our stuff so I'm just going to copy this this line here or you can use the connection URI I'm gonna I'm gonna do this method here so let's just paste that in okay so we bring it in create a variable we need to put in our database name which is gonna be what do they call it code gig the user name is gonna be postgrads that's what I used in my password for postgrads is just 1 through 6 I'm actually going to call this D be instead of sequel eyes though and let's see host is localhost now dialect since I'm using postgrads that's what I'm going to use but if you're using one of these other ones make sure that you choose the correct one so let's do Postgres ok it should automatically know the port and stuff like that the connection pool I'm gonna leave those settings let's get rid of this that's for SQL Lite only and now we should be able to test the connection so let's say test DB and we should be able to do that by taking our DB and calling authenticate which returns a promise so we just want to do dot then and if it if it successfully connects let's just say database connected okay and then if there's if something goes wrong it'll run the catch with the error and if there's an error let's just whoops I forgot the console.log definitely want to do that so we'll just console.log the error actually we'll just put in like error and then the error all right so let's save and there we go down here you can see in the console we have database connected it's also going to show us all the queries whenever we run a query it should show us down here but yeah so we aren't connected to the database which is good so like I said I don't want to keep this in here this this really clutters up our app.js file so what I'm gonna do is create a folder called config and inside there I'll create a file called database dot j/s and I want to move everything that we just did so bringing in sequel eyes creating the database mmm I guess we'll leave the test in here I guess but let's grab the rest of this stuff and let's go into database j/s and we paste that in now instead of creating a variable here we want to just export this so let's say module dot exports equals and then that okay so that we can access it from other files and then we're gonna go into app KS and we need to bring that in because right now it has no idea what DB is right here so let's say just a database and we'll create a variable called DB and we're going to set that to require and we want to go into a config slash data bass let's save that and there we go so database connected so it cleans it up a little bit we don't have to have everything in our app J s alright now the next thing we want to do is create a model okay when you have a resource whether it's users or posts whatever it may be in our case it's gigs you need to have a model for that resource just like Mongoose with MongoDB if you guys have ever used that so let's create a folder called models and inside models we're gonna create a file called gig dot j s uppercase G our model should be uppercase by convention and let's bring in sequel eyes so we'll say kant's equalise equals require sequel eyes okay and next thing we want to bring in is our database so it's like consti B equals require and let's see we're gonna be we're gonna go outside of the models folder inside the config folder and then into database and to create a model we're gonna create a variable called gig and we're gonna set this to DB dot define and we're gonna call this gig and then the second parameter is gonna be an object whips it's gonna be an object of fields ok very similar to how Mongoose works so we're gonna have a title and it's gonna have a type okay the type is gonna be sequel eyes dot string all right let's actually copy this we'll copy this down let's see we're gonna have two three four five of these and they're actually all going to be strings so we can leave that not but the next one is going to be technologies this one is going to be description this one is going to be budget and this one is contact email okay and that should do it now it's just export so we'll say module dot exports equals gig okay so now that we have our model let's try why do we want to do this you know we're gonna have this this index route right here all its gonna do is load the the homepage which is you know we're using handlebars for that which we still need to set up then we're gonna have a separate file for all the routes that have to do with gig so fat fetching them you know the the the post or I'm sorry the the search adding them things like that so let's create another folder in our route here called routes okay and inside there we're gonna have a file called gigs dot J s okay so this is going to be all the routes for our gigs now we have to point to this file so I'm going to go back to AB Jas and down here at the bottom right above the port let's say gig routes so we're going to create say app dot use and for anything that's slash gigs we want to require the gigs route so it's gonna be dot slash routes slash gigs okay so we'll save that and then it's going to give us an error because we haven't set up the router in the gigs file so let's go to routes slash gigs J s and let's bring in Express because we need to use the Express router okay so we're gonna say require Express and let's say Const router is gonna equal Express dot outer and then let's just create let's create a route for just slash gigs so let's say router dot get now since we pointed to the two we pull already pointed slash gigs to this file then all we need to put here is a slash and that's actually going to pertain to slash gigs and let's do request response and let's just do a res dot send just to test it I'll make sure this works and we'll just say gigs like that oh I forgot to export it I always do that so let's see module dot exports equals router okay so now everything looks good let's go to our browser and let's go to slash gigs okay so we know that that's working good now the what I want to do in this row is I want to be able to fetch all the gigs that are in the database which right now we don't have any but I still want to add the functionality for that and then we'll we'll start to work on adding the form so that we can actually add gigs and stuff like that you can go into PG admin or PHP myadmin whatever or the terminal and you can add data that way but I'm not going to bother doing that so the way that this works is we need to bring in our database so let's do that let's say Const DB equals require and we want to bring in let's see where are we we're in route so we want to go outside of routes into config and then database and then instead of doing this res dot send I'm sorry we need to bring in our model as well so let's say Const I'm going to call it gig we're gonna set this to choir and then point to the model so outside of routes and to models and then we want gig and instead of res dots and we'll get rid of that let's say gig dot and then we can use any method that's available with sequel eyes such as find all okay so we want to use find all now what this does is it returns a promise so I'm actually going to go on to the next line here and it returns a promise so we're going to use a dot then okay so we'll do dot then and we'll do dot catch in case there's some kind of error and if there's an error then let's just console dot log the error alright so in the dot then what it's going to do is it's going to give us our gigs right so we're gonna say gigs and we'll have an arrow and then let's just do a console dot log of the gigs actually you know what I'll do is wrap this in curly braces because after the console.log I'm just gonna rezzed send will just send stuff status of 200 just so it's not hanging alright so let's just try that and see what we get so if we go to slash gigs and reload we get okay because we're just sending a 200 status but let's go back into vs code and take a look down here so what we get is an empty array and that's exactly what we expect because we don't have any data in there but it shows us the the query so select ID title technologies all of our fields it's selecting from gigs as gig so even though we don't have any data it everything looks okay and it should be working and if you like I said if you want to add data through PG admin or my PHP myadmin if you're using MySQL you can do that but I think that I'm gonna stop the video here now that we have our database set up we are connected to it we're able to query it using our model that we created so in the next video we're gonna start to implement our template engine which is handlebars so that we can we can have an interface to work with and we want to be able to add gigs through a form alright so we'll get into that next
Info
Channel: Traversy Media
Views: 265,865
Rating: undefined out of 5
Keywords: node.js sequelize, node.js postgres, node postgresql, sequelize, sequelize tutorial
Id: bOHysWYMZM0
Channel Id: undefined
Length: 24min 6sec (1446 seconds)
Published: Wed Dec 12 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.