Check out a free preview of the full Build an AI-Powered Fullstack Next.js App, v3 course

The "Adding Analysis Data to Entry" Lesson is part of the full, Build an AI-Powered Fullstack Next.js App, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Scott calls the OpenAI API when entry content is updated. The upsert method is used, so any entries without analysis will have one created. The analyze branch can be used as a starting point for this lesson.

Preview
Close

Transcript from the "Adding Analysis Data to Entry" Lesson

[00:00:00]
>> We left off on bringing in our analysis that we created using Lange chain and OpenAI. We created it alongside the entry when it gets created. We just need to make sure we update it alongside the entry when it gets updated as well. So let's do that. It should be an easy fix because we already have the utility function that we need to do so, we just got to go put it in the right place.

[00:00:25]
If we head over to app slash, why am I doing this? Let me just open the file explorer. If we just go to where we're updating, which in this case will be api slash journal slash id route ts, that's where we're doing an update. We just need to update the analysis as well can I know why that word trips me up?

[00:00:51]
I'm like what is the analyses, analysis, analysis? Okay. All right, let's do it so we can say wait, and then we can just say Prisma dot analysis dot update, like this and where the ID, it's gonna be the updated entry dot. Can you go away? Updated entry dot now.

[00:01:32]
Wait, hold on. Let's look at the schema. It's the best way to do this. An entry has an analysis type. Okay, so we'll just have to look it up by entry id then. So where entryId equals entry.id. Why does it keep saying entry card for me? I don't know.

[00:01:55]
Entry.id. And then you need to add what you wanna update. So you add data. Yeah, updated entry in my case, updated Entry.id. And then the data is what you wanna update. So in this case, we just wanna update all the analysis. So we can just do that in line right here.

[00:02:20]
We're going to say it's a really cool trick but this parenthesis you say, wait, and then analyze, import that, call it, pass in updated entry content. And there you go. [LAUGH]. Yeah, you can just do that with a single eight. It's pretty cool.
>> You're just spreading the object that's returned.

[00:02:48]
>> Yeah, I'm spreading the object that's returned. Yeah. I mean, you couldn't handle errors this way, but I'm just assuming everything works every time. Yeah, cuz it always does.
>> We have entries that don't have analysis will update at a new one.
>> If we have entries that don't have an analysis, so we can account for that.

[00:03:15]
Let's account for that because I guess we do have some of those right now because we add analysis after we've made the entries. So let's account for that. So what you can do is you can change this to up cert. So up cert is like update this if you find it, but if you don't find it create a new one.

[00:03:32]
So data is all the things you want to update. But then now you have to say update here like this, and this is where you want to. So now, data is where, I am sorry data is where now gets created. Update is where the update is, and data is like everything you need to create one of these, which is literally the same thing as this.

[00:03:55]
So we have to reuse this, so I can't do it here because I'm not calling it twice. So I'm just gonna bring that up here now. And we will just say analysis. And the update will still just be the, analysis, but the data will be the entry, Id.

[00:04:21]
And it'll be updated Entry.id, and then also the analysis. This thing, I think, is just going to freak out with types. But it's because this has like a or undefined. And it's like, we don't like that this is or undefined. It just has to be that so that this should work.

[00:04:48]
So again absurd is like updated if you find it. If you don't find it, create a new one with this. If you do find it, update it with this. Well, it'll probably still break if you try to go to one of the journals on that editor page without it, because we're just assuming that it's there and our UI is not accounting for it not to be there.

[00:05:14]
So you'd have to, like, go on the front end and do some conditionals on that. I'm just gonna delete the journals that don't have it so I don't run into that problem, or [LAUGH].
>> I'm looking at those they can't be destructured
>> Yeah, or just not, yeah, you can't destructure it because it's not there, right?

[00:05:29]
So you don't you don't even get the chance to do the auto save to even get it to trigger this up cert so you won't even see that happen. If you can do, you can handle that. I mean, that's a that's a common JavaScript problem. You wouldn't have this problem in production because if you ran a migration that needed something you would make defaults for that migration and you won't have this problem.

