diff --git a/README.md b/README.md index 6a39e0e9..54d3b6f1 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,13 @@ config: { ssl: false }; ``` +Optionally, *postConnectionQuery* can be used to specify any SQL that should be run after opening a connection, prior to running any other query. This is intended for setting [client connection defaults](http://www.postgresql.org/docs/9.4/static/runtime-config-client.html). For example: +```javascript +config: { + url: 'postgres://username:password@hostname:port/database', + postConnectionQuery: 'SET extra_float_digits = 3;' +}; +``` We are also testing features for future versions of waterline in postgresql. One of these is case sensitive string searching. In order to enable this feature today you can add the following config flag: diff --git a/lib/adapter.js b/lib/adapter.js index 50bd7769..e2dd010e 100644 --- a/lib/adapter.js +++ b/lib/adapter.js @@ -1007,8 +1007,8 @@ module.exports = (function() { if(!connectionObject) return cb(Errors.InvalidConnection); // If the connection details were supplied as a URL use that. Otherwise, - // connect using the configuration object as is. - var connectionConfig = connectionObject.config; + // connect using the configuration object as is (minus 'postConnectionQuery'). + var connectionConfig = _.omit(connectionObject.config, 'postConnectionQuery'); if(_.has(connectionConfig, 'url')) { var connectionUrl = url.parse(connectionConfig.url); connectionUrl.query = _.omit(connectionConfig, 'url'); @@ -1020,7 +1020,9 @@ module.exports = (function() { after(err, client, done); }); - // Run logic using connection, then release/close it + // Run postConnectionQuery if it exists + // , then run logic using connection + // , then release/close the connection function after(err, client, done) { if(err) { // console.error("Error creating a connection to Postgresql: " + err); @@ -1123,12 +1125,32 @@ module.exports = (function() { return cb(err); } - logic(client, function(err, result) { + // If the connectionObject config has a 'postConnectionQuery' + // then run it if it hasn't already been run + var postConnectionQuery = connectionObject.config.postConnectionQuery; + if(postConnectionQuery && !client.connection._sails_hasRunPostConnectionQuery) { + client.query(postConnectionQuery, function(err, result) { + if(err) { + // be sure to release connection + done(); + return cb(err); + } + client.connection._sails_hasRunPostConnectionQuery = true; + // Run logic using connection, then release/close it + runLogic(); + }); + } else { + // Run logic using connection, then release/close it + runLogic(); + } - // release client connection - done(); - return cb(err, result); - }); + function runLogic() { + logic(client, function(err, result) { + // release client connection + done(); + return cb(err, result); + }); + } } }