Skip to content

Commit

Permalink
feat(db): add runSeeders method
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Apr 27, 2024
1 parent fca797d commit 0c251e3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/database",
"version": "4.45.0",
"version": "4.46.0",
"description": "The Athenna database handler for SQL/NoSQL.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down
10 changes: 1 addition & 9 deletions src/commands/DbSeedCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* file that was distributed with this source code.
*/

import { Module, Path } from '@athenna/common'
import { Database } from '#src/facades/Database'
import { BaseCommand, Option } from '@athenna/artisan'

Expand Down Expand Up @@ -38,16 +37,9 @@ export class DbSeedCommand extends BaseCommand {
this.logger.simple('({bold,green} [ SEEDING DATABASE ])\n')

const DB = Database.connection(this.connection)
const seeds = await Module.getAllFrom(Path.seeders())
const task = this.logger.task()

seeds.forEach(Seed => {
if (this.classes?.length && !this.classes.includes(Seed.name)) {
return
}

task.addPromise(`Running "${Seed.name}" seeder`, () => new Seed().run(DB))
})
await DB.runSeeders({ task, classes: this.classes })

await task.run().finally(() => DB.close())
}
Expand Down
38 changes: 38 additions & 0 deletions src/database/DatabaseImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import type { Knex } from 'knex'
import { Path, Module, Options } from '@athenna/common'
import { DriverFactory } from '#src/factories/DriverFactory'
import type { Connections, ConnectionOptions } from '#src/types'
import type { FakeDriver } from '#src/database/drivers/FakeDriver'
Expand Down Expand Up @@ -141,6 +142,43 @@ export class DatabaseImpl<Driver extends DriverImpl = any> {
return this.driver.startTransaction()
}

/**
* Run datase seeders.
*/
public async runSeeders(
options: {
task?: any
path?: string
classes?: string[]
} = {}
): Promise<void> {
options = Options.create(options, {
classes: [],
task: null,
path: Path.seeders()
})

const seeds = await Module.getAllFrom(options.path)

const promises = seeds.map(Seed => {
if (options.classes?.length && !options.classes.includes(Seed.name)) {
return {}
}

if (!options.task) {
return new Seed().run(this)
}

return options.task.addPromise(`Running "${Seed.name}" seeder`, () =>
new Seed().run(this)
)
})

if (!options.task) {
await Promise.all(promises)
}
}

/**
* Run database migrations.
*/
Expand Down

0 comments on commit 0c251e3

Please sign in to comment.