Postgres on Heroku with a NodeJS Express Knex stack

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
one all right what's up everybody my name is Jay this is buddy and his a back-end engineer for his build sprint project and what we want to talk about today is how you can move your project from utilizing a sequel database and you know your development environment and in production use Postgres using Heroku so this is pretty easy setup here buddy has already deployed his app to Heroku as we can see but he's gonna run this get request in post man and it's gonna return back this error message right everything's everything's working right locally but now that we have it up in Heroku he doesn't have his database configured to be persistent so what we're gonna do is we're gonna hop into buddy's code and we're gonna take a look at a couple of his files and get them tuned up let's go ahead and let's see your code there yes sir all right so but he created his connects file using his connects init command and this is what you get out of the box by running that we can see that we have production and I'm gonna just concentrate on the production build if you want to you know work on the testing using PG then you can this will give you the stepping-stone to be able to do that the first part that we're gonna modify is the client we're gonna change it from pro stress SQL to just saying PG the next part is is uh inside of that connection we're gonna delete everything that's inside of there and leave and even take out the object itself and here we're going to access our process a and V and this variable name is going to be called database underscore URL and this is going to be assigned by Heroku automatically when we configure um inside of the Heroku dashboard now the next piece that we're going to work on is we're actually going to remove the table name connects migrations inside of the migrations object and leave the object though and we're going to make a key value of directory and then we're going to for the value of this we're going to make it be the route to the migrations folder so in this case for our buddy would be dot slash database slash migrations and then we're going to create a similar pattern for the seeds file so underneath there buddy if you want to just put a comma yep and do a seeds that's an object with the same thing of course though going to the seed folder alright great so this is looking good we have everything set up here to where when production is initialized by Heroku that it's going to get this object and it's going to put the correct things in motion quickly I would like to look at the DB config file though so that way I can explain how the DB is being configured that's why we're calling this the DB config Fong's it is the database configuration on line five are on line four I'm sorry we can see that we are creating a variable called env and we're assigning it the process a and B dot DB and V variable name or if that's not present we're standing at the development so this is how when we run our back-end they were automatically just using the development in the connects file all the time so when we go into the ropey dashboard we're going to actually create a DB and V that is going to be injected into line eight inside of that config and it's going to be the you know production right because we access the production object inside of the connects file so buddy know if you could please because obviously when we are working on our code locally Heroku is only servicing the master branch that is published to github so go ahead and save all your files and let's push that up looks like you get ignore I mean this is a good point right here when we are working with databases it's often left off for new developers or even to accidentally forget this for people living in US for a while to get ignore our databases when we're talking about databases we can have sensitive information that's in there and obviously if you put your database into github any public repository this will expose it to where anybody could download that database and gain access to all the sensitive data inside of it so we always want to make sure that we keep this private this is why a lot of times right our API is we're using access tokens or some type of security measure to prevent people from accessing it so I'm go ahead and write a commit and push it on it but he's using the awesome power of vs code here he's cheating by not using his terminal I'm just a little bit it's like it's like trying to use a hammer instead of a nail gun like come on already done already done all right let's go to our Heroku [Music] dashboard so pretty much everybody should know how to deploy continuous integration for their apps on back-end you know obviously that's like your prerequisite to this video so we're just gonna watch it build right and as soon as it says it's done building we're gonna verify that the app is still working and we can see that instead of just now it's deployed sometimes for Roku though we'll say that your apps deployed and then build this seated when it actually has not succeeded I don't know why the great question for Heroku steamed why they would like build a crashed out and not tell us in our dashboard but let's verify that it's working by opening the app all right so it's working so you go ahead and close it so we know that his house working but we're still missing our database right so how we're gonna do this is right here installed add-ons we're going to click on the configure add-ons we're going to in the the search bar that we're going to type in Postgres right there Roku Postgres we're gonna click that you know give us a pop up yep reviews hobby dad for free we're gonna provision this for our application KaBlam we have now added a Postgres database to our back-end but we need to do a couple more steps the first thing we need to do is we need to go into settings and we're gonna scroll down a little bit and we have this reveal config bars this is basically how Heroku creates a DMV file so when we open this up look there's that database underscore URL I was talking about you guys I've ever typed process a MV dot port here's your port right it's automatically treated did you create that variable though buddy the port the port one yeah okay it should be manually created one but should be a problem it was the other one that we needed to create you remember our DB e underscore env and we're gonna sign that the value of production production because we're in production we want it to hit alright all right we've got a couple more steps we're gonna scroll back up to the top of the page we're gonna go to more more and we're going to hit run console all right so this should look familiar then so everybody just so you know you can actually install globally the hero KU CLI and run these commands from your computer they will have a little bit of a difference out of your bash terminal basically you'll have to use a - a flag which stands for the app so that way the ropey knows which application to run it for and then you have to have the name of your app since we're running this command inside of the dashboard itself we don't have to run that so now but you can use your regular connects flow to migrate and run your seats this quad didn't know that you could do this from here yeah I learned about this a while ago as well and I thought it was pretty cool so there we go that's looking good so now we just gotta so then you see how I said process exudate on the bottom so you see on the bottom right it says run another command oh here we go oh so now we're gonna run that seed fingers crossed fingers crossed you value is out of range for type integer two three four five six seven eight nine 10 11 12 13 14 15 is that the value that you're trying to insert in there buddy oh this value right here yeah that looks like it would be what's called a big ant yeah yeah so and this is a right one this is a really good thing I'm actually glad we caught this go ahead and let's jump into your code and get that migration fixed I'm fairly certain that that's the correct thing that we need to use is big int I can also look really quick if you'd like YouTube and Rosen projects and nuts oh wait in my mind creations here let's see large numbers and connect integer has a restriction Inc okay thank so so yeah it's gonna be big integer big integer yep instead of integer and so there's gonna be some things in between Postgres and my sequel that will matter and also Heroku is just a lot more pickier for things things will work locally and they will not work in production so be prepared to fight through these errors they will happen that wasn't me yeah that was a date a millisecond so for further reference you can use a date time there's a timestamp can't remember which one uses milliseconds was not prepared for that one date think it's timestamp bro I think you can use timestamp instead a big in a big yeah instead of using big integer [Music] vibrations users yeah I just use timestamp okay with that allow sorry this will probably edit it out but will this allow can I set it future date with timestamp because that's what this is for like so why were you I can yeah okay that timestamp would be the more appropriate and it's all lower all lowercase that's what I was thinking and then my peen was like no using the integer yeah okay now Paul jazz are you good well that do you think I'd have to update my C files just cuz so this is how I'm seeking well no okay yeah it's still a number so you be good all right back on track okay so now you're gonna have to okay yep we're gonna put a build verify that it has built and the app is functioning then we're gonna hop back into the console roll back run latest seize eat and hopefully the foreign Keys don't cause issues okay it is possible alright so we are back on track working and console two girls you you all right here's the big one big the seats always are oh my goodness date/time field out of range that's a big one five eight three zero three one three five four four times this is like three days in advance to like March first oh I see what so this is more of a logical issue yeah I apologize and that's why we sometimes write just to talk about coding in general the way that we think about things can be tricky right and in my experience dates times are very difficult to manipulate so I think the easiest way that we can get past this is to change this to be a big int okay if we were going to do it more and I suggest you do this buddy you look into different ways to configure this okay cuz you're taking Milla set you're using a very small thing right a millisecond and you're expanding it past you know three days which is a lot like when you could reduce that you know what I'm saying yeah like there's there's certain there's like with so go ahead and hop in your migration file and give it out yeah big integer I'm about for example right so there's a thousand set milliseconds in a second right mm-hmm oh that's so in one minute we're already at 60,000 you know what I'm saying and then so in one hour we're at three million six thousand six hundred thousand yeah one hour you know what I'm saying and then we multiply that by 72 yeah we've got 259 million that's huge so I suggest you look at a date parsec service there's plenty of it'll come out of the box with node and you can utilize those that look just look at the documentation okay but this this will get you going okay just that's what I was saying you can't like and then I have a PE and who was in labs like he's further head but he's making up a project like a build week and he was like no the best way for us to just compare dates is to use an integer and I was like I don't think that's the best one he's like I've done it before that's how I've always done and I was like I mean you've done this so if that's like what you say I'll roll with it I'm like and then use the like third party like deep parsing services you know saying like there's already people that are smart enough to do this without like sweating it I'm not one of those people okay same time it's like the worst so wait what I apologize for for going off-track [Music] date dot now you see the date dot now here you go so this is like Trump JavaScript I'm just looking this up by your goodness kind of find like oh yeah there's like all sorts of things bro oh we're good to go hey we ran our C's let's go alright go go now we can circle back and we can hit up that post man hey we did it there we go we started from the bottom now we're here the bottom of the migration table alright so we worked through a small issue with the difference between Heroku and development yeah yes we did it set up a Postgres database that will now live forever in Heroku and we're gonna do one better and we're gonna talk about postman and documentation means I've ever seen a document set that was hand typed with markdown from a person that doesn't use markdown every day this probably wasn't the best thing you ever you ever had in your life if you're the person had to create this you probably probably hated life okay this sucks why would we do this so postman is really badass if you guys have never used a collection like learn how to make a collection isn't so easy I'll roar that new collection button there buddy you literally click new collection and then when you make your requests you hit save and save it to the collection alright so simple but you see how many requests buddy has in his collection already is 22 this but you want to go and write 22 different pieces of code markdown Sal thank know we're gonna publish these documents so that way they're automatically updated MP goes in and it changes one of these and hit save it's automatically gonna push it to the live site of postman Docs this is super dope all right so hover over the better professor app folder see how there's three little dots hot dog style we're going to click that we're gonna scroll down until we see a publish Docs we're gonna click that bad boy we're gonna see the postman dashboard alright there's all sorts of stuff you guys can set up in here alright have fun figuring it out I have not really dug deep into this but I know how to get us to get going scroll down to the bottom we're gonna hit publish talk publish collection so now there's your URL right there you see it hope right here they get look at this is glorious document set that we have now with everything look there's his schema there's just everything right oh we need an authorization you hand this to your front end there you go now unfortunately it doesn't have Axios let's see open the top right where we get at language Perl we can't we can see it in a couple different types you see we could do it in a CTP we could do it in JavaScript with fetch but you do a JavaScript and jQuery so however your your front-end wants to manipulate this and whatever way they can read you know how they're doing this like they could figure it out now right pick out dope is this like all 22 end points like that there you go and then like I said buddy hands this off to his friend developers and then you know maybe on Sunday but he has to go in and he changes the register user end point and then when he goes to tech to Heroku Hurley was the postman he changes he know how he maybe he changes it from email to use her neighbor from username to email and he saves it to his collection guess what postman pumps it back out up into this route without him having to do anything super dope that is alrighty thanks for joining us you can message me for my Ben mo for my services all right cheers guys
Info
Channel: Jay Maas
Views: 1,268
Rating: 5 out of 5
Keywords:
Id: 6uzy77UtphE
Channel Id: undefined
Length: 25min 40sec (1540 seconds)
Published: Fri Feb 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.