Advertisement
Guest User

Untitled

a guest
Jun 9th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. // Require the core node modules.
  2. var chalk = require( "chalk" );
  3. var mysql = require( "mysql" ); // v2.11.1
  4.  
  5. // Define our connection to the MySQL database.
  6. // --
  7. // NOTE: In this demo, we are using a single connection.
  8. var connection = mysql.createConnection({
  9. host : "127.0.0.1",
  10. user : "node",
  11. password: "node",
  12. database : "testing"
  13. });
  14.  
  15.  
  16. // ----------------------------------------------------------------------------------- //
  17. // ----------------------------------------------------------------------------------- //
  18.  
  19.  
  20. // When we submit a query before a connection is available, the driver will queue the
  21. // query until the connection becomes available.
  22. connection.on(
  23. "enqueue",
  24. function hanldeEvent() {
  25.  
  26. console.log( chalk.bgYellow.white( "Waiting for connection slot." ) );
  27.  
  28. }
  29. );
  30.  
  31. // Run a few queries in serial. Since there is only one connection (we are not pooling),
  32. // some of these queries will be enqueued before they are sent to the server.
  33. for ( var i = 0 ; i < 5 ; i++ ) {
  34.  
  35. connection.query(
  36. `
  37. SELECT
  38. COUNT( * ) AS userCount
  39. FROM
  40. user
  41. `,
  42. function handleResponse( error, records, fields ) {
  43.  
  44. if ( error ) {
  45.  
  46. console.log( chalk.bgRed.white( "Error:" ) );
  47. console.log( error );
  48. return;
  49.  
  50. }
  51.  
  52. console.log( chalk.bgGreen.white( "Count:", records[ 0 ].userCount ) );
  53.  
  54. }
  55. );
  56.  
  57. } // END: For loop.
  58.  
  59. // Close the connection to the database so the Node.js process can close. Otherwise,
  60. // the process will remain open until the database kills the connection.
  61. // --
  62. // NOTE: The .end() is a graceful operation on the connection - it will flush any
  63. // queued queries before it sends the quit command to the MySQL server.
  64. connection.end();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement