Loading
Current section: Generating Seed Data 5 exercises
solution

Generating Unique and Valid Usernames in Seed Data

Transcript

00:00 Before I actually enforce this uniqueness, I'm going to come down here and we're going to add a catch right here because if there is a problem creating one of these users, this isn't all that critical that we actually fail everything. I'd rather just have some seed data

00:16 created rather than blowing up the whole seed script. This is especially useful if you're creating a lot of users and it takes a while and you're waiting and you're waiting, and then the second to last user, it blows up and you're like, that's so annoying. So yeah, we're going to just add a catch here, and we will just ignore the error entirely.

00:36 So yeah, rather than what Copilot is saying, we're just going to say console.error, and then we'll just ignore it. It's fine. It's not a big deal. So that way, we could say error creating user. There we go. So now we're not going to have

00:54 any problems if there's an error creating the user. But we're also going to reduce the likelihood of there being an error creating the user using the unique enforcer. So we're going to create a unique username enforcer by creating a new unique enforcer,

01:13 and we don't need to specify any options or anything like that. So then we'll come over here and we'll use the username unique enforcer here by saying, our username actually comes from Enforce,

01:29 and then here's our callback for generating that username. Now, we actually want to make some adjustments to this username as well to make sure that it conforms to our requirements in the database, some validation stuff. So we're going to do one thing to

01:49 actually make it a little more likely to be unique, and then another thing to make sure that it stays within the bounds of what we want as far as our usernames are concerned. So here's what we're going to do. We're going to wrap this up like this.

02:08 We're going to add a faker.string.alphanumeric with two characters, and then we'll have an underscore. So that way, this will not be count, it's length. So this way, we just have some random string.

02:26 So even if we somehow managed to get the same first name and last name, we're still going to probably be unique because we have this random alphanumeric two-character code before any of our usernames. For these randomly generated users, that is perfectly fine. I don't really care about

02:45 that for my local development and stuff. So once we have that, then we can add a couple of pieces of transformation to make sure that this matches up with our requirements. So we have slice 0-20 because we have a limit on our usernames to be 20 characters long.

03:03 We also have our usernames as lowercase, so we'll add that. Then we also have a requirement on our usernames to be alphanumeric. So if there are any extra characters that are thrown in there by this random generator, we'll replace those.

03:19 So replace anything other than A-Z and 0-9 with an underscore. That will make our generation of usernames a little bit more reliable and the username is used as part of the e-mail as well,

03:37 so that will take care of that as well. Great. So let's run our seed scripts. Here we go, refresh that. So now we've got those alphanumeric characters at the beginning, no big deal. We've got our e-mails. Looks like we're missing Cody. I probably refreshed too early. Yeah, there's Cody. There's our usernames,

03:56 matches the e-mail, and then our names. Like I said, it's pretty unlikely that we ever would have had this problem, but especially once you get a lot of generated data, it definitely can happen. So using this enforcer, unique enforcer, basically the idea is you just pass it

04:13 this function and if it gets the same thing twice in a row, or twice in at all, then it will just call the function again and it will keep calling it until it times out or it runs out of iterations. I think it defaults to like 50 times or something like that, which of course is configurable as well.

04:31 So that takes care of our username. Not only does it ensure that it's unique, but it also ensures that it falls within our requirements for what a username is. That as a general principle is a good idea to make sure that the seed data that you're generating falls within the requirements of

04:51 your application and what validation you would have for those different fields. So there you go.