WEBVTT

00:00.120 --> 00:00.810
All right.

00:00.810 --> 00:01.850
So we're back here now.

00:02.100 --> 00:08.180
And our goal for this video just laid out there is to have communication established between a mass

00:08.180 --> 00:12.290
school database and this file of javascript if node.

00:12.320 --> 00:13.570
Not going can be crazy.

00:13.590 --> 00:20.470
We're just going to be basically running a very simple query like this select we could do curred date.

00:20.610 --> 00:27.060
Or we could do something even sillier like select one plus four.

00:27.150 --> 00:32.350
But those are sequel queries that we could run in a minuscule database and get an answer.

00:32.370 --> 00:34.410
So we're going to run them from this file.

00:34.650 --> 00:37.810
And to do that we need to first establish a connection.

00:38.280 --> 00:43.110
And even before that we have a bigger thing to take care of which is that we don't have might as well

00:43.110 --> 00:45.550
set up in this cloud nine instance.

00:45.570 --> 00:52.570
So if I'm back in the cloud nine docs can see remember when we ran this very very early on my Q Well

00:52.590 --> 00:56.550
dash Seitel space start and what it does is it starts my s.

00:56.570 --> 01:02.730
Q Well in the current workspace that you're in and if you read this note says the nice thing is that

01:02.760 --> 01:06.990
every workspace will run a separate database so your project will never interfere with each other and

01:06.990 --> 01:08.520
that's really nice and useful.

01:08.520 --> 01:14.170
But what that means is none of our data from our previous workspace is available here.

01:14.550 --> 01:16.810
So all we need to do is just run this command.

01:17.100 --> 01:17.850
So we'll do that.

01:17.840 --> 01:24.900
Now here I am in cloud 9 just going to run it and all that we should see is that it installs my ass.

01:24.900 --> 01:31.030
Q Well everything's good but something to take note of right now the root user.

01:31.050 --> 01:34.330
So we need to remember this and you'll see why in just a moment.

01:34.410 --> 01:36.030
So I'll just put a note down there.

01:36.030 --> 01:38.010
Don't make fun of my username please.

01:38.070 --> 01:42.220
I regret it deeply but tyr to learn with code.

01:42.330 --> 01:45.070
Yours will be something different but we need that credential.

01:45.540 --> 01:52.080
OK so then the next thing we now have in my Escorial database I'm going to open up a new terminal over

01:52.080 --> 01:56.850
here and just keep it open and we're going to connect to the CLID.

01:56.970 --> 01:57.950
Remember how we do that.

01:57.960 --> 02:03.730
That's my as well dash DTL space space CLID OK.

02:04.100 --> 02:11.440
And if you look at databases we have nothing really we have these kind of basic ones that we get from

02:11.440 --> 02:12.350
cloud nine.

02:12.430 --> 02:22.300
Let's create a new database and we'll just call it join us already forgetting the semi-colons.

02:22.310 --> 02:22.770
OK.

02:22.870 --> 02:24.100
And then we'll use.

02:24.220 --> 02:25.580
US just like that.

02:25.810 --> 02:27.340
So we have a database.

02:27.340 --> 02:33.480
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

02:33.480 --> 02:36.380
to select one plus for and add a semicolon.

02:36.430 --> 02:37.300
We get an answer.

02:37.300 --> 02:38.900
So this is in my ask.

02:38.950 --> 02:42.010
Well hopefully that's no surprise that that works.

02:42.010 --> 02:47.980
Now the question is how do we get this file which is not sequel at all that's javascript.

02:48.070 --> 02:53.920
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

02:53.920 --> 02:59.110
a couple of components that answer the first thing we have to do is install the package.

02:59.290 --> 03:01.460
So just like before it's NPM install.

03:01.480 --> 03:04.180
But instead of faker we type my ass.

03:04.180 --> 03:06.130
Q Well it's the name of the package.

03:07.240 --> 03:13.390
We hit enter and once it finishes if you open up Node modules there's a new MY ask you a folder.

03:13.410 --> 03:17.420
So this is the code that connects so on its own.

03:17.430 --> 03:19.490
It's not doing any connecting right now.

03:19.590 --> 03:22.930
It's a library it's a toolset that we can utilize.

03:23.010 --> 03:26.080
But we have it on our machine now we have it in cloud 9.

03:26.220 --> 03:28.130
So I'm going to come up here and require it.

03:28.130 --> 03:31.820
And just like I did for faker I'll type my ask you out.

03:31.820 --> 03:36.840
It will require my as well if I can type

03:40.130 --> 03:42.110
that on its own doesn't do anything.

03:42.320 --> 03:45.980
Well that's a lie but it doesn't actually talk to our database in any way.

03:46.130 --> 03:47.900
There's a bit more we need to do.

03:47.900 --> 03:49.810
And it looks like this.

03:49.820 --> 03:54.120
So what we do is establish a connection with the database.

03:54.320 --> 04:02.360
So we require this first we require my Eski Well we save it to a variable then we run as well create

04:02.360 --> 04:08.170
connection which is a method that it comes with and we have to tell it a host which we'll go over later.

04:08.170 --> 04:15.370
We're leaving it as localhost then user which is going to be whatever your username was mine learned

04:15.410 --> 04:16.230
with Colt.

04:16.520 --> 04:18.140
And then a database name.

04:18.440 --> 04:20.310
So we just made a new database.

04:20.900 --> 04:22.110
I think it was called join us.

04:22.120 --> 04:23.700
That's the one we want to work with.

04:23.720 --> 04:31.190
So if we copy this over and get rid of our generate address for now I need to fill this in.

04:31.190 --> 04:35.650
So some user name for me is this right here.

04:35.720 --> 04:38.900
Learn with code that is what helps.

04:39.020 --> 04:41.500
That is what my school was expecting.

04:43.670 --> 04:45.480
Says Please make note of these credentials.

04:45.530 --> 04:47.610
We want to keep on a keep track of that.

04:47.830 --> 04:48.280
OK.

04:48.290 --> 04:53.840
So we put that in there and then the database that we want to use is the one we just created which was

04:53.840 --> 04:58.140
called joyn underscore us.

04:58.350 --> 05:10.370
Just like that so if we try this right now we try running this file node apt J us and nothing happens.

05:10.370 --> 05:12.230
And that's actually a good site.

05:12.230 --> 05:13.880
That means that there wasn't a problem.

05:14.090 --> 05:17.240
But at the same time we're not running any secret code.

05:17.240 --> 05:21.190
The point was by the end of this video we want to do something like this.

05:21.230 --> 05:25.030
So then the next thing to do that looks like this.

05:25.220 --> 05:26.750
And there's a lot to take in here.

05:26.930 --> 05:30.990
Totally understand that we're going to start by just copying this over.

05:31.100 --> 05:35.320
Or you can type it as well but this is how we run queries.

05:37.370 --> 05:37.840
OK.

05:38.080 --> 05:39.130
So there's a lot.

05:39.130 --> 05:41.820
First thing I'll show you is we have a query.

05:41.830 --> 05:44.620
I'll just do select let's do Kuhr date.

05:44.650 --> 05:46.280
That's more accessible.

05:46.330 --> 05:48.290
So we write our sequel code in here.

05:48.400 --> 05:49.000
OK.

05:49.210 --> 05:51.370
So we could do whatever sequel we want.

05:51.370 --> 05:54.910
We don't have any tables yet in there so we're not going to work with tables but we could do something

05:54.910 --> 05:57.020
like this like Khurd.

05:57.220 --> 06:00.760
Then we just save it to a variable called queue.

06:00.770 --> 06:02.340
Now this is the tricky part.

