Transcript from the "Get Database Records" Lesson
>> Kyle Simpson: 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. So we're gonna say, records = await getAllRecords.
>> Kyle Simpson: And then, if we've received those records, if (records && records.length > 0, that means there are records in the database.
[00:00:34] 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 prettified version of our two dimensional array. So we need to implement the getAll records function.
>> Kyle Simpson: We're gonna do a join select here, so I'm gonna say result = SQL3, and this time we're gonna call .all because we can get multiple records back instead of just one.
[00:01:09] .all is gonna give us back an array of records.
>> Kyle Simpson: I need an await there.
>> Kyle Simpson: So I'm gonna do a SELECT and I'm gonna select the Other.data AS 'other', and I'm gonna select Something.data AS 'something'.
>> Kyle Simpson: FROM, and I'll select from the something table. JOIN with Other ON,
>> Kyle Simpson: We want Something.otherID = Other.id. That's how we join. And then for good measure I'm gonna go ahead and do an order by clause here. I'm gonna ORDER BY, and let's see, I wanna ORDER BY Something, no, ORDER BY Other.id DESC. So I want the newest other values first, and then within that group we'll order by Something.data ASC.
>> Kyle Simpson: We don't need to pass in any values to be interpolated, so if (result && result.length > 0),
>> Kyle Simpson: Then return the result. If I wrote my SQL correctly, which about half the time I write SQL correctly, then we should be able to call this again. And now calling it the third time there should be three records that we've inserted in the database.
[00:03:27] Ut-oh, I forgot to make it an async fraction.
>> Kyle Simpson: It'll be a win if I didn't make a SQL mistake in the entire exercise. There we go. All right, so we have three different values in there. Let’s give it a different other, we give it world.
>> Kyle Simpson: And it seems to be working.
[00:03:57] I guess the last thing for good measure is that we should be returning here so that we don’t end up printing out that oops message.
>> Kyle Simpson: All right, so now we’re able to save data to a database and retrieve it from a database. So the question is in get all records I'm not returning anything, if this fails to get us anything or if it gave us an empty array.
[00:04:31] If we come up here, I'm only in my if statement. I'm only gonna do console.table if we got an actual array that was non empty, otherwise we fall through to that oops. So I think we only need to return an array if it's non empty.
>> Kyle Simpson: Something would have had to have gone wrong for us to get to this point.
[00:04:57] We thought we inserted some stuff to get to this point and still have an empty array. Something definitely more drastic has gone wrong, so.