Current section: Generating Seed Data 5 exercises

Generating Dynamic Seed Data


00:00 Let's improve this seed script so we have dynamic number of users so right down here we're going to say for loop and Index is fine, and then we've got our array of Actually, it's not an array. It's a total users, and so what's cool about this is once we move this into here

00:20 Then we can make a change to our total users from five to 5,000 if we wanted to and then now we've got 5,000 users. We're creating. I don't recommend that that's a lot For our test data, but but yeah, you totally could and in fact toward the end. We're going to be doing some Performance testing and stuff that will require

00:43 Loading up our database with tons of users and so yes that will be quite handy in that scenario Okay, great, so let's Follow Marty the money bag suggestion of making our image for our user be a create and

01:01 There it is and here's our data for the create right here, so the user images It's just an array of all these image calls and again that image call is just the data that you would provide to that data property, so we've got our user image our users are now going to have a

01:20 User image, we're just going to iterate through each one. We'll use one at a time here And so all of the user images that we have all ten of them will be used at some point Okay, great So now we're going to change this hard-coded array of two notes per user to anything from zero to three notes

01:38 And there are some ways that you could do this the way that I'm going to do this Is I'm going to just take this note hard-coded right here And then we're going to get rid of all this and we're going to say array dot from and Yeah, you sort of got the idea copilot. We're going to say our array is going to be a

01:57 random assortment between zero and three so we'll say faker dot number dot int and Min will be at zero and max will be three and then we can map on that And whoops there we go And then we can return our

02:16 Note information here, right? So now we're creating anywhere from zero to three notes with random title and content But we're not done because I want those notes to have a random number of images from zero to three images each so we're going to say images and we're going to create and

02:35 Another array dot from yes, and we're going to map on that as well So this is going to be Have a length from zero to three and then we'll map across those to create our images now Different from what we did with our user images. I

02:51 Want this to actually just be totally random which images are picked and so we're going to get our image number Using faker number int from min of zero and a max now, we'll just do a max of three And

03:09 With that then we can return the image for that image number And actually, sorry, that's not a max of three. That's how many images we want. We've already got that right here That's going to be a max of nine. So we have Index zero to nine. We have ten images that are available in our tests

03:30 fixtures image place Okay, great. So Then let's come down here Let's get rid of that comment. We're done with that and then we come down here and we're going to add Cody's image so image create and

03:50 There's our image for Cody and then we have some Cody images right up here that we can reference that are like our hard-coded Images so Cody is going to be our user that has like the consistent Notes and the consistent images and everything the consistent IDs

04:06 Stuff like that to make it a lot easier to work with at least one user that has all that consistency So instead of creating these hard-coded images, we've got the cute koala and koala eating So this is going to be Cody images cute koala and koala eating. There we go, and

04:25 We are squared away. So now let's come over here and PX Prisma DBC And we get our logs for how long things are taken which is pretty cool and then it's finally been executed now We refresh and boom. We've got all of our notes in here. Some of those notes have images some of them don't We also have our users right here

04:44 so a bunch of different users and some of them have notes some of them don't and then we've got user images in here as well and They have their blob stuff in here. And then of course we have our note images, too So we've got 11 note images in here, which is pretty awesome

05:02 So in a review the the most like important key takeaways here are yes Of course, these utilities are super super helpful. That's just like write JavaScript, right? So Yes, write JavaScript to make your life a little easier in this seed, but also

05:21 Take advantage of the ability to use JavaScript to generate any number of random Users and within those the any random number of things that they own so we have notes and images as well So that can be really helpful when you're generating your seed data to make sure that you're generating enough

05:42 For what you're trying to accomplish in your work. And there you go. That's dynamic data with Faker and Prisma's seed script