06:02.710 --> 06:06.700
So we've got connection that we this is our connection we created.

06:07.390 --> 06:13.040
So we're referencing it Connection dot queery we're telling it to run the query queue.

06:13.210 --> 06:16.550
So I could have just taken this and put it directly here.

06:16.570 --> 06:21.520
It just makes it a little longer of a line to look at but I'll do that to consolidate it and show you

06:21.970 --> 06:23.220
that it works.

06:23.380 --> 06:27.560
Then we have this whole function error results fields kind of a mess here.

06:27.580 --> 06:34.780
So a lot to look at what this is saying is when this query is finished so we're going to reach out to

06:34.780 --> 06:36.450
our database over here.

06:36.640 --> 06:42.790
We're going to try running select Kuhr date and if we just run it here you can see it works.

06:42.790 --> 06:50.000
So if we try to run it from this connection whatever happens when it's done run all of this code here.

06:50.410 --> 06:51.570
It's called a callback.

06:51.790 --> 06:59.080
And again I wish I could go into more detail but basically this will run and if there is an error there

06:59.080 --> 07:01.770
will be something stored in error here.

07:02.350 --> 07:04.870
If we get results will be stored in results.

07:05.020 --> 07:06.560
We'll talk about fields later.

07:06.610 --> 07:07.920
We're not going to use that now.

07:08.470 --> 07:13.930
So I have this if statement that just says if something happened if there's an error then just stop

07:13.990 --> 07:16.700
throw error right there otherwise.

07:16.900 --> 07:22.960
And we can even get rid of all of this and just print out even more of let's just get rid of everything

07:22.960 --> 07:25.290
and just print results so you can see what we get back.

07:25.390 --> 07:26.960
Just like that.

07:27.040 --> 07:28.200
So there's still a lot going on.

07:28.200 --> 07:34.960
I realize Unfortunately it doesn't get simpler but hopefully you can see the core idea.

07:34.960 --> 07:40.510
We have our query and everything else here is just kind of the JavaScript wrapper around it to run this

07:40.510 --> 07:44.250
query connect to database run the query.

07:44.630 --> 07:45.980
So let's see what happens.

07:48.480 --> 07:51.830
OK so two things to notice.

07:51.910 --> 07:56.390
One we get this road data packet thing back.

07:56.460 --> 08:03.510
We have heard date colon and then the answer for what her date was which is great but then we also have

08:03.510 --> 08:06.570
this other thing I don't know if you picked up on my cursor is changed.

08:06.600 --> 08:08.930
I'm not in the regular terminal.

08:08.970 --> 08:11.550
I have to hit Control C to get out of there.

08:12.030 --> 08:17.970
And that's because the connection was opened but it actually was never closed and never ended.

08:18.240 --> 08:22.320
So it's not a problem but basically that connection is persisting.

08:22.320 --> 08:29.770
So at the end we can do a connection dot either close and let's try it again.

08:31.390 --> 08:32.830
There you go and it ends now.

08:32.890 --> 08:34.110
And you can see we're done.

08:34.440 --> 08:35.000
OK.

08:35.380 --> 08:37.170
So now let's talk about our results.

08:37.180 --> 08:40.420
First of all pat ourselves on the back for getting this far.

08:40.480 --> 08:46.500
We now are running simple queries Yes but we're running queries here in this node 5.

08:46.510 --> 08:48.870
This sets the groundwork for everything else we need to do.

08:48.880 --> 08:51.960
So imagine that instead of a single select KEUR date.

08:52.090 --> 09:00.280
What if this was insert 500 users and we're passing that in and we're running it from node talking to

09:00.670 --> 09:03.490
our database which hopefully you think is cool.

09:03.700 --> 09:09.070
Let's try changing this to something like select one plus five.

09:09.070 --> 09:18.910
Saving it and you can see we're now getting road data packet 1 plus 5 colon is 6.

09:18.910 --> 09:22.060
So this what we're getting back to this syntax is kind of wonky.

09:22.060 --> 09:27.760
I totally get that way we can get around it if we just want the answer.

09:27.760 --> 09:32.170
We can start by adding in the zero which is basically if you're familiar with an array it's going to

09:32.170 --> 09:33.900
give us the first item.

09:33.940 --> 09:35.340
So if we try this again.

09:35.710 --> 09:40.920
Now we're working with this row data packet in particular and to make this easier to access.

09:40.990 --> 09:44.400
I can just go in here and add in as.

09:44.790 --> 09:51.600
So if I do as answer for example select one plus five as Answer I mean if we run it over here

09:54.550 --> 09:55.760
this is what we get right.

09:55.990 --> 09:58.240
Well this is not Javascript.

09:58.270 --> 10:04.060
So the whole job with this my as Cuba library is to or the package is to figure out how to take this

10:04.660 --> 10:06.520
and turn it into something we can use.

10:06.520 --> 10:09.900
So if I do as answer now I'd try it again.

10:11.130 --> 10:15.010
You'll see that it's stored now under the key word answer.

10:15.120 --> 10:18.050
So I can access it just like that.

10:19.360 --> 10:20.150
Perfect.

10:20.440 --> 10:22.500
So last thing it I'll show you.

10:22.690 --> 10:29.150
Let's do select and I'm going to do this one in a separate line or is it a separate variable.

10:29.170 --> 10:38.680
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

10:39.250 --> 10:44.350
comma curred date as date then finally.

10:44.350 --> 10:50.550
Now as now and I do need that right there too.

10:50.890 --> 10:51.390
OK.

10:51.640 --> 11:00.050
So if we do that and I put Q Here I'm basically just saving myself from having to make a gigantic line.

11:00.880 --> 11:05.100
So by referencing Q It's referencing this query which is very useful.

11:05.380 --> 11:06.820
It's going to run it.

11:07.660 --> 11:12.670
And rather than doing all of this let's just look at results like that.

11:13.620 --> 11:16.480
So what we get OK.

11:16.780 --> 11:22.520
Now if you look at our results we can do results zero which will just get us basically the same thing

11:22.640 --> 11:24.840
but we don't have this array around anymore.

11:25.060 --> 11:30.940
More importantly we now have three items coming back which should make sense right.

11:30.970 --> 11:32.880
If we went and took this over to my.

11:32.880 --> 11:39.550
Q Well to create the semi-colon this time we get three results time date and now.

11:39.550 --> 11:43.540
So then the question is OK let's print them out all separately.

11:43.540 --> 11:47.980
So we'll have three print but we need to access them individually.

11:47.980 --> 11:53.120
So we'll do time first and then date that now.

11:53.140 --> 11:59.350
So by putting in this alias the as when it comes back from my ask you well.

11:59.350 --> 12:04.270
So imagine that diagram we're sending it over we're saying hey please send me let's say curred time

12:04.840 --> 12:06.230
as time and it does that.

12:06.240 --> 12:06.810
OK.

12:07.000 --> 12:13.510
Here's Khurd time as time I'm sending it back to you javascript this javascript library takes it sticks

12:13.510 --> 12:21.000
it in the results under the name time only because we gave it that alias so whatever it changes to I

12:21.000 --> 12:23.970
would need to access it under that name anyways.

12:24.010 --> 12:29.820
Now if we do it there we go from getting to three different things that we asked for.

12:30.210 --> 12:30.540
OK.

12:30.540 --> 12:33.060
So that's the extent that we're going to work with it here.

12:33.060 --> 12:37.920
Next up we're going to create our table and we're going to see how we start inserting and selecting

12:37.920 --> 12:39.290
data from this file.

12:39.330 --> 12:44.160
So rather than just selecting curr time we're going to actually do something with the database something

12:44.160 --> 12:46.430
that we couldn't do without my ASKEY Well.
