From the course: Hands-On AI: Build an AI Chatbot with GPT-4o and Next.js

Unlock this course with a free trial

Join today to access over 24,700 courses taught by industry experts.

Saving and retrieving data from the database

Saving and retrieving data from the database - ChatGPT Tutorial

From the course: Hands-On AI: Build an AI Chatbot with GPT-4o and Next.js

Saving and retrieving data from the database

- [Instructor] Now we'll create functions to interact with our database. In the db.js file, let's start with a function to create new chats. We'll export these functions so we can use them in other parts of our application. On line 10, we'll start with export const createChat and that's equal to an arrow function. Now for the parameter we have title and its value is a string new chat. This creates a function called createChat that takes an optional title parameter. If no title is provided, it's defaults to new chat. For the function body, we'll add db.chats.add and then give it an object argument. So we'll call that, and here we're using Dexie's add method to insert a new record into a chats table. Our object argument will contain two properties. The first property is title and we can have that as title, but since it's the same thing, we'll just leave it as this. The second is createdAt and for its value, new date, we'll call that, then .toISOString, let's call that as well. This does…

Contents