get-connection.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. module.exports = {
  2. friendlyName: 'Get connection',
  3. description: 'Get an active connection to a MySQL database from the pool.',
  4. inputs: {
  5. manager: {
  6. friendlyName: 'Manager',
  7. description: 'The connection manager instance to acquire the connection from.',
  8. extendedDescription:
  9. 'Only managers built using the `createManager()` method of this driver are supported. ' +
  10. 'Also, the database connection manager instance provided must not have been destroyed--' +
  11. 'i.e. once `destroyManager()` is called on a manager, no more connections can be acquired ' +
  12. 'from it (also note that all existing connections become inactive-- see `destroyManager()` ' +
  13. 'for more on that).',
  14. example: '===',
  15. required: true
  16. },
  17. meta: {
  18. friendlyName: 'Meta (custom)',
  19. description: 'Additional stuff to pass to the driver.',
  20. extendedDescription: 'This is reserved for custom driver-specific extensions. Please refer to the documentation for the driver you are using for more specific information.',
  21. example: '==='
  22. }
  23. },
  24. exits: {
  25. success: {
  26. description: 'A connection was successfully acquired.',
  27. extendedDescription: 'This connection should be eventually released. Otherwise, it may time out. It is not a good idea to rely on database connections timing out-- be sure to release this connection when finished with it!',
  28. outputVariableName: 'report',
  29. outputDescription: 'The `connection` property is an active connection to the database. The `meta` property is reserved for custom driver-specific extensions.',
  30. outputExample: '==='
  31. // example: {
  32. // connection: '===',
  33. // meta: '==='
  34. // }
  35. },
  36. failed: {
  37. description: 'Could not acquire a connection to the database using the specified manager.',
  38. extendedDescription: 'This might mean any of the following:\n' +
  39. ' + the credentials encoded in the connection string are incorrect\n' +
  40. ' + there is no database server running at the provided host (i.e. even if it is just that the database process needs to be started)\n' +
  41. ' + there is no software "database" with the specified name running on the server\n' +
  42. ' + the provided connection string does not have necessary access rights for the specified software "database"\n' +
  43. ' + this Node.js process could not connect to the database, perhaps because of firewall/proxy settings\n' +
  44. ' + any other miscellaneous connection error',
  45. outputVariableName: 'report',
  46. outputDescription: 'The `error` property is a JavaScript Error instance explaining that a connection could not be made. The `meta` property is reserved for custom driver-specific extensions.',
  47. outputExample: '==='
  48. // example: {
  49. // error: '===',
  50. // meta: '==='
  51. // }
  52. }
  53. },
  54. fn: function getConnection(inputs, exits) {
  55. // Note that if this driver is adapted to support managers which spawn
  56. // ad-hoc connections or manage multiple pools/replicas using PoolCluster,
  57. // then relevant settings would need to be included in the manager instance
  58. // so that connections can be appropriately fetched/opened here.
  59. //
  60. // For now, since we only support a single pool, we simply acquire a
  61. // connection from the pool.
  62. inputs.manager.pool.getConnection(function _gotConnection(err, connection) {
  63. if (err) {
  64. return exits.failed({
  65. error: err,
  66. meta: inputs.meta
  67. });
  68. }
  69. // Now pass back the connection so it can be provided
  70. // to other methods in this driver.
  71. return exits.success({
  72. connection: connection,
  73. meta: inputs.meta
  74. });
  75. });
  76. }
  77. };