Skip to content

Conversation

@CHOIJEWON
Copy link

Summary

Closes #3764

Adds a state getter to Connection for tracking connection lifecycle states

Motivation

Users need a reliable way to monitor connection status for debugging and health checks. Previously, there was no standard property
to check the current connection state.

Changes

  • New property: connection.state (readonly)
  • States: disconnectedprotocol_handshakeconnectedauthenticated / error
  • TypeScript: Added ConnectionState type definition
  • Tests: 11 unit tests covering all states and priority rules

Usage

const connection = mysql.createConnection(config);

console.log(connection.state); // 'disconnected'

connection.connect((err) => {
  if (err) {
    console.log(connection.state); // 'error'
  } else {
    console.log(connection.state); // 'authenticated'
  }
});

State Priority

Error > Disconnected > Authenticated > Connected > Protocol Handshake

Test Results

 All 11 state transition tests passing

   Adds a readonly state property to Connection that returns the current connection state. This enables users to monitor and debug
   connection lifecycle issues more effectively.

   The state property returns one of five possible states:
   - 'disconnected': Initial state or when connection is closed/destroyed
   - 'protocol_handshake': Connection established, handshake in progress
   - 'connected': Handshake completed but not yet authenticated
   - 'authenticated': Fully authenticated and ready for queries
   - 'error': Fatal or protocol error occurred

   State priority: error > closing/destroyed > authenticated > connected > protocol_handshake > disconnected
@codecov
Copy link

codecov bot commented Dec 10, 2025

Codecov Report

❌ Patch coverage is 90.32258% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.78%. Comparing base (9049277) to head (5cbbac8).

Files with missing lines Patch % Lines
lib/base/connection.js 90.32% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #3958   +/-   ##
=======================================
  Coverage   89.78%   89.78%           
=======================================
  Files          86       86           
  Lines       13607    13638   +31     
  Branches     1607     1616    +9     
=======================================
+ Hits        12217    12245   +28     
- Misses       1390     1393    +3     
Flag Coverage Δ
compression-0 88.89% <90.32%> (+<0.01%) ⬆️
compression-1 89.76% <90.32%> (+<0.01%) ⬆️
static-parser-0 87.36% <90.32%> (+<0.01%) ⬆️
static-parser-1 88.12% <90.32%> (+<0.01%) ⬆️
tls-0 89.19% <90.32%> (+<0.01%) ⬆️
tls-1 89.55% <90.32%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Request: Public API for connection validation

1 participant