diff --git a/helpers/index.js b/helpers/index.js index c3ace315..714153ab 100644 --- a/helpers/index.js +++ b/helpers/index.js @@ -14,5 +14,6 @@ module.exports = { setSequence: require('./set-sequence'), sum: require('./sum'), teardown: require('./teardown'), - update: require('./update') + update: require('./update'), + validateConnection: require('./validate-connection') }; diff --git a/helpers/validate-connection.js b/helpers/validate-connection.js new file mode 100644 index 00000000..d74f4721 --- /dev/null +++ b/helpers/validate-connection.js @@ -0,0 +1,72 @@ +// ██╗ ██╗ █████╗ ██╗ ██╗██████╗ █████╗ ████████╗███████╗ +// ██║ ██║██╔══██╗██║ ██║██╔══██╗██╔══██╗╚══██╔══╝██╔════╝ +// ██║ ██║███████║██║ ██║██║ ██║███████║ ██║ █████╗ +// ╚██╗ ██╔╝██╔══██║██║ ██║██║ ██║██╔══██║ ██║ ██╔══╝ +// ╚████╔╝ ██║ ██║███████╗██║██████╔╝██║ ██║ ██║ ███████╗ +// ╚═══╝ ╚═╝ ╚═╝╚══════╝╚═╝╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝ +// +// ██████╗ ██████╗ ███╗ ██╗███╗ ██╗███████╗ ██████╗████████╗██╗ ██████╗ ███╗ ██╗ +// ██╔════╝██╔═══██╗████╗ ██║████╗ ██║██╔════╝██╔════╝╚══██╔══╝██║██╔═══██╗████╗ ██║ +// ██║ ██║ ██║██╔██╗ ██║██╔██╗ ██║█████╗ ██║ ██║ ██║██║ ██║██╔██╗ ██║ +// ██║ ██║ ██║██║╚██╗██║██║╚██╗██║██╔══╝ ██║ ██║ ██║██║ ██║██║╚██╗██║ +// ╚██████╗╚██████╔╝██║ ╚████║██║ ╚████║███████╗╚██████╗ ██║ ██║╚██████╔╝██║ ╚████║ +// ╚═════╝ ╚═════╝ ╚═╝ ╚═══╝╚═╝ ╚═══╝╚══════╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ +// + +module.exports = require('machine').build({ + + + friendlyName: 'Validate Connection', + + + description: 'Test connectivity with the database.', + + + inputs: { + + datastore: { + description: 'The datastore to use for connections.', + extendedDescription: 'Datastores represent the config and manager required to obtain an active database connection.', + required: true, + type: 'ref' + } + }, + + + exits: { + + success: { + description: 'Connection to the database was successful.' + }, + + error: { + friendlyName: 'Error in connection', + description: 'A connection could not be obtained.' + } + }, + + + fn: function validateConnection(inputs, exits) { + // Dependencies + var Helpers = require('./private'); + + // ╔═╗╔═╗╔═╗╦ ╦╔╗╔ ┌─┐┌─┐┌┐┌┌┐┌┌─┐┌─┐┌┬┐┬┌─┐┌┐┌ + // ╚═╗╠═╝╠═╣║║║║║║ │ │ │││││││├┤ │ │ ││ ││││ + // ╚═╝╩ ╩ ╩╚╩╝╝╚╝ └─┘└─┘┘└┘┘└┘└─┘└─┘ ┴ ┴└─┘┘└┘ + // Spawn a new connection. + Helpers.connection.spawnConnection(inputs.datastore, function spawnConnectionCb(err, connection) { + if (err) { + return exits.error(err); + } + + // Release the connection. + Helpers.connection.releaseConnection(connection, false, function releaseConnectionCb(err) { + if (err) { + return exits.error(err); + } + + return exits.success(); + }); // + }); // + } +}); diff --git a/lib/adapter.js b/lib/adapter.js index c8627018..f6b88b57 100644 --- a/lib/adapter.js +++ b/lib/adapter.js @@ -73,6 +73,21 @@ module.exports = (function sailsPostgresql() { }); }, + validateConnection: function validateConnection(datastoreName, cb) { + var datastore = datastores[datastoreName]; + + Helpers.validateConnection({ + datastore: datastore + }).switch({ + error: function error(err) { + return cb(redactPasswords(err)); + }, + success: function success() { + return cb(); + } + }); + }, + // ╔╦╗╔═╗╔═╗╦═╗╔╦╗╔═╗╦ ╦╔╗╔ ┌─┐┌─┐┌┐┌┌┐┌┌─┐┌─┐┌┬┐┬┌─┐┌┐┌ // ║ ║╣ ╠═╣╠╦╝ ║║║ ║║║║║║║ │ │ │││││││├┤ │ │ ││ ││││