Seed classes in OstroJS allow you to populate your database with data. The database/seeders
directory contains all seed classes. A DatabaseSeeder
class is created for you by default. You may use the call function to execute additional seed classes from this class, enabling you to customise the seeding order.
Use the make:seeder
Assistant command to create a seeder. The database/seeders
directory will include all seeders created by the framework:
node assistant make:seeder UserSeeder
By default, a seeder class has exactly one method: run
. When the db:seed
Assistant command is run, this method is invoked. You may enter data into your database in whatever way you choose using the run method. To manually input data, you can use the query builder or Eloquent model factories.
Let's take the default DatabaseSeeder
class as an example and add a database insert statement to the run
method:
const Str = require('@ostro/support/string')
const Hash = require('@ostro/support/facades/hash')
const DB = require('@ostro/support/facades/database')
const Seeder = require('@ostro/database/seeder')
class DatabaseSeeder extends Seeder {
/**
* Run the database seeders.
*
* @return void
*/
async run() {
await DB.table('users').insert({
'name' : Str.random(10),
'email' : Str.random(10).'@gmail.com',
'password' : Hash.make('password'),
});
}
}
module.exports = DatabaseSeedercon
By default, a seeder class has exactly one method: run
. When the db:seed
Assistant command is run, this method is invoked. You may enter data into your database in whatever way you choose using the run method. To manually input data, you can use the query builder or Eloquent model factories.
Let's take the default DatabaseSeeder
class as an example and add a database insert statement to the run
method:
/**
* Run the database seeders.
*
* @return void
*/
async run() {
await this.call([
UserSeeder,
PostSeeder,
CommentSeeder,
]);
}
You may seed your database using the db:seed
Assistant command. The db:seed
command runs the DatabaseSeeders
class by default, which may call additional seed classes. You may, however, use the —class
option to execute a single seeder class separately:
node assistant db:seed
node assistant db:seed --class=UserSeeder
You may also use the migrate:fresh
command with the —seed
option to seed your database, which will delete all tables and re-run all of your migrations. This command may be used to rebuild your database from the ground up:
node assistant migrate:fresh --seed
Some seeding processes may result in data loss or modification. Before the seeders are conducted in the production environment, you will be requested for confirmation to prevent you from performing seeding instructions against your production database. Use the —force
flag to force the seeders to run without prompting:
node assistant db:seed --force