Welcome back.
Let's connect our database to our server. And in order to do that there's tons of tools as you might
have known.
Yes we're using NPM again, a node package that allows us to connect to a database.
There's many options.
And in this video we're going to work with my favorite KNEX.js.
It works with PostgreSQL, MSSQL,
MySQL,
MariaDB, SQLite,
Oracle, pretty much any relational database KNEX works with and is super simple to use.
I absolutely love it and you're going to love it too.
Just to show you there's different options there is something like pg-promise here.
That again is very popular and you can see it here that it's very easy.
You just import it and do .any and do your SQL statement. And you can pick whichever one you
want.
Although pg-promise really allows you to write simple SQL queries, KNEX is just a really good tool
that if you're ever working on a project I would recommend, so I'm going to use KNEX.js to do
it. And if you go into installation and Node.js, of course they have good documentation of how to install
it. In our case
We can just NPM install KNEX.
I'm just going to stop the server for now and run NPM install.
Let's open up the package.json file.
Make sure that it's working. There is
KNEX and they also say that we need to install the PostgreSQL or whichever database we want.
In our case we want PostgreSQL.
So we're going to NPM install PG.
All right we have PostgreSQL installed as well. Amazing.
Again the reason that I love this library is it makes things so simple you can see over here that in
their document it tells you exactly what you need to do to connect to the database.
So let's copy this, go to our server.js. Let's import by doing const KNEX = require('knex');
and in here we'll just copy and paste, and we can remove the variable
and just use it like that.
Now this syntax might be confusing to you but all it's doing is (if we remove this) is just doing knex
and running that KNEX function that we just had.
So let's just keep it like that because it looks better.
We want to update our client to PostgreSQL.
So again if we do PostgreSQL it'll be PG because that's what we just downloaded and then we have to
tell it where this database lives. And we would modify this according to where we set up our database
on a hosted platform.
But for now we have localhost and localhost is the same thing as this number 127.0.0.1
is home.
There's a funny t-shirt that I saw a developer wear once, that says there's no place like 127.0.0.1
because it means localhost and it was a play on there's no place like home.
I thought it was funny.
Anyway this is the same thing as local host so we'll just keep it like that and we have our user in our case if you
remember and we go to our database the user I guess will be me so I'll just do my name.
There's no password to our database so we can just leave it blank.
There's actually no user either.
But for now that's fine.
And the database if you remember
is called 'smart-brain'.
Now if I actually exit this
and start over you see that it's the same thing that we're entering here.
We're just saying where the host is, the user password and database.
And because we're able to log in here without the user and password, Well, we don't really need that information.
Let's give that a go.
I'm going to save. Do npm start.
All right.
I'm not getting any errors here which is good.
Let's go back to the, to the page here
and copy and paste just a simple, maybe select statement to make sure that everything is working.
If we go back to the left hand side over here we have the query builder and select.
So let's just grab something from our database.
Even though we don't really have anything. You see that we just have knex.select and then whatever
we want to select from the database.
All right let's do that.
Let's do
knex.select(*).from('users');
database.
I'm going to save and I get an error which is good.
It's reading here but it says unexpected token because, well I have to wrap this as a string. Let's try
that again.
And you see here how I get connect select is not a function because here I have to assign this to a
variable.
And the way we do that is we can just say const database equals just like we had before.
KNEX.select.
But let's just do Postgres for now. Save and change this to the Postgres with save, and now there's no
errors.
That's awesome.
If we perhaps console.log
now and save.
All right we get a ton of information here.
But it looks like it's responding with something that's perfect.
It looks like we're connected to the smart-brain but this isn't exactly how we return information from
KNEX and we'll get into that into the next video.
But for now we've connected our server to our database.
I'll see you in the next one.
Bye-bye.