This may be more of a posgres question but I’m try...
# orm-help
f
This may be more of a posgres question but I’m trying to create a user and as part of this generate a unique username for that user. Any suggestions on the best way to go about this? My initial research showed postgres ‘triggers’ might be one way to do it but I’m not sure how to accomplish that with prisma. My naive implementation would otherwise be to include some random values along with lets say the user’s firstName.lastName and repeat until there are no collisions, but that’s probably not the best way to do this
j
do you have character requirements for this username? will it be overwritten by the user at some point?
f
I’d like to give users the opportunity to edit their username, yes. Ideally would have a minimum length and be alpha-numeric
j
you could combine first name and last name (sounds like you’re already requiring those) and mark it as unique
when you create a user, you could check if that username exists already and, if so, append something to the end of it
or you could use UUID to generate a random string, then ensure in your UI that you let users know to update their username
f
Gotcha. Yeah that was basically what I was getting at with my naive implementation. I guess that would be fine for now I’m just envisioning that’s probably not actually sufficient if you have a lot of users since you might have to ‘add something to the end of it’ more than once to avoid a collision. Thanks!
j
again, uuid would work
searching npm for unique username generator may net you something
depending on your registration flow you could require a username up front and graphql will let you know if it’s not unique
f
Yeah I think i’ll just do it as apart of my registration flow