OK.
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 up so we have a record of it and I'll make a note selecting data
OK.
Now we're going to try inserting data and you would think that all we needed to do something like insert
into users specify that say e-mail 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 or call it why it
dog at gmail dot com.
It's one of my family dogs.
So if we do that and we insert that.
So we kept everything else the same let's just cancel that log results just like that.
And if there's an error throw the error.
So this is regular sequel 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 that query this query function blah blah blah blah blah.
If there's an error let us know.
Otherwise print the results but see what happens.
OK.
So look at what we get back.
First of all it says OK packet we get field count zero effective rows affected.
Roads is one.
There's no message.
Everything looks OK.
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 were hard coding in where I'm saying rescue the dog that's just not
great right.
This is sequel 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 Internet
e-mail which is going to give me a random e-mail 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 e-mail address called fake or
Internet e-mail.
So there's another syntax we use.
But in truth you'll probably never use this for inserting where you're actually hard coding and the
data you're trying to insert what you actually do look something like this.
So we have a slightly different syntax.
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 a have a key email and then avowed you Jenny 4 6 7 at gmail dot com.
We'll come back to that.
Then our connection at queery 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 sequel.
This is something else.
And then also we have a new argument we're passing person as well.
And then our regular function with error and result.
So what I'm going to do is copy this over and we're going to comment this one out as well and I'll call
this inserting data.
Take two and paste this.
OK.
So a couple of things to go over.
One doesn't work.
We'll try that just a moment 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.
Entered into users but then we had to specify exactly what we're inserting.
So we'd say you know email and if we had 10 columns we'd have say email comma X have a y come 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 is Gennie.
We don't have a name but we could have if we had it in our database we could have name is Gennie we
could have 20 different things and to insert it all correctly all I have to do is connection not query
insert into users and I add set question mark and then the second argument is this object.
And what will happen is this Majeski will fancy library this package will basically take this object
and it will figure out OK there's something called e-mail.
Well then I'm going to turn this query into insert into user's e-mail values.
Jenny Jenny for 7 at gmail dot com we have a quote issue again so I'll make those double quotes.
OK.
So it will turn it into this.
But if I had something like e-mail and it did this a little bit better
I'd say we had e-mail comma and that's a birthday is two thousand and one comma 11 comma slash 11 and
you dashes from my school.
Well let's see if 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 are.
It wasn't a problem.
The same thing we ran into here.
Like if you're just inserting this one hard coded e-mail why does it matter.
And the answer is down here.
Well it matters because now we don't have to hard coded 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 Gennie works just like that.
Jenny 4 6 7 and she mailboat come save and let's see what happens now.
Insert into user set person is this OK.
And it looks like we got an error this time and it tells us oh OK 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 telling me error is not defined.
So it's saying this thing error is not defined in.
Actually I was ignoring these but I'm sure Cloud 9 is trying to tell me.
Yes it is says error.
It's not defined and that's because appear I called it.
E.R. 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 E-R though that's a standard thing just like that.
OK.
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 for six seven at gmail dot com.
And the reason we're getting this error is that we already have a genie in there.
So this is interesting and this is why I did that primary key.
So we just like one second ago inserted Jenny for 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 6:7 Then I screwed up our code.
And so it broke right here.
But genuine already added.
So now we just added another Gennie.
Here we tried to and this time it came back saying there's a problem and if we look at the error that
it through it's 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 for 6:7 waited until it went to the database.
The database had me think no sorry we already have Jenny for 6:7 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.
Ok cool.
So that's good to know it's working.
So rather than doing Gennie for 6:7 let's change this to something more interesting.
Anyways let's go back to using faker.
So we need to require F.A. again require faker and all we'll do is insert someone with a new e-mail.
So it's just faker dot Internet dot email like that.
So this will do is instead of having hardcoded it every time we run this file it will insert a new e-mail
that it just generates.
So let's try and see what happens.
OK it looks good.
Something happened.
Now you can see we got Velma 94 at gmail dot com and let's do Howard we only identified the last one
that was most recently added.
Quick review.
Well we could do an order by created that if we did it that way.
Can See The last one is vellum and we're going 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's 61.
And actually if I change limit to 2 because we just inserted 2.
We also have Travis 60 gmail dot com suite.
So I'll end the video here but we saw now how we can insert things dynamically and why this syntax is
preferrable preferable to this one up here where we are 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 or internet or email
will have.
This is pseudo code 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 work.
We don't know it right.
We can't type it out exactly.
We don't know what the e-mail is itself but we can save it in a variable.
OK.
So we'll go back to fake or dumb Internet e-mail and we'll leave it there for now.