[RFC] Seeds behaviour
See original GitHub issueHey everyone! 👋
Adonis is currently reading all files within the database/seeds
directory and run them in an alphabetical order.
This create multiple issues.
- You need to change the name of your file to define an order and it becomes hard to change when you want to add a seeds at position 7 but you already have 8+ seeds (need to change the name of all of them).
- You cannot define which seeds need to run in a specific environment.
- You cannot organise your seed files in multiple directory.
This is why I believe this feature needs to change.
One solution could be that the command adonis seed
would only run one file, called DatabaseSeeder
.
This file would have one function called run()
.
const Seeder = use('Seeder')
class DatabaseSeeder extends Seeder {
async run () {
//
}
}
From here, you can define in which order and how you want to run your seeds.
// Pseudo code syntax
async run () {
await this.callInOrder([
'Seeds/User/AdministratorSeeder',
'Seeds/User/ModeratorSeeder',
// ...
])
await this.callInParallel([
'Seeds/Lang/FrenchSeeder',
'Seeds/Lang/EnglishSeeder',
'Seeds/Lang/ChineseSeeder',
// ...
])
}
We could also think about adding an optional parameter that would define which function to run inside the DatabaseSeeder
file.
# $ adonis seed production
This command would call the production()
function inside the DatabaseSeeder
class.
It would help to define different seeds for different environment.
See https://github.com/adonisjs/adonis-lucid/issues/307#issuecomment-379006516
Let me know what you think about it!
c/c @teachmeanything
Issue Analytics
- State:
- Created 6 years ago
- Reactions:21
- Comments:16 (5 by maintainers)
Why don’t use dependencies?
In each seed file you can indicate which seeds need to be executed before that and resolve the dependency tree when executing the seed command.
Something like:
@RomainLanz Thanks for your attention.
I know it has not been implemented yet, I just found a simple way to solve it. Please take a look, as this may be a fairly viable solution to implement in the next release. It would only be necessary to modify the CLI to run the DatabaseSeeder.js file by default, and modify the structure of the run () method to static.