Seeding is simply a way to quickly seed, or put data into your tables.
Creating Seeds
You can create a seed file and seed class which can be used for keeping seed information and running it later.
To create a seed run the command:
$ masonite-orm seed User
This will create some boiler plate for your seeds that look like this:
from masoniteorm.seeds import Seeder
class UserTableSeeder(Seeder):
def run(self):
"""Run the database seeds."""
pass
From here you can start building your seed.
Building Your Seed
A simple seed might be creating a specific user that you use during testing.
from masoniteorm.seeds import Seeder
from models import User
class UserTableSeeder(Seeder):
def run(self):
"""Run the database seeds."""
User.create({
"username": "Joe",
"email": "joe@masoniteproject.com",
"password": "secret"
})
Running Seeds
You can easily run your seeds:
$ masonite-orm seed:run User
Database Seeder
Factories
Factories are simple and easy ways to generate mass amounts of data quickly. You can put all your factories into a single file.
Creating A Factory Method
Factory methods are simple methods that take a single Faker instance.
To use the factories you can import the Factory class from where you built your factories. In our case it was the config/factories.py file:
from config.factories import Factory
from models import User
users = Factory(User, 50).create() #== <masoniteorm.collections.Collection object>
user = Factory(User).create() #== <models.User object>
This will persist these users to the database. If you want to simply make the models or collection (and not persist them) then use the make method:
from config.factories import Factory
from models import User
users = Factory(User, 50).make() #== <masoniteorm.collections.Collection object>
user = Factory(User).make() #== <models.User object>
Again this will NOT persist values to the database.
Calling Named Factories
By default, Masonite will use the factory you created without a name. If you named the factories you can call those specific factories easily:
from config.factories import Factory
from models import User
users = Factory(User, 50).create(name="admin_users") #== <masoniteorm.collections.Collection object>
After Creating
You can also specify a second factory method that will run after a model is created. This would look like: