From 3b64561b986dc2e03c6574043f81480dea09effa Mon Sep 17 00:00:00 2001 From: Bhargav KN Date: Thu, 14 Oct 2021 23:02:00 +0530 Subject: [PATCH 1/3] validate datastore connectivity after registering it --- lib/waterline.js | 11 ++++- .../system/validate-datastore-connectivity.js | 48 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 lib/waterline/utils/system/validate-datastore-connectivity.js diff --git a/lib/waterline.js b/lib/waterline.js index 9882a24c0..78d28c9fd 100644 --- a/lib/waterline.js +++ b/lib/waterline.js @@ -17,6 +17,9 @@ var buildDatastoreMap = require('./waterline/utils/system/datastore-builder'); var buildLiveWLModel = require('./waterline/utils/system/collection-builder'); var BaseMetaModel = require('./waterline/MetaModel'); var getModel = require('./waterline/utils/ontology/get-model'); +var validateDatastoreConnectivity = require('./waterline/utils/system/validate-datastore-connectivity'); + + /** @@ -711,7 +714,13 @@ function Waterline() { });// // Call the `registerDatastore` adapter method. - datastore.adapter.registerDatastore(datastore.config, usedSchemas, next); + datastore.adapter.registerDatastore(datastore.config, usedSchemas, function registerDatastoreCb(err) { + if (err) { + return next(err); + } + + return validateDatastoreConnectivity(datastore, next); + }); } catch (err) { return next(err); } diff --git a/lib/waterline/utils/system/validate-datastore-connectivity.js b/lib/waterline/utils/system/validate-datastore-connectivity.js new file mode 100644 index 000000000..8bedca9c5 --- /dev/null +++ b/lib/waterline/utils/system/validate-datastore-connectivity.js @@ -0,0 +1,48 @@ +var _ = require('@sailshq/lodash'); + +/** + * validateDatastoreConnectivity() + * + * Validates connectivity to a datastore by trying to acquire and release + * connection. + * + * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + * @param {Ref} datastore + * + * @param {Function} done + * @param {Error?} err [if an error occured] + * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + */ + +module.exports = function validateDatastoreConnectivity(datastore, done) { + var adapterDSEntry = _.get(datastore.adapter.datastores, datastore.config.identity); + + // skip validation if `getConnection` and `releaseConnection` methods do not exist. + if (!(_.has(adapterDSEntry.driver, 'getConnection') + && _.has(adapterDSEntry.driver, 'releaseConnection'))) { + + return done(); + } + + // try to acquire connection. + adapterDSEntry.driver.getConnection({ + manager: adapterDSEntry.manager + }, function getConnectionCb(err, conn) { + // fail if connection could not be acquired. + if (err) { + return done(err); + } + + // release connection. + adapterDSEntry.driver.releaseConnection({ + connection: conn.connection + }, function releaseConnectionCb(err) { + // fail if could not release connection. + if (err) { + return done(err); + } + + return done(); + });// + });// +}; From d6aa8a6a46691badf8be5585c18574f0d944e637 Mon Sep 17 00:00:00 2001 From: eashaw Date: Fri, 15 Oct 2021 16:37:16 -0500 Subject: [PATCH 2/3] Update waterline.js --- lib/waterline.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/waterline.js b/lib/waterline.js index 78d28c9fd..4b704c32b 100644 --- a/lib/waterline.js +++ b/lib/waterline.js @@ -714,7 +714,7 @@ function Waterline() { });// // Call the `registerDatastore` adapter method. - datastore.adapter.registerDatastore(datastore.config, usedSchemas, function registerDatastoreCb(err) { + datastore.adapter.registerDatastore(datastore.config, usedSchemas, function(err) { if (err) { return next(err); } From 80ba7d828c4af35f1144540f92d6f138d50d1550 Mon Sep 17 00:00:00 2001 From: eashaw Date: Fri, 15 Oct 2021 16:44:28 -0500 Subject: [PATCH 3/3] Update validate-datastore-connectivity.js --- .../system/validate-datastore-connectivity.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/lib/waterline/utils/system/validate-datastore-connectivity.js b/lib/waterline/utils/system/validate-datastore-connectivity.js index 8bedca9c5..b7cfbed33 100644 --- a/lib/waterline/utils/system/validate-datastore-connectivity.js +++ b/lib/waterline/utils/system/validate-datastore-connectivity.js @@ -18,31 +18,28 @@ module.exports = function validateDatastoreConnectivity(datastore, done) { var adapterDSEntry = _.get(datastore.adapter.datastores, datastore.config.identity); // skip validation if `getConnection` and `releaseConnection` methods do not exist. - if (!(_.has(adapterDSEntry.driver, 'getConnection') - && _.has(adapterDSEntry.driver, 'releaseConnection'))) { - + // aka pretend everything is OK + if (!_.has(adapterDSEntry.driver, 'getConnection') || !_.has(adapterDSEntry.driver, 'releaseConnection')) { return done(); } // try to acquire connection. adapterDSEntry.driver.getConnection({ manager: adapterDSEntry.manager - }, function getConnectionCb(err, conn) { - // fail if connection could not be acquired. + }, function(err, report) { if (err) { return done(err); } // release connection. adapterDSEntry.driver.releaseConnection({ - connection: conn.connection - }, function releaseConnectionCb(err) { - // fail if could not release connection. + connection: report.connection + }, function(err) { if (err) { return done(err); } return done(); - });// - });// + });// + });// };