Welcome back.
We left off in the last video with this console log of the KNEX query that we just did.
Now this on the right here is what KNEX does. It builds our SQL statement for us.
So what we just console logged, was the query statement that postgres or KNEX created for postgres.
You can see over here that the builder is an object that has all the information we need to speak with
the smart-brain and do this query.
If I actually want to access something from the users, I will have to do our friend .then() because
this returns a promise and we get our response.
In our case it will be data, this data - again we don't have to do Json because we're not sending it through HTTP -
we get back the data and if we just console.log this data let's see what we get.
Save and go down and it's an empty array.
That's a good sign.
We don't have any users yet but we're getting a response.
So let's do that.
Let's create a statement to insert users when they register.
I'm going to change this into the DB variable just so it doesn't get confusing.
And we'll just keep that as database.
All right let's go to the register endpoint and I'm going to minimize this database because we're going
to stop using it and delete it afterwards.
And if we go to register we see that now instead of doing the database .users.push in the variable
we can just use KNEX we can remove this and do DB dot - Well we'll have to insert,
right.
Let's look at the KNEX docs and see and we see clear methods - insert.
All right look at this great documentation.
We can just find out exactly what we need. In order to insert something we have the insert method with
the data and what we want, or what is going to get returned.
We can't just copy and paste this, so we can just do DB into users we want to insert and it just reads so nicely.
I love KNEX.
We want to insert an object and this object is going to contain whatever we had in the users information.
So we want to insert an e-mail, in which case there will be email. We have the name, which again has the name
that we see from the body.
We also have the password but remember in our table we don't have the password under the users so we'll
leave that blank.
And if we go back to our table example here and connect back to here we see the user's ID is automatically
generated name, email, entries defaults to zero so we don't have to enter anything.
And joined is the date that they've joined.
So let's just do joined new date and once we insert let's just do a .then(console.log) to see what
we get back.
I'm going to save. We don't get any errors which is good and we can go back to our app. Actually, just
because it's faster, instead of doing register through here, we're going to open up postman and
postman already has all our previous commands here, which I love.
So let's click on the register and point with the body of Ann Apples and let's click send.
All right we get a response but remember we never change a response but if we go back to the server
we get result insert.
All right.
It looks like it worked.
If we go back to the database and do select star from users how cool is that.
We have Ann who has been entered as our very first user, with default entries of 0. Amazing.
It's like magic.
How simple was it to just use KNEX and postgres and create our first database.
All right.
In the next video we're going to work on this response.
I'll see you in that one.
Buh-bye.