Skip to content

Request: Public API for connection validation #3764

@mercmobily

Description

@mercmobily

Knex.js needs to validate mysql2 connections before use in connection pools. Currently, we're forced to use private properties that could break with any update.

Current Situation

// Knex's mysql2 dialect - relies on private properties
validateConnection(connection) {
  return (
    connection &&
    !connection._fatalError &&      // Private
    !connection._protocolError &&   // Private
    !connection._closing &&         // Private
    !connection.stream.destroyed
  );
}

For comparison, the original mysql driver provides public API:

// mysql driver - uses public 'state' property
validateConnection(connection) {
  return connection.state === 'connected' || connection.state === 'authenticated';
}

Background

In 2017, you kindly offered to add public API for this (knex#2175):

I'm happy to add anything that would work for you, ideally in a way compatible with mysqljs/mysql
what if I wrap _fatalError into a getter function and "bless" it to be public api?

This would still be incredibly helpful for connection pool management.

Request

Could mysql2 provide one of these public methods?

Option A: Simple boolean check

connection.isValid()  // returns true/false

Option B: State property (mysql compatible)

connection.state  // 'connected' | 'disconnected' | 'protocol_error' | 'closing'

Option C: Expose existing checks

connection.hasFatalError()     // or
connection.hasProtocolError()  // or
connection.isClosing()

Related Issues

Would you consider adding any of these public methods? Option B would maintain compatibility with the original mysql driver, making migration easier for users.

Thank you for maintaining this excellent library!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions