📜  adonis run seed (1)

📅  最后修改于: 2023-12-03 15:13:16.643000             🧑  作者: Mango

Adonis Run Seed

Adonis Run Seed is a command used to seed your database with data. Typically, seeding is used to populate the database with test data or initial data that is required for the application to function properly. The Adonis Run Seed command allows you to quickly and easily run seeders for your application's database.

Syntax

To run the Adonis Run Seed command, use the following syntax:

adonis run seed
How it Works

The Adonis Run Seed command searches for all seed files located in the database/seeds directory of your AdonisJS application. These seed files contain data that will be inserted into your application's database tables. Once the command finds the seed files, it will run the run method on each file. This method should be used to insert data into the database.

Creating Seed Files

To create a seed file, you can use the AdonisJS CLI command:

adonis make:seed

This command will generate a new seed file in the database/seeds directory. You can then populate the run method with code that inserts data into the database.

Here is an example seed file:

'use strict'

const User = use('App/Models/User')

class UserSeeder {
  async run () {
    const users = [
      {
        username: 'user1',
        email: 'user1@example.com',
        password: 'password1'
      },
      {
        username: 'user2',
        email: 'user2@example.com',
        password: 'password2'
      },
      {
        username: 'user3',
        email: 'user3@example.com',
        password: 'password3'
      }
    ]

    await User.createMany(users)
  }
}

module.exports = UserSeeder

In this example, the seed file is named UserSeeder.js and it inserts three users into the users table.

Running Seeders

Once you have created your seed files, you can run them using the Adonis Run Seed command:

adonis run seed

This command will run all seed files located in the database/seeds directory, in alphabetical order. If you want to run a specific seed file, you can pass the file name as an argument:

adonis run seed --file=UserRoleSeeder.js

This command will only run the UserRoleSeeder.js seed file.

Conclusion

The Adonis Run Seed command allows you to quickly and easily seed your application's database with data. This is a powerful tool that can save you a lot of time and effort when developing your application. By creating seed files, you can populate your database with test data or initial data that is required for the application to function properly.