From the course: Master Next.js by Building Scalable Apps with Routing, Databases, and Performance

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Adding users with server action

Adding users with server action

Now, let's create a server action for inserting users in the database. For that, on submitting the form, a server action should be called, which will execute the db.create method and add a record. So let's start coding. I'll create a folder named actions where all the server actions will be there. And inside it, I'll create a file user actions.js. Now let's create the action to create a new user. I'll say export const create user is equal to async form data. Now here, using the form data, we need to update the admin users table. And to add records in the database, we need the instance of the Prisma client. But before that, let me go to the screen component and make sure I have given the name attributes to all the inputs. Alright, now let's work on the Prisma client. I will create a db.js file inside the lib folder. And inside this file, I'll export the instance by saying export const db is equal to new Prisma client. And back to the server action, I will push the new user data into…

Contents