00:00
(upbeat music)
00:02
So to get started with querying
00:06
and building out information for our application,
00:09
it'd be really nice if we didn't have to go
00:11
and create hundreds of movies,
00:12
cast members and all that fun stuff ourselves,
00:15
but instead just had an automated system
00:17
to be able to easily spin up
00:18
as many of those particular items as we wanted to.
00:21
That's where factories come into play.
00:23
So within our terminal here,
00:24
if we do node ace list to see the list of the commands,
00:27
you'll see underneath the make section,
00:29
make factory.
00:30
A factory is essentially going to allow us
00:32
to stub a model with faker information or fake data.
00:37
So let's go ahead and create a factory here.
00:39
So let's do node ace make.
00:41
We're gonna need cynists before we actually have movies
00:44
or anything of the like.
00:45
So let's start with our cynists.
00:46
So let's do a factory
00:48
and let's do hyphen hyphen help here
00:50
to see the available commands.
00:51
You'll see that the first argument
00:52
is just going to be the model name.
00:54
So all that we need to do is node ace make factory
00:57
and to create one for our cynists,
00:58
we just do cynists.
00:59
I think I misspelled that.
01:00
There we go, cynists.
01:02
Hit enter there.
01:02
And that's gonna create a factory for us
01:04
inside of our database,
01:05
factories, cynists_factory.ts file.
01:08
So let's open up our text editor here
01:10
and let's take a peek at it and see what it is.
01:12
So let's go to factories, cynists factory.
01:14
Cool.
01:15
So you'll see that it's already importing
01:15
our cynists model automatically for us
01:17
and it's defining a factory specifically for that.
01:20
So it's returning back a callback function
01:22
inside of this define for our factory,
01:24
providing us faker information inside of here
01:26
and then returning back an empty object.
01:29
Lastly, calling build to build out the factory.
01:31
All that we need to do is define the information
01:34
for the model itself inside this object
01:36
that it's returning.
01:37
If we take a quick peek at our cynists model,
01:39
we have a first name, last name,
01:41
and headshot URL that we need to populate.
01:43
Everything else should be automatically taken care of
01:44
by either the database itself, which is our ID,
01:48
or by Lucid itself, which is our create_at and update_at.
01:50
Cool.
01:51
So let's dive back into our factory.
01:52
So start with first name.
01:54
We can reach for faker dot
01:56
and we'll have a number of different items
01:57
that we can select from.
01:58
So we have an airline, an animal, color, commerce,
02:00
company, database, data type, date, all that fun stuff.
02:04
We have image that might come in handy here in a second,
02:06
but if we keep scrolling down here, we'll see person.
02:09
Perfect.
02:09
That's exactly what we're looking for.
02:10
So we can take a look at what person offers
02:13
and look at that, we have first name right there.
02:15
So let's go ahead and use first name.
02:16
If there's a first name, we can probably assume
02:18
that's too going to have a last name.
02:20
So do faker person dot last.
02:22
Perfect.
02:23
And then I think the other one was a headshot URL.
02:25
Sure it was.
02:26
So we can do faker.
02:27
Let's see if person has a headshot URL
02:29
or anything of the like in here.
02:30
So let's see.
02:31
Bio job title prefix.
02:33
Nope.
02:34
It does not.
02:35
Though it does have a Zodiac sign
02:36
in case you have a need for that.
02:37
So cool.
02:38
Okay.
02:39
Let's try maybe image.
02:40
We saw that earlier on
02:41
and see a number of deprecated things within here.
02:44
So avatar right up here or avatar GitHub or legacy.
02:47
One of those would probably do just fine.
02:49
Maybe even Flickr.
02:50
We'll go ahead and use just avatar here.
02:52
That should serve our purpose well enough.
02:54
So we can give that a save
02:55
and we're off to the races.
02:56
Now our start seeder is specifically
02:58
for start information that our database needs
03:00
to get up and running.
03:01
Whenever it comes time to actually making use
03:03
of our application,
03:04
we're not going to want this faker information
03:06
to actually exist inside of our database.
03:08
So we'll define the faker information as a separate seeder
03:12
that we can use as an optional seed to get things going.
03:15
So we'll jump back into our terminal here
03:17
and we'll create a new fake seeder.
03:18
So node ace make fake seeder, just like so.
03:22
Oops, sorry.
03:23
Node ace make seeder, fake seeder.
03:26
Okay, there we go.
03:27
That one's right.
03:28
So now we have our fake seeder there.
03:30
And again, just like we did with our start seed,
03:32
all we need to do is create the information
03:34
that we want to exist inside of our database
03:36
inside the run method of the seeder.
03:38
So right in here.
03:40
And we can use our factory in place of our model
03:42
to create that information.
03:43
So we can await sin.
03:45
And you'll see within our autocomplete list here,
03:48
we have our sinist_factory.
03:49
Let's go ahead and click on that to import it
03:51
from our factory.
03:52
And let's see what our options are.
03:53
So if we do dot,
03:54
we'll see that we have both a create and create many method,
03:57
just as we do with our lucid model.
03:59
So we can use those to create information
04:01
inside of our database using the factory.
04:04
So let's click on create many there
04:05
because we're gonna want many sinist.
04:07
And the argument for this is just gonna take in
04:09
a count of the number of records that we wanna create.
04:12
So for now, let's just start it with 10.
04:14
Cool, so we can give this a save
04:15
and let's see if everything works.
04:16
So let's jump into our terminal here.
04:18
Let's clear this out so it's a little cleaner.
04:20
And let's try to run using the same command
04:22
that we ran last time.
04:23
So that's node, ace, db, colon, seed.
04:26
By default, this is going to run all of our seeders.
04:29
So if we hit run, you'll see that we get an error
04:31
because it cannot create another role
04:33
with an ID of one or two.
04:35
So that's gonna violate that unique constraint
04:37
on the primary keys.
04:38
So whenever we run this in the future,
04:40
although our fake seeder did complete successfully,
04:43
we're gonna want to use that files argument
04:45
to explicitly define that we just wanna run the fake seeder.
04:48
But since that did complete,
04:50
we should be a-okay to keep it as is for right now.
04:53
So let's hide that away
04:54
and just keep that in mind for future reference.
04:56
Dive back into pgAdmin
04:57
and we can right-click on our sinists,
04:59
go down to view edit data and click on all rows.
05:02
And voila, we have now 10 fake sinists,
05:05
each with a first name, last name,
05:07
and some form of a headshot URL.
05:09
Awesome, so everything seems to be working a-okay there.
05:12
Cool, so let's continue onward
05:13
and continue defining our factory.
05:15
So we'll do node, ace, make factory.
05:17
We have explicit values that we have within our start seeder
05:20
for both our roles and our movie statuses tables.
05:23
So we're not gonna need fakers for those
05:24
because we already have real-world data
05:26
that we want our application to use
05:28
populated within there from that start seeder,
05:30
which leaves us with our users, our sinists,
05:33
which we've just created a factory for, and our movie table.
05:35
Now, also remember that we have crew movies
05:37
and cast movies tables inside of our database
05:39
that serve as many-to-many pivot tables.
05:42
We don't need those defined as models
05:44
because they'll be able to be joined together
05:46
via the relationship that we'll define in the next module.
05:49
So we can ignore those for now
05:51
and just focus on these models that we have here.
05:54
So let's do a factory for our movie next.
05:56
Okay, cool, so we have that one created.
05:58
Dive down into there.
05:59
And we have a number of different properties on our movie,
06:01
so I'm just gonna drag our movie model over here
06:04
onto the right-hand side of our screen
06:06
and hide our explorer away using Command or Control + B.
06:08
Now, this model in particular
06:10
has a number of different relationship-based IDs
06:14
specifically on it,
06:15
and we haven't defined anything relationship-based yet,
06:18
but these IDs are required.
06:20
So for right now, for our factory,
06:22
we're just going to stub these with hard values.
06:24
So for our status ID,
06:25
we actually have this within the enum,
06:27
so we can put a specific hard value here if we wanted to,
06:30
so we could import our status ID, enum,
06:33
movie status is right there.
06:34
And for right now, let's just put them all in writing.
06:36
And then we have our writer ID.
06:38
We'll just hard-code those to one,
06:40
and our director ID as two,
06:42
so that there's a slight difference between the two.
06:44
And then we should be good to continue onward
06:45
with the rest of our Faker information
06:46
for the rest of the properties.
06:48
We'll circle back whenever we get to relationships
06:50
and fill these two in with relationship-based information.
06:53
Cool, so let's do our title next,
06:55
and we can reach for Faker.
06:57
And let's see what we have.
06:57
Let's see if there's a movie in here.
06:59
I doubt it, but you never know.
07:00
We have music.
07:01
I don't see movie.
07:02
Okay, yeah, so I don't see anything from movies,
07:04
but we could probably use music as that's fairly close.
07:08
It's probably the closest thing
07:09
that we have available in here.
07:10
So we'll go ahead and click on music,
07:12
and let's see what we got there.
07:13
So we have genre and song name.
07:14
So let's go and do song name.
07:16
That'll serve our purpose well enough.
07:17
And next we have our slug.
07:19
Now in our final application,
07:20
we're gonna want our slug to be a URL safe representation
07:23
of our title.
07:24
For right now, we don't have anything for that in place,
07:26
so we can use Faker to just add in some slug within here
07:30
so that we have that populated.
07:31
Later on, we'll automate that process inside of our model
07:34
so that anytime that we save a new record
07:36
inside of our movie table,
07:38
our model will convert the title
07:39
into a URL safe representation for our slug.
07:42
For now though, we will just use Faker,
07:44
and we can use the string option
07:45
and just plop a UUID in here for right now.
07:48
That should serve as a URL safe version of a string
07:51
that we can plop on the slug for right now.
07:53
Okay, and then we have our summary.
07:56
For this, let's just put some lorem on it,
07:57
and let's just put it to a single sentence.
07:59
Okay, and then we have our abstract,
08:01
which is a long form string.
08:03
So we could put whatever we want in there.
08:05
So let's do abstract.
08:07
Lorem will probably be the most appropriate,
08:09
so we'll do that.
08:10
And for this one, we can do paragraphs,
08:11
and we'll just let it put whatever it wants inside of there.
08:14
And then we have our poster URL.
08:16
So Faker, just like our headshot URL,
08:18
this will be an image dot,
08:20
and let's just use pick some photos for this one.
08:22
Okay, and that should do it.
08:24
We're now down to our created at and updated at,
08:26
which our model will take care of for us.
08:28
So that should be our movies factory.
08:30
We can close our model and open back up our explorer
08:32
with Command or Control + B.
08:33
And lastly, let's create our user factory
08:35
before we do anything else.
08:36
So let's jump back into our terminal,
08:38
node, ace, make, factory, user.
08:41
There we go.
08:42
Jump back into our text editor,
08:43
jump into our user factory,
08:45
and let's define the properties for that.
08:46
So we can drag that back over to the right-hand side
08:48
as a reference, close our explorer,
08:51
and scroll down just slightly here.
08:52
Again, we have our role ID, which uses a relationship.
08:56
For right now, we'll just hard code something in here.
08:58
So we can do roles to import our rolling num,
09:00
and we can assign a user,
09:02
or we could leave this out altogether
09:03
and just have the database's default value
09:05
auto-populate this for us,
09:06
but so that we don't forget about it,
09:08
let's leave this in here as an explicit value.
09:10
And for this one, we have full name.
09:12
So we can do full name as faker,
09:15
leave person was the property that we used on the other one,
09:17
and there should be a full name option in here too.
09:19
There we go.
09:20
Then we have our avatar URL.
09:22
So we'll do faker image, and let's do avatar.
09:25
And then we have an email.
09:26
So faker, the internet property
09:28
should be what we want for that.
09:29
And there's an email right there.
09:31
All right, we'll scroll down a little bit.
09:32
Okay, looks like the last thing that we need is our password.
09:35
So we'll do password faker.
09:37
I bet you internet's gonna have that.
09:38
So we'll do internet dot, yep, sure enough,
09:40
password, cool.
09:41
Now we haven't discussed this at all yet,
09:43
but if we take a look at our user model,
09:45
scroll up to the top, it's using this auth finder right here
09:48
and that's being composed into our user model.
09:50
Remember composed just kind of joins together
09:52
two classes that we want to extend
09:54
and it's composing that with the base model.
09:55
So everything that provides us lucid functionality.
09:58
This auth finder is going to add in a hook
10:01
so that anytime that we save the model instance,
10:03
it will check to see whether or not
10:04
the password value has been changed.
10:06
If the password value has been changed,
10:08
it will hash it and secure it inside of our database
10:11
so that it's not stored in plain text automatically for us.
10:14
So let's jump back into our factory
10:16
and whenever we create our user, we'll see exactly that.
10:19
So we'll get some plain text value provided into faker here
10:22
and then our system will automatically hash it
10:25
and secure it inside of our database.
10:26
So let's jump back down to our fake cedar
10:28
and let's await, we'll do our movie factory here,
10:31
dot create many.
10:33
Let's just create three of those
10:35
and then await user factory dot create many.
10:38
And let's create five users.
10:40
So we can give that a save, jump back into our terminal.
10:42
Let's clear this out and let's run node ace eb seed
10:47
hyphen hyphen files equals.
10:50
And then we want to point it to the particular file path
10:52
that we want to run, which is our fake cedar.
10:54
And this will allow us to only run our fake cedar
10:56
instead of our start cedar.
10:57
So we can do database cedars, fake underscore cedar,
11:01
TS, hit enter there and there we go.
11:04
It just ran our fake cedar and it completed successfully.
11:07
Now you'll note that we had already run this
11:09
with our Sinist factory.
11:11
So our Sinist factory has not run twice.
11:13
So we'll have 20 Sinists inside of our database,
11:15
but we'll only have three movies and five users
11:18
because those two have only run once.
11:20
So if we dive back into PG admin,
11:21
refresh our Sinist by hitting play button right there,
11:25
we should now have 20, which we do indeed.
11:27
We can go over to our movies, right click on that,
11:29
view edit data, all rows.
11:31
We should have three of those, sure enough.
11:33
And remember we hard coded the status ID, writer ID
11:36
and director ID, so those will all be the same,
11:38
but we do have our dynamic movie titles,
11:40
our slugs, which we set to the type of GUID,
11:42
which looks right, some lorem summary and a lorem abstract.
11:45
And if we continue scrolling over,
11:47
we should lastly have our poster URL, sure enough.
11:49
And Oh, forgot all about the release step,
11:51
but that is nullable, so that's a okay.
11:54
Okay, cool.
11:55
So let's scroll back over and let's lastly run our users.
11:57
So let's right click on that, view edit, all rows.
12:00
There we go, we have five of these
12:02
with the full name populated.
12:03
They have an avatar URL, there's their email
12:06
and there is their secure password.
12:08
It was secured using script,
12:10
which is the default hashing mechanism.
12:12
So we are unable to see the plain text value
12:14
that was assigned into Faker inside of our database,
12:17
making our passwords nice and secure.
12:19
You never wanna store your passwords in plain text.
12:21
If your database ever becomes compromised,
12:23
their password is also compromised.
12:25
Always make sure you never store your passwords
12:27
in plain text, okay.
12:28
And then we have our created that and updated that there.
12:30
So everything looks good.
12:31
I'm gonna go ahead and put the release that
12:33
inside of our factory so that we don't forget about it.
12:35
Again, let's see, that was our movie factory.
12:37
Okay, so released.
12:39
And we can just put that the null since it's nullable.
12:41
And the status ID is being set to writing
12:43
instead of released at this point in time anyway,
12:45
so that matches with what we would expect.
12:47
Okay, cool.
12:48
Now, since our fake cedar specifically creates
12:51
fake information using our factories,
12:53
we don't want this to actually execute within production.
12:56
So we can use an environment specification
12:58
inside of the cedar to specify that we only want it to run
13:02
in certain environments.
13:04
Specifically for this use case, probably just development,
13:07
but we could also include testing if we wanted to.
13:09
So we're gonna add a static property onto the class here
13:12
called environment and set this to an array.
13:15
Inside of this array is where we define the environments.
13:17
So first, we're definitely gonna want this to run
13:19
within development so that we can create the fake data
13:22
to actually use throughout development.
13:24
If you wanted it to also be executable within testing,
13:26
we can include testing there as well.
13:28
The one that we don't want to include is production.
13:31
So be sure that you leave that out of any faker-based cedars
13:35
so that you don't have fake data
13:36
out in your production environment.
13:37
So these are the only environments that the cedar will run
13:40
whenever we attempt to use node-ace-db-seed.
13:43
In fact, for right now, I'm gonna go ahead
13:45
and just leave this as development.
Join The Discussion! (0 Comments)
Please sign in or sign up for free to join in on the dicussion.
Be the first to Comment!