validate-connection.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. module.exports = require('machine').build({
  2. friendlyName: 'Validate connection',
  3. description: 'Check if this looks like a valid MySQL connection instance.',
  4. sideEffects: 'cacheable',
  5. sync: true,
  6. inputs: {
  7. connection: {
  8. friendlyName: 'Connection',
  9. description: 'An active database connection.',
  10. extendedDescription: 'The provided database connection instance must still be active. Only database connection instances created by the `getConnection()` machine in this driver are supported.',
  11. example: '===',
  12. required: true
  13. }
  14. },
  15. exits: {
  16. success: {
  17. outputFriendlyName: 'Is probably MySQL connection',
  18. outputDescription: 'If the provided appears to be a valid MySQL connection instance.',
  19. outputExample: true
  20. },
  21. },
  22. fn: function validateConnection(inputs, exits) {
  23. var _ = require('@sailshq/lodash');
  24. // Validate some basic assertions about the provided connection.
  25. // (this doesn't guarantee it's still active or anything, but it does let
  26. // us know that it at least _HAS_ the properly formatted methods and properties
  27. // necessary for internal use in this Waterline driver)
  28. return exits.success(
  29. _.isObject(inputs.connection) &&
  30. _.isFunction(inputs.connection.query) &&
  31. _.isFunction(inputs.connection.destroy) &&
  32. (
  33. // • If you are pooling: `.release()`
  34. _.isFunction(inputs.connection.release) ||
  35. // • AND/OR if you are not pooling: `.end()`
  36. _.isFunction(inputs.connection.end)
  37. )
  38. );
  39. }
  40. });