Build an AI-Powered Fullstack Next.js App, v3

Adding Sentiments to Database

Scott Moss

Scott Moss

Superfilter AI
Build an AI-Powered Fullstack Next.js App, v3

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

The "Adding Sentiments to Database" 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 begins building the history page, which will plot the sentiment scores for the user. The userId is added to the Analysis schema to make it easier to query a user's sentiment scores. This change requires new journal entries to be created.


Transcript from the "Adding Sentiments to Database" Lesson

>> Cool, so we got our sentiment score. Now what we wanna do is go to our history page and grab the sentiment score for every single entry. For now, let's get every single entry. Eventually you probably wanna do it like gimme last week's. Or however you wanna plot it, there's really, it's up to you.

So, we'll do that. Get data, async. And we need the user, so user = await getUser from clerkId, and Analyses, that's the plural of analysis, I'm pretty sure. [LAUGH] Equals, we want to do prisma here. So, all right, oops! Why did I do that? That's not what I wanted.

So the prisma,analysis.findMany, there we go. So do find many where. Okay, let's go look at the schema. We probably, we're gonna have an issue here, right? So, How do we know what analysis to get? And how do we get it? We wanna get, let's say we wanna get all the analysis for a certain user.

What is the query that we have to run to do that based off the analysis schema?
>> You have to grab every entry and then every-
>> Just grab all entry where we have the clerId.
>> Yeah.
>> Yeah, you'd have to do that first, right? You'd have to say, gimme all the entries for this user first, and then gimme all the analysis Ids for all these entries.

That's gross, we're not doing that.
>> Does prisma have a type of join?
>> Does what?
>> Does prisma have a type of join?
>> They do, but you have to have that relationship set up. So, we actually have to go back and set up a relationship for all of these.

So we need all of our analysis to know about the user in which they belong to. So, let's go back and do that. And of course, this is 100% gonna break our database. So we will probably be dropping the database. And coming back and adding new stuff, that's okay.

I'll have ChatGPT write me out some journal entries or something.
>> [LAUGH]
>> [LAUGH] It's so much better at it than me. Okay, so let's do that. So let's just say, cool, userId, that's a string. And then you have user, which is tied to user. And it's a relationship that has a fields of userId and a reference of Id and undelete of cascade.

So we got that. And then we'll just, we'll have to index this so we can actually use it. So, I guess the best way we can do it is we could, I wonder if I put this here, would I be able to use it by itself? I don't know, I'm just gonna just say index userId.

And then we've gotta go back to the user side and add the the other side of the relation on the user. So now a user also has analyses. Like that. I'm gonna make that optional. Well, I guess that would be that. What am I doing? What, wait, go back, there we go.

Maybe a bunch of analysis. Cool. Now, let's go ahead and watch what happens when prisma says that you can't fix this. So, let's see. It's gonna say, yeah, you added this thing and it's required. And there's already three analysis in your database. So this is just not possible to execute.

But I'll do it if you're okay with me just completely dropping the database. So I'm, yeah, go ahead and drop the database. So, now we have nothing in our database. So I can't even log into the app right now. So I got to just kinda go home. And I'm going to clear all my stuff.

Just moving on out, clear all my stuff. Do that, do that. Okay, and I'm also gonna restart my app because I did some database stuff and it won't be good if you don't. All right, so we got that. Give you all some time to do that. I won't do anything other than just logging in Hopefully, all this still works, we haven't done it in a while.

[LAUGH] It still works. And we're back to where we started. So, I'm gonna go to ChatGPT, and I'm gonna say. I got my unfortunate day one right here. I'm just copy, this one was good. So, actually I made a happy one too. So, just to even it out.

So, to be fair. [LAUGH] What's funny is that this one carried over from the previous one, it was the first sentence. Today was a remarkable turnaround from yesterday's debacle.
>> [LAUGH]
>> [LAUGH] When I saw that, I was like, wait, this could be a story. I should just keep asking it

