Next.js Discord

Discord Forum

Ideal way to check if user exists in db using prisma

Unanswered
<Milind ツ /> posted this in #help-forum
Open in Discord
So in a situation where you need to check if user's username or email already exists in the db, we would approach this by 2 alternative ways:

1. Check for username and email separately in their separate prisma.user.findUnique() call
2. Check for both using OR operator

When doing the first approach, we have the flexibility to set the kind of error message we want to return from API route. example, when checking only username, we can throw "An user with this username already exists" and similar way for email.

When doing the second approach, we dont have much flexibility since we can not predict if its username or email that database already contains.

So what's the ideal way or are there any alternative ways?

1 Reply

bump