[00:05:51]
So whenever you add a new field to something, especially if it's like required or something or you're writing code as if it's required either supply a default. The reason planet scale didn't say we needed to is because there's a question mark here. Like this is optional, so you don't need to run a migration.

[00:06:12]
But unfortunately for us, we wrote code as if this was not optional. We just assumed that everything had it. So that's why databases are powerful like if you just listen to the database and do what it says, write your code in such a way where you follow these assumptions and don't follow any other assumptions you're coding will mostly be good.

[00:06:32]
Cool. I'm just not going to click on any of those things. Or I might just go and plan it, or I might just go in Prisma and delete them. Actually, I'll just do that. So I don't have that problem. Let me do that. We'll just do MPX, Prisma Studio.

[00:06:48]
And I'll just delete all my journal entries honestly, ultimately them all and actually, let's see, this is a great example you might run into an error here if you try to delete these. Yeah, cool. Awesome. So yeah, so you get an error here. It's not by accident your thing is not broken.

[00:07:06]
It's basically saying, hey, you can't delete this because there's something depending on this. There's an analysis that's attached to this, what do you want me to do with those things? So we have to tell Prisma what to do with those things when we delete them. So what we can do is we can go to analysis on this entry relation, and we can say on delete, like so, and we can say, cascade.

[00:07:29]
So this just means whenever this journal entry that's linked to this analysis gets deleted cascade that delete down to this analysis so it also gets deleted by default it doesn't do that. So now you got to do your push again Cool, and then I'm gonna do the studio.

[00:07:56]
Got a lot of studio tabs over here. That's my cat. Refresh that. And now I'm gonna try to delete it, and confirm the deletion. And boom. Now it's deleted this time. All right, so I should have no more journals. I'm just going to stop my server and start it again 'cause I changed the database and I don't want no problems.

[00:08:29]
So this is gonna break, I can't even refresh this page the ID the logger is this in a database. So I'm gonna go here. And I should just see that at least on my page because I deleted everything. If you didn't do everything you won't see empty page Okay, so let's try to see what happens.

[00:08:49]
We did the update, so let's see. I'm gonna click New Entry. I think for some reason, every time I click it, the first time it takes forever. Okay, there we go. We have our new entry. We can see our analysis is still working on that. Now, I'm just gonna change this, and today was really sad for me.

[00:09:09]
My cat decided that he was not going to snuggle anymore while we watch TV and instead hung out with the dog. So I was really sad about that. So we'll let that save. I could tell that it's talking to, okay, did it? Okay, so I think it did pull it back.

[00:09:42]
Yeah, it worked. We ran into another problem that we gotta fix. This actually worked. So if I refresh this, we'll see the analysis get updated, right? Or maybe not, let's see. Are you gonna get updated? Did it actually do it? Let's log it. There's another problem. Not a problem, but something I want to talk about with client side state, which is why that wouldn't update but let's just make sure it's actually updating here.

[00:10:14]
So let's see. We got that. You can say update it. Let's just log it. Cool. We'll log that. I will just, I'm not okay. So I could tell his talk and opening, I that was taken a long time so I know something's happening there. Here we go. We got a database error.

[00:10:48]
So this thing says, I guess that data one was wrong. It says data and data files, did you mean update? Argument create? I guess that's not data anymore. It's create up cert. Yeah, I guess this is just create now. Okay. Let's just create. Let's run that and see.

[00:11:13]
I'll just get rid of that. We'll see what happens. Yeah, I guess that was it. So I guess data is now create when you do an up cert. I can't tell with all my TypeScript errors, but it still didn't update here, right? So now if I refresh, it does update.

[00:11:35]
Here's a classic example of it being, it chose a color black and now you can't see the text there, but that's fine. So I guess it thought I was so down bad that it chose the color of black, which is probably like really dark emo color subject, My Cat.

[00:11:49]
[LAUGH] That's so funny.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now