>> [LAUGH]
>> So what happened today? [LAUGH] Really? And what else? [LAUGH] This is really, this is good. This is, I could turn this into a video. It's like a TV show, every day I just generate a new one and make a video out of it. And you just tune in and see what happens.

You just don't know what's gonna happen, it's hilarious. Anyway, so we got that. [LAUGH] I'll make a new entry here. Well, okay, what did we get? We got an error here. 500 for entry, what is this? Yeah, so we gotta go back. This makes sense, we need to go back and add all this stuff.

We gotta add the user now, right? We added the user to the to the analyze, but we didn't add it in our query, right? So if we go back to where we're making the analyze on the post request to a new entry, we don't add the userId here.

So we have to do that now. That makes sense, so we'll do that. And then we need to-
>> Sorry, where do you just go to do that?
>> Sorry, yeah, so that will be an API journal route.tss in the post request on which we make a new entry.

Inside the prisma.analysis.create, you need to also parse in the userId. Because now an analysis has to have a userId on it, which is what just failed.
>> Okay.
>> I think that's everywhere we need to add it. We shouldn't have to add it on update cuz it should already be there.

Well, no, that's wrong cuz we do an UPSERT. So we have to add it in UPSERT as well, just in case. So we got to go into the UPSERT, inside the create. And do as well here, just in case that happens. Awesome. That should fix that. Yeah, I guess it did, it just did it.

So, for this summer.
>> Yeah, I got this here. [LAUGH]
>> Let's see. This is, why would we get this? I think this one just broke because that one didn't have an analysis on it. So I think that's gonna be the only broken one. But I think if I make another one, it should be good.

Let's see. I don't know why that takes forever sometimes. We'll try that again. There we go. Yeah, so that one broke because it created the entry within the analysis query after that failed. So, the entry was there, but it doesn't have an analysis. So that one is broken.

I'm gonna clean that one up in a minute. But, yeah.
>> My analysis just isn't working somewhere under the hood.
>> We'll put that in there. See what it's talking about. This thing's lagging behind. Cool, okay. What are those days? [LAUGH] My goodness! A day filled with miseries and trials, but with a glimmer of hope for tomorrow.

>> [LAUGH]
>> My goodness! That is funny. Let's make another one. I'm gonna go grab the happy one.
>> [INAUDIBLE] for temperature 0.
>> I know, well, no,. Well, ChatGPT is not temperature 0. Yeah, this one's something else.
>> [LAUGH]
>> So, find it now.
>> Can you continue the thread for the rest of the-

>> Yeah.
>> Entries, one more paragraph-
>> Yeah. [INAUDIBLE]
>> And what a remarkable turn around! [LAUGH]
>> [LAUGH]
>> So good. We'll just do one more, we'll just do one more.
>> The first rays of dawn.
>> [LAUGH]
>> The cage of the sky was huge and warm.

>> [LAUGH] We'll do one more here. One more, one more. I'll just have it, I'll just do another one, okay. Okay, this is great. What happened last night? Make a entry about that, be short. Aah, man! I hope this is good. And calm. [LAUGH]
>> [LAUGH]
>> Wow!

>> [LAUGH]
>> Wow! Okay, got it. You are just in your feelings.
>> [LAUGH]
>> I get it. My goodness, okay.
>> Maybe this is Drake's journal.
>> It's Drake's journal.
>> [LAUGH]
>> It's Drake's journal.
>> Thank you.
>> That makes sense. Tranquil [LAUGH] tranquil. Wow, that's a, you know what?

That color, it is pretty [INAUDIBLE]. I don't know why, but that's the right color right there.
>> Mm-hm.
>> I don't know why but they picked a really good color for that. Retreating to my study [LAUGH] found solace and surrendering to rest after a trying day. [LAUGH] My God!

It's so good. Okay, I think we're good there. I do have to delete the other one, otherwise, it's just gonna break everything. So I'm gonna go do that in prisma. Stop that and start it again. So hopefully, it's, it's this one. You can see it doesn't have 1, it's grayed out.

So, I'll delete that one. Okay, cool, good there.

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