Stubbing Fake Data with Model Factories
In this lesson, we'll learn how we can quickly and easily bulk-create dummy/fake data within our database for each of our Lucid Models using Model Factories
- Author
- Tom Gobich
- Published
- Mar 02
- Duration
- 13m 48s
Developer, dog lover, and burrito eater. Currently teaching AdonisJS, a fully featured NodeJS framework, and running Adocasts where I post new lessons weekly. Professionally, I work with JavaScript, .Net C#, and SQL Server.
Adocasts
Burlington, KY
Transcript
Stubbing Fake Data with Model Factories
-
(upbeat music)
-
So to get started with querying
-
and building out information for our application,
-
it'd be really nice if we didn't have to go
-
and create hundreds of movies,
-
cast members and all that fun stuff ourselves,
-
but instead just had an automated system
-
to be able to easily spin up
-
as many of those particular items as we wanted to.
-
That's where factories come into play.
-
So within our terminal here,
-
if we do node ace list to see the list of the commands,
-
you'll see underneath the make section,
-
make factory.
-
A factory is essentially going to allow us
-
to stub a model with faker information or fake data.
-
So let's go ahead and create a factory here.
-
So let's do node ace make.
-
We're gonna need cynists before we actually have movies
-
or anything of the like.
-
So let's start with our cynists.
-
So let's do a factory
-
and let's do hyphen hyphen help here
-
to see the available commands.
-
You'll see that the first argument
-
is just going to be the model name.
-
So all that we need to do is node ace make factory
-
and to create one for our cynists,
-
we just do cynists.
-
I think I misspelled that.
-
There we go, cynists.
-
Hit enter there.
-
And that's gonna create a factory for us
-
inside of our database,
-
factories, cynists_factory.ts file.
-
So let's open up our text editor here
-
and let's take a peek at it and see what it is.
-
So let's go to factories, cynists factory.
-
Cool.
-
So you'll see that it's already importing
-
our cynists model automatically for us
-
and it's defining a factory specifically for that.
-
So it's returning back a callback function
-
inside of this define for our factory,
-
providing us faker information inside of here
-
and then returning back an empty object.
-
Lastly, calling build to build out the factory.
-
All that we need to do is define the information
-
for the model itself inside this object
-
that it's returning.
-
If we take a quick peek at our cynists model,
-
we have a first name, last name,
-
and headshot URL that we need to populate.
-
Everything else should be automatically taken care of
-
by either the database itself, which is our ID,
-
or by Lucid itself, which is our create_at and update_at.
-
Cool.
-
So let's dive back into our factory.
-
So start with first name.
-
We can reach for faker dot
-
and we'll have a number of different items
-
that we can select from.
-
So we have an airline, an animal, color, commerce,
-
company, database, data type, date, all that fun stuff.
-
We have image that might come in handy here in a second,
-
but if we keep scrolling down here, we'll see person.
-
Perfect.
-
That's exactly what we're looking for.
-
So we can take a look at what person offers
-
and look at that, we have first name right there.
-
So let's go ahead and use first name.
-
If there's a first name, we can probably assume
-
that's too going to have a last name.
-
So do faker person dot last.
-
Perfect.
-
And then I think the other one was a headshot URL.
-
Sure it was.
-
So we can do faker.
-
Let's see if person has a headshot URL
-
or anything of the like in here.
-
So let's see.
-
Bio job title prefix.
-
Nope.
-
It does not.
-
Though it does have a Zodiac sign
-
in case you have a need for that.
-
So cool.
-
Okay.
-
Let's try maybe image.
-
We saw that earlier on
-
and see a number of deprecated things within here.
-
So avatar right up here or avatar GitHub or legacy.
-
One of those would probably do just fine.
-
Maybe even Flickr.
-
We'll go ahead and use just avatar here.
-
That should serve our purpose well enough.
-
So we can give that a save
-
and we're off to the races.
-
Now our start seeder is specifically
-
for start information that our database needs
-
to get up and running.
-
Whenever it comes time to actually making use
-
of our application,
-
we're not going to want this faker information
-
to actually exist inside of our database.
-
So we'll define the faker information as a separate seeder
-
that we can use as an optional seed to get things going.
-
So we'll jump back into our terminal here
-
and we'll create a new fake seeder.
-
So node ace make fake seeder, just like so.
-
Oops, sorry.
-
Node ace make seeder, fake seeder.
-
Okay, there we go.
-
That one's right.
-
So now we have our fake seeder there.
-
And again, just like we did with our start seed,
-
all we need to do is create the information
-
that we want to exist inside of our database
-
inside the run method of the seeder.
-
So right in here.
-
And we can use our factory in place of our model
-
to create that information.
-
So we can await sin.
-
And you'll see within our autocomplete list here,
-
we have our sinist_factory.
-
Let's go ahead and click on that to import it
-
from our factory.
-
And let's see what our options are.
-
So if we do dot,
-
we'll see that we have both a create and create many method,
-
just as we do with our lucid model.
-
So we can use those to create information
-
inside of our database using the factory.
-
So let's click on create many there
-
because we're gonna want many sinist.
-
And the argument for this is just gonna take in
-
a count of the number of records that we wanna create.
-
So for now, let's just start it with 10.
-
Cool, so we can give this a save
-
and let's see if everything works.
-
So let's jump into our terminal here.
-
Let's clear this out so it's a little cleaner.
-
And let's try to run using the same command
-
that we ran last time.
-
So that's node, ace, db, colon, seed.
-
By default, this is going to run all of our seeders.
-
So if we hit run, you'll see that we get an error
-
because it cannot create another role
-
with an ID of one or two.
-
So that's gonna violate that unique constraint
-
on the primary keys.
-
So whenever we run this in the future,
-
although our fake seeder did complete successfully,
-
we're gonna want to use that files argument
-
to explicitly define that we just wanna run the fake seeder.
-
But since that did complete,
-
we should be a-okay to keep it as is for right now.
-
So let's hide that away
-
and just keep that in mind for future reference.
-
Dive back into pgAdmin
-
and we can right-click on our sinists,
-
go down to view edit data and click on all rows.
-
And voila, we have now 10 fake sinists,
-
each with a first name, last name,
-
and some form of a headshot URL.
-
Awesome, so everything seems to be working a-okay there.
-
Cool, so let's continue onward
-
and continue defining our factory.
-
So we'll do node, ace, make factory.
-
We have explicit values that we have within our start seeder
-
for both our roles and our movie statuses tables.
-
So we're not gonna need fakers for those
-
because we already have real-world data
-
that we want our application to use
-
populated within there from that start seeder,
-
which leaves us with our users, our sinists,
-
which we've just created a factory for, and our movie table.
-
Now, also remember that we have crew movies
-
and cast movies tables inside of our database
-
that serve as many-to-many pivot tables.
-
We don't need those defined as models
-
because they'll be able to be joined together
-
via the relationship that we'll define in the next module.
-
So we can ignore those for now
-
and just focus on these models that we have here.
-
So let's do a factory for our movie next.
-
Okay, cool, so we have that one created.
-
Dive down into there.
-
And we have a number of different properties on our movie,
-
so I'm just gonna drag our movie model over here
-
onto the right-hand side of our screen
-
and hide our explorer away using Command or Control + B.
-
Now, this model in particular
-
has a number of different relationship-based IDs
-
specifically on it,
-
and we haven't defined anything relationship-based yet,
-
but these IDs are required.
-
So for right now, for our factory,
-
we're just going to stub these with hard values.
-
So for our status ID,
-
we actually have this within the enum,
-
so we can put a specific hard value here if we wanted to,
-
so we could import our status ID, enum,
-
movie status is right there.
-
And for right now, let's just put them all in writing.
-
And then we have our writer ID.
-
We'll just hard-code those to one,
-
and our director ID as two,
-
so that there's a slight difference between the two.
-
And then we should be good to continue onward
-
with the rest of our Faker information
-
for the rest of the properties.
-
We'll circle back whenever we get to relationships
-
and fill these two in with relationship-based information.
-
Cool, so let's do our title next,
-
and we can reach for Faker.
-
And let's see what we have.
-
Let's see if there's a movie in here.
-
I doubt it, but you never know.
-
We have music.
-
I don't see movie.
-
Okay, yeah, so I don't see anything from movies,
-
but we could probably use music as that's fairly close.
-
It's probably the closest thing
-
that we have available in here.
-
So we'll go ahead and click on music,
-
and let's see what we got there.
-
So we have genre and song name.
-
So let's go and do song name.
Join The Discussion! (0 Comments)
Please sign in or sign up for free to join in on the dicussion.
Be the first to Comment!