Okay.
So then that brings us to inserting data.
But if I wanted to instead insert a new user, so I'm going to copy this.
Well, first, I'm going to comment that out.
So we have a record of it and I'll make a note selecting data.
Okay.
Now we're going to try inserting data.
And you would think that all we needed to do is something like insert into users, specify, let's say
email values, and then all we want is we'll do one here and we have to pay attention to quotes because
we're working inside of a string.
So we need to use double quotes here.
And let's say we just make up an email, we'll call it Why it?
The dog at gmail.com.
It's one of my family dogs.
So if we do that and we insert that, so if we kept everything else the same, let's just console.log
results just like that.
And if there's an error, throw the error.
So this is regular SQL, right?
If we ran this and we could verify it, of course, if we ran this, right now it works.
So now let's let's change this a little bit so that we don't end up with two of them, but everything
else is valid.
So let's do that, run it connection, not query this query function, blah, blah, blah, blah, blah.
If there's an error, let us know.
Otherwise print the results.
Let's see what happens.
Okay.
So look at what we get back.
First of all, it says, okay, packet, we get field count, zero affected rows.
Affected rows is one.
There's no message.
Everything looks okay.
If we went over here and did a select star from users, you'd see that Rusty, the dog is now in here.
So all of that is looking good and that's correct.
This is working how it should work, which is good, but this is not something that really is helpful
to us inserting one item at a time that we're hard coding in where I'm saying, Rusty, the dog, that's
just not great.
Like this is SQL writing.
We could if we wanted to insert Rusty the dog.
Exactly.
We could just copy it and do what we did and paste it here.
What we want is to be able to do dynamic data.
So I want to be able to say which we don't have fake or set up, but I want to be able to say faker,
dot internet, dot email, which is going to give me a random email address.
And I can't just stick that in here.
That's not going to work because it's going to just try and insert an email address called fake Internet
email.
So there's another syntax we use.
In truth, you'll probably never use this for inserting where you're actually hard coding in the data.
You're trying to insert what you'll actually do, look something like this.
So we have a slightly different syntax.
The first thing you'll notice is that I'm creating a variable that is storing the person or the user
we're inserting, and it's a JavaScript object where I have a key email and then a value.
Jenny Four, six, seven at gmail.com.
We'll come back to that.
Then our connection query looks the same, but our query is different.
We're doing insert into users and then this weird set question mark thing, we've never seen that.
Is that valid SQL is is something else.
And then also we have a new argument we're passing person in as well and then our regular function with
error and result.
So what I'm going to do is copy this over.
I'm going to comment this one out as well and I'll call this inserting data, take two and paste this.
So a couple of things to go over.
One, does it work?
We'll try that in just a moment.
And two, how does it work?
So we've got this insert into users.
That's the same, right?
That's what we had up here, insert into users.
But then we had to specify exactly what we're inserting.
So we'd say email and if we had ten columns, we would have to say email, comma, X, Y, whatever
they are, right, as we're used to.
But there's another way.
What we can do instead is make a separate entity in JavaScript and object, where I could come in here
and do something like email as Jenny.
We don't have name, but we could have if we had it in our database.
We could have name as Jenny.
We could have 20 different things.
And to insert it all correctly, all I have to do is connection, query, insert into users and I add
set question mark.
And then the second argument is this object and what will happen is this my SQL Fancy library?
This package will basically take this object and then we'll figure out, okay, there's something called
email.
Well, then I'm going to turn this query into insert into user's email values.
Jenny or Jenny, four, six, seven at gmail.com.
We have a quote issue again.
So I'll make this double quotes so it will turn it into this.
But if I had something like email and let me indent this a little bit better.
Let's say we had email comma and let's say birthday is 2001, comma, 11 or slash 11.
We'd actually need dashes for MySQL, but let's say we have that.
Well, by just passing it in here, I never have to mention birthday.
It will know when inserting it to then have birthday equal to this.
So I won't go and type the whole thing.
But the point is that it can dynamically do that for us, and that doesn't seem that helpful when we're
inserting one thing like this.
It's the same problem that we or it wasn't a problem with the same thing we ran into here.
Like if you're just inserting this one hard coded email, why does it matter?
And the answer is down here, well, it matters because now we don't have to hard code it.
What we can do is change this string because we're no longer part of a peer.
Whatever we're typing is inside this text.
But down here, it's not.
It's separate.
So we can replace this with something dynamic, like using faker.
But let's start by just making sure that Jenny works just like that.
Jenny four, six, seven at gmail.com.
Let's save and let's see what happens now.
Insert it into user set person is this.
OC.
And it looks like we got an error this time and it tells us, Oh, och, well, this is a silly one.
This is my fault with the code, but it's something it's good to talk about.
If you look at what the error is, it's telling me error is not defined.
So it's saying this thing error is not defined.
And actually I was ignoring these, but I'm sure Cloud9 is trying to tell me.
Yes, it is.
So it's error is not defined and that's because up here I called it error.
You can call it whatever you want.
So I could call this mistake in all caps.
It's a bad idea.
It would be a mistake to use all caps, but as long as they match.
So I'll go with error, though.
That's a standard thing.
Just like that.
Now.
Now we get a different error this time.
Oh, my goodness.
It's the same one.
Geez.
Now we get a different error.
This time it's saying duplicate entry.
Great.
This is good to know.
Now it's telling us we already have someone named Jenny.
Four, six, seven at gmail.com.
And the reason we're getting this error is that we already have a Jenny in there.
So this is interesting, and this is why I added that primary key.
So we just like 1/2 ago inserted Jenny four, six, seven at Gmail and we hit this problem with the
error coming back where we had a syntax error, but it was still inserted.
Remember, this code only runs when the insert or the query is done.
So we inserted Jenny for six seven.
Then I screwed up our code and so it broke right here.
But Jenny was already added.
So now we just added another Jenny, or we tried to, and this time it came back saying there's a problem.
And if we look at the error that it threw says duplicate entry for key primary.
So you can see now how JavaScript is talking.
This is the kind of remember those arrows.
I won't go back to the slides, the two way arrows where we tentatively tried to insert from Node.
Hey, can we insert Jenny four, six, seven.
Waited until it went to the database.
The database head.
Let me think.
No, sorry.
We already have Jenny.
Four, six, seven.
And there's a primary key constraint.
Don't you remember?
You made the schema.
Well, that's a problem.
I'm going to have to refuse your request and send you back an error, which then is passed into this.
And this part runs if error throw the error.
Okay, cool.
So that's good to know.
It's working.
So rather than doing Jenny four, six, seven, let's change this to something more interesting.
Anyways, let's go back to using Faker.
So we need to require faker again, require faker.
And all we'll do is insert someone with a new email.
So it's just faker, dot internet, dot email like that.
So what this will do is instead of having us hardcoded it, every time we run this file, it will insert
a new email that it just generates.
So let's try it, see what happens.
Okay.
It looks good.
Something happened.
Now you can see we got Velma 94 at gmail.com.
And let's do.
How would we only identify the last one that was most recently added?
Quick review.
Well, we could do an order by created at if we did it that way.
You can see the last one is Velma.
We're going in the wrong order so we can do descending and we could throw in limit one.
Now we get the most recent one.
So if we go back to Bash, try running it again.
And again.
We've got Sanford, Gaylord, 61.
And actually if I change limit to two because we just inserted two, we also have Travis 60 at gmail.com.
Sweet.
So I'll end the video here.
But we saw now how we can insert things dynamically and why this syntax is preferable.
Preferable to this one up here where we're hard coding it as a string, as text.
This works, but it's limited, right?
We can only insert whatever we type.
This allows us to be dynamic and shortly in our web application we won't have fake internet email.
We'll have this is pseudocode whatever the user entered into the form.
So when we have that form, the user enters their email, whatever they type in there, we're going
to stick here and we don't know it, right?
We can't type it out exactly.
We don't know what the email is itself, but we can save it in a variable.
So we'll go back to fake or Internet email and we'll leave it there for now.