Skip to content

Commit

Permalink
handle connection errors @ MssqlDriver.
Browse files Browse the repository at this point in the history
  • Loading branch information
igalklebanov committed Jun 17, 2024
1 parent 2473948 commit 1894ffc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/dialect/mssql/mssql-dialect-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ export interface TediousConnection {
cancel(): boolean
reset(callback: (error?: Error | null) => void): void
close(): void
off(event: string, listener: (...args: any[]) => void): void
on(event: 'error', listener: (error: unknown) => void): this
on(event: string, listener: (...args: any[]) => void): this
once(event: 'end', listener: () => void): this
once(event: string, listener: (...args: any[]) => void): this
connect(callback?: (error?: Error) => void): void
Expand Down
34 changes: 25 additions & 9 deletions src/dialect/mssql/mssql-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,12 @@ export class MssqlDriver implements Driver {
this.#pool = new this.#config.tarn.Pool({
...this.#config.tarn.options,
create: async () => {
const connection = await this.#config.tedious.connectionFactory()
const tediousConnection = await this.#config.tedious.connectionFactory()

await new Promise((resolve, reject) =>
connection.connect((error) => {
if (error) reject(error)
else resolve(undefined)
}),
)

return new MssqlConnection(connection, this.#config.tedious)
return await new MssqlConnection(
tediousConnection,
this.#config.tedious,
).connect()
},
destroy: async (connection) => {
await connection[PRIVATE_DESTROY_METHOD]()
Expand Down Expand Up @@ -104,6 +100,11 @@ class MssqlConnection implements DatabaseConnection {
constructor(connection: TediousConnection, tedious: Tedious) {
this.#connection = connection
this.#tedious = tedious

this.#connection.on('error', console.error)
this.#connection.once('end', () => {
this.#connection.off('error', console.error)
})
}

async beginTransaction(settings: TransactionSettings): Promise<void> {
Expand Down Expand Up @@ -132,6 +133,21 @@ class MssqlConnection implements DatabaseConnection {
)
}

async connect(): Promise<this> {
await new Promise((resolve, reject) => {
this.#connection.connect((error) => {
if (error) {
console.error(error)
reject(error)
} else {
resolve(undefined)
}
})
})

return this
}

async executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>> {
try {
const deferred = new Deferred<OnDone<O>>()
Expand Down

0 comments on commit 1894ffc

Please sign in to comment.