All right.
So we're back here now.
And our goal for this video just laid out there is to have communication established between a mass
school database and this file of javascript if node.
Not going can be crazy.
We're just going to be basically running a very simple query like this select we could do curred date.
Or we could do something even sillier like select one plus four.
But those are sequel queries that we could run in a minuscule database and get an answer.
So we're going to run them from this file.
And to do that we need to first establish a connection.
And even before that we have a bigger thing to take care of which is that we don't have might as well
set up in this cloud nine instance.
So if I'm back in the cloud nine docs can see remember when we ran this very very early on my Q Well
dash Seitel space start and what it does is it starts my s.
Q Well in the current workspace that you're in and if you read this note says the nice thing is that
every workspace will run a separate database so your project will never interfere with each other and
that's really nice and useful.
But what that means is none of our data from our previous workspace is available here.
So all we need to do is just run this command.
So we'll do that.
Now here I am in cloud 9 just going to run it and all that we should see is that it installs my ass.
Q Well everything's good but something to take note of right now the root user.
So we need to remember this and you'll see why in just a moment.
So I'll just put a note down there.
Don't make fun of my username please.
I regret it deeply but tyr to learn with code.
Yours will be something different but we need that credential.
OK so then the next thing we now have in my Escorial database I'm going to open up a new terminal over
here and just keep it open and we're going to connect to the CLID.
Remember how we do that.
That's my as well dash DTL space space CLID OK.
And if you look at databases we have nothing really we have these kind of basic ones that we get from
cloud nine.
Let's create a new database and we'll just call it join us already forgetting the semi-colons.
OK.
And then we'll use.
US just like that.
So we have a database.
And just to show you you know if we run a command like this it's like her date we get an answer or free
to select one plus for and add a semicolon.
We get an answer.
So this is in my ask.
Well hopefully that's no surprise that that works.
Now the question is how do we get this file which is not sequel at all that's javascript.
How do I get it to talk to them I ask you all server that's running and run some code in it and there's
a couple of components that answer the first thing we have to do is install the package.
So just like before it's NPM install.
But instead of faker we type my ass.
Q Well it's the name of the package.
We hit enter and once it finishes if you open up Node modules there's a new MY ask you a folder.
So this is the code that connects so on its own.
It's not doing any connecting right now.
It's a library it's a toolset that we can utilize.
But we have it on our machine now we have it in cloud 9.
So I'm going to come up here and require it.
And just like I did for faker I'll type my ask you out.
It will require my as well if I can type
that on its own doesn't do anything.
Well that's a lie but it doesn't actually talk to our database in any way.
There's a bit more we need to do.
And it looks like this.
So what we do is establish a connection with the database.
So we require this first we require my Eski Well we save it to a variable then we run as well create
connection which is a method that it comes with and we have to tell it a host which we'll go over later.
We're leaving it as localhost then user which is going to be whatever your username was mine learned
with Colt.
And then a database name.
So we just made a new database.
I think it was called join us.
That's the one we want to work with.
So if we copy this over and get rid of our generate address for now I need to fill this in.
So some user name for me is this right here.
Learn with code that is what helps.
That is what my school was expecting.
Says Please make note of these credentials.
We want to keep on a keep track of that.
OK.
So we put that in there and then the database that we want to use is the one we just created which was
called joyn underscore us.
Just like that so if we try this right now we try running this file node apt J us and nothing happens.
And that's actually a good site.
That means that there wasn't a problem.
But at the same time we're not running any secret code.
The point was by the end of this video we want to do something like this.
So then the next thing to do that looks like this.
And there's a lot to take in here.
Totally understand that we're going to start by just copying this over.
Or you can type it as well but this is how we run queries.
OK.
So there's a lot.
First thing I'll show you is we have a query.
I'll just do select let's do Kuhr date.
That's more accessible.
So we write our sequel code in here.
OK.
So we could do whatever sequel we want.
We don't have any tables yet in there so we're not going to work with tables but we could do something
like this like Khurd.
Then we just save it to a variable called queue.
Now this is the tricky part.
So we've got connection that we this is our connection we created.
So we're referencing it Connection dot queery we're telling it to run the query queue.
So I could have just taken this and put it directly here.
It just makes it a little longer of a line to look at but I'll do that to consolidate it and show you
that it works.
Then we have this whole function error results fields kind of a mess here.
So a lot to look at what this is saying is when this query is finished so we're going to reach out to
our database over here.
We're going to try running select Kuhr date and if we just run it here you can see it works.
So if we try to run it from this connection whatever happens when it's done run all of this code here.
It's called a callback.
And again I wish I could go into more detail but basically this will run and if there is an error there
will be something stored in error here.
If we get results will be stored in results.
We'll talk about fields later.
We're not going to use that now.
So I have this if statement that just says if something happened if there's an error then just stop
throw error right there otherwise.
And we can even get rid of all of this and just print out even more of let's just get rid of everything
and just print results so you can see what we get back.
Just like that.
So there's still a lot going on.
I realize Unfortunately it doesn't get simpler but hopefully you can see the core idea.
We have our query and everything else here is just kind of the JavaScript wrapper around it to run this
query connect to database run the query.
So let's see what happens.
OK so two things to notice.
One we get this road data packet thing back.
We have heard date colon and then the answer for what her date was which is great but then we also have
this other thing I don't know if you picked up on my cursor is changed.
I'm not in the regular terminal.
I have to hit Control C to get out of there.
And that's because the connection was opened but it actually was never closed and never ended.
So it's not a problem but basically that connection is persisting.
So at the end we can do a connection dot either close and let's try it again.
There you go and it ends now.
And you can see we're done.
OK.
So now let's talk about our results.
First of all pat ourselves on the back for getting this far.
We now are running simple queries Yes but we're running queries here in this node 5.
This sets the groundwork for everything else we need to do.
So imagine that instead of a single select KEUR date.
What if this was insert 500 users and we're passing that in and we're running it from node talking to
our database which hopefully you think is cool.
Let's try changing this to something like select one plus five.
Saving it and you can see we're now getting road data packet 1 plus 5 colon is 6.
So this what we're getting back to this syntax is kind of wonky.
I totally get that way we can get around it if we just want the answer.
We can start by adding in the zero which is basically if you're familiar with an array it's going to
give us the first item.
So if we try this again.
Now we're working with this row data packet in particular and to make this easier to access.
I can just go in here and add in as.
So if I do as answer for example select one plus five as Answer I mean if we run it over here
this is what we get right.
Well this is not Javascript.
So the whole job with this my as Cuba library is to or the package is to figure out how to take this
and turn it into something we can use.
So if I do as answer now I'd try it again.
You'll see that it's stored now under the key word answer.
So I can access it just like that.
Perfect.
So last thing it I'll show you.
Let's do select and I'm going to do this one in a separate line or is it a separate variable.
I'll call it R Q And instead of just selecting one for 6 or 1 4 let's do select curred time as time
comma curred date as date then finally.
Now as now and I do need that right there too.
OK.
So if we do that and I put Q Here I'm basically just saving myself from having to make a gigantic line.
So by referencing Q It's referencing this query which is very useful.
It's going to run it.
And rather than doing all of this let's just look at results like that.
So what we get OK.
Now if you look at our results we can do results zero which will just get us basically the same thing
but we don't have this array around anymore.
More importantly we now have three items coming back which should make sense right.
If we went and took this over to my.
Q Well to create the semi-colon this time we get three results time date and now.
So then the question is OK let's print them out all separately.
So we'll have three print but we need to access them individually.
So we'll do time first and then date that now.
So by putting in this alias the as when it comes back from my ask you well.
So imagine that diagram we're sending it over we're saying hey please send me let's say curred time
as time and it does that.
OK.
Here's Khurd time as time I'm sending it back to you javascript this javascript library takes it sticks
it in the results under the name time only because we gave it that alias so whatever it changes to I
would need to access it under that name anyways.
Now if we do it there we go from getting to three different things that we asked for.
OK.
So that's the extent that we're going to work with it here.
Next up we're going to create our table and we're going to see how we start inserting and selecting
data from this file.
So rather than just selecting curr time we're going to actually do something with the database something
that we couldn't do without my ASKEY Well.