Skip to content

Commit

Permalink
feat(command): use finally to close db con
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Dec 27, 2024
1 parent 35e03e1 commit 2196fb9
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 29 deletions.
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": "5.6.0",
"version": "5.7.0",
"description": "The Athenna database handler for SQL/NoSQL.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down
15 changes: 14 additions & 1 deletion src/commands/DbFreshCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,24 @@ export class DbWipeCommand extends BaseCommand {
await Artisan.call(`db:wipe --connection ${this.connection}`)
console.log()

await Artisan.call(`migration:run --connection ${this.connection}`)
if (this.getConfig('driver') !== 'mongo') {
await Artisan.call(`migration:run --connection ${this.connection}`)
}

if (this.withSeeders) {
console.log()
await Artisan.call(`db:seed --connection ${this.connection}`)
}
}

private getConfig(name: string, defaultValue?: any) {
return Config.get(
`database.connections.${
this.connection === 'default'
? Config.get('database.default')
: this.connection
}.${name}`,
defaultValue
)
}
}
20 changes: 10 additions & 10 deletions src/commands/DbSeedCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ export class DbSeedCommand extends BaseCommand {

await DB.runSeeders({ task, classes: this.classes })

await task.run().finally(async () => {
const dbName = await DB.getCurrentDatabase()

await DB.close()

console.log()
this.logger.success(
`Database ({yellow} "${dbName}") successfully seeded.`
)
})
await task
.run()
.then(async () => {
const dbName = await DB.getCurrentDatabase()
console.log()
this.logger.success(
`Database ({yellow} "${dbName}") successfully seeded.`
)
})
.finally(() => DB.close())
}

private getConfig(name: string, defaultValue?: any) {
Expand Down
17 changes: 10 additions & 7 deletions src/commands/DbWipeCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,17 @@ export class DbWipeCommand extends BaseCommand {
)
}

await task.run().finally(async () => {
const dbName = await DB.getCurrentDatabase()
await task
.run()
.then(async () => {
const dbName = await DB.getCurrentDatabase()

await DB.close()

console.log()
this.logger.success(`Database ({yellow} "${dbName}") successfully wiped.`)
})
console.log()
this.logger.success(
`Database ({yellow} "${dbName}") successfully wiped.`
)
})
.finally(() => DB.close())
}

private getConfig(name: string, defaultValue?: any) {
Expand Down
13 changes: 8 additions & 5 deletions src/commands/MigrationRevertCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ export class MigrationRevertCommand extends BaseCommand {
}

const DB = Database.connection(this.connection)
const dbName = await DB.getCurrentDatabase()

await DB.revertMigrations().finally(() => DB.close())
await DB.revertMigrations()
.then(async () => {
const dbName = await DB.getCurrentDatabase()

this.logger.success(
`Successfully reverted migrations on ({yellow} "${dbName}") database.`
)
this.logger.success(
`Successfully reverted migrations on ({yellow} "${dbName}") database.`
)
})
.finally(() => DB.close())
}
}
13 changes: 8 additions & 5 deletions src/commands/MigrationRunCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ export class MigrationRunCommand extends BaseCommand {
}

const DB = Database.connection(this.connection)
const dbName = await DB.getCurrentDatabase()

await DB.runMigrations().finally(() => DB.close())
await DB.runMigrations()
.then(async () => {
const dbName = await DB.getCurrentDatabase()

this.logger.success(
`Successfully ran migrations on ({yellow} "${dbName}") database.`
)
this.logger.success(
`Successfully ran migrations on ({yellow} "${dbName}") database.`
)
})
.finally(() => DB.close())
}
}

0 comments on commit 2196fb9

Please sign in to comment.