Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add connection.end() call to quickstart documentation #2543

Open
neoncube2 opened this issue Mar 30, 2024 · 3 comments
Open

Add connection.end() call to quickstart documentation #2543

neoncube2 opened this issue Mar 30, 2024 · 3 comments

Comments

@neoncube2
Copy link

The quickstart examples (https://sidorares.github.io/node-mysql2/docs) don't yet document that people should call either connection.end() or connection.close() one they're finished with their connection.

@wellwelwel
Copy link
Collaborator

wellwelwel commented Mar 30, 2024

Hey @neoncube2, would you like to contribute? 🙋🏻‍♂️

It could be included in the first example (for both the promise and the callback), closing the connection at the end of the examples:

  • Promise:

// Get the client
import mysql from 'mysql2/promise';
// Create the connection to database
const connection = await mysql.createConnection({
host: 'localhost',
user: 'root',
database: 'test',
});
// A simple SELECT query
try {
const [results, fields] = await connection.query(
'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45'
);
console.log(results); // results contains rows returned by server
console.log(fields); // fields contains extra meta data about results, if available
} catch (err) {
console.log(err);
}
// Using placeholders
try {
const [results] = await connection.query(
'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
['Page', 45]
);
console.log(results);
} catch (err) {
console.log(err);
}

  • Callback:

// Get the client
const mysql = require('mysql2');
// Create the connection to database
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
database: 'test',
});
// A simple SELECT query
connection.query(
'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45',
function (err, results, fields) {
console.log(results); // results contains rows returned by server
console.log(fields); // fields contains extra meta data about results, if available
}
);
// Using placeholders
connection.query(
'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
['Page', 45],
function (err, results) {
console.log(results);
}
);

@neoncube2
Copy link
Author

@wellwelwel Sure, I'd be happy to! :)

I'm thinking I'd use a try/finally statement, like this, if that sounds good?

// Get the client 
 import mysql from 'mysql2/promise'; 
  
 // Create the connection to database 
 const connection = await mysql.createConnection({ 
   host: 'localhost', 
   user: 'root', 
   database: 'test', 
 }); 

try {
  // A simple SELECT query 
  try { 
    const [results, fields] = await connection.query( 
      'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45' 
    ); 
   
    console.log(results); // results contains rows returned by server 
    console.log(fields); // fields contains extra meta data about results, if available 
  } catch (err) { 
    console.log(err); 
  } 
   
  // Using placeholders 
  try { 
    const [results] = await connection.query( 
      'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?', 
      ['Page', 45] 
    ); 
  
    console.log(results); 
  } catch (err) { 
    console.log(err); 
  }
} finally {
  connection.end()
} 

@wellwelwel
Copy link
Collaborator

Thanks, @neoncube2 🤝

I think we can keep it simple for the quick examples:

// Get the client 
 import mysql from 'mysql2/promise'; 
  
// Create the connection to database 
const connection = await mysql.createConnection({ 
 host: 'localhost', 
 user: 'root', 
 database: 'test', 
}); 

// A simple SELECT query 
try { 
  const [results, fields] = await connection.query( 
    'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45' 
  ); 
 
  console.log(results); // results contains rows returned by server 
  console.log(fields); // fields contains extra meta data about results, if available 
} catch (err) { 
  console.log(err); 
} 
 
// Using placeholders 
try { 
  const [results] = await connection.query( 
    'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?', 
    ['Page', 45] 
  ); 

  console.log(results); 
} catch (err) { 
  console.log(err); 
}

// Close the connection
await connection.end();

What do you think? 🙋🏻‍♂️

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

No branches or pull requests

2 participants