Check out a free preview of the full Digging Into Node.js course
The "Insert into Database Solution" Lesson is part of the full, Digging Into Node.js course featured in this preview video. Here's what you'd learn in this lesson:
Kyle live codes the solution to the exercise.
Transcript from the "Insert into Database Solution" Lesson
[00:00:00]
>> Kyle Simpson: Let's talk about how to write the insertSomething function. To save myself a little bit of work, I'm gonna do some copying and pasting here. So I'm gonna pull in this part and then we'll just adopt it.
>> Kyle Simpson: So I'm gonna say var result = and we're gonna be doing a run because we're doing an INSERT.
[00:00:20]
We don't need to do the lookup here, we always unconditionally insert the something value. We're gonna be inserting into Something. And the two fields that we're gonna be inserting, if we look at our schema, they're gonna be called otherID and data. So let's say,
>> Kyle Simpson: otherID and data.
[00:00:46]
And then we're gonna have two values that we're inserting into Something. And then we need to supply otherID and data. if (result), and the object that comes back might still indicate that Something didn't work. So to make sure that there really was legitimately something that was changed about the database, like there was a record added to the database.
[00:01:13]
There's a property in here called result.changes. If that is greater than 0, because that would indicate how many rows where changed on the database. In this case, how many rows Inserted, as long as that's greater than zero than 0, know that we definitely were successful in inserting the record, so then we can simply return true.
[00:01:35]
And if we don't get there, then we'll just return false just for good measure.
>> Kyle Simpson: Just to test ourselves and make sure we haven't gone awry here, let's try console.log("Success!"). Let's try to run this at the command line and see if it works.
>> Kyle Simpson: So we're gonna say node ex4.js.
[00:02:14]
And if we run it without anything, it's gonna tell us out and you got to specify another. So if we say -- other =, And then we give it some name like hello. Yep, something's wrong. So where was data?
>> Kyle Simpson: That says it's on line 113.
>> Kyle Simpson: Of course, cuz it's called something.
[00:02:40]
>> Kyle Simpson: There we go. Try to run it again, we got a success let's do it a couple of times, all right. So we know that things are working there but let's see the contents of our database. So let's make one final addition to this exercise. That if we have successfully inserted, let's get all the records out of the database.
[00:03:02]
So we're gonna say records = await getAllRecords. And then, if we've received those records, if (records && records.length > 0), it means there are records in the database. Then we wanna go ahead and print out that array, so that's when we're gonna use console.table, which prints out a nice prettyfide version of our two dimensional array.
[00:03:37]
So we need to implement the getAllRecords function.
>> Kyle Simpson: We're going to do join select here. So I'm going to say result = SQL3, and this time we're going to call .all because we can get multiple records back instead of just one .all is going to give us back an array of records.
[00:04:05]
>> Kyle Simpson: I need an await there.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops