Skip to content

Commit f6427c6

Browse files
authored
Merge pull request #164 from AthennaIO/develop
chore(npm): update dependencies
2 parents 705fcbd + d015dbc commit f6427c6

26 files changed

+525
-844
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

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

src/database/DatabaseImpl.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import type { Knex } from 'knex'
1111
import { Path, Module, Options } from '@athenna/common'
12-
import { DriverFactory } from '#src/factories/DriverFactory'
1312
import type { Connections, ConnectionOptions } from '#src/types'
1413
import type { FakeDriver } from '#src/database/drivers/FakeDriver'
1514
import { QueryBuilder } from '#src/database/builders/QueryBuilder'
@@ -36,7 +35,7 @@ export class DatabaseImpl<Driver extends DriverImpl = any> {
3635
* Creates a new instance of DatabaseImpl.
3736
*/
3837
public constructor(athennaDbOpts?: ConnectionOptions) {
39-
this.driver = DriverFactory.fabricate(
38+
this.driver = ConnectionFactory.fabricate(
4039
this.connectionName
4140
) as unknown as Driver
4241

@@ -69,7 +68,7 @@ export class DatabaseImpl<Driver extends DriverImpl = any> {
6968
): DatabaseImpl<typeof FakeDriver>
7069

7170
public connection(
72-
con: Connections,
71+
con: 'mongo' | 'mysql' | 'sqlite' | 'postgres' | 'fake' | string,
7372
options?: ConnectionOptions
7473
):
7574
| DatabaseImpl<MongoDriver>
@@ -82,7 +81,7 @@ export class DatabaseImpl<Driver extends DriverImpl = any> {
8281
* Change the database connection.
8382
*/
8483
public connection(con: Connections, options?: ConnectionOptions) {
85-
const driver = DriverFactory.fabricate(con)
84+
const driver = ConnectionFactory.fabricate(con)
8685
const database = new DatabaseImpl<typeof driver>(options)
8786

8887
database.connectionName = con
@@ -118,7 +117,14 @@ export class DatabaseImpl<Driver extends DriverImpl = any> {
118117
* Close all the connections with all databases.
119118
*/
120119
public async closeAll(): Promise<void> {
121-
await ConnectionFactory.closeAllConnections()
120+
const cons = ConnectionFactory.availableConnections()
121+
const promises = cons.map(con => {
122+
const driver = ConnectionFactory.fabricate(con)
123+
124+
return driver.close().then(() => ConnectionFactory.setClient(con, null))
125+
})
126+
127+
await Promise.all(promises)
122128
}
123129

124130
/**
@@ -143,7 +149,7 @@ export class DatabaseImpl<Driver extends DriverImpl = any> {
143149
}
144150

145151
/**
146-
* Run datase seeders.
152+
* Run database seeders.
147153
*/
148154
public async runSeeders(
149155
options: {

src/database/drivers/Driver.ts

+32-6
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@
77
* file that was distributed with this source code.
88
*/
99

10-
import type { Knex, TableBuilder } from 'knex'
11-
import type { ModelSchema } from '#src/models/schemas/ModelSchema'
12-
import type { Transaction } from '#src/database/transactions/Transaction'
13-
import type { Direction, ConnectionOptions, Operations } from '#src/types'
14-
import { NotFoundDataException } from '#src/exceptions/NotFoundDataException'
1510
import {
16-
Collection,
11+
Module,
1712
Options,
13+
Collection,
1814
type PaginatedResponse,
1915
type PaginationOptions
2016
} from '@athenna/common'
17+
import type { Knex, TableBuilder } from 'knex'
18+
import type { ModelSchema } from '#src/models/schemas/ModelSchema'
19+
import type { Transaction } from '#src/database/transactions/Transaction'
20+
import type { Direction, ConnectionOptions, Operations } from '#src/types'
21+
import { NotFoundDataException } from '#src/exceptions/NotFoundDataException'
2122

2223
export abstract class Driver<Client = any, QB = any> {
2324
/**
@@ -70,9 +71,34 @@ export abstract class Driver<Client = any, QB = any> {
7071
if (client) {
7172
this.client = client
7273
this.isConnected = true
74+
this.isSavedOnFactory = true
7375
}
7476
}
7577

78+
/**
79+
* Import knex in a method to be easier to mock.
80+
*/
81+
public getKnex() {
82+
const require = Module.createRequire(import.meta.url)
83+
84+
return require('knex')
85+
}
86+
87+
/**
88+
* Import mongoose in a method to be easier to mock.
89+
*/
90+
public getMongoose() {
91+
const require = Module.createRequire(import.meta.url)
92+
93+
let mongoose = require('mongoose')
94+
95+
if (!mongoose.createConnection) {
96+
mongoose = mongoose.default
97+
}
98+
99+
return mongoose
100+
}
101+
76102
/**
77103
* Clone the driver instance.
78104
*/

src/database/drivers/FakeDriver.ts

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
Options,
1616
type PaginatedResponse
1717
} from '@athenna/common'
18+
1819
import type { ConnectionOptions, Operations } from '#src/types'
1920
import { Transaction } from '#src/database/transactions/Transaction'
2021
import { WrongMethodException } from '#src/exceptions/WrongMethodException'
@@ -28,6 +29,7 @@ export class FakeDriver {
2829
if (client) {
2930
FakeDriver.client = client
3031
FakeDriver.isConnected = true
32+
FakeDriver.isSavedOnFactory = true
3133
}
3234

3335
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -50,6 +52,10 @@ export class FakeDriver {
5052
return this
5153
}
5254

55+
public static getKnex() {}
56+
57+
public static getMongoose() {}
58+
5359
public static clone() {
5460
return Json.copy(FakeDriver)
5561
}

src/database/drivers/MongoDriver.ts

+33-10
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import {
1717
} from '@athenna/common'
1818

1919
import { debug } from '#src/debug'
20+
import { Log } from '@athenna/logger'
2021
import { Driver } from '#src/database/drivers/Driver'
21-
import { DriverFactory } from '#src/factories/DriverFactory'
2222
import { ModelSchema } from '#src/models/schemas/ModelSchema'
2323
import { Transaction } from '#src/database/transactions/Transaction'
2424
import { ConnectionFactory } from '#src/factories/ConnectionFactory'
@@ -75,15 +75,41 @@ export class MongoDriver extends Driver<Connection, Collection> {
7575
return
7676
}
7777

78-
this.client = ConnectionFactory.mongo(this.connection)
78+
const mongoose = this.getMongoose()
79+
const configs = Config.get(`database.connections.${this.connection}`, {})
7980

80-
if (options.saveOnFactory) {
81-
DriverFactory.setClient('mongo', this.client)
81+
if (configs.debug !== undefined) {
82+
mongoose.set('debug', configs.debug)
83+
}
84+
85+
const mongoOpts = Json.omit(configs, [
86+
'url',
87+
'debug',
88+
'driver',
89+
'validations'
90+
])
91+
92+
debug('creating new connection using mongoose. options defined: %o', {
93+
url: configs.url,
94+
debug: configs.debug,
95+
...mongoOpts
96+
})
97+
98+
this.client = mongoose.createConnection(configs.url, mongoOpts)
99+
100+
if (Config.is('rc.bootLogs', true)) {
101+
Log.channelOrVanilla('application').success(
102+
`Successfully connected to ({yellow} ${this.connection}) database connection`
103+
)
82104
}
83105

84106
this.isConnected = true
85107
this.isSavedOnFactory = options.saveOnFactory
86108

109+
if (this.isSavedOnFactory) {
110+
ConnectionFactory.setClient(this.connection, this.client)
111+
}
112+
87113
this.qb = this.query()
88114
}
89115

@@ -95,18 +121,15 @@ export class MongoDriver extends Driver<Connection, Collection> {
95121
return
96122
}
97123

98-
if (this.isSavedOnFactory && DriverFactory.hasClient('mongo')) {
99-
await DriverFactory.getClient('mongo').close()
100-
DriverFactory.setClient('mongo', null)
101-
} else {
102-
await this.client.close()
103-
}
124+
await this.client.close()
104125

105126
this.qb = null
106127
this.tableName = null
107128
this.client = null
108129
this.session = null
109130
this.isConnected = false
131+
132+
ConnectionFactory.setClient(this.connection, null)
110133
}
111134

112135
/**

src/database/drivers/MySqlDriver.ts

+42-18
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,26 @@
88
* file that was distributed with this source code.
99
*/
1010

11+
import {
12+
Exec,
13+
Is,
14+
Json,
15+
Options,
16+
type PaginatedResponse,
17+
type PaginationOptions
18+
} from '@athenna/common'
19+
1120
import type { Knex } from 'knex'
1221
import { debug } from '#src/debug'
22+
import { Log } from '@athenna/logger'
1323
import { Driver } from '#src/database/drivers/Driver'
14-
import { DriverFactory } from '#src/factories/DriverFactory'
15-
import { Transaction } from '#src/database/transactions/Transaction'
1624
import { ConnectionFactory } from '#src/factories/ConnectionFactory'
25+
import { Transaction } from '#src/database/transactions/Transaction'
1726
import type { ConnectionOptions, Direction, Operations } from '#src/types'
1827
import { MigrationSource } from '#src/database/migrations/MigrationSource'
1928
import { WrongMethodException } from '#src/exceptions/WrongMethodException'
2029
import { PROTECTED_QUERY_METHODS } from '#src/constants/ProtectedQueryMethods'
2130
import { NotConnectedDatabaseException } from '#src/exceptions/NotConnectedDatabaseException'
22-
import {
23-
Exec,
24-
Is,
25-
Options,
26-
type PaginatedResponse,
27-
type PaginationOptions
28-
} from '@athenna/common'
2931

3032
export class MySqlDriver extends Driver<Knex, Knex.QueryBuilder> {
3133
/**
@@ -46,15 +48,40 @@ export class MySqlDriver extends Driver<Knex, Knex.QueryBuilder> {
4648
return
4749
}
4850

49-
this.client = ConnectionFactory.mysql(this.connection)
51+
const knex = this.getKnex()
52+
const configs = Config.get(`database.connections.${this.connection}`, {})
53+
const knexOpts = {
54+
client: 'mysql2',
55+
migrations: {
56+
tableName: 'migrations'
57+
},
58+
pool: {
59+
min: 2,
60+
max: 20,
61+
acquireTimeoutMillis: 60 * 1000
62+
},
63+
debug: false,
64+
useNullAsDefault: false,
65+
...Json.omit(configs, ['driver', 'validations'])
66+
}
67+
68+
debug('creating new connection using Knex. options defined: %o', knexOpts)
5069

51-
if (options.saveOnFactory) {
52-
DriverFactory.setClient('mysql', this.client)
70+
if (Config.is('rc.bootLogs', true)) {
71+
Log.channelOrVanilla('application').success(
72+
`Successfully connected to ({yellow} ${this.connection}) database connection`
73+
)
5374
}
5475

76+
this.client = knex.default(knexOpts)
77+
5578
this.isConnected = true
5679
this.isSavedOnFactory = options.saveOnFactory
5780

81+
if (this.isSavedOnFactory) {
82+
ConnectionFactory.setClient(this.connection, this.client)
83+
}
84+
5885
this.qb = this.query()
5986
}
6087

@@ -66,17 +93,14 @@ export class MySqlDriver extends Driver<Knex, Knex.QueryBuilder> {
6693
return
6794
}
6895

69-
if (this.isSavedOnFactory && DriverFactory.hasClient('mysql')) {
70-
await DriverFactory.getClient('mysql').destroy()
71-
DriverFactory.setClient('mysql', null)
72-
} else {
73-
await this.client.destroy()
74-
}
96+
await this.client.destroy()
7597

7698
this.qb = null
7799
this.tableName = null
78100
this.client = null
79101
this.isConnected = false
102+
103+
ConnectionFactory.setClient(this.connection, null)
80104
}
81105

82106
/**

0 commit comments

Comments
 (0)