spawn-connection.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // ███████╗██████╗ █████╗ ██╗ ██╗███╗ ██╗
  2. // ██╔════╝██╔══██╗██╔══██╗██║ ██║████╗ ██║
  3. // ███████╗██████╔╝███████║██║ █╗ ██║██╔██╗ ██║
  4. // ╚════██║██╔═══╝ ██╔══██║██║███╗██║██║╚██╗██║
  5. // ███████║██║ ██║ ██║╚███╔███╔╝██║ ╚████║
  6. // ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚══╝╚══╝ ╚═╝ ╚═══╝
  7. //
  8. // ██████╗ ██████╗ ███╗ ██╗███╗ ██╗███████╗ ██████╗████████╗██╗ ██████╗ ███╗ ██╗
  9. // ██╔════╝██╔═══██╗████╗ ██║████╗ ██║██╔════╝██╔════╝╚══██╔══╝██║██╔═══██╗████╗ ██║
  10. // ██║ ██║ ██║██╔██╗ ██║██╔██╗ ██║█████╗ ██║ ██║ ██║██║ ██║██╔██╗ ██║
  11. // ██║ ██║ ██║██║╚██╗██║██║╚██╗██║██╔══╝ ██║ ██║ ██║██║ ██║██║╚██╗██║
  12. // ╚██████╗╚██████╔╝██║ ╚████║██║ ╚████║███████╗╚██████╗ ██║ ██║╚██████╔╝██║ ╚████║
  13. // ╚═════╝ ╚═════╝ ╚═╝ ╚═══╝╚═╝ ╚═══╝╚══════╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝
  14. //
  15. // Instantiate a new connection from the connection manager.
  16. var MySQL = require('machinepack-mysql');
  17. module.exports = function spawnConnection(datastore, cb) {
  18. // Validate datastore
  19. if (!datastore || !datastore.manager || !datastore.config) {
  20. return cb(new Error('Spawn Connection requires a valid datastore.'));
  21. }
  22. MySQL.getConnection({
  23. manager: datastore.manager,
  24. meta: datastore.config
  25. })
  26. .switch({
  27. error: function error(err) {
  28. return cb(err);
  29. },
  30. failed: function failedToConnect(err) {
  31. return cb(err);
  32. },
  33. success: function success(connection) {
  34. return cb(null, connection.connection);
  35. }
  36. });
  37. };