Skip to content

Commit 2196fb9

Browse files
committed
feat(command): use finally to close db con
1 parent 35e03e1 commit 2196fb9

6 files changed

+51
-29
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@athenna/database",
3-
"version": "5.6.0",
3+
"version": "5.7.0",
44
"description": "The Athenna database handler for SQL/NoSQL.",
55
"license": "MIT",
66
"author": "João Lenon <lenon@athenna.io>",

src/commands/DbFreshCommand.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,24 @@ export class DbWipeCommand extends BaseCommand {
3636
await Artisan.call(`db:wipe --connection ${this.connection}`)
3737
console.log()
3838

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

4143
if (this.withSeeders) {
4244
console.log()
4345
await Artisan.call(`db:seed --connection ${this.connection}`)
4446
}
4547
}
48+
49+
private getConfig(name: string, defaultValue?: any) {
50+
return Config.get(
51+
`database.connections.${
52+
this.connection === 'default'
53+
? Config.get('database.default')
54+
: this.connection
55+
}.${name}`,
56+
defaultValue
57+
)
58+
}
4659
}

src/commands/DbSeedCommand.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ export class DbSeedCommand extends BaseCommand {
4848

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

51-
await task.run().finally(async () => {
52-
const dbName = await DB.getCurrentDatabase()
53-
54-
await DB.close()
55-
56-
console.log()
57-
this.logger.success(
58-
`Database ({yellow} "${dbName}") successfully seeded.`
59-
)
60-
})
51+
await task
52+
.run()
53+
.then(async () => {
54+
const dbName = await DB.getCurrentDatabase()
55+
console.log()
56+
this.logger.success(
57+
`Database ({yellow} "${dbName}") successfully seeded.`
58+
)
59+
})
60+
.finally(() => DB.close())
6161
}
6262

6363
private getConfig(name: string, defaultValue?: any) {

src/commands/DbWipeCommand.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,17 @@ export class DbWipeCommand extends BaseCommand {
5555
)
5656
}
5757

58-
await task.run().finally(async () => {
59-
const dbName = await DB.getCurrentDatabase()
58+
await task
59+
.run()
60+
.then(async () => {
61+
const dbName = await DB.getCurrentDatabase()
6062

61-
await DB.close()
62-
63-
console.log()
64-
this.logger.success(`Database ({yellow} "${dbName}") successfully wiped.`)
65-
})
63+
console.log()
64+
this.logger.success(
65+
`Database ({yellow} "${dbName}") successfully wiped.`
66+
)
67+
})
68+
.finally(() => DB.close())
6669
}
6770

6871
private getConfig(name: string, defaultValue?: any) {

src/commands/MigrationRevertCommand.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@ export class MigrationRevertCommand extends BaseCommand {
3838
}
3939

4040
const DB = Database.connection(this.connection)
41-
const dbName = await DB.getCurrentDatabase()
4241

43-
await DB.revertMigrations().finally(() => DB.close())
42+
await DB.revertMigrations()
43+
.then(async () => {
44+
const dbName = await DB.getCurrentDatabase()
4445

45-
this.logger.success(
46-
`Successfully reverted migrations on ({yellow} "${dbName}") database.`
47-
)
46+
this.logger.success(
47+
`Successfully reverted migrations on ({yellow} "${dbName}") database.`
48+
)
49+
})
50+
.finally(() => DB.close())
4851
}
4952
}

src/commands/MigrationRunCommand.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@ export class MigrationRunCommand extends BaseCommand {
3838
}
3939

4040
const DB = Database.connection(this.connection)
41-
const dbName = await DB.getCurrentDatabase()
4241

43-
await DB.runMigrations().finally(() => DB.close())
42+
await DB.runMigrations()
43+
.then(async () => {
44+
const dbName = await DB.getCurrentDatabase()
4445

45-
this.logger.success(
46-
`Successfully ran migrations on ({yellow} "${dbName}") database.`
47-
)
46+
this.logger.success(
47+
`Successfully ran migrations on ({yellow} "${dbName}") database.`
48+
)
49+
})
50+
.finally(() => DB.close())
4851
}
4952
}

0 commit comments

Comments
 (0)